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

What's on this Page

  • Maven settings
    • 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 : HAHAHUGOSHORTCODEs2HBHB WebDriver Management
    • Driver Wrapper class
    • First let’s take a look at our DriverWrapper class : HAHAHUGOSHORTCODEs3HBHB Full Selenium example class
  • step
  • Resources
  • Tutorials
  • Browser-based automation with step and Selenium
Categories: TUTORIALS SELENIUM HTTP BROWSER
This article references one of our previous releases, click here to go to our latest version instead.

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 Selenium as a library

Selenium requires dependencies as illustrated in 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>ch.exense.step</groupId>
        <artifactId>step-api-keyword</artifactId>
        <version>${step-api.version}</version>  
    </dependency>

      <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
  • Whats new?
  • Admin guide
  • User guide
  • Developer guide
  • Plugins
  • 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
      • Tutorials
      • Case Studies
      • White papers
      • Libraries