Skip to main content

Cucumber Datatable testing without using scenario outline and Issues



 




Before going through this blog please visit blog

The issue you might encounter while running cucumber project-

1.  you have created the cucumber all file in src/test/java and you are trying to  move it in another folder

transition might not be smooth, It will give error one fix will led to other.(depends on the setup)

2. If on running a feature file and successfully creating a stepdef file, If application still says that  few steps are still needs to be implemented or all of them(not detecting creation of step definition) then there is a high chance you have mistakenly wrote some other annotation in stepdef when compares to you feature file

eg:- feature file

@And("^user close browser$")

public void user_close_browser() throws Throwable {

Stepdef:-
            @Then("^user close browser$")

public void user_close_browser() throws Throwable {

driver.quit();


3. Duplicate StepdefinitionException- this issue is found when you have matching steps of your current  stedpdefintion with earlier created  stepdef in order to overcome this, you need to modify your gherkin language

1st  feature file

Scenario Outline:

Given user launching Chrome browser 

Given user navigates to application

When user enters username as "<username>"

When user clicks on continue

When user enters password as "<password>"

When user clicks on Login 

Then user verify login success status

And user close browser 


2nd .feature file

Scenario:

Given user launching Chrome browser 

Given user navigates to application

When user enters username and password

|abcd@gmail.com|India@1212|

When user clicks on login

Then user verify login success status

And user close browser


These above 2 files will throw error though they are meant for different application in order to curb the issue you can make your 2nd feature file as

2nd feature

Scenario:

Given user launches browser 

Given user scrolls through facebook

When user enters username and password

|abcd@yahoo.com|India@1212|

When user clicks on login

Then user verfies the status

And user quits the browser


Implementation using datatable

Here I have created a new source folder  src/test/resource  (right click on project click on source folder) and created all the files like earlier



By using Scenario outline with Examples , examples keyword meant for whole  scenario outline

when using datatable it's meant for only the line above

Testdata table created


Given user launches browser 

Given user scrolls through facebook

When user enters username and password

|priaynka@gmail.com| India@123@123 |

When user clicks on login

Then user verfies the status

And user quits the browser



Rest of the steps will be same as per scenario outline only change will be in step- 3 
Stepdef generated for this would be-

@When("^user enters username and password$")

public void user_enters_username_and_password(DataTable arg1) throws Throwable {


in scenario outline regular expression came into play here we are getting Datable instead arg1 is a variable 
you have to use raw method here which gives you complete excess of datatable and it  is stored in List of list of string
to fetch the username and password from the feature file location we need to pass object created-data with the location priyankac@yahoo.com is at 0th  row 0th column passkey at 0th  row 1st column
Similarly if there  is another entry then it would be 1st row 0th column and password at 1st row 1st column
             @When("^user enters username and password$")

public void user_enters_username_and_password(DataTable arg1) throws Throwable {

List<List<String>> data= arg1.raw();

driver.findElement(By.xpath("//input[@name='email']")).sendKeys(data.get(0).get(0));

System.out.println("user enters username");

driver.findElement(By.xpath("//input[@name='pass']")).sendKeys(data.get(0).get(1));

System.out.println("user enters password");



Your full stepdef code

public class Stepdef1 {

WebDriver driver;


@Given("^user launches browser$")

public void user_launching_Chrome_browser() throws Throwable {

      System.out.println("Launching Browser");

  System.setProperty("Webdriver.chrome.driver", "chromedriver");

    driver = new ChromeDriver();


}


@Given("^user scrolls through facebook$")

public void user_navigates_to_application() throws Throwable {

driver.get("https://www.facebook.com/");

}


@When("^user enters username and password$")

public void user_enters_username_and_password(DataTable arg1) throws Throwable {

List<List<String>> data= arg1.raw();//.raw method give you complete access to the datatble 

driver.findElement(By.xpath("//input[@name='email']")).sendKeys(data.get(0).get(0));

System.out.println("user enters username");

driver.findElement(By.xpath("//input[@name='pass']")).sendKeys(data.get(0).get(1));

System.out.println("user enters password");


}


@When("^user clicks on login$")

public void user_clicks_on_login() throws Throwable {

      driver.findElement(By.xpath("//button[@name='login']")).click();

  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

System.out.println("user clicks on login");



}


@Then("^user verfies the status$")

public void user_verfies_the_status() throws Throwable {

  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

String title=driver.getTitle();

System.out.println(title);

Assert.assertEquals("Facebook – log in or sign up", title);

System.out.println("user verified title");

}


@And("^user quits$")

public void user_close_browser() throws Throwable {

driver.quit();

}


}




Runner class


Code execution


Refresh cucumber project

Go to Index.html (steps mentioned in earlier blog)  and check the report



Drawback of this approach is if you have more then 2 entries

Then this code  won't work it will add first and 2nd username together  from feature file and enter it in "username field" section either you need to write code again of work with loop

Scenario outline is better when comes to parameterisation



Check out the execution here




    






Comments

Post a Comment

Popular posts from this blog

Cucumber - Execution of test cases and reporting

Before going through this blog please checkout blog on   Cucumber Fundamentals Cucumber is testing tool which implements BDD(behaviour driven development).It offers a way to write tests that  anybody can understand, regardless of there technical knowledge. It users Gherkin (business readable language) which helps to  describe behaviour without going into details of implementation It's helpful for  business stakeholders who can't easily read code ( Why cucumber tool,  is  called  cucumber , I have no idea if you ask me I could have named it "Potato"(goes well with everything and easy to understand 😂) Well, According to its founder..... My wife suggested I call it  Cucumber  (for no particular reason), so that's how it got its  name . I also decided to give the Given-When-Then syntax a  name , to separate it from the  tool . That's why it's  called  Gherkin ( small variety of a cucumber that's been pickled. I...

Jmeter 5.4.1- Config Elements - Part-03

  Part-01- Installation of Jmeter and HTTP's Recorder click  here Part 02--Previous blog on Assertion Config elements in Jmeter are used to configure or modify the samplers requests made to the server. These elements are added at the same or higher level of the samplers that we want to configure  Let' start with  CSV data config As the name suggest it used to read data from CSV first we need to put data in variables and then use the variables in sampler request. create a new test plan add CSV data set config Add a Thread Group and then add Sampler "Java Request"  Create a CSV file  with some data (Name and Data) and save it  Now go to Jmeter CSs data set config browse and upload the css file create Make few more changes in place of  variable name - Name and Dept Ignore first line - True Delimeter - \t (as suggested) Now move on the Sampler-" Java Request" and rename it with header elements of CSV As we have Name and d...

Beginners tutorial -:working with JMeter in Mac and windows - Part-01

  Prequisite   you should have Java downloaded in your system with Home path set under environment variables.(as of today Java version 8 and higher are required fro jmeter ) for help check out this link Note Always run the jmeter on your secondary browser,  if you give the primary browser for proxy settings then your internet connection will be disrupted for the browser as well as system For ex if you have chrome and firefox and your primary or default browser is chrome then do all the proxy setting in firefox so it won't hamper the system Internet connection  if you have safari as your default browser in your mac os then set proxy in chrome/firefox  MAC Download jmeter from the link  here click on the hypelink under section Binaries  "Apache JMeter( 5.3 ). tgz" file  for Mac   Tar file will get downloaded Double click on the tar file to unzip  once you open the folder  got to bin and search for jmeter.sh file this is a executa...