问题
I have below feature which have 2 scenarios when one scenario finish executing i want cucumbe to run 2nd scenario.Right now it when it finish executing one scenario it open another thread of browser and does not execute 2nd scenario. I dont want to open another thread, I want to execute the 2nd scenario after 1st in single browser thread.
Feature: Add New Contact As a user, I want to insert new contact using Contacts link
Scenario Outline: Insert a New Contact Given user clicks on contacts link When user clicks on link Create New Contact And user enters first name as And user enters the last name as And user enters the Email as And user enters the Agency as And user click on save button Then user should see message "The contact was successfully saved."
Examples:
|first_name||last_name||email||Agency|
|test2 | | test3 ||qa@incircuit.com||0000 - SURPLUS PROPERTY|
Scenario: create new user
Given user clicks on create new user
below is my code
public class insert_contact extends BasePage{
private static Initialize init;
private Insert_Contact contact=new Insert_Contact(driver);
public insert_contact(Initialize init) throws IOException {
super(driver);
init.Setup();
init.getEnvironmentandCustomer();
}
@Given("^user clicks on contacts link$")
public void userClicksOnContactsLink() throws Throwable {
contact.click_contacts_tab();
}
@When("^user clicks on link Create New Contact$")
public void userClicksOnLinkCreateNewContact() throws Throwable {
contact.click_create_contact();
}
@And("^user enters first name as (.+)$")
public void userEntersFirstNameAsFirst_name(String fname) throws Throwable {
contact.clickon_firstname();
this.type(contact.first_name,fname);
}
@And("^user enters the last name as (.+)$")
public void userEntersTheLastNameAsLast_name(String lname) throws Throwable {
contact.clickon_lastname();
this.type(contact.last_name,lname);
}
@And("^user enters the Email as (.+)$")
public void userEntersTheEmailAsEmail(String email_address) throws Throwable {
contact.cickon_email();
this.type(contact.email,email_address);
}
@And("^user enters the Agency as (.+)$")
public void user_enters_Agency_as(String agency){
contact.click_onAgency();
this.type(contact.Agency,agency);
}
@And("^user click on save button$")
public void userClickOnSaveButton() throws Throwable {
contact.clickon_Save();
}
@Then("^user should see message \"([^\"]*)\"$")
public void userShouldSeeMessage(String message) throws Throwable {
Assert.assertEquals("The contact was successfully saved.",verifyTextPresent(By.id("success-message")));
}
@Given("^user clicks on create new user$")
public void userClicksOnCreateNewUser() throws Throwable {
contact.click_onCreateNewUser();
}
}
回答1:
The simple answer to your question is that each scenario reruns your driver creation code which creates a new instance of the browser. I don't see an @After method so the browser instance created by the first scenario never has the quit() method run on it thus leaving it open when the second scenario runs.
There are two general ways to handle this.
Close (quit()) the browser instance in an @After method so that a fresh one can be created at the start of the second scenario.
Have a test around the creation of your driver and only create a new instance if (driver == null). The second scenario will then reuse the same browser session as the first scenario. Be sure to include:
driver.manage().deleteAllCookies();
来源:https://stackoverflow.com/questions/41632886/running-all-cucumber-scenarios-for-a-feature-in-one-browser-session