Optimize Selenium WebDriver Automated Test Scripts: Maintainability

[A must read for all SDETs and Automation Engineers].

This time Zhimin Zhan provides a number of code examples for improving test maintainability.

When you can run automated tests often, and the application changes all the time, it is important that test scripts are: Quick and easy to maintain. Easy to read.

The leading reason for test automation to fail is that the team fails to maintain its automated scripts. In this article, Zhimin Zhan helps to understand some simple and practical tips at the test steps level that can help you make your automated functional tests more maintainable. These techniques are:

  • One Test Script Line for One User Operation
  • Default Parameter Value
  • Extend function behaviour with optional Hashmap

Click here to Read complete article.

Uploading Files with Selenium Java – Test Automation Cookbook – via Applitools

Angie Jones and Applitools has recently launched a new initiative – Automation Cookbook. It has free bite-size recipes related to JS (#Cypress) & Java (#Selenium).

Here is a tip from cookbook: Uploading files with Selenium Java:

Code:

package file_upload;

import base.BaseTests;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;

public class FileUploadTests extends BaseTests {

    @BeforeEach
    public void launchApp(){
        driver.get("https://kitchen.applitools.com/ingredients/file-picker");
    }

    @Test
    public void testFileUpload() {
        String filePath = "/Users/angie/workspace/recipes/resources/images/mac-n-cheese.jpg";
        driver.findElement(By.id("photo-upload")).sendKeys(filePath);
    }
}

You can practice above code in this file picker: https://kitchen.applitools.com/ingredients/file-picker

Find more Selenium and Cypress recipes over at the Automation Cookbook: https://applitools.com/cookbook/

Thanks to Angie and Applitools for making our life easy.

Top Selenium Commands Cheat Sheet

Fetching a web page
Using Get method:
driver.get(“www.google.com”)

Using Navigate method:
driver.navigate().to(“https://google.com “);

Locating forms and sending user inputs
driver.findElement(By.id(“lst-ib”)).sendKeys(“selenium tutorials”);

Clearing User inputs
driver.findElement(By.name(“q”)).clear();

Fetching data over any web element
driver.findElement(By.id(“element567”)).getText();

Performing Click event
driver.findElement(By.id(“btnK”)).click();

Navigating backward in browser history
driver.navigate().back();

Navigating forward in browser history
driver.navigate().forward();

Refresh/ Reload a web page
driver.navigate().refresh();

Closing Browser
driver.close();

Closing Browser and other all other windows associated with
the driver
driver.quit();

Moving between Windows
driver.switchTo().window(“windowName”);

Moving between Frames
driver.switchTo().frame(“frameName”);

Drag and Drop
WebElement element = driver.findElement(By.name(“source”));
WebElement target = driver.findElement(By.name(“target”));
(new Actions(driver)).dragAndDrop(element, target).perform();

Difference between WebDriverlistener and TestNG listener?

You must have always craved for more logs from the Webdriver so that you can debug your scripts or may be log more information about your tests. 

Here is your answer to it, WebDriverEventListner and TestNgListner(ITestListener Interface.)

 In TestNg you can use Listeners in Annotation.

WebDriverEventListener – This is an interface, which have some predefined methods so we will implement all of these methods.

Difference:- TestNG Listener are triggered at test level such as before starting test after the test or when test fails etc; whereas WebDriver Listener are triggered at component level such as before click, after click etc

Selenium code to capture screenshot only when test fails.

ITestResult is an interface which give information about our current test case which is under execution
result variable will store all the details of @Test along with the pass or fail result.


——————-
@AfterMethod
public void tearDown(ITestResult result) throws IOException
{
if(ITestResult.FAILURE==result.getStatus())
{
UtilityScrnSht.ScreenShot(Page.driver, result.getName());
//user created method
}
}
——————–

Simplest Guide to XPath Axes in Selenium Webdriver by Codoid

Simplest guide to XPath Axes by Codoid. Must Read:

To use XPath effectively in Selenium WebDriver, you should be familiar with XPath Axes and HTML. There are thirteen axes available for XPath. As per the definition in Mozilla Web Docs – “An axis represents a relationship to the context node, and is used to locate nodes relative to that node on the tree.”

As an automation tester, if you are familiar with XPath Axes, you can write robust locators for dynamic WebElements.

Let’s look into all the XPath Axes which are useful for test automation scripting.

Let’s say you want to select a parent node using its child node. You first write XPath to pick the child node and the parent node using XPath parent axis.

Read More at: https://codoid.com/how-to-use-xpath-axes-in-selenium-webdriver/

Read More at: https://codoid.com/how-to-use-xpath-axes-in-selenium-webdriver/

Selenium – Python – Get all child elements

There are various ways to find child elements:

  • find_elements_by_name
  • find_elements_by_xpath
  • find_elements_by_link_text
  • find_elements_by_partial_link_text
  • find_elements_by_tag_name
  • find_elements_by_class_name
  • find_elements_by_css_selector

For instance, consider this page source:

<html>
<body>
<form id=”loginForm”>
<input name=”username” type=”text” />
<input name=”password” type=”password” />
<input name=”continue” type=”submit” value=”Login” />
</form>
</body>
<html>

The form element can be located like this:

login_form = driver.find_element_by_id(‘loginForm’)

More references here

Most Exhaustive WebDriver Locators Cheat Sheet.

Download the most extensive Selenium WebDriver Locators cheat sheet. All you need to to know- the most basic locators to the most advanced ones. Contains:

  • full list of the most used CSS selectors
  • complete list of the most used XPath selectors
  • CSS and XPath locators about parent and child relations
  • most widely used XPath axes selectors
  • CSS pseudo-classes locators
  • selectors for working with HTML tables

Click here to download.

Cheatsheet provided by www.automatetheplanet.com.

[Tip] How to Speed up the UI Automation tests?

When we ran our scenarios through the UI using Selenium it made our test suite slow.
What could you do to speed it up?

Often, the best way to speed up a UI test is to remove it, or rather, refactor it to a domain layer test that doesn’t go through the UI, but talks directly to the domain layer.
If you’re unable to do that, there are still things you can do to speed up UI tests. Start by
identifying what steps and hooks are slow.

Are you starting your web app in a Before hook for every scenario? Try starting your app only once, for all scenarios.

Are you launching a new browser for every scenario? Try reusing the browser instance.
Do your scenarios need to go through a lot of pages before you get to the UI you really want to test? Try to go directly to the page you need to test, and set up the data you need “underneath” the UI.

Many of these optimisations introduce a new problem – you might have state leaking between scenarios. When state leaks between scenarios they might pass when run in a certain order, but they might fail when run in a different order. This can make it very difficult and time consuming to fix failing scenarios. To avoid this we recommend you use Before hooks to explicitly clear state that might have been left over from the previous scenario.

Source/References: https://cucumber.io/