首页 > 解决方案 > 如何在不影响 Cucumber 和 Selenium 中的其他步骤的情况下 close() 和 quit() Selenium 驱动程序?

问题描述

我有两个功能文件Cucumber链接到相应的步骤文件。问题是,当其中一个步骤文件完成执行时,它会关闭所有浏览器窗口(因为driver.quit()),从而杀死尚未完成处理的其他步骤文件的执行。

这里每个步骤文件都会打开一个新的浏览器窗口,在其中执行测试,然后关闭并退出浏览器。目前我只有两个步骤文件,但将来会有更多。

在执行完所有步骤后,有什么东西Cucumber总是会被执行吗?

我该如何解决这个问题?

帮助步骤.java

@Ignore
public class HelpStep {

    private WebDriver driver;

    @Before
    public void setup() {
        System.out.println("Into the setup method of HelpStep...");
        this.driver = BrowserConfig.getIEWebDriver();
    }

    @Given("^The user is on the Help page$")
    public void onPage() {
        System.out.println("The user is on the Help page");
    }

    @When("^The user clicks on the links within the Help page$")
    public void clickLinks() {
        System.out.println("The user clicks on the links within the Help page");
    }

    @Then("^The user is navigated to that section$")
    public void validate() {
        System.out.println("The user is navigated to that section");
    }

    @After
    public void cleanUp() {
        System.out.println("Into the cleanUp method of HelpStep...");
        //FOLLOWING METHOD CALL KILLS ALL THE OPEN BROWSERS ALSO :(
        BrowserConfig.releaseResources(this.driver);
    }

}

LinkStatsStep.java

@Ignore
public class LinkStatsStep {

    private WebDriver driver;

    @Before
    public void setup() {
        System.out.println("Into the setup method of LinkStatsStep...");
        this.driver = BrowserConfig.getIEWebDriver();
    }

    @Given("^The user is on the Link Statistics page$")
    public void onPage() {
        System.out.println("The user is on the Link Statistics page");
    }

    @When("^The user does a search$")
    public void clickLinks() {
        System.out.println("The user does a search");
    }

    @Then("^The user is displayed search result$")
    public void validate() {
        System.out.println("The user is displayed search result");
    }

    @After
    public void cleanUp() {
        System.out.println("Into the cleanUp method of LinkStatsStep...");
        BrowserConfig.releaseResources(this.driver);
    }

}

TestRunner.java

@RunWith(Cucumber.class)
@CucumberOptions(
        plugin = {"pretty", "json:target/cucumber-reports/cucumber.json"},
        features = {"src/test/resources/features"})
public class TestRunner extends ApplicationTests {

}

BrowserConfig.java

public class BrowserConfig {

    private static final String IE_DRIVER_EXE = "drivers/IEDriverServer.exe";
    private static final String WEBDRIVER_IE_DRIVER = "webdriver.ie.driver";
    private static final String BASE_URL = "https://www.google.com";

    public static WebDriver getIEWebDriver() {
        String filePath = ClassLoader.getSystemClassLoader().getResource(IE_DRIVER_EXE).getFile();
        System.setProperty(WEBDRIVER_IE_DRIVER, filePath);
        InternetExplorerOptions options = new InternetExplorerOptions().requireWindowFocus();
        options.setCapability(INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
        options.setCapability(ENABLE_ELEMENT_CACHE_CLEANUP, true);
        options.setCapability(IE_ENSURE_CLEAN_SESSION, true);
        options.setCapability(ACCEPT_SSL_CERTS, true);
        options.setCapability("nativeEvents", false);
        options.setCapability(INITIAL_BROWSER_URL, BASE_URL);
        System.out.println("Initializing IE Driver now...........");
        WebDriver driver = new InternetExplorerDriver(options);
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        return driver;
    }

    public static void releaseResources(WebDriver driver) {
        System.out.println("Releasing resources now.....");
        if (null != driver) {
            driver.close();
            driver.quit(); //CLOSES ALL THE OPEN BROWSER SESSIONS LEAVING OTHER STEP EXECUTIONS INCOMPLETE
        }
    }

}

帮助功能

Feature: Check that the user is able to navigate to Help page

  Scenario:
    Given The user is on the Help page
    When The user clicks on the links within the Help page
    Then The user is navigated to that section

链接-stats.feature

Feature: Check that the user is able to navigate to Link Statistics page

  Scenario:
    Given The user is on the Link Statistics page
    When The user does a search
    Then The user is displayed search result

系统输出

Initializing IE Driver now...........
Listening on port 47613
Into the setup method of LinkStatsStep...
Initializing IE Driver now...........
Listening on port 5009
The user is on the Help page
The user clicks on the links within the Help page
The user is navigated to that section
Into the cleanUp method of HelpStep...
Releasing resources now.....
Into the cleanUp method of LinkStatsStep...
Releasing resources now.....


Into the setup method of HelpStep...
Initializing IE Driver now...........
Listening on port 17291
Into the setup method of LinkStatsStep...
Initializing IE Driver now...........
Listening on port 23793
The user is on the Link Statistics page
The user does a search
The user is displayed search result
Into the cleanUp method of HelpStep...
Releasing resources now.....
Into the cleanUp method of LinkStatsStep...
Releasing resources now.....

标签: javaseleniumjunitcucumberbrowser-automation

解决方案


查看您的代码,它似乎是正确的。

调用quit应关闭与该 webdriver 会话关联的所有打开的窗口。它不应该关闭其他 webdriver 会话的窗口。所以我认为你在IEDriverServer.

如果是这种情况,并且如果您在所有测试执行后关闭的 JVM 中运行测试。然后作为解决方法,您可以使用关闭挂钩来调用quite和关闭所有 Web 驱动程序会话。例如:

private static final Thread CLOSE_THREAD = new Thread() {
    @Override
    public void run() {
      // Start a new webdriver to call quit on
      // For IE this will terminate all webdriver sessions
      getIEWebDriver().quit();
    }
};

static {
    Runtime.getRuntime().addShutdownHook(CLOSE_THREAD);
}

推荐阅读