EC2 as a Selenium Grid Node with Windows and IE
In the previous article I have described how to configure Selenium Grid in the AWS infrastructure. Based on that we are able to run our tests against Google Chrome and Firefox. Unfortunately in many projects it is not enough - the requirement can be that the application have to work also in Internet Explorer. Running Selenium tests via Selenium Grid on Windows with IE is a little bit more complicated. Many times I wanted to do that I heard "don't do it" or just "it is not possible". In this article I will show that it is possible and describe how to do it.
As in the previous article I assume that you are able to create new EC2 instance and
configure whole infrastructure in AWS to assure connectivity between all required
machines.
In short we will create new EC2 instance with Windows which has to have access to
Selenium Grid Hub(usually on port 4444). Machine with Selenium Grid Hub should have
access to this newly created machine to Selenium Grid Node(usually on port 5555). In
my configuration Selenium Node will be accessible by private AWS ip address from
other AWS machines. Jenkins and your computer should have also access by RDP to the
newly created machine.
Starting point of whole process is that we have EC2 instance ready and we are logged in to the machine via RDP. First thing we have to do is to add the windows firewall inbound rule to allow the connection to Selenium Grid Node (usually on port 5555). Next we have to add a new user from which we will be running selenium grid node. To do that open up the Control Panel, then User Accounts, then User Accounts again, then click on Manage User Accounts. Here you’ll click on 'Add a user account'. Once the user - lets call it SeleniumUser - is created, open 'User Accounts' details by pressing Win+R > enter 'netplwiz' > click Ok. In the newly opened window click on SeleniumUser and uncheck 'Users must enter a user name and password to use this computer' checkbox. Now we have to add remote access to the SeleniumUser. To do that open 'Remote Settings' in Control Panel > System and Security > System and in newly created window select 'Allow remote connections to this computer', click on 'Select Users...' and choose SeleniumUser.
Now lets focus on Internet Explorer settings. All things listed in this section
should be done as an SeleniumUser but with Administrator privileges so for a moment
we can change SeleniumUser account type to Administrator.
First thing we have to do is to disable Enhanced Security. To do that we have open
Server Manager from Start, then switch to Local Server and in that view change
'IE Enhanced Security Configuration' to Off. Now we have to
open the Internet Explorer and go to Settings > Internet Options.
In the Security tab uncheck 'Enable Protected Mode'
for all four zones. Verify also if zoom level in Internet Explorer is set to 100%.
Next point is to download Selenium Server Standalone and IE Driver Server from SeleniumHQ Downloads page and put them in e.g. C:\SeleniumGridNode folder. In the same folder create file run-selenium-grid.bat which will be responsible for running Selenium Grid Node (remember that java jre has to be installed). Example content of that file is:
cmd /C start/MIN java -Dwebdriver.ie.driver=C:\SeleniumGridNode\IEDriverServer.exe -jar "C:\SeleniumGridNode\selenium-server-standalone.jar" -role node -hub http://selenium-hub-ec2:4444/grid/register -browser browserName="internet explorer",maxInstances=4 -port 5555
Now open Task Scheduler and create new task with name e.g. RunSeleniumGridNode. In Security options in section 'When running the task, use following user account' select SeleniumUser and check 'Run only when user is logged in'. Set the trigger 'At log on' and set specific user to 'SeleniumUser' and as an action 'Start a program' and choose the run-selenium-grid.bat script.
The last part should be done on the Jenkins workers side. On every worker machine we have to install Xvfb and rdesktop which will be required to make the screen resolution higher.
sudo apt update; sudo apt-get -y install xvfb sudo apt-get -y install xserver-xorg-core sudo apt-get install rdesktop
Now lets set DISPLAY to :99 and export it to environment variable
echo DISPLAY=":99" | sudo tee -a /etc/environment
The main part contains a few elements. The first is responsible for running instance with created Selenium Node and waiting for it to be ready. The next element is to create virtual screen with e.g. Full HD resolution, getting the private IP of the Selenium Node instance and establish RDO connection to it as a SeleniumUser (and setting resolution of the RDP client). This script should be executed by Jenkins job before running tests on Internet Explorer (assuming that Selenium Grid Hub is up and running).
Xvfb $DISPLAY -screen 0 1920x1080x24 -ac 2>&1 > /dev/null & GRID_NODE_WINDOWS="i-XXXXXXXXXXXXXX" aws ec2 start-instances --region "eu-west-1" --instance-ids $GRID_NODE_WINDOWS aws ec2 wait instance-running --region "eu-west-1" --instance-ids $GRID_NODE_WINDOWS instancePrivateIp=`aws ec2 describe-instances --region "eu-west-1" --instance-ids $GRID_NODE_WINDOWS --query "Reservations[*].Instances[*].[PrivateIpAddress]" | grep -Eo '([0-9]*\.){3}[0-9]*'` rdesktop -u 'SeleniumUser' -p 'password-for-selenium-user' -g 1920x1080 -a 16 $instancePrivateIp &
For the end we need a script which kills Xvfb, rdesktop if it is running and disconnects the EC2 with Selenium Grid Node.
if pgrep xfreerdp; then killall xfreerdp; fi if pgrep Xvfb; then killall Xvfb; fi aws ec2 stop-instances --region 'eu-west-1' --instance-ids $GRID_NODE_WINDOWS
This script can be executed for example by Post build task plugin to make sure that this part is executed even if the tests are failed. This reduce the monthly costs of running EC2 instances. Of course there are some things we could improve like encrypting user password but in this article I focused od describing the simplest solution for running tests on Internet Explorer when Selenium Grid in AWS infrastructure.