Wednesday, 24 June 2015

Cucumber Selenium JAVA Integration (Cucumber Project Using Selenium and JAVA)

For cucumber project there are 3 important files.

1. Feature File: It has .feature extension. We can have multiple feature files according to our requirements. We can place the feature file in any folder and specify the name of the folder in test runner file e.g. features = "Feature".




2. Java Step Definition File: This file describes the feature file. This is the main java file where do write our scripts. We can implement our functions in this file.

3. Java Test Runner File: This file acts as a controller to run different feature files and java files. In this file we specify the various parameters like features = "Feature"
,glue={"testfolder"}
,monochrome = false
,plugin={"pretty", "html:out"}


If you are using maven then include following dependencies in pom.xml or you can simply add jars to your project.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>ZS</groupId>
  <artifactId>automationProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>automationProject</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    
    <dependency>
 <groupId>com.theoryinpractise</groupId>
 <artifactId>cucumber-testng-factory</artifactId>
 <version>1.0.1</version>
</dependency>
    
    <dependency>
   <groupId>com.sun</groupId>
   <artifactId>tools</artifactId>
   <version>1.6</version>
   <scope>system</scope>
   <systemPath>C:\Program Files\Java\jdk1.8.0_40\lib\tools.jar</systemPath>
 </dependency>
    
    <dependency>
 <groupId>info.cukes</groupId>
 <artifactId>cucumber-core</artifactId>
 <version>1.2.0</version>
</dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.2.0</version>
    </dependency>

    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.0</version>
    </dependency>
    <dependency>
 <groupId>info.cukes</groupId>
 <artifactId>cucumber-jvm-deps</artifactId>
 <version>1.0.3</version>
</dependency>
    
    <dependency>
 <groupId>org.mockito</groupId>
 <artifactId>mockito-all</artifactId>
 <version>1.10.8</version>
</dependency>
    <dependency>
 <groupId>cobertura</groupId>
 <artifactId>cobertura</artifactId>
 <version>1.8</version>
</dependency>
    
    
    <dependency>
 <groupId>net.masterthought</groupId>
 <artifactId>cucumber-reporting</artifactId>
 <version>0.0.24</version>
</dependency>
    
    <dependency>
 <groupId>org.mockito</groupId>
 <artifactId>mockito-all</artifactId>
 <version>2.0.1-beta</version>
</dependency>
    <dependency>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>cobertura-maven-plugin</artifactId>
 <version>2.6</version>
</dependency>
    
    
    
    
    <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.10</version>
  <scope>test</scope>
</dependency>
    
    
    
  </dependencies>
</project>



<dependency>
 <groupId>com.theoryinpractise</groupId>
 <artifactId>cucumber-testng-factory</artifactId>
 <version>1.0.1</version>
</dependency>
    
    <dependency>
   <groupId>com.sun</groupId>
   <artifactId>tools</artifactId>
   <version>1.6</version>
   <scope>system</scope>
   <systemPath>C:\Program Files\Java\jdk1.8.0_40\lib\tools.jar</systemPath>
 </dependency>
    
    <dependency>
 <groupId>info.cukes</groupId>
 <artifactId>cucumber-core</artifactId>
 <version>1.2.0</version>
</dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.2.0</version>
    </dependency>

    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.0</version>
    </dependency>
    <dependency>
 <groupId>info.cukes</groupId>
 <artifactId>cucumber-jvm-deps</artifactId>
 <version>1.0.3</version>
</dependency>
    
    <dependency>
 <groupId>org.mockito</groupId>
 <artifactId>mockito-all</artifactId>
 <version>1.10.8</version>
</dependency>
    <dependency>
 <groupId>cobertura</groupId>
 <artifactId>cobertura</artifactId>
 <version>1.8</version>
</dependency>
    
    
    <dependency>
 <groupId>net.masterthought</groupId>
 <artifactId>cucumber-reporting</artifactId>
 <version>0.0.24</version>
</dependency>
    
    <dependency>
 <groupId>org.mockito</groupId>
 <artifactId>mockito-all</artifactId>
 <version>2.0.1-beta</version>
</dependency>
    <dependency>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>cobertura-maven-plugin</artifactId>
 <version>2.6</version>
</dependency>
       
    <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.10</version>
  <scope>test</scope>
</dependency>





You can run your test by right clicking on Java Test Runner file and runnig that file as JUNIT test.

Here is the structure for your project:



Test Result report can be seen under out folder in index.html


Please find below all 3 files:

1. Feature File:



Feature: Google Testing

Scenario: Click on Gmail
 Given User is on Google Home Page
 When Click on Gmail link
 Then Gmail opens successfully





2. google_test.java

package stepDefinition;

import java.util.concurrent.TimeUnit;
import junit.framework.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class google_test {
 public static WebDriver driver;
 @Given("^User is on Google Home Page$")
 public void User_is_on_Google_Home_Page() throws Throwable {
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.google.co.in/?gfe_rd=cr&ei=g1OKVcrzFOfI8Aff75zYBw&gws_rd=ssl");
        driver.manage().window().maximize();
  }
 @When("^Click on Gmail link$")
 public void Click_on_Gmail_link() throws Throwable {
  driver.findElement(By.className("gb_ma")).click();
  }
 @Then("^Gmail opens successfully$")
 public void Gmail_opens_successfully() throws Throwable {
  Assert.assertEquals(driver.getTitle(),"Gmail");
        System.out.println("Gmail opens successfully");
 }

}




3. testRunner.java



package cucumberTest;

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


@RunWith(Cucumber.class)
@CucumberOptions(
  features = "Feature"
  ,glue={"stepDefinition"}
  ,monochrome = false
  ,plugin={"pretty", "html:out"} 
  
  )
 
public class TestRunner {
 
}


No comments:

Post a Comment