selenium


Selenium WebDriver Automation With T-Plan

Version 5.0 delivers integration with Selenium WebDriver to enable combined image based and web browser automation from a single script. Features:

  • Version 5.0 integrates with the Selenium Java Client 3.13.0. The files are installed to the selenium/ folder and may be eventually replaced manually with a new version.
  • Supported browsers:
    • Microsoft Edge
    • Microsoft Internet Explorer
    • Firefox
    • Chrome
    • Safari (Mac OS)
  • Automation of remote browsers is not yet supported. Only one local browser can be automated at a time.

Set Up Instructions

  1. Open the Selenium Downloads page and scroll down to the "Third Party Drivers, Bindings, and Plugins" section. Download the target browser driver(s) and extract it to your hard drive.
  2. Select Selenium → Configure Selenium in the Robot menu and set the path(s) to the driver executables.



  3. Select Selenium → Open Browser... or click the Browser tool bar button. Select the browser you wish to open in the drop down and populate the target URL. Click OK to start.



  4. Once the browser is up and running you may start to create the script code to drive it. To view elements in the current page open the Selenium → View Elements menu item.

Automation

There are two new elements in the Robot scripting language:

  • The "browser" comparison method allows to search for elements in the web page using the standard CompareTo and Waitfor commands.
  • The "Browser" command allows to manage and drive the Selenium driven browser. For a formal specification of the command see the Language Reference.

    • Use "Browser open" to open the target browser and web site. Call "Browser close" to close it.



    • Use "Browser find" to locate an element (an HTML tag) in the page and apply an optional action to it (click, type, submit, clear, select). The target element may be identified as follows:
      • FAST: Enter an Xpath, CSS or one or more element attributes and click Verify. If there are multiple elements matching the criteria select the "Choose in viewer" button in the pop up window. Select the target element in the viewer and tick the criteria to identify it.
      • SLOW: Select "Choose in viewer" in the command property window to open the element viewer. As this involves loading of all elements in the current web page it is likely to be slow. Select the target element in the viewer and tick the criteria to identify it.



        NOTE: The "Text" and "Partial text" criteria alone is usually not sufficient because Selenium returns the element containing it as well as all its parent elements (div, form, ...). This can be used to retrieve the text of top elements or the whole page. For example, to get all text in the page set the "Tag Name" to "body". The calling script may then retrieve the text from the _WEB_TEXT variable.

    • Once you design the criteria you may select the Perform Action button to apply the selected action to the target element. This allows you to avoid direct interaction with the browser and to verify that it works as expected.
    • Select OK to save the command to the active script editor. If you wish to design another "Browser find" command on the next line click "OK & Create New" button instead.

Automation Example

Let's demonstrate Robot capabilities on a simple example:

  1. Open the Wikipedia home page in Firefox
  2. Type 'automation' into the search field and submit it to open the Automation topic.

A pure Selenium solution in Java would look like:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.By;


public class WikipediaAutomation {
    
    public static void main(String args[]) {
        // Set path to the Firefox/Gecko driver
        System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\geckodriver.exe");
        
        // Create the driver and execute the script
        WebDriver driver = new FirefoxDriver();
        // Open the Wikipedia home page
        driver.get("https://www.wikipedia.org");

        WikipediaAutomation script = new WikipediaAutomation();
        script.automate(driver);
    }
    
    public void automate(WebDriver driver) {       
        // Enter 'automation' into the search field
        driver.findElement(By.id("searchInput")).sendKeys("automation");
        
        // Submit the form using the button next to the field
        driver.findElement(By.xpath("//button")).submit();
    }
}

To design the same script in Robot use the Browser open and Browser find property windows described in the previous chapter.

  • If your script is a TPR one the corresponding code will look like:

Browser "open" browser="firefox" url="https://www.wikipedia.org"
Browser "find" id="searchInput" elaction="type" eltext="automation"
Browser "find" tagname="button" elaction="submit"

  • If your script is a Java one the property windows will generate an automation code similar to the one below. The rest of the Java script class is omitted for brevity.

