_python
Example
Part 1: Grid Setup
Selenium Grid comes built into the Selenium Standalone Server. So to get started we'll need to download the latest version of it from the Selenium releases.
Then we need to start the Hub.
> java -jar selenium-server-<version>.jar hub
...
16:49:29.816 INFO [Hub.execute] - Started Selenium Hub
...
After that we can register Nodes to it.
> java -jar selenium-server-<version>.jar node --hub http://localhost:4444
...
16:51:47.726 INFO [NodeServer.execute] - Started Selenium Node
...
NOTE: This example only demonstrates a single node on the same machine as the hub. To span nodes across multiple machines you will need to place the standalone server on each machine and launch it with the same registration command (replacing http://localhost
with the location of your hub, and specifying additional parameters as needed).
Now that the grid is running we can view the available browsers by visiting our Grid's console at http://localhost:4444/grid/console
.
To refine the list of available browsers, we can specify an additional --driver-implementation
parameter when registering the Node.
For instance, if we wanted to only offer Safari on a node, we could specify it with --driver-implementation safari
, which would look
like this:
java -jar selenium-server-<version>.jar node --hub http://localhost:4444 --driver-implementation safari
We could also repeat this parameter again if we wanted to explicitly specify more than one browser.
java -jar selenium-server-<version>.jar node --hub http://localhost:4444 --driver-implementation safari --driver-implementation chrome
There are numerous parameters that we can use at run time. You can see a full list at the Selenium Grid documentation.
Part 2: Test Setup
Now let's wire up a simple test script to use our new Grid.
First we'll need to pull in our requisite libraries (import unittest
for our test framework and from selenium import webdriver
to drive the browser), declare our test class, and wire up some test setUp
and tearDown
methods.
# filename: grid.py
import unittest
from selenium import webdriver
class Grid(unittest.TestCase):
def setUp(self):
firefox_options = webdriver.FirefoxOptions()
self.driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',
options=firefox_options
)
def tearDown(self):
self.driver.quit()
def test_page_loaded(self):
driver = self.driver
driver.get('http://the-internet.herokuapp.com')
assert driver.title == 'The Internet'
if __name__ == "__main__":
unittest.main()
Notice in setUp
we're using remote WebDriver in Selenium (e.g., webdriver.Remote
) to connect to the Grid.
And we are telling the Grid which browser we want to use with the browser options (e.g., firefox_options = webdriver.FirefoxOptions()
).
You can see a full list of the available browser options at the Selenium documentation.
Expected Behavior
When we save this file and run it (e.g., python grid.py
from the command-line) here is what will happen:
- Connect to the Grid Hub
- Hub determines which Node has the necessary browser/platform combination
- Hub connects the test to the Node
- Browser opens on the Node
- Test runs
- Browser closes on the Node
Summary
If you're looking to set up Selenium Grid to work with Internet Explorer or Chrome, be sure to read up on how to set them up since there is additional configuration required for each. And if you run into issues, be sure to check out the browser driver documentation for the browser you're working with:
Also, it's worth noting that while Selenium Grid is a great option for scaling your test infrastructure, it by itself will NOT give you parallelization. That is to say, it can handle as many connections as you throw at it (within reason), but you will still need to find a way to execute your tests in parallel.
Happy Testing!