在Selenium Cucumber-jvm中单独启动Webdriver

Start Webdriver separately in Selenium Cucumber-jvm

我正在使用黄瓜和硒。

我有三个 .feature 文件:

1)auth.feature

2)registration.feature

3)userInformation.feature

我有单独的步骤定义class它们

1)authSteps.class

2)registrationSteps.class

3)userInformationSteps.class

在每个 class 中,我都这样创建 webdriver

WebDriver driver = new WebDriver(ChromeDriver);

当我 运行 测试时,所有驱动程序一起启动,即使我标记了测试用例并且只有 运行 1 个案例,网络驱动程序也会启动。 @before 不起作用。

我只想 运行 该功能的网络驱动程序。如果我测试所有功能,我希望以线性方式 运行 网络驱动程序。

1.Create TestScript 包中的一个 AbstractHook class

public class AbstractHook {
protected static WebDriver driver;

    protected WebDriver getdriver() {
        if (driver == null) {
        System.setProperty("webdriver.gecko.driver","geckodriver.exe");
            driver = new FirefoxDriver();

        } else if (driver == null) {

            System.setProperty("webdriver.chrome.driver",
                "chromedriver.exe");
            driver = new ChromeDriver();

        } 
        return driver;
    }

2.Hook class

public class Hook extends AbstractHook {

    AbstractHook df = new AbstractHook();

    @Before
    public void createdriver() {
        df.getdriver();
        driver.get("some url");// no need 

    }
}

3.TestScript代码

    public class TestScript01 extends AbstractHook {
        WebDriver driver = getdriver();

        @Given("^I want to open the gmail url on firefox$")
        public void i_want_to_open_the_Gmail_url_on_firefox() throws Throwable {

            driver.get("give some url");
        }
}

4.Runner class

@RunWith(Cucumber.class)
@CucumberOptions(features = "Feature", monochrome = true, glue = { "com.scripts" }, plugin = { "pretty","html:target/cucumber" },
         tags = {"@sa,@sanity" },
        // dryRun = false)
public class RunnerClass {

}

这样试试,驱动会被初始化一次,在所有的TestScriptclasses中使用。