Testing web applications

with selenium webdriver


Jakub Cechacek

Senior Quality Engineer, Red Hat

OUTLINE

  • Testing in general

  • Selenium

  • Selenium WebDriver

  • Taking it a step further

 QA & Testing

Testing is the process of exercising software with the intent of finding errors.

  • Ensure that what we build does what it's supposed to do 
  • Something that works when used by few may break when used by many
  • There is always a chance that user will to that
  • There are a lot of different devices/systems/browsers
  • We should always strive to deliver the best application we can

Test types

Unit testS

  • Individual test for isolated components
  • Run fast

Integration TESTS

  • Cooperation of multiple units
  • Complex test setup 
  • Not so fast

System tests / End-TO-END tests

  • The ultimate integration test
  • Long running
  • As close to end user as possible

Selenium

WebDriver

What is selenium?

IDE

  • Firefox extension
  • Recording & Playback of test scenarios
  • Includes WebDriver 
  • Useful for exploratory testing and prototyping 

WEBDRIVER

  • Uses browser's native automation support
  • Language-specific bindings for Driving a web browser
  • communicates with each browser natively
  • compatible with most Browsers (Chrome, FF, IE, Safari, Android, etc.)

Standalone Server

  • Allows you to control remote WebDriver
  • Allows parallel execution of tests

What is selenium?

History

2004

2008

2016

2009

Jason Huggins creates Selenium

Simon Stewart starts WebDriver

Selenium WebDriver / Selenium 2.0

2013

Selenium 3.0

First draft of WebDriver W3C specification

WHAT IT CAN DO

Act like a user

  • Click, Drag & Drop, Fill in textareas and inputs
  • Navigate the page
  • Upload files
  • Conditionally wait (e.g. for elements to appear)
  • Operate multiple browser windows

Run Tests across browsers

  • Drive "real" browsers

            Firefox, Chrome, Internet Explorer, Safari, Opera...

  • Drive "headless" browsers

            PhantomJS, HtmlUnit

HOW webdriver WORKS

HOW webdriver WORKS

HOW webdriver WORKS

WHAT IT CAN'T DO

Outside the browser

Inside the browser

  • Interact with not visible elements
  • Consistently wait for ajax (e.g. angular)
  • Visual tests / UX

How To LOCATE ELEMENTS

tag name

 

tag attributes

CSS selector

XPATH

driver.findElements(By.tagName("a"));
driver.findElement(By.id("id"));
driver.findElement(By.name("name"));
driver.findElement(By.className("name"));
driver.findElement(By.cssSelector("form input#email"));
driver.findElement(By.cssSelector("#register input[class^='mandatory']"));
driver.findElement(
    By.xpath("//form[@id='register']/input[starts-with(@class,'mandatory')]")
);
@Test
public void userCanSearch() {
    // navigate to url
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.google.com/");

    // search for "Selenide"
    WebElement searchbox = driver.findElement(By.cssSelector("input[name='q']"));
    searchbox.sendKeys("Selenide");
    searchbox.submit();

    // wait for search to happen
    new WebDriverWait(driver, 4).until(visibilityOfElementLocated(By.cssSelector("#ires .g")));

    // locate search results
    List<WebElement> results = driver.findElements(By.cssSelector("#ires .g"));

    // assert that Google found the right thing
    String firstResult = results.get(0).getText();
    assertThat(firstResult, startsWith("Selenide: concise UI tests in Java"));
}

Automated TEST

That's it?

Previous example

  • Will search on Google
  • Can be run repeatedly with little effort
  • Is not really readable!
  • We care about application functionality
  • We don't care about HTML stuff

Can we do better?

Page objects

  • Introduces layer of abstraction
  • Push GUI control away from test
  • Encapsulate into into object
  • Objects on  / parts of the page
  • Reusability
  • Cleaner test code
  • No assertions!

http://martinfowler.com/bliki/PageObject.html

@Test
public void userCanSearch() {
    GooglePage page = new GooglePage(driver, "http://www.google.com");
    page.open();
    
    SearchResultsPage results = page.searchFor("Selenide");
    String firstResult = results.getResult(0).getText();
    
    assertThat(firstResult, startsWith("Selenide: concise UI tests in Java"));
}

Automated test 2.0

DEMO time!

Questions?

  • A tool for automated web browsing
  • Multiple browsers
  • Use Page Objects!
  • Might be still too low level
  • Consider using a facade (e.g. Selenide)

THANK YOU!