Selenium WebDriver in SoapUI

Abstract:

This tutorial will show you how to use Selenium WebDriver in SoapUI. Its goal obviously is not to teach you a complex Selenium project but it rather focuses on very basics. In fact building a complex project in SoapUI using Selenium would not be a best choice. However for some repetetive data preparation or "step in the middle" WebDriver from SoapUI will work just fine.

Good to know before we start:

Pre-requisites:

Content:

Environment settings

It may sound funny but it takes some time to find right combination of versions of the three softwares we are going to use to make Selenium work in SoapUI. Following is the list of software and its versions on which this tutorial will work for sure: This is another mixture which should work fine:

Get Selenium WebDriver and load it

Very first thing we have to do is to download Selenium Standalone Server (version 2.39.0) jar file from the official Selenium pages http://www.seleniumhq.org/download/ and place it in the bin/ext directory of your SoapUI installation like this:
Selenium in SoapUI
Once we have done that we can start or restart SoapUI and check if our selenium-server-standalone-2.39.0.jar file was loaded properly. We can check it in the SoapUI log as shown on this picture:
Selenium in SoapUI

Create a project in SoapUI

Now its time to start creating our new SoapUI project by pressing a New Generic project creation button in our Workspace (let's name our project selenium_webdriver:
New generic project
After that we will create new Test Suite as follows (we can keep default name):
New Test Suite
Now let's create new Test Case and name it "Login":
New Test Case
Since we are using an external library (our loaded jar file) we have to do some scripting to be able to use it. Therefore we are going to create a new Groovy Script as our first step. And in fact most of the steps will be treated as groovy scripts (let's also name it Login):
New Groovy script

Selenium Firefox WebDriver instance

Using the following code we can easily start using our Selenium WebDriver using the Firefox driver (IE, Chrome and other drivers can also be used but we willstickto Firefox in this tutorial):

// Import necessary classes
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.support.ui.ExpectedCondition
import org.openqa.selenium.support.ui.WebDriverWait

// Get the new driver
WebDriver driver=new FirefoxDriver();

// Go to page
driver.get("http://google.com")


Our groovy script in SoapUI will look as follows:
New Selenium Firefox driver
Unfortunatelly thee is a little bug in SoapUI 5.0.0. Once we run our brand new groovy script we will face the following pop-up window with following error (java.lang.NoClassDefFoundError: Could not initialize class org.apache.http.conn.ssl.SSLConnectionSocketFactory error at line: 10):
Selenium WebDriver SSL error
We don't like that. In order to get rid of this issue we need to download latest HttpClient libraries and copy them to SoapUI. Best way to start is to go to Apache HttpComponents download page http://hc.apache.org/downloads.cgi and download latest HttpClient package (at the time of creation of this tutorial latest stable version was 4.5.2).
Once downloaded we better unzip it and replace following old libraries from SoapUI lib directory (SoapUI_path/lib/):
Old lib directory
At the end content of our lib directory should be as follows:
New lib directory
Now when we run very same Groovy script which was failing due to SSL issue previously result will be much better. Press the Play button and observe:
google.com

Fill in the Login form

Now when we know that WebDriver instance can be initiated it's time to proceed with some very lightwieght form. In our case it will be a Login form available at http://soapui.tips-and-tricks.eu/selenium-webdriver-in-soapui/test_form.php. Obviously our goal will be to fill it in. To do so we will use following simple code:

// Go to page
driver.get("http://soapui.tips-and-tricks.eu/selenium-webdriver-in-soapui/test_form.php")

// Fill in the login form
driver.findElement(By.name("login")).clear();
driver.findElement(By.name("login")).sendKeys("login");
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("password");

Ok, let's describe above code a little. Of course we hade to change url from google.com to our Login form url under test. Afterwards we find elements login and password by its name (it is a parameter of Input element in HTML like this name="login"). Clear() it in case something was there before and fill in using a method sendKeys().
filled form
When the form is filled in (actually does not matter with what value, there is totaly no logic in our test form) we can proceed to find the submit button and press it using following piece of code:

// Click on Login button
driver.findElement(By.name("submit")).click();

Yet again we found submit button using name parameter. In order to submit the form we have to click() on button right?
After pressing Submit we should see following awesome confirmation of successfull login:
successfull login
Very last thing we should do to make our test case complete is to add an assertion in it. We can also close the browser of course. Code is as follows:

// Assertion
assert(driver.getPageSource().contains("Access granted"))

// Close the browser
driver.quit()

You can see above that we have simply asserted on a selenium driver methods getpageSource() and contains() looking for the test "Access granted". After the test we have closed the broser using quit() method.

That's it. Our simple groovy script looks as follows at the end of our journey:
groocy script
Run it and enjoy the show! Now you can amend the code to fit your needs.

You can download entire project from Downloads page.