Testing different Scenarios in “Selenium Cucumber with Java” without opening new Web browser -


i have feature file have 2 scenarios

feature: login online store  scenario: login successful valid credentials     given user on home page     when user navigates login page     , user provides username , password     message displays login  scenario: user logout     when user logouts application     message displays logout 

every time run runfeatures.java file, after first scenario, driver open new browser execute next scenario. can use same browser execute 2nd scenario? below code:

runfeatures.java

package cucumbertest;  import org.junit.runner.runwith;  import cucumber.api.cucumberoptions; import cucumber.api.junit.cucumber;  @runwith(cucumber.class) @cucumberoptions(features="src/test/java/features/"             ,glue={"steps"}             ,dryrun=false             ,monochrome=false)  public class runfeatures { } 

clientsteps.java:

package steps;  import java.util.concurrent.timeunit;  import org.openqa.selenium.by; import org.openqa.selenium.webdriver; import org.openqa.selenium.firefox.firefoxdriver;  import cucumber.api.java.en.*; import pages.homepage; import pages.loginpage;  public class clientsteps {     webdriver driver=new firefoxdriver();     @given("^user on home page$")     public void user_is_on_home_page() throws throwable {         new homepage(driver).user_is_on_home_page();     }      @when("^user navigates login page$")     public void user_navigates_to_login_page() throws throwable {         new homepage(driver).user_navigates_to_login_page();     }      @when("^user provides username , password$")     public void user_provides_username_and_password() throws throwable {         new loginpage(driver).user_provides_username_and_password();     }      @then("^message displays login successfully$")     public void message_displays_login_successfully() throws throwable {         new loginpage(driver).message_displays_login_successfully();     }      @when("^user logouts application$")     public void user_logouts_from_application() throws throwable {         new loginpage(driver).user_logout_from_the_application();     }      @then("^message displays logout successfully$")     public void message_displays_logout_successfully() throws throwable {         new loginpage(driver).message_displayed_logout_successfully();     } } 

homepage.java file

package pages;  import java.util.concurrent.timeunit;  import org.openqa.selenium.by; import org.openqa.selenium.webdriver; import org.openqa.selenium.firefox.firefoxdriver;  import cucumber.api.java.en.given; import cucumber.api.java.en.when;  public class homepage {     webdriver driver;       public homepage(webdriver driver)     {         this.driver=driver;           }     public void user_is_on_home_page() throws throwable {          driver.manage().timeouts().implicitlywait(10, timeunit.seconds);         driver.manage().window().maximize();         driver.get("http://www.store.demoqa.com");      }      public void user_navigates_to_login_page() throws throwable {         driver.findelement(by.xpath(".//*[@id='account']/a")).click();     }  } 

loginpage.java

package pages;  import org.openqa.selenium.by; import org.openqa.selenium.webdriver; import org.openqa.selenium.firefox.firefoxdriver;  import cucumber.api.java.en.then; import cucumber.api.java.en.when;  public class loginpage {     webdriver driver;     public loginpage(webdriver driver)     {         this.driver=driver;     }      public void user_provides_username_and_password() throws throwable {         // first data of set (first row + first column)         driver.findelement(by.id("log")).sendkeys("tri.nguyen");         // first data of set (first row + second column)         driver.findelement(by.id("pwd")).sendkeys("test@123");         driver.findelement(by.id("login")).click();     }       public void message_displays_login_successfully() throws throwable {         system.out.println("login successfully");     }      public void user_logout_from_the_application() throws throwable {         driver.findelement (by.xpath(".//*[@id='account_logout']/a")).click();     }       public void message_displayed_logout_successfully() throws throwable {         system.out.println("logout successfully");         driver.quit();     } } 

as practice, test cases should atomic. automation test case shouldn't dependent on test case browser instance, data etc.

you rather should close browser windows after every test case , open browser again new instance next test case.

use @before , @after in stefdef file achieve this.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -