Webdriver Script : Handling Multiple Windows

Hi All,

This is the second script for beginners. This script is based on multiple windows scenario where you face another window, have to verify something and switch back to original one and verify some element again. Actions are defined as per comments.

/*
 *
 * @Author : Gaurav Khanna
 * 
 */

package webdriverScripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class HandlingMultipleWindows1 {

    // Declaring variable 'webDriver' of WebDriver Type
    WebDriver webDriver;

    // Declaring baseURL variable of String Type
    String baseUrl;

    @Test
    public void testhandlingMultipleWindows() {

        // Initializing FireFox Driver
        webDriver = new FirefoxDriver();

        // Assigning URL to variable baseUrl
        baseUrl = "http://book.theautomatedtester.co.uk/";

        // Open the link
        webDriver.get(baseUrl);

        // Maximize browser window
        webDriver.manage().window().maximize();

        // Click on link
        webDriver.findElement(By.linkText("Chapter1")).click();

        //
        webDriver.findElement(By.id("multiplewindow")).click();

        //
        java.util.Set<String> b = webDriver.getWindowHandles();

        //
        String j = (String) b.toArray()[1];

        //
        System.out.println(j);

        //
        webDriver.switchTo().window(j);

        // Verify Text Present
        Assert.assertEquals("Text within the pop up window", webDriver
                .findElement(By.id("popuptext")).getText());

        //
        webDriver.findElement(By.id("closepopup")).click();

        //
        String n = (String) b.toArray()[0];

        //
        System.out.println(b.toArray()[0]);

        //
        webDriver.switchTo().window(n);

        //
        Assert.assertEquals("Verify this button he here", webDriver
                .findElement(By.id("verifybutton")).getAttribute("value"));

        // This will close the browser
        webDriver.quit();
    }

}

First Basic Script

Hi All,

This is basic script for beginners. Actions are defined as comments :

package webdriverScripts.others;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.Test;

public class BasicScript {

// Declaring variable 'webDriver' of WebDriver Type
WebDriver webDriver;

// Declaring variable 'baseUrl' of String Type
String baseUrl;

@Test
public void basicScriptExample() {

// Initializing FireFox Driver
webDriver = new FirefoxDriver();

// Assigning URL to variable 'baseUrl'
baseUrl = "http://book.theautomatedtester.co.uk";

// Open the link
webDriver.get(baseUrl);

// Maximize browser window
webDriver.manage().window().maximize();

// Get Page Title
String PageTitle = webDriver.getTitle();

// Printing Page Title
System.out.println("Page Title : " + PageTitle);

// Click on link
webDriver.findElement(By.linkText("Chapter1")).click();

// Click on radio button
webDriver.findElement(By.id("radiobutton")).click();

// Click on Dropdown
Select dropdown = new Select(webDriver.findElement(By.id("selecttype")));

// Select option from dropdown
dropdown.selectByVisibleText("Selenium Core");

// Verify Text Present
Assert.assertEquals("Assert that this text is on the page",
webDriver.findElement(By.id("divontheleft")).getText());

// Verify Button Present
Assert.assertEquals("Verify this button he here",
webDriver.findElement(By.id("verifybutton")).getAttribute("value"));

// This will close the browser
webDriver.quit();
}
}

Junit Annotations With Description

@Test 
public void method()
The annotation @Test identifies that a method is a test method.

@Before
public void method()
Will execute the method before each test. This method can prepare the test environment (e.g. read input data, initialize the class).

@After
public void method()
Will execute the method after each test. This method can cleanup the test environment (e.g. delete temporary data, restore defaults).

@BeforeClass
public void method()
Will execute the method once, before the start of all tests. This can be used to perform time intensive activities, for example to connect to a database.

@AfterClass
public void method()
Will execute the method once, after all tests have finished. This can be used to perform clean-up activities, for example to disconnect from a database.

@Ignore
Will ignore the test method. This is useful when the underlying code has been changed and the test case has not yet been adapted. Or if the execution time of this test is too long to be included.

@Test (expected = Exception.class)
Fails, if the method does not throw the named exception.

@Test(timeout=100)
Fails, if the method takes longer than 100 milliseconds.

Features of Selenium IDE



Introduction to Selenium Webdriver

Selenium Webdriver

