LambdaTest Community

Find answers, support, and inspiration from other users

Questions
LambdaTest Community
Answered

Why should we use Gherkin with Selenium testing? What are its benefits?

I want know why should we use Gherkin with Selenium testing? What are its benefits. Can anyone please help me out.

Up Vote Down Vote 0 Votes
Posted 3 months ago

Answers


Selenium is a widely used tool for functional testing across top organizations as it is easy to integrate with Gherkin and makes understanding and automating of the flow both easy and possible to be done at the same time.

By doing so one makes it easy to maintain test cases which also serves as proper documentation for all the Stakeholders and Analysts. Selenium is easy to integrate and a widely liked automation tool adds up to the usage of Cucumber. Together they make up a framework that is very easy to set up and has a low maintenance cost.

Moreover, the ability to run feature files parallelly is another advantage. Also, both Gherkin and Selenium can be coded in Java for writing the glue code for steps and browser automation respectively.

Benefits Of Gherkin In Selenium Testing With Example:

Here I will explain the usage of Gherkin for Selenium Testing with some examples and the framework along with file structure by means of the following example.

Let’s take a test scenario with the development of a web application for an organization, let’s say ’Shopping.com’, and the team is discussing a process flow of order placement by a customer.

First Step, for a customer to place an order, he/she should navigate to the website and login.

Given Customer navigates to the website  
And Customer login to his account

After that customer searches for a product and click on add to cart button

when customer searches for laptop  
And clicks on add to cart button 

The customer reaches the cart page and is able to click place order button

Then Customer is redirected to cart page  
And Customer is able to click the place order button.

After the flow is passed on to the automation tester, he uses it to write automation cases. Further, he adds automation code steps for the discussion file by converting it to a feature file.


Feature : Place new order  
   As a customer, I want to place an order for searched item  
      Scenario : Place an order for the required product  
            Given Customer navigates to the website  
            And Customer login to his account  
            When customer searches for laptop  
            And clicks on add to cart button  
            Then Customer is redirected to cart page  
            And Customer is able to click the place order button 

Now to make these steps work I’ll write step definition code

package Cucumber.stepDefinitions;  
public class PlaceOrderStepDefs {  
      @Given(“Customer navigates to the website”)  
      public void navigateToWebsite(){  
      }  
      @And(“Customer login to his account”)  
      public void customerLogin(){  
      } 
      @When(“customer searches for laptop”)  
      public void searchForLaptop(){  
      } 
      @And(“clicks on add to cart button”)  
      public void clickAddToCart(){  
      }        
      @Then(“Customer is redirected to cart page”)  
      public void redirectionToCartPage(){  
      } 
      @And(“Customer is able to click the place order button”)  
      public void clickPlaceOrderButton(){  
      }  
}

Now the point to note here is that there would be some data related to customers that is required and might be used by other feature files and scenarios as well. So it makes sense to create a separate file for such data called a properties file. Also, steps like loading this property file, initializing the driver, closing the browser after each scenario also needs to be done and this would be common to all tests. So it makes sense to create a base steps file and inherit that to all step definition files.

#  APP        
url=http://www.shopping.com/
#  Selenium      
webdriver.chrome.driver=resources/chromedriver  
#  CUSTOMER      
customerId=12345  
customerUsername=steven  
customerPassword=password123
public class BaseTests {  
    public static WebDriver driver;  
      @BeforeClass  
      public static void initialiseDriverAndProperties() {  
            loadTestProperties();  
            driver = new ChromeDriver();  
            driver.manage().window().maximize();  
       }  
      @AfterClass  
      public static void closeBrowser() {  
            driver.quit();  
      }  
      private static void loadTestProperties(){  
            Properties properties = System.getProperties();  
            try {  
                  properties.load(  
                  new FileInputStream(new File("resources/test.properties")));  
            } catch(Exception ex) {  
                  ex.printStackTrace();  
                  System.exit(-1);  
            }  
      }  
}

Having written BaseSteps.java and test.properties file now let us complete the step definition file

package Cucumber.stepDefinitions;  
public class PlaceOrderStepDefs extends BaseSteps{  
      @Given(“Customer navigates to the website”)  
      public void navigateToWebsite(){  
            driver.get(System.getProperty(“url”));  
      }  
      @And(“Customer login to his account”)  
      public void customerLogin(){
            driver.findElement(http://By.id(“username”)).enterData(“username”);  
            driver.findElement(http://By.id(“password”)).enterData(“password”);  
            driver.findElement(http://By.id(“login”)).click();  
      }  
      @When(“customer searches for laptop”)  
      public void searchForLaptop(){  
            driver.findElement(http://By.id(“searchBox”)).enterData(“Laptop”);  
            driver.findElement(http://By.id(“searchButton”)).click();  
      }  
      @And(“clicks on add to cart button”)  
      public void clickAddToCart(){  
            driver.findElement(http://By.id(“addToCart”)).click();  
      }  
      @Then(“Customer is redirected to cart page”)  
      public void redirectionToCartPage(){  
            Assert.assertTrue(driver.findElement(http://By.id(“cartPageHeader”)).isDisplayed());  
      }  
      @And(“Customer is able to click the place order button”)  
      public void clickPlaceOrderButton(){  Assert.assertTrue(driver.findElement(http://By.id(“placeOrderButton”)).isClickable());  
      }  
}

Finally, a step would be to a runner file to run this feature.

package Cucumber;  
@RunWith(Cucumber.class)  
@CucumberOptions(  
      plugin = {"pretty"},  
      glue = {"Cucumber.stepDefinitions"},  
      features = {"src/test/java/Cucumber/features"})  
public class CucumberTestRunner { 
}

So with this example, it is quite evident how easy it is to use Gherkin for Selenium Testing to make automation better.

Up Vote Down Vote 0 Votes
Posted 3 months ago