为什么我的IE浏览器与Selenium和Cucumber一起启动了两次? [英] Why is my IE browser getting launched twice with Selenium and Cucumber?

查看:107
本文介绍了为什么我的IE浏览器与Selenium和Cucumber一起启动了两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Selenium-Cucumber编写非常基本的自动化测试,该测试将启动IE浏览器并最终关闭它.

I am writing very basic automation test with Selenium-Cucumber that is launching an IE browser and closing it at the end.

问题是浏览器启动了两次.

The problem is that the browser gets launched twice.

除少数System.out语句外,该测试没有太多其他内容.对于基于selenium的自动化测试和Cucumber,我都是新手,却无法理解为什么它要两次启动.

The test does not have much it other than few System.out statements. I am kind of new to both selenium-based automation testing and Cucumber and not able to understand why is it getting launched twice.

请指导.

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);
        WebDriver driver = new InternetExplorerDriver(options);
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        return driver;
    }

    public static void releaseResources(WebDriver driver) {
        if (null != driver) {
            driver.close();
            driver.quit();
        }
    }

}

TestRunner.java

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

}

LoginStep.java

@Ignore
public class LoginStep {

    WebDriver driver;

    @Before
    public void setup() {
        if (this.driver == null) {
            this.driver = BrowserConfig.getIEWebDriver();
        }
    }

    @After
    public void cleanUp() {
        BrowserConfig.releaseResources(driver);
    }

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

    @When("^The user enters the correct credentials on the Login page$")
    public void setWelcomePage() {
        System.out.println("The user enter the correct credentials on the Login page");
    }

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

}

HelpStep.java

@Ignore
public class HelpStep {

    WebDriver driver;

    @Before
    public void setup() {
        if (this.driver == null) {
            this.driver = BrowserConfig.getIEWebDriver();
        }
    }

    @After
    public void cleanUp() {
        BrowserConfig.releaseResources(driver);
    }

    @When("^The user clicks on the Help menu link from the Welcome page$")
    public void setWelcomePage() {
        System.out.println("The user clicks on the Help menu link from the Welcome page");
    }

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

}

help.feature

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

  Background:
    Given The user is on the Login page
    When The user enters the correct credentials on the Login page
    Then The user is displayed Welcome page

  Scenario:
    When The user clicks on the Help menu link from the Welcome page
    Then The user is displayed Help page

pom.xml

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>cucumber-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cucumber-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <cucumber.version>4.2.3</cucumber.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-spring</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
            <plugin>
                <groupId>net.masterthought</groupId>
                <artifactId>maven-cucumber-reporting</artifactId>
                <version>3.14.0</version>
                <executions>
                    <execution>
                        <id>execution</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <projectName>${project.artifactId}</projectName>
                            <outputDirectory>${project.build.directory}/cucumber-reports</outputDirectory>
                            <cucumberOutput>${project.build.directory}/cucumber-reports/cucumber.json</cucumberOutput>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

推荐答案

因为您正在初始化,因此在功能文件中两次调用了驱动程序.

Because you are initializing and call your driver twice in feature files.

Background部分首先在LoginStep.java中初始化浏览器,然后您的ScenarioHelpStep.java中也初始化浏览器.

Background part of your feature file are initializing browser firstly in LoginStep.java then your Scenario is also initialize browser in HelpStep.java.

我更喜欢对@Before@After挂钩使用全局Hooks.java类,并在不同的.java类之间插入驱动程序.

I prefer using global Hooks.java class for @Before and @After hooks and inject driver between different .java classes.

这篇关于为什么我的IE浏览器与Selenium和Cucumber一起启动了两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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