Monday, August 15, 2016

Set up Selenium Grid2 on Windows for testing with IE Safari Chrome Opera and Firefox

Set up Selenium Grid2 on Windows for testing with IE Safari Chrome Opera and Firefox



Update: If you have an Amazon Web Services (AWS) account, heres a nice CloudFormation template that will do everything for you!

~~~~~~~~~~~

Confused by Selenium?  Youre not the only one!  There are many Selenium guides out there but I couldnt find any that provided a complete tutorial for installing it on Windows for IE, Safari, Chrome, and Firefox testing so I thought Id document my process with Windows Server 2008 R2:

1. login to Windows with an account that has administrator rights

2. if not already open, click on Start and search for Server Manager to open; then click on Configure IE ESC link (in the Security Information section) and turn off IE ESC for both Administrators and Users

3. if not already done, use this guide to show file extensions and hidden folders

4. install Java

5. install Chrome

6. install Firefox

7. install Safari for Windows  Update: Safari for Windows is no longer supported and does not provide an accurate testing platform -- either use a cloud service to test Safari on Mac or install a Selenium Grid Node on a Mac

8. create a folder in C: called selenium and download http://selenium-release.storage.googleapis.com/2.41/selenium-server-standalone-2.41.0.jar into it

9. download and extract the InternetExplorerDriver and ChromeDriver drivers into the C:selenium folder (Notes: Use the 32-bit versions even if your Windows OS is 64-bit because even if your OS is 64-bit, browsers run as 32-bit.  The Firefox and Safari drivers are already included in the selenium-server-standalone.jar file as pre-built extensions.  Opera uses the same engine as Chrome so you dont have to run separate tests for it.)

10. click Start and search for Edit the system environment variables to open the System Properties dialog, then click the Environment Variables... button, then click on the Path variable in the System variables list and click the Edit... button.  At the end of the Variable value textbox add:
;C:Program Files (x86)Javajre7 in;C:selenium
11. restart Windows

12. create a file called hub.json in the C:selenium folder with the following contents:

{
  "host": null,
  "port": 4440,
  "newSessionWaitTimeout": -1,
  "servlets" : [],
  "prioritizer": null,
  "capabilityMatcher": "org.openqa.grid.internal.utils.DefaultCapabilityMatcher",
  "throwOnCapabilityNotPresent": true,
  "nodePolling": 5000,
  "cleanUpCycle": 5000,
  "timeout": 300000,
  "browserTimeout": 0,
  "maxSession": 5,
  "jettyMaxThreads":-1
}

13. create a file called node.json in the C:selenium folder with the following contents:

{
  "capabilities":[
    {
      "platform": "WINDOWS",
      "browserName": "firefox",
      "maxInstances": 1,
      "seleniumProtocol": "WebDriver"
    },{
      "platform": "WINDOWS",
        "browserName": "chrome",
        "maxInstances": 1,
      "seleniumProtocol": "WebDriver"
    },{
      "platform": "WINDOWS",
      "browserName": "internet explorer",
      "version": "11",
      "maxInstances": 1,
      "seleniumProtocol": "WebDriver"
    },{
      "platform": "WINDOWS",
      "browserName": "internet explorer",
      "version": "10",
      "maxInstances": 1,
      "seleniumProtocol": "WebDriver"
    },{
      "platform": "WINDOWS",
      "browserName": "internet explorer",
      "version": "9",
      "maxInstances": 1,
      "seleniumProtocol": "WebDriver"
    },{
      "platform": "WINDOWS",
      "browserName": "internet explorer",
      "version": "8",
      "maxInstances": 1,
      "seleniumProtocol": "WebDriver"
    },{
      "platform": "WINDOWS",
      "browserName": "safari",
      "maxInstances": 1,
      "seleniumProtocol": "WebDriver"
    }
  ],
  "configuration":{
    "maxSession":7,
    "port":5550,
    "host":"127.0.0.1",
    "register":true,
    "registerCycle": 5000,
    "hubPort":4440,
    "hubHost":"127.0.0.1",
    "nodeTimeout":120,
    "nodePolling":2000,
    "registerCycle":10000,
    "cleanUpCycle":2000,
    "timeout":30000
  }
}

14. test the Selenium server by:

i. opening a command prompt and typing:
java -jar C:seleniumselenium-server-standalone-2.41.0.jar -role hub -hubConfig C:seleniumhub.json
ii. opening another command prompt window and typing:
java -jar C:seleniumselenium-server-standalone-2.41.0.jar -role node -nodeConfig C:selenium ode.json -Dwebdriver.chrome.driver=chromedriver.exe -Dwebdriver.ie.driver=IEDriverServer.exe
iii. open a browser and go to http://127.0.0.1:4440/grid/console and verify all browsers are displayed

15. stop the hub and node by closing each command prompt

16. download NSSM (used to start Selenium as a Windows service) and extract nssm-{version}win64 ssm.exe (assuming your OS is 64-bit, otherwise use the 32-bit NSSM executable) to C:selenium

17. open a command prompt and type:
nssm install seleniumhub java -jar C:seleniumselenium-server-standalone-2.41.0.jar -role hub -hubConfig C:seleniumhub.json
18. then, type:
nssm install seleniumnode java -jar C:seleniumselenium-server-standalone-2.41.0.jar -role node -nodeConfig C:selenium ode.json -Dwebdriver.chrome.driver=chromedriver.exe -Dwebdriver.ie.driver=IEDriverServer.exe
19. restart the computer

20. after it restarts, login, open a browser, and go to http://127.0.0.1:4440/grid/console to verify all browsers are displayed

21. add firewall exceptions by opening a command prompt and typing:
netsh advfirewall firewall add rule name="SeleniumIn" dir=in action=allow protocol=TCP localport=4440
netsh advfirewall firewall add rule name="SeleniumOut" dir=out action=allow protocol=TCP localport=4440

Congratulations!  Your Selenium grid is now functional and you can use a Selenium WebDriver-compatible testing framework (such as Codeception, WebDriverJS, etc) to connect to it (using the servers actual IP address, not localhost or 127.0.0.1).

P.S. For security reasons, in my tutorial Ive used non-default ports 5550 and 4440 instead of the default 5555 and 4444.  Feel free to use your own custom port numbers if you wish.

P.P.S. For ease and convenience, this tutorial installed the hub and node on the same server.  In a more realistic scenario, there would be a central hub on one server and multiple worker nodes on separate servers.  In that case, you would follow steps 1-4,8,10-12,14i,15-17,19,21 for the hub and all steps except 12,14i,14iii,17 for the node(s).  You will also need to update the "host" and "hubHost" settings in step 13 to point to the nodes and hubs actual IP address, respectively.

_____________________________

Additional resources:

  • DefaultHub.json
  • DefaultNode.json
  • grid2 configuration
  • grid2/webdriver docs

Available link for download