To run Selenium tests from ReadyAPI, you need to use the Groovy Script test step. The Groovy language allows you to import Selenium WebDriver and work with it.
Before you start
Notes:
-
You need to copy all Selenium WebDriver files to the ReadyAPI folder. For more information, see Requirements for Running Selenium Tests.
-
This example uses a virtual service from the REST Sample Project shipped with ReadyAPI.
-
The example requires the Mozilla Firefox web browser. You need to have it installed on your computer to be able to run the test.
Create Selenium test
-
To run the Selenium test from Groovy script, first, you need to import the needed WebDriver.
For example, you can use the following code to import the driver for Firefox:
Groovy
import org.openqa.selenium.firefox.FirefoxDriver;
-
You will also need to import packages that you will use in the code. For example:
Groovy
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By; -
To run tests in Firefox in Selenium 3 and later:
-
Download the Gecko driver.
-
Place it somewhere within the ReadyAPI installation folder – for example, in <ReadyAPI installation>/bin/ext.
-
Add the system property for the Gecko driver within the code:
Groovy
System.setProperty("webdriver.gecko.driver", "C:/Program Files/SmartBear/ReadyAPI-3.57.0/bin/ext/geckodriver.exe");
-
-
After that, define the driver in code.
Groovy
def driver = new FirefoxDriver();
-
Now, you can work with Selenium WebDriver. For example, the following code starts the browser, opens the ReadyAPI documentation and searches for Groovy.
Groovy
// Navigate to the ReadyAPI documentation
driver.get("https://support.smartbear.com/readyapi/docs/")
// Enter the search string
def searchbox = driver.findElement(By.id("inputSearchBox"));
searchbox.sendKeys("Groovy");
// Command to search for the typed text
def searchbtn = driver.findElement(By.id("btnSearch"));
searchbtn.click();
While testing, you can post information to the log by using the log
object.
-
To post an informative message, use the
log.info
method. -
To post a warning, use the
log.warn
method. -
To post an error, use the
log.error
orlog.fatal
method.Groovy
// List suggestions
def allSuggestions = driver.findElements(By.xpath("//div[@id='search_results_container']/div/h5/a"))
for (def suggestion : allSuggestions)
{
if(suggestion.getAttribute('href') != '')
log.info(suggestion.getAttribute('href'))
else
log.error('The test failed.')
}
After the test performs all the needed actions, close Selenium WebDriver.
Groovy
// Close the browser
driver.quit();
Full sample code
Here is a simple Groovy script that uses Selenium:
Groovy
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
System.setProperty("webdriver.gecko.driver", "C:/Program Files/SmartBear/ReadyAPI-3.57.0/bin/ext/geckodriver.exe");
def driver = new FirefoxDriver()
// Navigate to the ReadyAPI documentation
driver.get("https://support.smartbear.com/readyapi/docs/")
// Enter the search string
def searchbox = driver.findElement(By.id("inputSearchBox"));
searchbox.sendKeys("Groovy");
// Command to search for the typed text
def searchbtn = driver.findElement(By.id("btnSearch"));
searchbtn.click();
// List suggestions
def allSuggestions = driver.findElements(By.xpath("//div[@id='search_results_container']/div/h5/a"))
for (def suggestion : allSuggestions)
{
if(suggestion.getAttribute('href') != '')
log.info(suggestion.getAttribute('href'))
else
log.error('The test failed.')
}
// Close the browser
driver.quit();