Blog 0.7: Using AWS to Test Selenium WebDriver Remotely
Setting up a Remote Selenium WebDriver
Selenium is a collection of open source APIs which are used to automate the testing of a web appllication.
In my previous blog 0.6, I have demonstrated the testing using Selenium WebDriver locally. In this blog, I will demonstrate the options to use Selenium WebDriver REMOTELY. Yes, Remotely. We can use WebDriver remotely the same way you would use it locally.
Difference: The primary difference is that a remote WebDriver needs to be configured so that it can run our tests on a seperate machine.
A remote WebDriver is composed of two pieces: a client and a server. The client is your WebDriver test and the server is simply a JAVA servlet, which can be hosted in any modern JEE app server.
Creating a WEB SERVER using AWS EC2 (docker-compose)
We can create a EC2 instance with docker-compose AMI (Amazon Machine Image). By selecting the AMI with docker-compose we will have all the required software and java environment variables which is required to run the tests.
For this particular testing we will configure the Security Groups:
SSH TCP from anywhere Custom TCP port 4444 from anywhere
Configuring the EC2 Webserver
By hosting this web-server we can use ipv4 address and port number in our Selelnium Source Code to run tests remotely. Before we can use this web-server, we need to configure the environment settings inside the web-server by accessing using ssh.
To access the newly created EC2 instance:
sudo ssh -i
ec2-user@
Steps to configure:
- sudo yum update
- check the java version and remove/update to NEW ver.
- check if the docker is installed
Creating a Customer Docker file
We need to create a Docker File that can run a Selenium-HUB. Here, Selenium-Hub will install different Images of different web browsers. In other words, Selenium-Hub will hold different Nodes which runs tests for different types of web-browsers (Chrome, Firefox, Opera, etc.)
SOURCE code for Docker File
We can get different versions of docker-compose in yml format from the following github repo:
Architecture of Selenium-Hub hosted inside the EC2 WebServer
In the above diagram, we can see that Selenium-Hub running on EC2 Web-server will be the main target point for our tests. By pointing our url inside source code with the ip addr and port number, we can run tests remotely.
Example:
Importing the Libraries:
# Using selenium to get TRACK info
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
Acutal Source Code that takes Ipv4 address and Port Number of EC2 web-server that hosts the SELENIUM-HUB.
Steps:
- Create a WebDriver
- Options: tests without opening web browser. Here, using Firefox
- By using Remote Webdriver the target point for URL is the EC2 Webserver IPV4 address and port number
browser_options= Options()
browser_options.add_argument('--headless')
# REMOTE web driver from AWS
# pass the IPV4 address and port number AND dir: /wd/hub
# remotely testing, using AWS ec2
chrome_options= webdriver.ChromeOptions()
# REMOTE web driver via FIREFOX
browser= webdriver.Remote(command_executor="34.220.193.159:4444/wd/hub", options= chrome_options)
# PASS the URL of Wikipedia to search for specific artist
browser.get('https://en.wikipedia.org/wiki/Tupac_Shakur')
# wating time before scrapping
browser.implicitly_wait(5)
- Test: get element from the specified web page
Here, we can get the paragraph text from the wikipedia.
name_artist= browser.find_element_by_xpath('//*[@id="firstHeading"]')
print(name_artist.text)
Background work
Every time the source code runs, the EC2 web-server will get the request on Selenium-Hub. This request will be passed through the Nodes to the desired web-browser that has been assigned on the source code. In the above example, I have tested the web-server with FireFox.