且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

黄瓜@Before钩子运行两次@After一次

更新时间:2023-02-25 16:01:48

我认为您的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?

Link to repository

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();
   }
}