How to use SoapUI with Selenium

SoapUI is an open-source tool that provides users with a graphical user interface to create, execute, and manage various API tests. It is also widely used to validate web services and APIs’ behavior, performance, and security.

On the other hand, Selenium is mainly used to automate repetitive tasks and solve web-related tasks programmatically. Both these tools can be used in tandem for scenarios that involve testing web services and user interfaces.

These tools automate web interactions between SoapUI and a web application. This integration is helpful in scenarios where we need to test both web services and web interfaces, as SoapUI allows us to test web services, and Selenium provides the functionality to test interfaces.

Implementation

The first step to solving any problem is to have a mind map. The following image shows us the basic approach that we should take when implementing SoapUI with Selenium:

Basic mind map for implementation
Basic mind map for implementation

Let’s look at each of these steps to get a more in-depth understanding:

  • Prepare your environment: To do this, we need to fulfill the following steps:

    • Installation of SoapUI: Download and Install the SoapUI. We must then make a SoapUI project and set up the necessary API test cases.

    • Installation of Selenium web driver: Download the Selenium web driver’s libraries and integrate them with the SoapUI project. Choose the programming language of your choice, which Selenium supports. The easiest way to integrate Selenium is to use GroovyGroovy is a language that operates on the Java Virtual Machine (JVM) and shares a close resemblance to Java in terms of structure and syntax. . [object Object]

  • Make Selenium scripts: Instantiate the Selenium web driver inside the script to interact with the web browser. Use the web driver commands to interact with the user interface and automate your test cases. The following code block (in Python) shows a Selenium script that searches “What is SoapUI” on Google:

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys

# Initialize Chrome WebDriver
options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
prefs = {"download.default_directory": "."}
options.add_experimental_option("prefs", prefs)
browser = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

# Open Google
browser.get("https://www.google.com")

# Find the search box element
search_box = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.NAME, "q")))

# Enter the search query
search_box.send_keys("What is SoapUI")

# Submit the search (press Enter)
search_box.send_keys(Keys.RETURN)

# Wait for the search results to load
time.sleep(20)  # Adjust this time delay based on your network speed
Selenium script
  • Combine SoapUI with Selenium: To do this, we must identify the integration points where our test cases must interact with the browser interface. This call for interaction will be through the test case we define in SoapUI, and the Selenium script will do the interaction.

  • Reporting and assertions: We can incorporate assertions and create reports for SoapUI and Selenium. Assertions validate web service responses for SoapUI tests and verify web page elements in Selenium. Reports, on the other hand, are generated depending on our requirements. We can also generate combined test reports that include results from both SoapUI and Selenium.

  • Run tests and generate reports: We execute our combined tests in SoapUI. The SoapUI will then generate reports according to our needs, which we can use to analyze performance, identify issues, and validate the end-to-end functionality of our web application.

Execution of test cases

We have discussed the general mind map on how to use SoapUI with Selenium, but to properly execute tests in SoapUI, we need to follow the following steps:

Steps for running tests in SoapUI
Steps for running tests in SoapUI

Let’s look at the steps in a little detail:

  • Create a test suite: In SoapUI, we will create a new TestSuiteA collection of test cases that can be used for grouping functional tests into logical units or use an existing one containing the combined test cases we defined earlier.

  • Create test steps: Inside our TestSuite, we can add test steps by right-clicking it and clicking “Add Test Step.” There will be separate test steps for our API and web UI tests. These steps will also be added in order of API tests first and then web UI.

  • Configure test steps: We need to configure all of the test steps. For API tests, we need to call the respective API from our SoapUI project, and for web UI tests, we need to make sure that each step calls the correct Selenium script. We can use the “Groovy Script” test step from SoapUI to execute the Selenium script.

  • Set dependencies: Go to the “Dependencies” tab in the test steps’ properties and specify which test cases must be executed before the current step. It is generally preferred to have API tests before the web UI tests as they might affect the web application’s behavior.

  • Run test suite: To run the test suite, right-click the suite in the SoapUI Navigator panel and click the “Run TestSuite” button. Alternatively, the SoapUI command line runner can automate test execution. The following is an example of a command line where the test suite is called “MyTestSuite” in the SoapUI project TestProject.xml and the output file for results is -fResults.xml:

Conclusion

This Answer shows us all the general steps to use SoapUI with Selenium to test APIs and web UI simultaneously. Using SoapUI, we can create various reports of our tests, which include HTML, JUnit-style XML, and HTML reports for analysis as well, and it is up to us how to use this powerful tool to test our web applications.

Copyright ©2024 Educative, Inc. All rights reserved