Selenium-Grid allows you to run your tests on different machines against different browsers in parallel. That is, running multiple tests at the same time against different machines running different browsers and operating systems. Essentially, Selenium-Grid support distributed test execution. It allows for running your tests in a distributed test execution environment.
When to use
- To reduce the time it takes for the test suite to complete a test pass.
- To run your tests against multiple browsers, multiple versions of browser, and browsers running on different operating systems.
Selenium environment set up
- Download java and install
- Setting up the path for windows
- Download IE driver/Chrome driver
- Put IE driver/Chrome driver into system environment variables
- Download FF/Chrome/IE
- Download selenium-server-standalone-*.jar
Note: You need set up the environment for both hub and node.
How to start
Start hub:
java -jar selenium-server-standalone-2.45.0.jar -role hub
Start node:
java -jar selenium-server-standalone-2.45.0.jar -role node -port 5558 -hub http://10.2.20.84:4444/grid/register
Here 10.2.20.84 is the hub’s IP, we can use ipconfig command to get it. And 4444 is the default port (can be change by –port if you don’t want to use the default port). Obviously, you can start more than one node using the code above. One thing need to be careful is the port. When you start more than one node, you should using different port number to prevent the risk that port is already in used on your system.
When you got the information like “Registering the node to hub: xxxxxxxx”, that means node is ready to be used. You can use link http:// 10.2.20.84:4444/grid/console to get the node’s status.
Now selenium grid has been set up successfully, we can run a small case:
Using RemoteWebDriver() to connect the hub, and the connection information is http://10.2.20.84:4444/wd/hub. After connected to hub, there will create a session on hub, meanwhile, hub will mapping the appropriate node depending on the request.
TestNG Configuration
parallel: set the default mechanism used to determine how to use parallel threads when running tests.
thread-count: set the default maximum number of threads to use for running tests in parallel
Here we set the parallel value as “tests” and thread-count as “2”. Then when we run scripts as testNG suite, it will use 2 parallel threads for tests. Also you can change parallel value to methods or classes. And the thread-count can be 3, 4, 5… It depends on how many parallel threads you want.
Best Practice
- Port: When you start more than one node in one machine, you should using different port number to prevent the risk that port is already in used on your system.
- Do not use “static” variable to initialize driver with parallel execution of scripts. When you define your field as static, then every thread has access to it and every thread can modify that memory space. Initialize driver with “static” works fine with non-parallel execution of scripts but not parallel execution.
Reference