我目前正在使用Java中的Cucumber和Selenium WebDriver来测试Web应用程序.我对浏览器关闭并在每个测试用例之间重新打开这一事实感到不满意.
首先,它很慢,而且对我来说没有意义.
我想要做的是在每次测试之间注销应用程序并保持浏览器打开.
我认为这条线可以完成工作,但它不能按预期工作:
driver.navigate().to("http://myurl.url");
Run Code Online (Sandbox Code Playgroud)
代替 :
driver.get("http://myurl.url");
Run Code Online (Sandbox Code Playgroud)
它会打开一个新的浏览器.我理解为什么会这样做,但我想附加到我之前的浏览器会话中.
我发现了一种在测试之间保持浏览器畅通的方法.我用Singleton Design Pattern用picocontainer.我static在这堂课中宣布我的浏览器.
public class Drivers {
private static boolean initialized = false;
private static WebDriver driver;
@Before
public void initialize(){
if (!initialized){
initialized = true;
driver = new FirefoxDriver();
driver.get("http://myurl.url");
}
}
public static WebDriver getDriver(){
return driver;
}
}
Run Code Online (Sandbox Code Playgroud)
在每个StepDefinitions类中,我都有一个实例化我的构造函数Singleton.
public class DashboardSteps extends SuperSteps {
Drivers context;
@After
public void close(Scenario scenario) {
super.tearDown(scenario);
loginPage = homePage.clickLogout();
loginPage.waitPageLoaded();
}
public DashboardSteps(Drivers context) {
super(context);
}
Run Code Online (Sandbox Code Playgroud)
SuperSteps类:
public class SuperSteps {
protected Drivers context;
public SuperSteps(Drivers context){
this.context = context;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我在每次测试后返回logIn页面,这样我就可以独立运行每个测试.
| 归档时间: |
|
| 查看次数: |
5967 次 |
| 最近记录: |