This library allows you to write Selenium Java tests that integrate with the Testable platform. When
When developing locally include the following artifact in your build:
<dependency>
<groupId>io.testable</groupId>
<artifactId>testable-selenium-java</artifactId>
<version>0.0.14</version>
</dependency>A simple example test would look as follows:
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.chrome.ChromeOptions;
importio.testable.selenium.TestableSelenium;
publicclassTestableExample {
publicstaticvoidmain(String[] args) throwsException {
ChromeOptionsoptions = newChromeOptions();
WebDriverdriver = TestableSelenium.newWebDriver(options);
driver.get("https://www.google.com");
driver.close();
}
}When this example is run outside of Testable it will instantiate a
RemoteWebDriver instance with URL http://localhost:4444/wd/hub.
The takeScreenshot(driver, name) method will simply take the screenshot and
return the path when run locally. On the Testable platform it will also
copy the screenshot into the appropriate output folder to be reported
back as part of the test results.
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.chrome.ChromeOptions;
importio.testable.selenium.TestableSelenium;
publicclassTestableExample {
publicstaticvoidmain(String[] args) throwsException {
ChromeOptionsoptions = newChromeOptions();
WebDriverdriver = TestableSelenium.newWebDriver(options);
driver.get("https://www.google.com");
TestableSelenium.takeScreenshot(driver, "HomePage.png");
driver.close();
}
}Capture assertions or test steps as part of the test results including test step description, duration, and any errors that occurred.
In the results you can see these within the Assertions widget. For example:
TestableTesttest = TestableSelenium.startTest("Google Related");
test.startStep("Open google home page");
driver.get("https://www.google.com");
test.finishSuccessfulStep(); // or test.finishFailedStep("My error message");test.runStep("Open google news using Runnable", newRunnable() {
publicvoidrun() {
driver.get("https://news.google.com"); // any exception here gets logged as a test step failure
}
});
test.finish();Capture a custom counter, timing, or histogram metric. When run on Testable it is reported and aggregated into the test results. When run locally it will simply output the metric details to the console.
See https://testable.io/documentation/scripts/custom-metrics.html for more details.
Counter Example:
TestableSelenium.reportMetric(TestableMetric.newCounterBuilder()
.withName("My Request Counter")
.withVal(1)
.withUnits("requests")
.build());Add 1 to the "My Request Counter" metric.
Timing Example:
longstart = System.currentTimeMillis();
driver.get("https://www.google.com");
longloadTime = System.currentTimeMillis() - start;
TestableSelenium.reportMetric(TestableMetric.newTimingBuilder()
.withName("Page Load Time")
.withVal(loadTime)
.withUnits("ms")
.build());Capture how long it takes to open https://www.google.com and capture that as the "Page Load Time" metric.
Histogram Example:
Stringstatus = "MyStatus";
TestableSelenium.reportMetric(TestableMetric.newHistogramBuilder()
.withName("Status Histogram")
.withKey(status)
.withVal(1)
.build());Add 1 to the "Status Histogram" metric.
Log a message or exception into the test results at the specified level. When run locally it simply outputs to the console.
Trace logging will only be output during a smoke test. Fatal logging will cause the test run to stop immediately.
Example:
TestableSelenium.log(TestableLog.Level.Trace, "detailed stuff for smoke test only");
TestableSelenium.log(TestableLog.Level.Debug, "my debug message");
TestableSelenium.log(TestableLog.Level.Info, "some info");
TestableSelenium.log(TestableLog.Level.Error, newRuntimeException("An error occurred"));
TestableSelenium.log(TestableLog.Level.Fatal, newRuntimeException("Something bad happened stop everything!"));Read from a CSV file that has been uploaded to your scenario. When run locally this will load the CSV from the classpath or current working directory. It is assumed your CSV has a header row with column names.
Get row by index: Return a row by index. The first row after the header row is considered row 0.
TestableCSVReaderreader = TestableSelenium.readCsv("credentials.csv");
CSVRecordrecord = reader.get(2);
System.out.println(record.get("username"));Get random row: Returns a random row from the CSV.
TestableCSVReaderreader = TestableSelenium.readCsv("credentials.csv");
CSVRecordrecord = reader.random();
System.out.println(record.get("username"));Get the next row: Return the next row in the CSV using a global iterator. This means that the rows in the CSV will be evenly distributed across all virtual users that are part of your test execution.
TestableCSVReaderreader = TestableSelenium.readCsv("credentials.csv");
CSVRecordrecord = reader.next();
System.out.println(record.get("username"));