Thursday 15 May 2014

Cucumber BDD framework with Selenium WebDriver


Note|  Text in Blue [#PYTHON] and Orange [#JAVA] can be edited or mentioned important for the entire blog. All the posts are practically done by me.


Cucumber BDD Framework | selenium


1| Add an Eclipse plugin, Cucumber Eclipse feature using the below URL,
http://cucumber.github.com/cucumber-eclipse/update-site
2| Help > install new software... > paste the above url in work with text field and press enter key.
3| After the installation is done, restart Eclipse IDE.



4| Create a Maven project and add the following dependencies in POM.xml file

<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>BDD</groupId>
  <artifactId>Cucumber</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

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

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

      <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.2</version>
                <configuration>                
                    <useFile>false</useFile>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
      
 <dependencies>

   <dependency>
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId>
     <version>4.11</version>
     <scope>test</scope>
   </dependency>    

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.41.0</version>
</dependency>

<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.1.2</version>
</dependency>

<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.1.2</version>
</dependency>

<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.1.2</version>
</dependency>

<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-html</artifactId>
<version>0.2.2</version>
</dependency>

<dependency>
<groupId>info.cukes</groupId>
<artifactId>gherkin</artifactId>
<version>2.11.6</version>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
</dependency>

<dependency>
<groupId>info.cukes</groupId>
<artifactId>gherkin</artifactId>
<version>2.11.6</version>
</dependency>

<dependency>
            <groupId>com.rubiconproject.oss</groupId>
            <artifactId>jchronic</artifactId>
            <version>0.2.6</version>
            <scope>test</scope>
        </dependency>
                    
  </dependencies>
</project>


5| Now, create a test run class file

package BDD.Cucumber;

import org.junit.runner.RunWith;

import cucumber.api.junit.Cucumber;


@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty", "html:target/cucumber","json:target/cucumber.json"})
public class RunTest {

}


6| Now, create a test class file

package BDD.Cucumber;

import static org.junit.Assert.assertEquals;

import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import cucumber.api.Scenario;
import cucumber.api.java.After;

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



public class Googlesearch {

public WebDriver driver = new FirefoxDriver();

@Given("^Google page \"([^\"]*)\"$")
public void i_open_google_page_as(String URL) throws Throwable {
driver.get(URL);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

}


@When("^I enter \"([^\"]*)\" in search box$")
public void i_enter_in_search_box(String arg1) throws Throwable {
driver.findElement(By.name("q")).sendKeys(arg1);
}

@When("^I press enter key$")
public void i_press_enter_key() throws Throwable {
driver.findElement(By.name("q")).submit();

}

@Then("^I should get the results of \"([^\"]*)\"$")
public void i_should_see_results_of(String content) throws Throwable {

Thread.sleep(3000L);
boolean b = driver.getPageSource().contains(content);
Assert.assertTrue(b);
}

@After
public void after(Scenario scenario) {
driver.close();
}

}





7| Create a new source folder, "resources" under src/test/
8| Now, create a package, "BDD.Cucumber"
9| Then create a file with extension .feature
Right click BDD.Cucumber under src/test/resources > New > Other > File
10| Name the file with extension .feature; i.e., test.feature

Feature: test

@foo
Scenario: google search

Given Google page "http://www.google.com"
When I enter "Prashanth Sams" in search box
When I press enter key
Then I should get the results of "Prashanth Sams"


11| Right click on the project > Run As > maven clean
12| Again Right click on the project > Run As > maven install


Note: For more info. download my sample project from Github Link

20 comments:

  1. It was very nice article and it is very useful to Selenium learners.We also provide Cub training software online training.

    ReplyDelete
  2. Thanks for sharing this Information, Got to learn new things from your Blog on SELENIUM.
    Ref link : . http://thecreatingexperts.com/software-testing-training-in-chennai/selenium-training-in- chennai/

    ReplyDelete
  3. Thank You for this useful information and Awesome way to present your thoughts and knowledge.
    I am a beginner and I want to learn more .Keep sharing your knowledge with us.
    Selenium Interview Questions
    Selenium Tutorial
    Selenium Training in Noida

    ReplyDelete
  4. Thanks for sharing this information. It was very nice blog to learn about SAP FICO.
    http://thecreatingexperts.com/sap-fico-training-in-chennai/

    ReplyDelete
  5. Can you please extend this with 2 pages? After clicking on any link it will move to next page and verify some functionlity on 2nd page.

    ReplyDelete
  6. Thanks for the great information in your blog Selenium Training in Chennai

    ReplyDelete
  7. Thanks for sharing such a great information with us. If you are looking for Ruby on Rails experts in India then email us at: info@cryptextechnologies.com

    ReplyDelete
  8. Amazing, thanks a lot my friend, I was also siting like a your banner image when I was thrown into Selenium.When I started learning then I understood it has got really cool stuff.
    I can vouch webdriver has proved the best feature in Selenium framework.
    Thanks a lot for taking a time to share a wonderful article.

    Some of mine: Top5 Selenium WebDriver Coding Tips | Selenium FAQs

    ReplyDelete
  9. It was a great post. I really appreciate your knowledge that you have shared with. Thank you so much for sharing this blog.

    Selenium Automation Testing | Selenium with Core Java

    ReplyDelete

  10. Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.

    python Training in Bangalore | python Training in Bangalore

    ReplyDelete
  11. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care.As always, we appreciate you confidence and trust in us.
    selenium online training
    selenium with python training
    iframes in selenium
    selenium interview questions
    selenium certification

    ReplyDelete
  12. Wow this is really amazing post. Thanks for sharing the useful informative data. I appreciate your difficulty work. Keep blogging. Protractor Training in Electronic City

    ReplyDelete
  13. I am really really impressed with your writing skills as well as with the layout on your blog.
    uok bcom 2nd year result 2021-22 roll number wise

    ReplyDelete