selenium - Cucumber - Selenium - 失败场景后浏览器实例未关闭

标签 selenium intellij-idea gradle cucumber

我将 Selenium 与 Cucumber 以及 Gradle 和 TestNG 一起使用。相同的场景适用于多个参数(示例)。我面临的问题是,对于第一个断言成功,浏览器(驱动程序)关闭。但是对于随后的断言失败,浏览器(驱动程序)不会关闭,而是为下一组值启动一个新的浏览器实例。

我的功能文件

Feature: Using Contact Form
 To test the functionality of contact form

  Scenario Outline: Filling contact form
   Given I am on Home Page of "http://room5.trivago.com/contact/"
   And Dismiss cookies popup
   When I enter message as "<message>"
   And I enter full name as "<fullname>"
   And I enter email as "<email>"
   And I click on Submit button
   Then I see success message
   Examples:
   |message|fullname|email|
   |just some gibberish message|Ashish Deshmukh|ashish@deshmukh.com|
   | |Ashish Deshmukh|ashish@deshmukh.com|
   |just some givverish message| |ashish@deshmukh.com|
   |just some gibberish message|Ashish Deshmukh| |

我的 StepDefination 文件
package stepDef;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.Assert;
import pageObjectModels.ContactPageObjectModel;
import pageObjectModels.CookiesNoticePageObjectModel;

import java.util.logging.Logger;

public class Contact_Form extends Test_Base{
    public static WebDriver driver;
    public static ContactPageObjectModel objContact;
    public static CookiesNoticePageObjectModel objCookies;
    static Logger log = Logger.getLogger("com.gargoylesoftware");

    @Given("^I am on Home Page of \"([^\"]*)\"$")
    public void i_am_on_Home_Page_of(String arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        System.setProperty("webdriver.chrome.driver", "D:\\Selenium Webdriver/chromedriver.exe");
        driver = new HtmlUnitDriver(true);
        driver = new ChromeDriver();
        driver.get(arg1);
        objContact = new ContactPageObjectModel(driver);
        objCookies = new CookiesNoticePageObjectModel(driver);
    }

    @Given("^Dismiss cookies popup$")
    public void dismiss_cookies_popup() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        objCookies.acceptCookies();
    }

    @When("^I enter message as \"([^\"]*)\"$")
    public void i_enter_message_as(String arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        objContact.enterMessage(arg1);
    }

    @When("^I enter full name as \"([^\"]*)\"$")
    public void i_enter_full_name_as(String arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        objContact.enterFullName(arg1);
    }

    @When("^I enter email as \"([^\"]*)\"$")
    public void i_enter_email_as(String arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        objContact.enterEmail(arg1);
    }

    @When("^I click on Submit button$")
    public void i_click_on_Submit_button() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        objContact.clickSubmit();
    }

    @Then("^I see success message$")
    public void i_see_success_message() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        String status = objContact.getSuccessStatus();
        Assert.assertEquals(status, "Success");
        driver.quit();
    }
}

我的 build.gradle 运行测试
group 'com.seleniumtestcucumber.mytest'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'idea'

configurations {
    cucumberRuntime.extendsFrom testRuntime
}


task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "cucumber.api.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'stepDef', 'src/test/java']
        }
    }
}

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    compile 'org.seleniumhq.selenium:selenium-server:2.44.0'
    // https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.4.0'
    compile 'org.testng:testng:6.1.1'
    // https://mvnrepository.com/artifact/info.cukes/cucumber-testng
    compile group: 'info.cukes', name: 'cucumber-testng', version: '1.2.5'
    // https://mvnrepository.com/artifact/info.cukes/cucumber-java
    compile group: 'info.cukes', name: 'cucumber-java', version: '1.2.5'

}

控制台日志
Feature: Using Contact Form
  To test the functionality of contact form

  Scenario Outline: Filling contact form                           # features/Contact_Form.feature:5
    Given I am on Home Page of "http://room5.trivago.com/contact/"
    And Dismiss cookies popup
    When I enter message as "<message>"
    And I enter full name as "<fullname>"
    And I enter email as "<email>"
    And I click on Submit button
    Then I see success message

    Examples: 
