Cucumber Selenium Maven and Junit

cucumber

To execute our first cucumber script for selenium we need to follow the below steps –

01. Create a maven project in eclipse
02. Add the following dependency on POM.xml

<dependencies>
<dependency>
      <groupId>info.cukes</groupId>
      <artifactId>cucumber-java</artifactId>
      <version>1.1.2</version>
      <scope>test</scope>
  </dependency>
<dependency>
     <groupId>info.cukes</groupId>
     <artifactId>cucumber-junit</artifactId>
     <version>1.1.2</version>
     <scope>test</scope>
</dependency>
<dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.11</version>
     <scope>test</scope>
</dependency>
<dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.45.0</version>
</dependency>  
</dependencies>

03. Now create “StepDefinition.java”, “CucumberExecutor.java” and “Test_FF_01.feature” file in the following structure –

04. “StepDefinition.java” should contain the following code –

package org.sample;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class StepDefinition {
	public WebDriver driver ; 
    @Given("^open firefox$")
    public void open_firefox(){
    	driver = new FirefoxDriver();
    	driver.manage().window().maximize();
    }
    @Given("^open ie$")
    public void open_ie(){
    	System.setProperty("webdriver.ie.driver", "src/test/resources/IEDriverServer.exe");
    	driver = new InternetExplorerDriver();
    	driver.manage().window().maximize();
    }
    @And("^browse google$")
    public void browse_google(){
    	driver.get("http://google.co.in");
    }
    @When("^I enter the keyword$")
    public void I_enter_the_keyword(){
    	
    	WebElement searchBox = driver.findElement(By.xpath(".//*[@id='lst-ib']"));
    	WebElement searchBtn = driver.findElement(By.xpath(".//*[@id='sblsbb']/button"));
    	searchBox.clear();
    	searchBox.sendKeys("tanmaysarkar.com");
    	searchBtn.click();
    	
    }
    @And("^take first website from the result$")
    public void take_first_website_from_the_result() throws InterruptedException{
    	Thread.sleep(3000);
    	WebElement firstResult = driver.findElement(By.xpath(".//*[@id='rso']/div[2]/li[1]/div/h3/a"));
    	firstResult.click();
    	
    }
    @Then("^I should be my desired page$")
    public void I_should_be_my_desired_page(){           
    	System.out.println("Page Title should contain Tanmay : " + driver.getTitle().contains("Tanmay"));
    	driver.quit();
    }
}

05. “CucumberExecutor.java” should contain the following sample code –

package org.sample;

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

@RunWith(Cucumber.class)
@Cucumber.Options(format={"pretty","html:reports/test-report"},tags= "@smokeTest")
public class CucumberExecutor {

}

06. Open “Test_FF_01.feature” file and provide the steps

@smokeTest
Feature: search a website in google

Scenario: Provide keyword and navigate to website

Given open firefox 
And browse google
When I enter the keyword 
And take first website from the result
Then I should be my desired page

07. Now execute the executor and if everything goes well the output should be –

That’s it. :)

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *