What are the top practical tips for test automation with Selenium WebDriver?

Can anyone please tell me what are the top practical tips for test automation with Selenium WebDriver.

Selenium Tip #1 – Setting The Executable Path Of The Selenium Webdriver

In order to communicate with the browser under test, you need to first download the corresponding plugin/webdriver from their official website. This plugin would be responsible for communicating with the browser and it should be present in your machine (on which you are developing the tests). The plugin/webdriver path has to be set in the Selenium Webdriver configuration.

Though the plugin/webdriver can be placed in any location since you can provide the static/relative path in the Selenium Webdriver configuration, the approach can be error prone and you need to keep track of the file path. A better & more reliable approach is to place the corresponding Selenium Webdriver in the location where the driver executable is present, in which case you need not specify the executable path in the Selenium Webdriver configuration.

If the geckodriver is not present in the Browser location, you need to manually add the path of the same in the source code. We import the selenium.webdriver.firefox.firefox_binary module to provide the path to the Firefox executable.

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
ff_binary = FirefoxBinary('path/to/gecko driver')
browser = webdriver.Firefox(firefox_binary=ff_binary)

As seen in the code snippet below, we have not specified the location of the geckodriver (Firefox Webdriver) since it is placed in the same location where Firefox browser is present. This is more reliable approach compared to the previous one and can help in reducing basic errors in implementation of test automation with Selenium.

''' Import the required modules for development '''
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
'''Creation of Firefox Webdriver '''
driver = webdriver.Firefox()
driver.get("Free Cross Browser Testing Tool on Cloud | Selenium Automation Testing")

Selenium Tip #2 – Capture Screenshot Of Test Automation With Selenium WebDriver

While performing tests, you would have come across requirements where a screenshot has to be captured for verifying results of the tests. Selenium WebDriver offers three APIs through which you can take a screenshot of a web-page.

  1. save_screenshot(‘path-where-screenshot-should-be-saved/filename.png’)
  2. get_screenshot_as_file(‘path-where-screenshot-should-be-saved/filename.png’)
  3. get_screenshot_as_png()

The first two APIs lets you save the screen of the current window in a .png file. The API returns False if there is an IOError, else it returns True. These APIs would only work if the file extension is .png, else Python throws an error & the saved content might not be viewable. If you wish to capture a screen of your current window in a binary format then make use of get_screenshot_as_png() API.

''' Import the required modules for development '''
from selenium import webdriver
import StringIO
from PIL import Image
'''Creation of Firefox Webdriver '''
driver = webdriver.Firefox()
driver.get("Free Cross Browser Testing Tool on Cloud | Selenium Automation Testing")

‘’‘Taking screenshot of the web-page. File would be saved in the location where the source code is present ‘’’

'''Option - 1'''
driver.save_screenshot('screenshot_1.png');
'''Option - 2'''
driver.get_screenshot_as_file('screenshot_2.png');
'''Option - 3'''
screenshot = driver.get_screenshot_as_png();
screenshot_size = (20, 10, 480, 600)
image = Image.open (StringIO.StringIO(screen))
region = image.crop(screenshot_size)
region.save('screenshot_3.jpg', 'JPEG', optimize=True)

Selenium Tip #3 – Refreshing WebPage While Automation Testing With Selenium WebDriver

There might be scenarios where there is a requirement for refreshing a web page, especially while waiting for a specific condition. There are a number of ways through which a webpage can be refreshed while performing test automation with Selenium Webdriver, the popular one is listed below.

1. driver.refresh() method

As the name signifies, the refresh() method is used to refresh the web page. It is asynchronous in nature hence; you should make use of this API in conjunction with document.readyState().

''' Import the required modules for development '''
from selenium import webdriver
'''Creation of Firefox Webdriver '''
driver = webdriver.Firefox()
driver.get("Free Cross Browser Testing Tool on Cloud | Selenium Automation Testing")
driver.refresh()

2. ActionChains() method

ActionChains() are another way to automate low-level interactions for automation testing with Selenium, such as key press, mouse button actions, etc. In order to refresh the webpage, we make use of the ‘CTRL + F5’ combination.

import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
'''Creation of Firefox Webdriver '''
# driver = webdriver.Chrome()
driver = webdriver.Firefox()
driver.get("Free Cross Browser Testing Tool on Cloud | Selenium Automation Testing")
time.sleep(5)
print("Before refresh")
ActionChains(driver) \
 .key_down(Keys.CONTROL) \
 .send_keys(Keys.F5) \
 .key_up(Keys.CONTROL) \
 .perform()
print("After refresh")
sleep(5)
driver.quit()

Selenium Tip #4 – Open A Webpage In A New Tab

execute_script can be used to synchronously execute JavaScript code in current window/frame. Argument (a JavaScript) to open the webpage is passed as the argument to execute_script

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
driver = webdriver.Firefox()
driver.get("Google")
driver.implicitly_wait(10)
#open tab
driver.execute_script("window.open('Free Cross Browser Testing Tool on Cloud | Selenium Automation Testing', 'new tab')")
sleep(5)
driver.quit()

Selenium Tip #5 – Saving Partial Screenshot Of A Web page

There are cases where you might need to take a partial screenshot of a webpage as your perform test automation with Selenium. In such cases, you can make use of the Pillow module. You need to first install the Pillow/PIL module using the command

pip install pillow

Screenshot of the entire webpage is taken using the get_screenshot_as_png() API. Once the screenshot is ready, the PIL library is used to open the captured image in memory, after which the image (which contains the entire webpage screenshot) is cropped to get the resultant image.

from selenium import webdriver
''' Install the Pillow module using the command pip install pillow '''
from PIL import Image
from io import BytesIO
driver = webdriver.Firefox()
driver.get('Google')
# Use the Inspection tool to find the location of the logo
element = driver.find_element_by_id('hplogo')
image_location = element.location
size = element.size
png = driver.get_screenshot_as_png()
''' Since the webpage screenshot is ready, we can exit the browser.'''
driver.quit()
''' PIL Library is used to open the image in memory '''
crop_image = Image.open(BytesIO(png))
''' Extract the Left, Right, Top, and Bottom co-ordinates ''' 
left = image_location['x']
top = image_location['y']
right = image_location['x'] + size['width']
bottom = image_location['y'] + size['height']
crop_image = crop_image.crop((left, top, right, bottom))
crop_image.save('logo-screenshot.png')

Selenium Tip #6 – Execute JavaScript Code

execute_script is used to execute JavaScript code as you perform test automation with Selenium WebDriver. The syntax is driver.execute_script(“javascript code here”).

As shown in the example below, a on_click action of Register is performed [Class name is home-cta].

from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get("Free Cross Browser Testing Tool on Cloud | Selenium Automation Testing")
driver.execute_script("document.getElementsByClassName('home-cta')[0].click()")
sleep(10)
driver.close()

Selenium Tip #7 – Extracting Results Of JavaScript Code

After invoking the JavaScript code for automation testing with Selenium, you need to extract the results of these JavaScript codes. You can make use of the return keyword in order to get the result of a JavaScript code as shown in the extended example where we explained JavaScript.

from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get("Free Cross Browser Testing Tool on Cloud | Selenium Automation Testing")
driver.execute_script("document.getElementsByClassName('home-cta')[0].click()")
result = driver.execute_script("return 0")
print(result)
sleep(10)
driver.close()