• Documentation
  • Tutorials
  • Case studies
  • White papers
  • Product

What's on this Page

    • Maven settings
      • Using the script-dev-selenium artefact
      • Using Selenium as a library
    • Code
    • WebDriver Management
    • Driver Wrapper class
    • Full Selenium example class
 > step  > Resources  > Tutorials  > Browser-based automation with step and Selenium
Categories: TUTORIALS SELENIUM H T T P BROWSER

Browser-based automation with step and Selenium

Selenium framework provides the ability to automate browsers actions (browsing, clicking, filling forms etc…) and is used a lot for automation testing.
You can find more information on the official Selenium website.

Examples of Selenium Keywords are available on github.

Maven settings

Using the script-dev-selenium artefact

We’ve made a public maven artefact available on our nexus for the developer’s convenience. This artefact allows you to develop Selenium Keywords without having to manage the Selenium version explicitly, but with the version embedded in step (Version 2.53.1 for Selenium 2 and 3.5.3 for Selenium 3). 

Depending on which version of Selenium you intend to use, you can choose from the following two dependencies:

  • Version 2
    <dependency>
        <groupId>ch.exense.step</groupId>
        <artifactId>script-dev-selenium-2</artifactId>
        <version>3.10.0</version>
        <type>pom</type>
    </dependency>
  • Version 3
    <dependency>
        <groupId>ch.exense.step</groupId>
        <artifactId>script-dev-selenium-3</artifactId>
        <version>3.10.0</version>
        <type>pom</type>
    </dependency>

You will then be able to create your Selenium Keywords by choosing the Selenium Type and the proper major version:

1554131223843-534.png

Using Selenium as a library

If you want more flexibility for managing your Selenium versions (multiple versions per controller, …), you can create a library containing the needed dependencies with the following pom.xml:

<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>my.project.example</groupId>
    <artifactId>selenium-dependencies</artifactId>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <selenium.version>3.14.0</selenium.version>
    </properties>

    <dependencies>
      <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-api</artifactId>
          <version>${selenium.version}</version>
      </dependency>
      <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-java</artifactId>
          <version>${selenium.version}</version>
      </dependency>
    </dependencies>

    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-shade-plugin</artifactId>
          <version>3.2.1</version>
          <executions>
            <execution>
              <phase>package</phase>
                <goals>
                  <goal>shade</goal>
                </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
</project>

You will then be able to create your Selenium Keywords by choosing the Java Type and adding the generated jar as a library:

1554131366662-800.png

Code

In the following example we will create a simple keyword which is going to create a WebDriver, navigate to the url passed in Input and finally check if one of the h1 tag contains the text “Discover”. Here the code :

@Keyword(name="Selenium_Example")
public void Selenium_Example() {
   // In this example we will use Chrome, so we explicitly set the location of its web driver
   System.setProperty("webdriver.chrome.driver", "C:\\Tools\\chromedriver.exe");
   // Create a new driver object in order to execute actions
   ChromeDriver chrome = new ChromeDriver();
   // Get the url value from the input
   String homeUrl = input.getString("url");
   // Navigate to the url
   chrome.navigate().to(homeUrl);
   // Look for an h1 tag containing the text "Discover"
   chrome.findElement(By.xpath("//h1[contains(text(),'Discover')]"));
}

WebDriver Management

Regarding webdriver management, we’ve opted to not force a certain lifecycle management approach onto the developer, but instead let them decide how and when they want to store the driver.

