【问题标题】:Browser Session testing with Selenium WebDriver使用 Selenium WebDriver 进行浏览器会话测试
【发布时间】:2016-03-10 01:32:45
【问题描述】:
我有以下自动化场景:
- 用户登录网站。导航到网站上的特定页面
- 保存该页面的网址。
- 关闭浏览器
- 再次打开浏览器并导航到保存的 URL。
- 用户应该能够在不登录的情况下看到该页面。
我可以自动执行前 3 个步骤。但是当我在第 4 步中再次打开浏览器时,会启动一个新会话,并且我的用户会看到登录页面。
我相信网络驱动程序浏览器处于隐身模式。因此不存储会话 cookie。
有没有办法使用 selenium 网络驱动程序自动化这个场景?
【问题讨论】:
标签:
session
selenium
selenium-webdriver
【解决方案1】:
这是因为每次调用浏览器时,selenium 都会打开一个新的浏览器配置文件..所以您的 cookie 会丢失..
你可以第二次注入cookie了..
Cookie ck = new Cookie("name", "value");
driver.manage().addCookie(ck);
或者您可以为驱动程序使用相同的浏览器配置文件..
FirefoxBinary binary = new FirefoxBinary();
File firefoxProfileFolder = new File("/Users/xxx/work/xxx/selenium/src/test/resources/firefoxprofile");
FirefoxProfile profile = new FirefoxProfile(firefoxProfileFolder);
webDriver driver= new FirefoxDriver(binary, profile);
【解决方案2】:
在启动保存的 url 之前,您必须退出 webdriver。
在第三步写driver.quit();,在第四步写:
driver.get("<saved_url>");