Selenium Java Overview

Introduction

Check out the Getting Started with Selenium Java guide for a quick introduction on how to run your test cases. Also check out our Selenium overview guide to better understand how Selenium tests run on Testable in general.

This document goes into further detail on the various options provided for running Selenium Java scenarios on Testable.

Creating a Scenario

There are two ways to create a scenario:

  1. Create a new Test Case (New Test Case button on the dashboard or Test Cases -> New... on the left nav), select Selenium during step 2.
  2. Go to an existing test case and click on the Scenario tab. Click the New Scenario button and select Selenium as the scenario type.

Source

There are several ways to load your Selenium Java scenario into the Testable platform.

  1. Upload Jar(s)/Zip(s): Upload any jar/zip files needed to run your Selenium code and indicate the name of the main class. No compilation/build step occurs on Testable in this case.
  2. Create/Edit/Upload *.java File(s): Upload all *.java files that are required or create/edit them on our website. Note that no package structure is supported in this case. All code must be in the default package and will be compiled on the test runner at test execution time.
  3. Upload Project Source as Zip File: Upload a zip file with all source code and configuration. Code is unzipped then built and run using Maven or Gradle. See below for more details.
  4. Build Project from Version Control: Project is cloned from version control onto the test runner. Code is built and run using Maven or Gradle. See below for more details.

Integration API: testable-selenium-java

The WebDriver client instance must be instantiated to communicate with the local Selenium standalone server. Testable provides a simple library to help with this. The testable-selenium-java library can be found at Maven central:

<dependency>
  <groupId>io.testable</groupId>
  <artifactId>testable-selenium-java</artifactId>
  <version>0.0.2</version>
</dependency>

An example test that uses this library:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import io.testable.selenium.TestableSelenium;

public class TestableExample {

    public static void main(String[] args) throws Exception {
        ChromeOptions options = new ChromeOptions();
        WebDriver driver = TestableSelenium.newWebDriver(options);
        driver.get("https://www.google.com");
        TestableSelenium.takeScreenshot(driver, "HomePage.png");
        driver.close();
    }

}

See the testable-selenium-java README for more details.

Build Step

If you choose to link your Git repository or upload a source zip your code needs to be compiled and built on the test runner. We support both Gradle and Maven to do this.

Gradle Build

Testable will use Gradle to build and run your project if it detects a build.gradle file in the project root. To simulate this locally simply run gradle clean build. We then run the main class specified in your scenario definition by injecting the application plugin into your project. All scenario parameters will be available as system properties and environment variables at runtime.

Our example project is a good place to start.

build.gradle

group 'io.testable'
version '1.0-SNAPSHOT'

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    compile group:'io.testable', name: 'testable-selenium-java', version: '0.0.2'
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

Maven Build

Testable will use Maven to build and run your project if it detects a pom.xml file in the project root. To simulate this locally simply run mvn package exec:java -Dexec.cleanupDaemonThreads=false -Dexec.mainClass=[your_main_class_here]. All scenario parameters will be available as system properties and environment variables at runtime.

Our example project is a good place to start.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>io.testable</groupId>
    <artifactId>selenium-java-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>io.testable</groupId>
            <artifactId>testable-selenium-java</artifactId>
            <version>0.0.2</version>
        </dependency>
    </dependencies>
</project>

If both a build.gradle and pom.xml file are detected in your project root folder, Gradle takes precedence.

Testable APIs: screenshots and more

The testable-selenium-java library provides Testable APIs for:

  1. Screenshots
  2. Custom metrics
  3. Logging
  4. Read from CSV

All these APIs will work when run locally as well. See the testable-selenium-java README for more details.