WebDriver is a tool for automating testing web applications, and in particular to verify that they work as expected. It aims to provide a friendly API that’s easy to explore and understand, easier to use than the Selenium-RC (1.0) API, which will help make your tests easier to read and maintain. It’s not tied to any particular test framework, so it can be used equally well in a unit testing or from a plain old “main” method. This section introduces WebDriver’s API and helps get you started becoming familiar with it. Start by setting up a Webdriver project if you haven’t already. This was described in the previous section, Setting Up a Selenium-WebDriver Project.

Once your project is set up, you can see that WebDriver acts just as any normal library: it is entirely self-contained, and you usually don’t need to remember to start any additional processes or run any installers before using it, as opposed to the proxy server with Selenium-RC. Note: additional steps are required to use Chrome Driver, Opera Driver, Android Driver and iPhone Driver.

Introduction to Selenium RC

Selenium RC

Selenium-RC allows the test automation developer to use a programming language for maximum flexibility and extensibility in developing test logic. For instance, if the application under test returns a result set, and if the automated test program needs to run tests on each element in the result set, the programming language’s iteration support can be used to iterate through the result set, calling Selenium commands to run tests on each item.

Selenium-RC provides an API (Application Programming Interface) and library for each of its supported languages: HTML, Java, C#, Perl, PHP, Python, and Ruby. This ability to use Selenium-RC with a high-level programming language to develop test cases also allows the automated testing to be integrated with a project’s automated build environment.

Introduction to Selenium IDE

Selenium IDE

Selenium-IDE is the Integrated Development Environment for building Selenium test cases. It operates as a Firefox add-on and provides an easy-to-use interface for developing and running individual test cases or entire test suites. Selenium-IDE has a recording feature, which will keep account of user actions as they are performed and store them as a reusable script to play back. It also has a context menu (right-click) integrated with the Firefox browser, which allows the user to pick from a list of assertions and verifications for the selected location. Selenium-IDE also offers full editing of test cases for more precision and control.

Although Selenium-IDE is a Firefox only add-on, tests created in it can also be run against other browsers by using Selenium-RC and specifying the name of the test suite on the command line.

Accessibility Testing

Accessibility testing is the technique of making sure that your product is accessibility compliant. There could be many reasons why your product needs to be accessibility compliant as stated above.

Typical accessibility problems can be classified into following four groups, each of them with different access difficulties and issues:

Visual impairments

Such as blindness, low or restricted vision, or color blindness. User with visual impairments uses assistive technology software that reads content loud. User with weak vision can also make text larger with browser setting or magnificent setting of operating system.

Motor skills

Such as the inability to use a keyboard or mouse, or to make fine movements.

Hearing impairments

Such as reduced or total loss of hearing

Cognitive abilities

Such as reading difficulties, dyslexia or memory loss.

Development team can make sure that their product is partially accessibility compliant by code inspection and Unit testing. Test team needs to certify that product is accessibility compliant during the functional testing phase. In most cases, accessibility checklist is used to certify the accessibility compliance. This checklist can have information on what should be tested, how it should be tested and status of product for different access related problems. Template of this checklist is available here.

For accessibility testing to succeed, test team should plan a separate cycle for accessibility testing. Management should make sure that test team have information on what to test and all the tools that they need to test accessibility are available to them.

Typical test cases for accessibility might look similar to the following examples -
  • Make sure that all functions are available via keyboard only (do not use mouse)
  • Make sure that information is visible when display setting is changed to High Contrast modes.
  • Make sure that screen reading tools can read all the text available and every picture/Image have corresponding alternate text associated with it.
  • Make sure that product defined keyboard actions do not affect accessibility keyboard shortcuts.
And many more..

There are many tools in the market to assist you in your accessibility testing. Any single tool cannot certify that your product is accessibility compliant. You will always need more than one tool to check accessibility compliance of your product. Broadly, tools related to accessibility can be divided into two categories. Inspectors or web checkers

This category of tool allows developer or tester to know exactly what information is being provided to an assistive technology. For example, tools like Inspect Object can be used to get information on what all information is given to the assistive technology. Assistive Technologies (AT)

This category of tools is what a person with disability will use. To make sure that product is accessibility compliant, tools like screen readers, screen magnifiers etc. are used. Testing with an assistive technology has to be performed manually to understand how the AT will interact with the product and documentation. More information on the tools is present in tool section of this website for you to explore.

Some useful links for Accessibility testing :

Chrome Extension : ChromeShades

Firefox Extension : WAVE Firefox toolbar

Online Web Accessbility Testing: http://wave.webaim.org/