Lab 8

Lab 7 SVV

Web testing I

Selenium

For this lab you will have to install the Firefox browser, in case you don't have it already.

Then, you install the Selenium IDE plugin in Firefox. You can also find it as attachment (rename downloaded file, change suffix from -xpi to .xpi) to this page.

Please follow then the tutorial from the Selenium IDE documentation section.

Take a look at how you can create a test suite, either by hand, or via the GUI offered specifically by Selenium IDE for easier test automation.

Pay attention to the set of good practices when writing Selenium tests.

  1. Make sure to set Base URL
  2. Use clickAndWait when navigating between pages
  3. Use Verify or Assert for verification. The only difference between the two verification types regards how the test execution follows afterwards
    • Assert - in case of failure, the test execution will stop here
    • Verify - in case of failure, the test execution will continue with the following test components, and the test result will only be test at the end.
  4. Pick up an appropriate verification level: Generic vs Concrete
    • AssertTextPresent - very generic, it checks that the text exists somewhere in the page
    • AssertText - checks that the text exists at a specific location on the page. See the ways available to specify it (html id, xpath etc.)
    • before checking AssertText one can also check whether the desired element exists on the page AssertElementPresent

Exercise: create a test that would check for a specific train in the list of Timisoara Nord - Arad trains that can be obtained at www.infofer.ro.

Optionally: try automating (via Selenium tests) several frequent actions on web pages which you often visit.

Selenium WebDriver

Is the successor of Selenium RC. We will now consider integrating your Selenium tests in an automated process using Selenium WebDriver.

In order to install Selenium WebDriver we will take the following steps:

  1. Make sure you have maven installed, by executing mvn --version in the terminal. If it's missing, you can install it from here.
  2. Create a directory, where you will add a pom.xml file with the content below:
  <?xml version="1.0" encoding="UTF-8"?>
  <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>MySel20Proj</groupId>
        <artifactId>MySel20Proj</artifactId>
        <version>1.0</version>
        <dependencies>
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-server</artifactId>
                <version>3.0.1</version>
            </dependency>
        </dependencies>
  </project>
  1. Now run the mvn clean install command from the terminal, having your created directory as current directory.
  2. Import your new maven project in Eclipse. To do this, select Import -> Maven -> Existing Maven project, and then select your project directory.
  3. If option Import -> Maven is not available, then we create an Eclipse project and import it as Existing Project.... In order to do this we run the mvn eclipse:eclipse command, which will generate the Eclipse project.
  4. Create a directory src and add it to the path (Right-click + Use as source folder), or add it directly from Eclipse: Right-click -> New -> Source folder.
  5. Create a class file with the below content.
   1 package org.openqa.selenium.example;
   2 
   3 import org.openqa.selenium.By;
   4 import org.openqa.selenium.WebDriver;
   5 import org.openqa.selenium.WebElement;
   6 import org.openqa.selenium.firefox.FirefoxDriver;
   7 import org.openqa.selenium.support.ui.ExpectedCondition;
   8 import org.openqa.selenium.support.ui.WebDriverWait;
   9 
  10 public class Selenium2Example  {
  11     public static void main(String[] args) {
  12         // Create a new instance of the Firefox driver
  13         // Notice that the remainder of the code relies on the interface, 
  14         // not the implementation.
  15         WebDriver driver = new FirefoxDriver();
  16 
  17         // And now use this to visit Google
  18         driver.get("http://www.google.com");
  19         // Alternatively the same thing can be done like this
  20         // driver.navigate().to("http://www.google.com");
  21 
  22         // Find the text input element by its name
  23         WebElement element = driver.findElement(By.name("q"));
  24 
  25         // Enter something to search for
  26         element.sendKeys("Cheese!");
  27 
  28         // Now submit the form. WebDriver will find the form for us from the element
  29         element.submit();
  30 
  31         // Check the title of the page
  32         System.out.println("Page title is: " + driver.getTitle());
  33         
  34         // Google's search is rendered dynamically with JavaScript.
  35         // Wait for the page to load, timeout after 10 seconds
  36         (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
  37             public Boolean apply(WebDriver d) {
  38                 return d.getTitle().toLowerCase().startsWith("cheese!");
  39             }
  40         });
  41 
  42         // Should see: "cheese! - Google Search"
  43         System.out.println("Page title is: " + driver.getTitle());
  44         
  45         //Close the browser
  46         driver.quit();
  47     }
  48 }
  1. Export your Selenium tests as unit tests: Selenium File ->Export TestCase as -> JUnit4 (WebDriver) (We now assume that the test is named SeleniumTest, see code below)
  2. The generated tests can be executed from Eclipse, as unit tests, or can be run as a Java application (see the Java code below for an example).
   1 import org.junit.runner.JUnitCore;
   2 import org.junit.runner.RunWith;
   3 import org.junit.runners.Suite;
   4 
   5 public class cfr {
   6 
   7         public static void main(String[] args) {
   8                 JUnitCore.runClasses(new Class[] { SeleniumTest.class });
   9         }
  10 }

Check also the possibility of using a browser driver for better automation.

Twill

Twill is a tool that allows us to explore web pages using a simple, easy to understand language.

For the examples below we will again refer to the www.infofer.ro site.

The small twill script below allows us to see what kind of form elements we have in our page, together with the names of their fields (which we will further need).

  go http://www.infofer.ro

  showforms

Notice that the page has two forms, we want to test the first one further on. In order to see what commands are available in this sens, please take a look at the available documentation.

  go http://www.infofer.ro

  formvalue 1 txtPlecare 'Timisoara Nord'
  formvalue 1 txtSosire 'Arad'
  submit
  code 200

  submit
  code 200

  find '2613'

We will now modify our script and fill in the fields txtPlecare and txtPlecare with our chosen test values (see the modified script above), then send the form values and check that the operation has been successfully performed (returned code is HTTP 200). Then we reach the confirmation page, where we don't need to do anything, so we just click the Cautare button, in order to submit the current query. We then check that in the resulting page we get text 2613, corresponding to a regional train number.

Make sure to further explore the options offered by the tool, as, although the set of commands is quite simple, the possibilities offered for web testing are actually quite rich and diverse.