However we have strong recommendations and best practices to offer in that department :

  • for beginners who are using Selenium or step on their first attempt, we recommend packing the whole scenario including driver creation and destruction in a single keyword and playing around with the API until they’ve built a basic test plan that does what they expect
  • for more advanced users or load testers who need to have more control over driver creation and destruction and not tie these steps to the rest of the workflow, we encourage you to:
    • create explicit Keywords for the creation and destruction of the driver and implement the rest of the workflow’s logic in (a) separate keyword(s)
    • work in “stateful” mode by introducing a Session object in your test plan, which will guarantee at runtime that the context of each thread / virtual user is maintained and isolated from the others
    • this will allow you to add a For loop object and have your users iterate in a certain scenario without destroying the driver (and if you wish, without logging out)
    • attach the driver object to step‘s session object, which is managed internally by step and will allow for the “business keywords” to use a properly initialized driver (see example below)
    • we also recommend wrapping the driver in a class which implements Closable to help step clean up in error scenarios (see below as well)
  • lastly, in order to achieve maximum Keyword granularity (and reduce maintenance efforts), especially in environments involving many partially overlapping workflows, we recommend separating each logical step of the workflow in distinct Keywords and passing the driver between keywords

Driver Wrapper class

First let’s take a look at our DriverWrapper class :

public class DriverWrapper implements Closeable {
   // Our driver object
   final WebDriver driver;
   // Constructor assigning the given driver object to the local one
   public DriverWrapper(WebDriver driver) {
      this.driver = driver;
   }
   // Implementing the Closeable interface requires to override the close() method, this is where we destroy the driver object
   @Override
   public void close() throws IOException {
     driver.quit();
   }
}

Full Selenium example class

We can now work on our Selenium class, which will define 3 Keywords :

  • OpenChrome
    • will be used to create the driver object and the driver wrapper, and pass the driver wrapper to the step’s session
  • NavigateTo
    • will be used to navigate to an url passed via the input
  • CloseChrome
    • will be used to explicitly close the driver

Here the entire class code :

public class SeleniumExample extends AbstractKeyword {
   // Private method in order to get the WebDriver from the DriverWrapper object
   final WebDriver getDriver() {
      return session.get(DriverWrapper.class).driver;
   }
   @Keyword(name="OpenChrome")
   public void OpenChrome() throws Exception {
      // Set explicitly web driver location
      System.setProperty("webdriver.chrome.driver", "C:\\Tools\\chromedriver.exe");
      // Create the driver object
      final WebDriver driver = new ChromeDriver();
      // Tell the driver to timeout after 10 seconds
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      // Put the DriverWrapper object into step's session
      session.put(new DriverWrapper(driver));
   }
   @Keyword(name="NavigateTo")
   public void NavigateTo() throws Exception {
      // Check if the Input contains the url
      if(input.containsKey("url")) {
         // Get the driver from step's session
         final WebDriver driver = getDriver();
         // Navigate to the provided url
         driver.navigate().to(input.get("url").toString());
      } else {
         output.setError("Input parameter 'url' not defined");
      }
   }
   @Keyword(name="CloseChrome")
   public void CloseChrome() throws Exception {
      // Get the driver from step's session
      final WebDriver driver = getDriver();
      // Close explicitly the driver
      driver.quit();
      }
}
We can now deploy and create our Keywords as described in the previous section and create a test plan using them :

1520350152966-795.png
  • Getting Started
    • Overview
    • Quick setup
    • My first Execution
    • Browser automation
    • Real-world scenario
    • Scaling out
  • Whats new?
    • Release notes
    • Roadmap
    • Weekly news
  • Admin guide
    • Database installation
    • Controller installation
    • Agent installation
    • Backup & Restore
    • Housekeeping
    • Migration
    • Troubleshooting
    • Custom Groovy Macros
  • User guide
    • Keywords
    • Plans
    • Executions
    • Parameters
    • Notifications
    • Event Broker Monitor
  • Developer guide
    • Development Overview
    • Keyword API
    • step client API
    • Event Broker API
  • Plugins
    • .NET agent
    • Async packages
    • Node.js agent
    • ALM
    • JMeter
    • SoapUI
    • PDF compare
  • Resources
    • Tutorials
    • Case Studies
    • White papers
    • Libraries
Step Logo
    • Documentation
    • Tutorials
    • Case studies
    • White papers
    • Product
    • Getting Started
    • Whats new?
    • Admin guide
    • User guide
    • Developer guide
    • Plugins
    • Resources