Starting ChromeDriver 2.29.461591 (62ebf098771772160f391d75e589dc567915b233) on port 42643
Only local connections are allowed.
Aug 10, 2017 4:21:10 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Accepted cookies notice.
Entered message: just some gibberish message
Entered name: Ashish Deshmukh
Entered email: ashish@deshmukh.com
Clicked on Submit button.
Success

  Scenario Outline: Filling contact form                           # features/Contact_Form.feature:15
    Given I am on Home Page of "http://room5.trivago.com/contact/" # Contact_Form.i_am_on_Home_Page_of(String)
    And Dismiss cookies popup                                      # Contact_Form.dismiss_cookies_popup()
    When I enter message as "just some gibberish message"          # Contact_Form.i_enter_message_as(String)
    And I enter full name as "Ashish Deshmukh"                     # Contact_Form.i_enter_full_name_as(String)
    And I enter email as "ashish@deshmukh.com"                     # Contact_Form.i_enter_email_as(String)
    And I click on Submit button                                   # Contact_Form.i_click_on_Submit_button()
    Then I see success message                                     # Contact_Form.i_see_success_message()
Starting ChromeDriver 2.29.461591 (62ebf098771772160f391d75e589dc567915b233) on port 11801
Only local connections are allowed.
Aug 10, 2017 4:21:46 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Accepted cookies notice.
Entered message: 
Entered name: Ashish Deshmukh
Entered email: ashish@deshmukh.com
Clicked on Submit button.
Error

  Scenario Outline: Filling contact form                           # features/Contact_Form.feature:16
    Given I am on Home Page of "http://room5.trivago.com/contact/" # Contact_Form.i_am_on_Home_Page_of(String)
    And Dismiss cookies popup                                      # Contact_Form.dismiss_cookies_popup()
    When I enter message as ""                                     # Contact_Form.i_enter_message_as(String)
    And I enter full name as "Ashish Deshmukh"                     # Contact_Form.i_enter_full_name_as(String)
    And I enter email as "ashish@deshmukh.com"                     # Contact_Form.i_enter_email_as(String)
    And I click on Submit button                                   # Contact_Form.i_click_on_Submit_button()
    Then I see success message                                     # Contact_Form.i_see_success_message()
      java.lang.AssertionError: expected [Success] but found [Error]
        at org.testng.Assert.fail(Assert.java:94)
        at org.testng.Assert.failNotEquals(Assert.java:513)
        at org.testng.Assert.assertEqualsImpl(Assert.java:135)
        at org.testng.Assert.assertEquals(Assert.java:116)
        at org.testng.Assert.assertEquals(Assert.java:190)
        at org.testng.Assert.assertEquals(Assert.java:200)
        at stepDef.Contact_Form.i_see_success_message(Contact_Form.java:72)
        at ✽.Then I see success message(features/Contact_Form.feature:12)

Starting ChromeDriver 2.29.461591 (62ebf098771772160f391d75e589dc567915b233) on port 22809
Only local connections are allowed.
Aug 10, 2017 4:22:39 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS

请建议如何克服这个问题。有没有办法可以在其中使用@BeforeTest @AfterTest?

最佳答案

你应该阅读 cucumber 钩 详细说明。

对于您的具体问题,您可以使用@Afte r 和 @Before .这适用于 Junit Runner,从未使用 TestNG 测试过,但我猜应该可以工作,因为钩子(Hook)是 Cucumber 的一部分,而不是 JUnit/TestNG。

import cucumber.annotation.After;
import cucumber.annotation.Before;

@Before
public void beforeScenario() 
{
     System.setProperty("webdriver.chrome.driver", "D:\\Selenium Webdriver/chromedriver.exe");
     driver = new HtmlUnitDriver(true);
     driver = new ChromeDriver();
}

@After
public void afterScenario() 
{
     driver.quit()
}

关于selenium - Cucumber - Selenium - 失败场景后浏览器实例未关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45611918/

相关文章:

java - 如何将当前页面源保存在不同的名称和文件夹中

java - 使用 Selenium 验证 <div> 之间的文本

java - maven 没有正确添加主类

java - 如何使用复合构建构建 3 个单独的 gradle 项目

android - Gradle 同步失败 : Could not find com. android.tools.build :gradle:5. 5.1

linux - 从 Vagrant for Firefox 反向 X11 转发

java - 如何使用 Selenium 和 Java 通过类名识别元素并单击它?

java - 运行 JUnit 测试有效,但 Gradle 测试无效。怎么修?

java - IntelliJ Java Type Renderers for Eclipse Collections with primitives

eclipse - 使用 gradle eclipse 插件指定 JRE Con​​tainer