Execute Python Selenium tests in Selenium Grid

The Blog To Learn Selenium and Test Automation

Execute Python Selenium tests in Selenium Grid

In this blog post, let’s see how can we execute Python selenium tests in Selenium Grid.

Before moving to python selenium test setup, let us understand what is selenium Grid and how to start it.

What is Selenium Grid?

Selenium-Grid allows you 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.

Selenium Grid Setup:

Selenium grid installation is simple. All you have to do is download the Selenium standalone server jar file from the
SeleniumHq website’s download page and start server using java command.

You need to have Java installed in your machine as prerequisite for this activity. This is because selenium standalone server is available only as jar that can be executed using java command.

A grid consists of a single hub, and one or more nodes. Both are started using the selenium-server-standalone.jar executable.

The hub receives a test to be executed along with information on which browser and platform (WINDOWS, LINUX, etc…) where the test should be run. Hub knows the configuration of each node that has been registered to the hub. Using this information it selects an available node that has the requested browser-platform combination. Once a node has been selected, Selenium commands initiated by the test are sent to the hub, which passes them to the node assigned to that test. The node runs the browser, and executes the Selenium commands within that browser against the application under test.

Start Hub:

Open command prompt and go to the folder where you have downloaded selenium-standalone-server jar file and run command:

$ java -jar selenium-server-standalone-3.11.0.jar -port 4444 -role hub

Default port – 4444
Default host – localhost

Start Node:

Open command prompt and go to the folder where you have downloaded selenium-standalone-server jar file and run command:

$ java -jar selenium-server-standalone-3.11.0.jar -host localhost -port 5555 -role node -hub http://localhost:4444/grid/register -browser browserName=firefox,platform=LINUX

Default port – 5555
Default host – localhost

If you want more than one node in the same machine, just run the command again with some other port that is not use.

Note:
1. You can start more than one nodes and register to same hub
2. If no -browser param and values specified, nodes by default starts with 5 chrome and 5 firefox intances in case of Linux; nodes starts with 5 chrome, 1 internet explorer and 5 firefox browser intances in case of Windows

To Check Status:

Go to your browser and type “http://localhost:4444/grid/console” to see Selenium Grid console up and running.

Now Selenium grid is up and running. Hub will be listening for incoming test execution requests and hub will forward to appropriate nodes once a request is received. If there is no matching nodes with desired capabilities passed in test execution request, excpetion is returned to requestor.

In our test, let’s open an url in browser and print title of that page.

from selenium import webdriver

desiredCapabilities={
"browserName":"chrome"
}

driver = webdriver.Remote(desired_capabilities = desiredCapabilities)
driver.get("https://www.google.co.in/")
print(driver.title)
driver.quit()

Explanation:

Remote method call creates Remote webdriver object which is required to execute tests in Selenium grid.

There are two parameters required to create ‘Remote’ webdriver object:

1. desired_capabilities – this is required to create respective browser session.

2. command_executor – Selenium grid hub Url for driver to communicate. If your test is running from the server where hub is running, then no need to provide this; Because if no server url given, Selenium will try to connect default url – http://localhost:4444/wd/hub

from selenium import webdriver

desiredCapabilities={
"browserName":"chrome"
}

driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub',desired_capabilities = desiredCapabilities)
driver.get("https://www.google.co.in/")
print(driver.title)
driver.quit()

Note:
Please note that “browserName”:”Chrome” and “browserName”:”chrome” are different.

“browserName”:”Chrome” – This will throw below exception

selenium.common.exceptions.WebDriverException: Message: Error forwarding the new session cannot find : Capabilities {browserName: Chrome}

Please let me know your thoughts and queries in comments.

 

Leave a Reply

Your email address will not be published. Required fields are marked *