Introduction to Selenium Grid with examples — Webner Blog

Webner Solutions
3 min readApr 27, 2020

--

Introduction to Selenium Grid with examples — Webner Blog
Introduction to Selenium Grid with examples — Webner Blog

Introduction to Selenium Grid with examples

Selenium Grid is software from Selenium which allows us to run multiple tests across different browsers, operating systems, and machines in parallel.

This Grid follows the hub-node concept where we run the test on just one machine i.e. hub, but the execution will be done by different machines known as nodes.

Scenarios where Selenium Grids are used

We should use it when we want to do either one or both of the following:

  • Execute your tests against different browsers, OS, and machines all at the same time. This will ensure your application is widely compatible with different browsers and platforms.
  • You want to save time by running multiple tests in parallel.

Code Examples of How to use it
A. Single Test
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Remote(
driver = webdriver.Remote(
command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities={
"browserName": "chrome",
"version": "latest",
"video": "True",
"platform": "VISTA",
"platformName": "windows",
})
print ("Video: " + VIDEO_URL + driver.session_id)
try:
driver.implicitly_wait(30)
driver.maximize_window() # Note: driver.maximize_window does notwork on Linux selenium version v2, instead set window size and window position like driver.set_window_position(0,0) and driver.set_window_size(1920,1080)
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("documentation")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
finally:
driver.quit()

B. Multiple tests
Install
pip install pytest
Pip install pytest-xdist
pip install pytest-rerunfailures
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import logging
logging.basicConfig(filename="log.txt", level=logging.INFO)
class TestExamples(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Remote(
command_executor="'http://127.0.0.1:4444/wd/hub'",
desired_capabilities={
"browserName": "chrome",
"version": "latest",
"video": "True",
"platform": "VISTA",
"platformName": "windows",
})
self.driver.implicitly_wait(30)
self.driver.maximize_window() # Note: driver.maximize_window does not work on Linux, instead set window size and window position like driver.set_window_position(0,0) and driver.set_window_size(1920,1080)
def test_one(self):
try:
driver = self.driver
driver.get("http://www.python.org")
self.assertIn("Python", driver.title)
elem = driver.find_element_by_name("q")
elem.send_keys("documentation")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
finally:
logging.info("Test One Video: " + VIDEO_URL + driver.session_id)
def test_two(self):
try:
driver = self.driver
driver.get("http://www.google.com")
elem = driver.find_element_by_name("q")
elem.send_keys("webdriver")
elem.send_keys(Keys.RETURN)
finally:
logging.info("Test Two Video: " + VIDEO_URL + driver.session_id)
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()

Execute
py.test -n 2 --rerun 2 test_unittest.py

Log Output
Test_One Video:
https://s3-us-west-1.amazonaws.com/027a15f2-530d-31e5-f8cc-7ceaf6355377/239a61a7-c526-ceb8-9sfd-1752b682a464/play.html?4f461455-51b5-42c6-a454-1320b302862d
Test_Two Video: https://s3-us-west-1.amazonaws.com/027a15f2-530d-31e5-f8cc-7ceaf6355377/239a61a7-c526-ceb8-9sfd-1752b682a464/play.html?5505d8ed-02da-44eb-b8d4-aa010ef465f7

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Originally published at https://blog.webnersolutions.com on April 27, 2020.

--

--

Webner Solutions
Webner Solutions

Written by Webner Solutions

Our team in Salesforce is very strong, with in-depth knowledge of Salesforce classic and Lightning development as well as the Service cloud.

No responses yet