当前位置: 首页 > 知识库问答 >
问题:

cucumber-selenium-browser实例在失败的场景后没有关闭

汪思博
2023-03-14

我在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| |
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();
    }
}
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?

共有1个答案

宗政海
2023-03-14

你应该详细了解一下cucumber钩。

对于特定的问题,可以使用@afteR和@before。这适用于Junit Runner,它从未用TestNG测试过,但我想应该可以,因为钩子是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()
}
 类似资料:
  • 我想自动重试失败的测试,以提高测试的可靠性,类似于 Junit 中的 TestRule,我希望能够灵活地在测试周围插入逻辑,以便实现重试循环: > 我正在使用Cucumber-JVM,需要一个涉及Java或Gradle的解决方案 我通过Gradle javaexec尝试了以下cucumber选项: //--format pretty--format rerun--out tmp/rerun.txt

  • 我有一个示例项目,其中使用了Maven、TestNg和Cucumber。我使用testrunner类运行测试。 我创建了一个包含两个方案的功能文件,但两个方案都失败了。我有两个具有不同功能文件的测试运行者类 - 1。特征文件指向所有功能,2。指向仅失败的方案。 当我尝试重新运行场景时,它只运行一个场景。 1- 请告知如何执行所有失败的方案。

  • 我不确定如何在cucumber-JVM中实现它。有线索吗?

  • 我已经在Cucumber中有了我的特性文件,并且正在使用Given,When,Then语法(Gherkin)在其中编写我的场景,并在步骤定义文件中有相应的步骤。我有一个场景,我正在检查特定数据集是否存在(这是我给定步骤的一部分),然后继续执行“然后”、“和”等的下一步,但如果不存在,则应跳过其余步骤并退出步骤定义文件,而不会使场景失败。有人能告诉我cucumber是否支持这一点吗?如果是,最好的方

  • 当我只接受请求的一个子集(~100)时,模拟工作得更好(用户初始化更快,可以容纳170多个用户,等等)。 我的问题是,首先,正如我所理解的,JMeter加载场景树,每个线程都播放它,不应该有任何重复,那么到底是什么导致了如此大的加载?其次,我能做些什么呢? PS:当我查看系统瓶颈时,我注意到长文件的CPU和内存值都很高,而短文件的CPU和内存值都很低。有人能解释吗? PS2:请求之间有大约7秒的延

  • 如果我运行特定的场景,它都是绿色的。如果我只运行cucumber套件(又名。耙cucumber),它都是绿色的。但是,如果我运行完整的测试套件,(又名。耙),单个场景失败。 我假设这与数据库的状态和我的配置有关。我编写了cucumber场景以假设最初为空的数据库,并且我正在为各个场景构建小型数据集。 我正在使用DatabaseCleaner和截断策略,我的理解是,这将清除数据库。我弄错了吗?单元测