我使用Selenium和黄瓜以及Gradle和TestNG。对多个参数运行相同的方案(示例)。我面临的问题是,对于第一个断言成功,浏览器(驱动程序)关闭。但是对于后续的断言失败,浏览器(驱动程序)不会关闭,而是为下一组值启动新的浏览器实例。
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
1st level = partner_company
2nd level = partner
3rd level = partner_deals
请建议如何过来。有没有办法可以在这里使用@BeforeTest @AfterTest?
答案 0 :(得分:1)
您应该详细了解黄瓜钩。
针对您的具体问题,您可以使用@Afte
r和@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()
}