Reputation: 448
I'm using Selenium with Cucumber along with Gradle and TestNG. The same scenario runs for multiple parameters (Examples). Issue I'm facing is that for the first assertion success, browser (driver) closes. But for subsequent assertions failures, browser (driver) does not close, instead a new browser instance is launched for next set of values.
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|[email protected]|
| |Ashish Deshmukh|[email protected]|
|just some givverish message| |[email protected]|
|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: [email protected]
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 "[email protected]" # 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: [email protected]
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 "[email protected]" # 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
Kindly suggest how to over come this. Is there a way I can use @BeforeTest @AfterTest in this?
Upvotes: 1
Views: 7375
Reputation: 12930
You should read about cucumber hooks in details.
For your specific problem, you can use @Afte
r and @Before
. This works for Junit Runner, never tested with TestNG but should work I guess as hooks are part of Cucumber and not 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()
}
Upvotes: 1