【问题标题】:Start Webdriver separately in Selenium Cucumber-jvm在 Selenium Cucumber-jvm 中单独启动 Webdriver
【发布时间】:2018-10-25 23:22:13
【问题描述】:

我正在使用黄瓜和硒。

我有三个 .feature 文件:

1)auth.feature

2)registration.feature

3)userInformation.feature

我有单独的步骤定义类

1)authSteps.class

2)registrationSteps.class

3)userInformationSteps.class

在每个类中我都这样创建 webdriver

WebDriver driver = new WebDriver(ChromeDriver);

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

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

【问题讨论】:

  • 删除每个类中的驱动程序初始化并创建一个类(如ex:AbstractHook)将返回驱动程序和另一个类(Hook)包含@Before方法并在Before方法中调用驱动程序和每个 Testscript 类都应该扩展 AbstractHook 并在每个 TestScript 中调用驱动程序

标签: java selenium selenium-webdriver cucumber


【解决方案1】:

1.在TestScript包中创建一个AbstractHook类

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类

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类

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

}

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

【讨论】:

    猜你喜欢
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 2014-01-09
    • 2016-09-20
    • 1970-01-01
    相关资源
    最近更新 更多