Saturday, 30 April 2016

Accessing Links & Tables using Selenium Webdriver

Key points to Accessing Links & Tables using Selenium WebDriver


  • Accessing links using their exact match is done using By.linkText() method.

  • Accessing links using their partial match is done using By.partialLinkText() method.

  • If there are multiple matches, By.linkText() and By.partialLinkText() will only select the first match.
  • Pattern matching using By.linkText() and By.partialLinkText() is case-sensitive.
  • The By.tagName("a") method is used to fetch all links within a page.
  • Links can be accessed by the By.linkText() and By.partialLinkText() whether they are inside or outside block-level elements.
  • Accessing image links are done using By.cssSelector() and By.xpath() methods.
  • By.xpath() is commonly used to access table elements.

Accessing Links

Links Matching a Criterion

Links can be accessed using an exact or partial match of their link text. The examples below provide scenarios where multiple matches would exist, and would explain how WebDriver would deal with them.

Exact Match

Accessing links using their exact link text is done through the By.linkText() method. However, if there are two links that have the very same link text, this method will only access the first one. Consider the HTML code below

When you try to run the WebDriver code below, you will be accessing the first "click here" link
.

As a result, you will automatically be taken to Google.

Partial Match

Accessing links using a portion of their link text is done using the By.partialLinkText() method. If you specify a partial link text that has multiple matches, only the first match will be accessed. Consider the HTML code below.
When you execute the WebDriver code below, you will still be taken to Google.

Thursday, 28 April 2016

Accessing Forms using Selenium WebDriver



In this tutorial , we will learn how to access forms and its  elements using Webdriver


Links

Links also are accessed by using the click() method.
Consider the below link found in Mercury Tours' homepage.
You can access this link using linkText() or partialLinkText() together with click(). Either of the two lines below will be able to access the "Register here" link shown above.

Drop-Down Box

Before we can control drop-down boxes, we must do following two things :
  1. Import the package org.openqa.selenium.support.ui.Select
  2. Instantiate the drop-down box as a "Select" object in WebDriver
As an example, go to Mercury Tours' Registration page (http://newtours.demoaut.com/mercuryregister.php) and notice the "Country" drop-down box there.  
Step 1
Import the "Select" package.
Step 2
Declare the drop-down element as an instance of the Select class. In the example below, we named this instance as "drpCountry".
Step 3
We can now start controlling "drpCountry" by using any of the available Select methods. The sample code below will select the option "ANTARCTICA".

Selecting Items in a Multiple SELECT element

We can also use the selectByVisibleText() method in selecting multiple options in a multi SELECT element. As an example, we will take http://jsbin.com/osebed/2 as the base URL. It contains a drop-down box that allows multiple selections at a time.
The code below will select the first two options using the selectByVisibleText() method.

Select Methods

The following are the most common methods used on drop-down elements.
Method
Description
selectByVisibleText() and
deselectByVisibleText()
Example:
  • Selects/deselects the option that displays the text matching the parameter.
  • Parameter: The exactly displayed text of a particular option
selectByValue() and
deselectByValue()
Example:
  • Selects/deselects the option whose "value" attribute matches the specified parameter.
  • Parameter: value of the "value" attribute
  • Remember that not all drop-down options have the same text and "value", like in the example below.

selectByIndex() and
deselectByIndex()
Example:

  • Selects/deselects the option at the given index.
  • Parameter: the index of the option to be selected.
isMultiple()
Example:

  • Returns TRUE if the drop-down element allows multiple selections at a time; FALSE if otherwise.
  • Needs parameters needed
deselectAll()
Example:

  • Clears all selected entries. This is only valid when the drop-down element supports multiple selections.
  • No parameters needed

Submitting a Form

The submit() method is used to submit a form. This is an alternative to clicking the form's submit button.
 You can use submit() on any element within the form, not just on the submit button itself.
When submit() is used, WebDriver will look up the DOM to know which form the element belongs to, and then trigger its submit function.

Accessing Form Elements

Input Box
Input boxes refer to either of these two types:
  1. Text Fields- text boxes that accept typed values and show them as they are.
  2. Password Fields- text boxes that accept typed values but mask them as a series of special characters (commonly dots and asterisks) to avoid sensitive values to be displayed.

Entering Values in Input Boxes

The sendKeys() method is used to enter values into input boxes.

Deleting Values in Input Boxes

The clear() method is used to delete the text in an input box. This method does not need any parameter. The code snippet below will clear out the text "tutorial" in the User Name text box.

Radio Button

Toggling a radio button on is done using the click() method.

Check Box

Toggling a check box on/off is also done using the click() method.
The code below will click on Facebook's "Keep me logged in" check box twice and then output the result as TRUE when it is toggled on, and FALSE if it is toggled off.

Wednesday, 27 April 2016

Creating your First Script in WebDriver


 Create a WebDriver script that would:
  1. fetch Mercury Tours' homepage
  2. verify its title
  3. print out the result of the comparison
  4. close it before ending the entire program.
Below is the actual WebDriver code for the logic presented by the scenario above

package mypackage;
 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
 
public class myclass {
 
    public static void main(String[] args) {
        // declaration and instantiation of objects/variables
        WebDriver driver = new FirefoxDriver();
        String baseUrl = "http://newtours.demoaut.com";
        String expectedTitle = "Welcome: Mercury Tours";
        String actualTitle = "";
 
        // launch Firefox and direct it to the Base URL
        driver.get(baseUrl);
 
        // get the actual value of the title
        actualTitle = driver.getTitle();
 
        /*
         * compare the actual title of the page witht the expected one and print
         * the result as "Passed" or "Failed"
         */
        if (actualTitle.contentEquals(expectedTitle)){
            System.out.println("Test Passed!");
        } else {
            System.out.println("Test Failed");
        }
        
        //close Firefox
        driver.close();
        
        // exit the program explicitly
        System.exit(0);
    }
 
}


Code Explanation


Importing Packages

To get started, you need to import following two packages:
  1. org.openqa.selenium.*- contains the WebDriver class needed to instantiate a new browser loaded with a specific driver
  2. org.openqa.selenium.firefox.FirefoxDriver - contains the FirefoxDriver class needed to instantiate a Firefox-specific driver onto the browser instantiated by the WebDriver class
If your test needs more complicated actions such as accessing another class, taking browser screenshots, or manipulating external files, definitely you will need to import more packages.

Instantiating objects and variables

Normally, this is how a driver object is instantiated.
A FirefoxDriver class with no parameters means that the default Firefox profile will be launched by our Java program. The default Firefox profile is similar to launching Firefox in safe mode (no extensions are loaded).
For convenience, we saved the Base URL and the expected title as variables.

Launching a Browser Session

WebDriver's get() method is used to launch a new browser session and directs it to the URL that you specify as its parameter.

Get the Actual Page Title

The WebDriver class has the getTitle() method that is always used to obtain the page title of the currently loaded page.

Compare the Expected and Actual Values

This portion of the code simply uses a basic Java if-else structure to compare the actual title with the expected one.

Terminating a Browser Session

The "close()" method is used to close the browser window.

Terminating the Entire Program

If you use this command without closing all browser windows first, your whole Java program will end while leaving the browser window open.

Running the Test

There are two ways to execute code in Eclipse IDE.
  1. On Eclipse's menu bar, click Run > Run.
  2. Press Ctrl+F11 to run the entire code.
 If you did everything correctly, Eclipse would output "Test Passed!"