16 Selenium Best Practices For Efficient Test Automation

Image for post
Image for post

Note: The code samples may be displayed improperly because of markdown. I recommend continuing to read the original article on our blog to make sure all the examples are displayed properly.

Image for post
Image for post

1. Avoid Blocking Sleep Calls

driver = webdriver.Chrome()
driver.get(“https://www.lambdatest.com/")

time.sleep(5000)
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver = webdriver.Chrome()
driver.get(“https://www.lambdatest.com/")
timeout = 10

try:
element_present = EC.presence_of_element_located((By.LINK_TEXT, ‘Sitemap’))
WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
print(“Timed out while waiting for page to load”)

2. Name the Test Cases & Test Suites Appropriately

Image for post
Image for post

3. Set the Browser Zoom Level to 100 percent

Image for post
Image for post

4. Maximize the Browser Window

5. Choose the Best-Suited Web Locator

Image for post
Image for post

6. Create a Browser Compatibility Matrix for Cross Browser Testing

Image for post
Image for post

7. Implement Logging and Reporting

8. Use Design Patterns and Principles i.e. Page Object Model (POM)

Image for post
Image for post

9. Use BDD Framework with Selenium

Feature: LambdaTest search
Scenario: Search for LambdaTest on DuckDuckGo
Given I am on the DuckDuckGo homepage
When I enter search term as LambdaTest
Then Search results for LambdaTest should appear

10. Follow a Uniform Directory Structure

11. Use Data-Driven Testing for Parameterization

12. Do Not Use a Single Driver Implementation

13. Come Up with Autonomous Test Case Design

14. Use Assert and Verify in Appropriate Scenarios

15. Avoid Code Duplication (or Wrap Selenium Calls)

16. Leverage Parallel Testing in Selenium

Worst Practices for Automation Testing with Selenium

1. File Downloads

2. CAPTCHAS

3. Two-Factor Authentication (2FA)

4. Link Spidering (or Crawling)

5. Automation of Logins on Gmail, Facebook, & other Related Platforms

6. Avoid Test Dependency

7. Performance Testing

It’s a Wrap

Image for post
Image for post

Author Himanshu

Written by

Product Growth at @lambdatesting (www.lambdatest.com)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store