黄瓜 @Before 钩子运行两次 @After 一次 [英] Cucumber @Before hook runs twice @After once

查看:30
本文介绍了黄瓜 @Before 钩子运行两次 @After 一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对所有人.目前在写一个小BDD Test自动化框架,使用Java11+Junit5+Cucumber+Selenium,构建工具:Graddle.为验证 Google 标题创建了一个小测试.开始测试时,在 Graddle 中使用 Test task 或运行 CucumberRunner 类,两种情况下得到相同的结果:两次 @Before 方法被执行,一旦执行 @After 方法并且一个浏览器窗口保持打开状态.再添加一个测试后,同样的情况,只打开了 4 个浏览器,其中 2 个正在关闭.任何人都可以帮助解决这种情况吗?

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?

存储库链接

看了一些日志后发现,@Before 似乎没有执行两次,但是 Driver 类被初始化了两次,但是为什么会发生现在不知道...

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...

我现在的代码: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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