Showing posts with label BDD. Show all posts
Showing posts with label BDD. Show all posts

Selenium-BDD

 A complete program to run Maven BDD framework in Selenium:

//Feature File

             Feature: Test the functionality A


             @SmokeTest @SystemTest

             Scenario Outline: Test the functionality A

             Given open the browser

             When the URL "https://www.google.com" is entered

             And enter <UID> and <PWD> and click Login

             Then user should be logged in

            

             Examples:

             |UID|PWD|

             |RamSmith|Ram@123|

 

//Step Definition     

public class StepDefinition {

       WebDriver driver = null;

             @Before  //Global hook

             public void precondition() {

                    System.setProperty("webdriver.chrome.driver", "C:\\path\\chromedriver.exe");

                    driver = new ChromeDriver();

                    driver.manage().window().maximize();

                    driver.manage().deleteAllCookies();

             }

             @Before("@SystemTest")//tagged hook

             public void precondition() {

                        System.out.println(" This is Tagged Hook ");

             }

 

             @After //Global hook

             public void postCondition() {

                    driver.quit();

             }

 

             @Given("^open the browser$")

             public void openBrowser() throws Exception {

                    System.out.println("The browser is open");

             }

            

             @When("^the URL \"(.*)\" is entered$")

             public void brwserEntered(String url) throws Exception {

                    driver.get(url);

             }

            

             @And("^enter \"(.*)\" and \"(.*)\" and click Login$")

             public void brwserEntered(String uid, String pwd) throws Exception {

                    driver.findElement(By.xpath("..")).sendKeys(uid);

                    driver.findElement(By.xpath("..")).sendKeys(pwd);

                    driver.findElement(By.xpath("..")).click();

             }

            

             @Then("^user should be logged into the page$")

             public void insideaPge() throws Exception {

                    String pageTitle = driver.getTitle();

                    Assert.assertEquals(pageTitle, "Googlelogin", "This is not correct");

             }      

 

//Test Runner

             @RunWith(Cucumber.class)

             @CucumberOptions(

                           features = "C:\\asdf.feature",

                           glue = {"StepDefinition"},

                           dryRun = false ,

                           format = {"pretty","json:test-output/report.json","...xml","....html:test-output/report1.html"},

                           monochrome = true,

                           tags = {"~@SmokeTest", "@SystemTest"},//ignore @SmokeTest

                           strict = true

                          

                           )

             public class TestRunner() {

                   

             }

 

        

 

      


Global Hooks and Tagged Hooks in BDD cucumber


BDD Framework, HOOKs in Cucumber:
Global Hooks
Use @Before for common steps running before the actual test ,ie, precondition.
Use @After for common steps running after the actual test, ie, post condition.
@Before and @After are global hooks.
Example:
Let’s take a feature file:
Note: You can create 1 test case and parameterize with example. Three test cases are creates just to show, three different test cases are run in this.
@sprint7
Feature: Sprint7

@finance
Scenario Outline: TestCase1
#Precondition:User is on the facebook page. After execution, close the session.
    And Enter "<uid_ColumnName>" and "<pwd_ColumnName>" and click login
    Then Validate screen


Examples:
      | uid_ColumnName | pwd_ColumnName |
      | realuid1       | realpwd1       |

@humanResource
 Scenario Outline: TestCase2
#Precondition:User is on the facebook page. After execution, close the session.
    And Enter "<uid_ColumnName>" and "<pwd_ColumnName>" and click login
    Then Validate screen

Examples:
      | uid_ColumnName | pwd_ColumnName |
      | realuid2       | realpwd2       |
     
@marketing
Scenario Outline: TestCase3
#Precondition:User is on the facebook page. After execution, close the session.
    And Enter "<uid_ColumnName>" and "<pwd_ColumnName>" and click login
    Then Validate screen

Examples:
      | uid_ColumnName | pwd_ColumnName |
      | realuid3       | realpwd3      |

Step Definition:

package stepDefinition;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Then;

public class StepDefinitionClass {

      WebDriver driver;

      @Before
      public void OpenBrowserOpenfacebook() throws Throwable {
            System.setProperty("webdriver.chrome.driver",
                        "C:\\Path\\chromedriver.exe");
            driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.get("https://facebook.com/");
      }

      @After
      public void Close_Session() {
            driver.quit();
      }

      @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();
      }

      @Then("^Validate screen$")
      public void Validate_screen() throws Throwable {
            System.out.println("Screenshot taken");
            Thread.sleep(5000);
      }

}

Test Runner :
package testrunner;

import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@Cucumber.Options(
features="\\Users\\Badri\\Documents\\Selenium\\CucumberTest\\features\\featureFile.feature",
glue={"stepDefinition"},
format={"pretty"},
monochrome=true
)
public class TestRunner {

}

Tagged Hooks
Hooks for particular testcase or testcases
Feature file:
@sprint7
Feature: Sprint7

      @finance
  Scenario Outline: TestCase1
#Precondition:User is on the facebook page. After execution, close the session.
    And Enter "<uid_ColumnName>" and "<pwd_ColumnName>" and click login
    Then Validate screen


    Examples:
      | uid_ColumnName | pwd_ColumnName |
      | realuid1       | realpwd1       |

      @humanResource
  Scenario: TestCase2     #Outline should be added here to use example
#Hooks: Global Precondition:User is on the facebook page. After execution, close the session.
#Tagged Hooks:Precondition for this test case: User is on forgot pwd page
#Tagged Hooks: Click serach is post condition.
    And Enter mobile number

     
  @marketing
  Scenario Outline: TestCase3    #Outline should be added here to use example
#Precondition:User is on the facebook page. After execution, close the session.
    And Enter "<uid_ColumnName>" and "<pwd_ColumnName>" and click login
    Then Validate screen

    Examples:
      | uid_ColumnName | pwd_ColumnName |
      | realuid3       | realpwd3      |


Step Definition:
package stepDefinition;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Then;

public class StepDefinitionClass {

      WebDriver driver;

      @Before
      public void OpenBrowserOpenfacebook() throws Throwable {
            System.setProperty("webdriver.chrome.driver",
                  "C:\\Users\\Badri\\Documents\\Selenium\\chromedriver_win32\\chromedriver.exe");
            driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.get("https://facebook.com/");
      }

      @After
      public void Close_Session() {
            driver.quit();
      }

      @Before("@humanResource")//Tagged Hooks
      public void fogotPassword(){
            driver.findElement(By.xpath("//a[text()='Forgot account?']")).click();
      }
     
      @After("@humanResource")//Tagged Hooks
      public void clickSearch() {
      driver.findElement(By.xpath("//input[@value='Search']")).click();
      }
     
      @And("^Enter mobile number$")
      public void enterMobile() throws Throwable {
      driver.findElement(By.xpath("//input[@id='identify_email']")).sendKeys("123456");
      }


      @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();
      }

      @Then("^Validate screen$")
      public void Validate_screen() throws Throwable {
            System.out.println("Screenshot taken");
            Thread.sleep(5000);
      }

}

Test Runner:
package testrunner;

import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@Cucumber.Options(
features="\\Users\\path\\features\\featureFile.feature",
glue={"stepDefinition"},
format={"pretty"},
monochrome=true
)
public class TestRunner {

}

We can add priority to Hooks: 

@Before(order=0)
@After(order=0)
@Before(order=1) 
@After(order=1)