Python - Selenium Grid


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 Architecture
Selenium Grid has a Hub and Node Architecture.

Hub
The hub is the central point where you load your tests.
There should only be one hub in a grid.
The machine containing the hub is where the tests will be run, but you will see the browser being automated on the node.
Nodes
Nodes are the Selenium instances that will execute the tests that you loaded on the hub.
There can be one or more nodes in a grid.
Nodes can be launched on multiple machines with different platforms and browsers.
The machines running the nodes need not be the same platform as that of the hub.

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.

Note: 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.

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.141.59.jar -role hub

Default Port is : 4444
Default Host is : LocalHost

The hub should successfully be launched. Your command prompt should look similar to the image below.
Another way to verify whether the hub is running is by using a browser. Simply open up a browser (Where hub is running) and go to http://localhost:4444/grid/console
Now Hub is Running

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

java -Dwebdriver.chrome.driver="C:\resources\executables\chromedriver.exe" -jar selenium-server-standalone-3.141.59.jar -role webdriver -hub http://localhost:4444/grid/register -port 5577

The node should successfully be launched. Your command prompt should look similar to the image below.
Go to the Selenium Grid web interface and refresh the page. You should see something like this.
Note: If you want to launch node  in different machine(not in hub machine) then you should give ip address of the hub machine instead of locahost

Ex: java -Dwebdriver.chrome.driver="C:\resources\executables\chromedriver.exe" -jar selenium-server-standalone-3.141.59.jar -role webdriver -hub http://xx.xx.xx.xx:4444/grid/register -port 5577

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

Ex: java -Dwebdriver.chrome.driver="C:\resources\executables\chromedriver.exe" -jar selenium-server-standalone-3.141.59.jar -role webdriver -hub http://xx.xx.xx.xx:4444/grid/register -port 5566

Designing Test Scripts That Can Run on the Grid
To design test scripts that will run on the grid, we need to use DesiredCapabilites and the RemoteWebDriver objects.

DesiredCapabilites is used to set the type of browser and OS that we will automate
RemoteWebDriver is used to set which node (or machine) that our test will run against.
   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

Scenario :
Invoke Firefox browser.
Click on Sign in Button
Enter UserName and Password
Click on the Login button.
Verify the Window Title
Close the browser

Note: above scenario  will run 2 tests in parallel in different machines for that you have to install pytest and xdist modules.

Below is the code for Grid example
from selenium import webdriver
import pytest
import time
import xdist
import pytest_html

@pytest.fixture()
def WebDriver1():
    base_url =
"http://automationpractice.com/"
   
desiredCapabilities = {
      
"browserName": "chrome"
   
}
    driver = webdriver.Remote(
command_executor='http://localhost:4444/wd/hub',desired_capabilities=desiredCapabilities)
    driver.get(base_url)
   
return driver

# --------------------------------------------------------------
def test_one(WebDriver1):
    WebDriver1.find_element_by_xpath(
"//a[@title='Log in to your customer account']").click()
    WebDriver1.find_element_by_xpath(
"//input[@id='email']").send_keys("abc111@gmail.com")
    WebDriver1.find_element_by_xpath(
"//input[@name='passwd']").send_keys("abcd12341")
    WebDriver1.find_element_by_xpath(
"//button[@type='submit' and @id='SubmitLogin']").click()
    time.sleep(
3)
    errormsg = WebDriver1.find_element_by_xpath(
"//div[@class='alert alert-danger']//li").text
   
assert (errormsg == "Authentication failed.1")
    WebDriver1.quit()
#-------------------------------------------------------------------------------------
def test_two(WebDriver1):
    WebDriver1.find_element_by_xpath(
"//a[@title='Log in to your customer account']").click()
    WebDriver1.find_element_by_xpath(
"//input[@id='email']").send_keys("abc111@gmail.com")
    WebDriver1.find_element_by_xpath(
"//input[@name='passwd']").send_keys("abcd1234")
    WebDriver1.find_element_by_xpath(
"//button[@type='submit' and @id='SubmitLogin']").click()
    time.sleep(
3)
   
assert "My account - My Store" in WebDriver1.title
    WebDriver1.quit()

Comments