Tuesday, July 31, 2012

Functional testing(Selenium Web driver, Java, Maven, TestNG/JUnit)

Functional testing is a black box testing, because we do not how the software works(I mean we do not see code). We know what we should click and what output should be. Yes, we have specification. So, I will not give you here theoretical overview, just create a custom sample project and will show you the main features of Selenium Java Web Driver, JUnit and lets get it done using Maven.

The first thing we have to do is to select the page we want to test. I do not like thinking too much - lets just test my blogspot page - http://jevgenimarenkov.blogspot.com/. When we click on the link byJevgeni(its on the right just below the about section) we want to get label "On Blogger since September 2010". So lets start:

1. Download and Install Selenium IDE for Firefox (http://release.seleniumhq.org/selenium-ide/1.8.1/). You can read some tutorials on the same page(http://seleniumhq.org/projects/ide/) as well. It is not prohibited!

2. Start Selenium IDE,it starts automatically recording actions that you perform. To understand that Selenium is recording your actions, make sure that red button in the right upper corner is pressed.
Go to http://jevgenimarenkov.blogspot.com/
Click on link by Jevgeni.
Right click on label (On Blogger since September 2010)
Choose assertText css=p.sidebar-item.item-key On Blogger since September 2010

3. If You watch on Selenium ID console than you will get a picture like that one. In order to confirm that you recorded a valid test than click on green button (play entire suite) browser will automatically perform recorder actions in the right order.

4.Next step is to generate Java code from this test. To generate java code press File/Export Test Case As/JUnit 4 (Web Driver). Will get the next java code:
package com.example.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class JUnitWebDrivver {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();
    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://jevgenimarenkov.blogspot.com/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testJUnitWebDrivver() throws Exception {
        driver.get(baseUrl + "/");
        driver.findElement(By.cssSelector("a.profile-name-link")).click();
        assertEquals("On Blogger since September 2010", driver.findElement(By.cssSelector("p.sidebar-item.item-key")).getText());
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }
}


5. We have to change out Java code a little in order to do it more flexible. JUnit by default searches for Firefox in Program Files, so if You have installed somewhere else than it will not find it. It is better to define the path to Firefox. But you can try without it.

     @Before
    public void setUp() throws Exception {
        FirefoxBinary binary = new FirefoxBinary(new File(
                "path//to//firefox"));
        driver = new FirefoxDriver(binary, null);
        baseUrl = "http://jevgenimarenkov.blogspot.com";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

6. Next step is to create a Maven project using Eclipse (or something else). If you do not have Maven than just install it! So You installed Maven and You created a sample project. What is your next step you ask?(at least i believe so)

7. Your next step is to add next dependencies to the pom.xml file: org.seleniumhq.selenium and junit

8. You added dependencies, you have Maven project and you have a java file somewhere on your desktop. You have to add generated by Selenium java class to the project classpath and that is it!

9. Run that class as JUnit(if you use Eclipse it is installed by default). And that fluke! firefox starts automatically and performs some actions!.

That is all. In conclusions, goes my 4 if:
- if you have questions ask them here
- if you want more tutorials ask me
- if you want to speak with me do it
- if you want to add me as a friend on Facebook do it

And of course, my code is available at GitHub - https://github.com/jevgenimarenkov/selenium-test

No comments:

Post a Comment