browserOpen("firefox", "https://www.wikipedia.org");
browserFindAndType("automation", "id", "searchInput");
browserFindAndSubmit("tagname", "button");

Selenium API Access

Robot does not support all the Selenium functionality yet. You may reach an unsupported Selenium feature at the level of the Java code from a Robot Java test script as follows:

WebDriverAdapter adapter = WebDriverAdapterFactory.getInstance().getAdapter(getContext(), WebDriverAdapter.ADAPTER_NAME_SELENIUM);

org.openqa.selenium.WebDriver driver = (org.openqa.selenium.WebDriver) adapter.getDriver();

For example, to accept and dispose a JavaScript pop up from a TPR test script use the following Java code block:

java {
   import com.tplan.robot.remoteclient.webdriver.WebDriverAdapter;
   import com.tplan.robot.remoteclient.webdriver.WebDriverAdapterFactory;

   WebDriverAdapter adapter = WebDriverAdapterFactory.getInstance().getAdapter(getContext(), WebDriverAdapter.ADAPTER_NAME_SELENIUM);
   org.openqa.selenium.WebDriver driver = (org.openqa.selenium.WebDriver) adapter.getDriver();
   driver.switchTo().alert().accept();
} endjava

Integration of Existing Selenium Scripts

Existing Selenium Java code may be freely combined with Robot scripts. The only thing the two entities must share is the Selenium web driver object (the org.openqa.selenium.WebDriver instance). The key class in the Robot framework is com.tplan.robot.remoteclient.webdriver.WebDriverAdapterFactory which allows to access the driver wrapper class (WebDriverAdapter) assigned to the executing scripts.

For example, to let Robot open the Wikipedia home page in Firefox and then perform the automation using our pure Selenium example above create a Robot Java script as follows:

package test;

import
com.tplan.robot.scripting.*;

import com.tplan.robot.*;
import com.tplan.robot.remoteclient.webdriver.*;
import java.io.*;

public class RobotWikipediaScript extends DefaultJavaTestScript  {

    public void test() {
        try {
            // Call "Browser open" to create the driver
            browserOpen("firefox", "https://www.wikipedia.org");

            // Obtain a reference to the Selenium driver from the Robot framework
            WebDriverAdapter adapter = WebDriverAdapterFactory.getInstance().getAdapter(getContext(), WebDriverAdapter.ADAPTER_NAME_SELENIUM);
            org.openqa.selenium.WebDriver driver = (org.openqa.selenium.WebDriver) adapter.getDriver();
            
            // Call the existing example Selenium script
            WikipediaAutomation seleniumScript = new WikipediaAutomation();
            seleniumScript.automate(driver);
            
        } catch (StopRequestException ex) {
            throw ex;
        } catch (IOException ex) {
            ex.printStackTrace();
            throw new IllegalStateException(ex);
        }
    }

    public static void main(String args[]) {
        RobotWikipediaScript script = new RobotWikipediaScript();
        ApplicationSupport robot = new ApplicationSupport();
        AutomatedRunnable runnable = robot.createAutomatedRunnable(script, "RobotWikipediaScript@" + Integer.toHexString(script.hashCode()), args, System.out, false);
        new Thread(runnable).start();
    }
}

It is not possible to call existing Selenium scripts from TPR ones directly. You must wrap the Selenium execution into a Robot Java script and then call it using the Run command.

For example, to drive our automation from a TPR script remove the "browserOpen("firefox", "https://www.wikipedia.org")" method call from the RobotWikipediaScript example above and call it from TPR as:

Browser "open" browser="firefox" url="https://www.wikipedia.org"
Run "test.RobotWikipediaScript"

To make the above example work please put the compiled Selenium code (a JAR or a class path with the compiled WikipediaAutomation class) onto the Robot class path.