黄瓜@Before钩子运行两次@After一次 [英] Cucumber @Before hook runs twice @After once
问题描述
对所有人.
用Java11 + Junit5 + Cucumber + Selenium编写一个BDD Test自动化框架,构建工具:Graddle.创建了一个用于验证Google标题的小测试.当开始测试时,在Graddle中使用Test task
或运行CucumberRunner
类,在两种情况下都得到相同的结果:两次执行@Before
方法,一次执行@After
方法,并且一个浏览器窗口保持打开状态.添加了一个以上的测试后,相同的情况,仅打开了4个浏览器,其中2个正在关闭.任何人都可以帮助解决这种情况吗?
在看完日志后,似乎@Before没有执行两次,但是Driver
类被初始化了两次,但是为什么现在不知道这件事…… >
我现在的代码:
CucumberRunner.java
:
@RunWith(Cucumber.class )
@CucumberOptions(
features = "src\\test\\java\\features",
glue = {"steps", "utils"},
tags = "@smoke")
public class CucumberRunner {
}
Driver.java
:
public class Driver {
private WebDriver driver;
public Driver(){
driverInitialization();
}
private void driverInitialization(){
System.setProperty("webdriver.chrome.driver", "D:\\Soft\\selenium-drivers\\chromedriver.exe");
System.out.println("Starting driver.");
var browserName = "chrome";
switch (browserName.toLowerCase()){
case "chrome":
System.out.println("Starting chrome");
driver = new ChromeDriver();
System.out.println("Before break.");
break;
case "firefox":
driver = new FirefoxDriver();
break;
default:
throw new NotFoundException("Browser not found: " + browserName);
}
}
public WebDriver getDriver(){
return driver;
}
public WebDriverWait getWebDriverWait(){
return new WebDriverWait(driver, 120);
}
public void terminateDriver(){
System.out.println("Terminating driver.");
if (driver != null) {
driver.close();
driver.quit();
}
}
}
Hooks.java
:
public class Hooks {
private Driver driver;
@Before
public void setup(){
System.out.println("In the Setup method.");
driver = new Driver();
}
@After
public void tearDown(){
System.out.println("In the TearDown method.");
driver.terminateDriver();
}
}
我认为您的Hook类应该是这样,因为您正在使用selenium-picocontainer DI.
public class Hooks {
private Driver driver;
public Hooks(Driver driver) {
this.driver = driver;
}
@Before
public void setup(){
System.out.println("In the Setup method.");
}
@After
public void tearDown(){
System.out.println("In the TearDown method.");
driver.terminateDriver();
}
}
to all.
Curently writing a little BDD Test automation framework, using Java11+Junit5+Cucumber+Selenium, build tool: Graddle. Created a little test for validating Google title. When starting test, using Test task
in Graddle or running CucumberRunner
class, in both cases getting the same result: two times @Before
method is executed, once @After
method is executed and one browser windows is staying open. After added one more test, the same situation, only 4 browsers are opened, 2 of them are closing. Can anyone help with this situation?
After some watching of logs saw, that, seems, @Before is not executed twice, but Driver
class is initialized twice, but why it happens no idea for now...
My code for now:
CucumberRunner.java
:
@RunWith(Cucumber.class )
@CucumberOptions(
features = "src\\test\\java\\features",
glue = {"steps", "utils"},
tags = "@smoke")
public class CucumberRunner {
}
Driver.java
:
public class Driver {
private WebDriver driver;
public Driver(){
driverInitialization();
}
private void driverInitialization(){
System.setProperty("webdriver.chrome.driver", "D:\\Soft\\selenium-drivers\\chromedriver.exe");
System.out.println("Starting driver.");
var browserName = "chrome";
switch (browserName.toLowerCase()){
case "chrome":
System.out.println("Starting chrome");
driver = new ChromeDriver();
System.out.println("Before break.");
break;
case "firefox":
driver = new FirefoxDriver();
break;
default:
throw new NotFoundException("Browser not found: " + browserName);
}
}
public WebDriver getDriver(){
return driver;
}
public WebDriverWait getWebDriverWait(){
return new WebDriverWait(driver, 120);
}
public void terminateDriver(){
System.out.println("Terminating driver.");
if (driver != null) {
driver.close();
driver.quit();
}
}
}
Hooks.java
:
public class Hooks {
private Driver driver;
@Before
public void setup(){
System.out.println("In the Setup method.");
driver = new Driver();
}
@After
public void tearDown(){
System.out.println("In the TearDown method.");
driver.terminateDriver();
}
}
I think your Hook Class should be like this As You Are Using selenium-picocontainer DI.
public class Hooks {
private Driver driver;
public Hooks(Driver driver) {
this.driver = driver;
}
@Before
public void setup(){
System.out.println("In the Setup method.");
}
@After
public void tearDown(){
System.out.println("In the TearDown method.");
driver.terminateDriver();
}
}
这篇关于黄瓜@Before钩子运行两次@After一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!