Friday, December 18, 2015

JVM Cucumber Hooks Part 1

As the name itself suggests hooks are used to hook any feature or scenario. In a simpler terms you can create a function and then hook that into any feature or scenario.

There are two types of hooks you can create.  Please not there ain't such categories, I have just divided them into following categories for better understanding.
  • Generic Hooks:  Hooks that executes before or after each and every scenario.
  • Specific Hooks: Hooks that executes before or after every specific(mentioned) scenario or feature. 

Now let's see how to implement this, for this I am going to use the same example I used in the last blog post. if you haven't read that yet, please visit it here. 

Generic Hooks

if you will check the code of the previous post, we were creating the driver object, in the class file as we had only one test case(scenario).  if we have multiple scenarios and we want to open new browser before each and every scenario and want to close after each and every scenario we can utilize the hooks.

In the following example, I am going to create a hook (setUp method) that will be called before each and every scenario. this method will open a firefox browser and will navigate to site home page. I will also create an another hook (tearDown method) which will be called after each and every scenario. This method will close the browser after each and every scenario.

Hooks.java
public class Hooks extends WebDriverManager{

    @Before
    public void setUp(){
        driver = new FirefoxDriver();
        wait = new WebDriverWait(driver, 30);
        driver.get("https://en.wikipedia.org");
        driver.manage().window().maximize();
    }

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

WebDriverManager.java
public class WebDriverManager {
    public static WebDriver driver;
    public static WebDriverWait wait;
}

HomePage.java
public class HomePage extends WebDriverManager{
 
    @When("^I search \"(.*?)\"$")
    public void i_search(String arg1) throws Throwable {
        WebElement search_box = driver.findElement(By.id("searchInput"));
        search_box.sendKeys(arg1);
        search_box.submit();
    }

    @Then("^I should get \"(.*?)\" on page$")
    public void i_should_get_on_page(String arg1) throws Throwable {
        Assert.assertTrue(driver.findElement(By.xpath("//body")).getText().contains(arg1));
    }
}

wkipedia_search.feature
@Search
Feature: Verify WikiPedia Search

  @Regression @Functional
  Scenario: Verify Wikipdia for indirect posts-BDD
  When I search "BDD"
  Then I should get "Behavior-driven development" on page

  @Regression
  Scenario: Verify Wikipdia for indirect posts-TDD
  When I search "TDD"
  Then I should get "Test-driven development" on page

Tuesday, May 5, 2015

BDD (Cucumber) with Selenium Webdriver

In the following post I am going to explain how to setup cucumber on Java with selenium to use for Behavior Driven Development.

I am going to use IntelliJ for writing this test cases, however you can choose eclipse or any other IDE as well.

How to create project.

I have also hosted the demo project on GitHub you can access that using following link 


  • Go to File >> New >> Project and select Maven as shown in the screenshot
  • Provide GroupId and ArtifactID
  • Provide the Project Name, After this step, IntelliJ will create new project for you. 

  • Add Maven Dependencies.

    We need to add Cucumber, Selenium and JUnit dependencies to pom.xml. Open pom.xml and add the following code to it.

    <dependencies>
     <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>2.40.0</version>
     </dependency>
    
     <dependency>
      <groupId>info.cukes</groupId>
      <artifactId>cucumber-java</artifactId>
      <version>1.2.2</version>
     </dependency>
    
     <dependency>
      <groupId>info.cukes</groupId>
      <artifactId>cucumber-junit</artifactId>
      <version>1.2.2</version>
     </dependency>
    
     <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
    
     </dependency>
    </dependencies>
    

    Automating Your First Test case


    I am going to automate following test case for this BDD (Cucumber) demo
    1. Open wikipedia.com website 
    2. Search for BDD 
    3. Result page should contain Behavior-driven development text. 
    Now before we begin writing test cases, let's create proper packages. I have created the packages in following, however you can create the way you like. 

    Just make sure that all your code (directly or indirectly) is under test package. 

    The first thing we need to do, to write cucumber tests, is to convert existing test case into Gherkin Syntax.  In the resources folder create one package and inside this package create one feature file, mine is called wikipedia_search.feature however you can give the name you want. 

    Feature: Verify WikiPedia Search
    
      Scenario: Verify Wikipdia for indirect posts
      When I search "BDD"
      Then I should get "Behavior-driven development" on page
    

    Feature:, is a keyword through which you can specify which feature you are testing
    Scenario:, is a keyword though which you can specify of which scenario following test cases belong to
    Your test cases start after the scenario line. When specifies the step we need to perform and Then specifies the part we need to verify.

    Now the next thing we need to do is, implement this test cases and write selenium code for it. however before that you need to write a Java class which runs your test cases. Create a java class under test\java\com.gaurang package and paste the following code.

    Please create one file with following code
    @RunWith(Cucumber.class)
    @CucumberOptions(plugin={"pretty", "html:target/cucumber-html-report"})
    public class RunAllTests {
    
    }
    

    Now everything is in place, let's write the selenium code for Gherkin statement we wrote above. There are actually two way to do it.

    1. Run the file created in above step, and on the console you will be able to see something like below. copy this code and paste into the class file. 
    2. You can open the feature file and press ALT+Enter key combination, this will ask you to choose the file name and then it will create the empty implementation of all the steps in that file. 
    Once this is done, the only thing remain is to write corresponding selenium code. 

    Report: 

    Cucmber generates it's own HTML report, which look like this. 

    Cucumber HTML Report

    Tuesday, March 17, 2015

    Selenium WebDriver with Python

    In the following blog I will show you how to setup and write your first test case using Webdriver in Python Language.

    Install Python:

    If you are using Linux or Mac you need not do this as Python comes pre-installed on these OS. however if you are using windows (most of us do), Please follow the steps mentioned blow to install python on windows platform.
    • Download the Python from following location for your windows OS and install it.
      https://www.python.org/downloads/windows/
    • Once installed you need to set the following paths in your system PATH variable. (Python folder name may be depends on the version of the Python you have installed)
      • C:\Python27
      • C:\Python27\Scripts
    • Now, let's test if Python has installed or not. Open command prompt (CMD) and enter following command, if it shows you version number then python has successfully installed.
      python --version

    Now let's Install WebDriver for Python

    Installing WebDriver in python is different than installing in Java, actually Installing webdriver for Python is pretty simple.  To install WebDriver, Just open command prompt (CMD) and enter following command. 
    • pip install selenium

    First WebDriver Test in Python.

    test_google.py
    from selenium import webdriver
    import unittest
    
    class TestGoogle(unittest.TestCase):
    
        def setUp(self):
            self.driver = webdriver.Firefox()
            self.driver.get("https://google.com")
    
        def tearDown(self):
            self.driver.quit()
    
        def test_search(self):
            search_edit_box = self.driver.find_element_by_name("q")
            search_edit_box.send_keys("webdriver with python")
            search_edit_box.submit()
    
    if __name__ == '__main__':
        unittest.main()

    Now let's see How to run Above Webdrive Test

    • First of all copy above file and paste it in any file and save as test_google.py
    • Open command prompt (CMD) and navigate to folder where you have stored this file. 
    • Now run the following command
      python test_google.py