BDD framework Script Example:
Below example opens logs into facebook.com and close the
session.
Needs to create:
1.
Feature file
2.
Test Runner Package and Class
3.
Test Definition Package and Class
1.
Feature file
Create a folder and add Feature file in the folder. Feature
file should have extension “.feature”
Feature file , this file should have extenshion “.feature”
Feature: Sprint7
Scenario: TestLogin
Given Open chrome
browser and maximize
And Open facebook.com
And Enter userid and password and
click login
Then Validate screen
And Close the session
2. Test
Runner Class:
Create a package inside src and add a Test runner
class:
package testrunner;
import
org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
//@Cucumber.Options(features="Path
of feature file folder", glue={"path
of Test Definition package"})
@Cucumber.Options(features="features",glue={"testDefinition"})
public class TestRunner {
}
3. Test
Definition class:
Create a package inside src and add Test definition
class:
package testDefinition;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.chrome.ChromeDriver;
import
cucumber.api.java.en.And;
import
cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
public class FacebookTestDef
{
WebDriver
driver;
@Given("^Open
chrome browser and maximize$")
public void
Open_chrome_browser_and_maximize() throws Throwable {
System.setProperty("webdriver.chrome.driver","path of
chromeDriver");
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@And("^Open
facebook.com$")
public void
Open_facebook_com() throws Exception {
driver.get("https://facebook.com/");
}
@And("^Enter
userid and password and click login$")
public void
Enter_userid_and_password_and_click_login() throws Exception {
driver.findElement(By.xpath("//input[@id='email']")).sendKeys("userID");
driver.findElement(By.xpath("//input[@id='pass']")).sendKeys("Password");
driver.findElement(By.xpath("//input[@type='submit']")).click();
}
@Then("^Validate
screen$")
public void
Validate_screen() throws Throwable {
//Write
Code to take screenshot.
}
@And("^Close
the session$")
public void Close_Session()
{
driver.quit();
}
}
Comments About Running:
Without glue:
The respective method format for Test Definition/Step definition
comes after you give path for featurefile folder(do not give glue) and run the
test runner class as Junit test. You can copy the console output and use in
Test/Step definition class. It makes the job easy and quick. It automatically
gives you suggestion in console like below:
You can implement missing steps
with the snippets below:
@Given("^Open chrome browser
and maximize$")
public void
Open_chrome_browser_and_maximize() throws Throwable {
// Express the Regexp above with the code
you wish you had
throw new PendingException();
}
@And("^Open
facebook.com$")
public void Open_facebook_com()
throws Throwable {
// Express the Regexp above with the code
you wish you had
throw new PendingException();
}
@And("^Enter userid and
password and click login$")
public void
Enter_userid_and_password_and_click_login() throws Throwable {
// Express the Regexp above with the code
you wish you had
throw new PendingException();
}
@Then("^Validate
screen$")
public void Validate_screen()
throws Throwable {
// Express the Regexp above with the code
you wish you had
throw new PendingException();
}
@And("^Close the
session$")
public void Close_the_session()
throws Throwable {
// Express the Regexp above with the code
you wish you had
throw new PendingException();
}
With glue:
After adding Test/Step definition and glue, run the test
runner class as Junit Test.
If you have more than one test cases then you can add in
feature file and run.
Feature file:
Feature: Sprint7
Scenario: TestCase1
Given Open chrome
browser and maximize
And Open facebook.com
And Enter userid and password and
click login
Then Validate screen
And Close the session
Scenario: TestCase2
Given Open chrome
browser and maximize
And Open facebook.com
And Enter userid and password and
click login
Then Validate screen
And Close the session
Parameterization in BDD framework without example:
In this , we will pass the parameters value from feature
file. Let’s parameterize userid and password.
Feature: Sprint7
Scenario: TestCase1
Given Open chrome
browser and maximize
And Open facebook.com
And Enter “userid” and “password” and
click login
Then Validate screen
And Close the session
Run the code as it is.
If you see in the console, you see the suggestion like
below:
@And("^Enter
\"([^\"]*)\" and \"([^\"]*)\" and click
login$")
public void
Enter_and_and_click_login(String arg1, String arg2) throws Throwable {
// Express the Regexp above with the code
you wish you had
throw new PendingException();
}
Use above console output to update code in your Test/Step definition:
@And("^Enter
\"([^\"]*)\" and \"([^\"]*)\" and click
login$")
public void
Enter_and_and_click_login(String userID, String password) throws Throwable {
driver.findElement(By.xpath("//input[@id='email']")).sendKeys(userID);
driver.findElement(By.xpath("//input[@id='pass']")).sendKeys(password);
driver.findElement(By.xpath("//input[@type='submit']")).click();
}
And update feature file with actual userID and password like
below and run:
Feature: Sprint7
Scenario: TestCase1
Given Open chrome
browser and maximize
And Open facebook.com
And Enter “JohnSmith” and “Great123”
and click login
Then Validate screen
And Close the session
Parameterization in Cucumber with Example, in other word, applying Data Driven approach in Cucumber.
For this you just need
to update in feature file. Below script with run three times , each time
picking a set of uid and pwd from the Examples section.
Feature: Sprint7
Scenario Outline: TestLogin #Outline should
be added here to use example
Given Open chrome browser and maximize
And Open facebook.com
And Enter "<uid_ColumnName>" and
"<pwd_ColumnName>" and click login
Then Validate screen
And Close the session
Examples:
| uid_ColumnName | pwd_ColumnName |
| realuid1 | realpwd1 |
| realuid2 | realpwd2 |
|
realuid3 | realpwd3 |
No comments:
Post a Comment