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 {
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
@When("^user enters username and password$")
public void user_enters_username_and_password(DataTable arg1) throws Throwable {
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");
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
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
Really nice content
ReplyDelete