Tuesday, October 13, 2020

6 practices for efficient Selenium Web Browser Automation

 

Given that Selenium is one of the most widely used frameworks for running automated tests on browsers, it is also one of the commonly discussed topics in testing circles. Selenium’s powerful open-source features and adoption across multiple browsers make it an exceptionally useful tool for browser automation.


Selenium allows the running of automated tests in multiple programming languages against browsers and devices by using a cloud Selenium Grid, similar to what BrowserStack provides. And since Selenium tests have become an indispensable part of the software testing pipeline, it makes sense to outline a few best practices.


This article will discuss five practices that contribute to efficient Selenium web browser automation, allowing thorough verification of websites within challenging timelines.


1. Correct Usage of Locators

The purpose of Selenium is to automate user actions, thus interacting with the browser in order to navigate, click, type, and run multiple operations to check objects within the DOM. Interacting with each web element on a website requires identifying those elements, using Locators in Selenium.


Selenium Locators are of numerous types:


Class

ID

Link Text

Tag Name

XPath

Selecting the right locator can make a test script flexible and offer a greater chance of success. Inversely a badly picked locator can result in a brittle test that breaks at the first sign of a UI change. Use unique Classes or IDs as locators, since it is unlikely that they will change without someone on the QA or dev team being informed. However, locators like link text can change quite often – for example, a dynamic button that appears only when a user is logged.


2. Use Data-Driven Testing

Data-Driven testing allows testers to use the same test and the same code for different input types and values. When combined with Selenium, it allows devs and QAs to insert modifications. Consequently, this can be used for system functional testing and browser compatibility testing.


Leverage Selenium’s proprietary test accelerator for automation in order to reduce the time required for each automation cycle to complete. Selenium also comes with more than 90 functional libraries for clients to use for the purpose of initiating the automation process.


3. Choose the Selector Order

Selectors like XPath and CSS are based on element locations. They operate slower when compared to locators like ID, Name, and Link Text. Name and ID are especially effective because they operate in a straightforward and direct way. CSS acts mostly as a combination of ID and Name. XPath, because of its complexity, should be used only as a last resort.


In degrees of their difficulty of use, Selenium locators stand in the following order: XPath < CSS < Links Text < Name < ID. Start with ID in the test code and make XPath the last selector.


4. Use Page Objects

Page Objects enhance test maintenance and reduce code duplication. Additionally, it is an object-oriented class (OOC) that serves as an interface to the page of the application under test. In other words, PageObject acts as an object-oriented design pattern, and web pages are defined as classes. The different web elements on each page become the variables for this pattern. User interactions (which are automated during testing) are implemented as methods.


Page Objects help create robust frameworks by resisting minor UI tweaks. They also help to separate test code and page code.

It ensures that services are not spread throughout the test but rather that there is a repository for all services offered by the page.

They are reliable, and easier to maintain.

They keep the script readable and code reusable.

They eliminate code duplication.

Try Running Tests on Cloud Selenium Grid for Free


5. Use Selenium Waits. Avoid Thread.Sleep

Instead of sleep, use Selenium Wait commands. When Thread.sleep() is used, the code will pause for the specified period of time, no matter what. However, with the Implicit Wait command, Selenium polls the DOM until an element is found or a condition is fulfilled. Its time is, by default, set to zero.


Obviously, using Implicit Wait is more effective than using Thread.sleep(). Why force the code to wait any amount of time it doesn’t have to? Every second counts within tight deadlines and waits are an excellent tool to establish better time management for Selenium tests.


6. Use Java Runtime Environment JRE 1.6

If, at the beginning of an integration test, the following error shows up


=java.lang.NoSuchFieldError:

java/util/concurrent/TimeUnit.HOURS.


It means that the latest version of Java is required to proceed with testing. Since the Selenium server is programmed with Java, a runtime error indicates that an upgrade is necessary. Download the latest version from the official Selenium website.


With the Java command present in the PATH, use command java -jar selenium-server-standalone-2.x.x.jar to start the Selenium server and replace 2.x.x with the actual Java version.


Automation testing with Selenium is a good way to create a stable, true, and reliable UI automation process. However, always pair Selenium tests with a multitude of real browsers and devices. Without being run in real user conditions, the result of any tests run will be inconclusive at best.


Simply use BrowserStack’s real device cloud to access thousands of real browsers and devices for testing purposes. Sign up, choose the required device-browser-OS combination, and start testing websites for free.

https://www.browserstack.com/guide/selenium-web-browser-automation

Read More

Modern Web Automation With Python and Selenium

 

TOPICS:

Motivation: Tracking Listening Habits

Setup

Test Driving a Headless Browser

Groovin’ on Tunes

Exploring the Catalogue

Building a Class

Collecting Structured Data

What’s Next and What Have You Learned?


Motivation: Tracking Listening Habits#

Suppose that you have been listening to music on bandcamp for a while now, and you find yourself wishing you could remember a song you heard a few months back.


Sure, you could dig through your browser history and check each song, but that might be a pain… All you remember is that you heard the song a few months ago and that it was in the electronic genre.


“Wouldn’t it be great,” you think to yourself, “if I had a record of my listening history? I could just look up the electronic songs from two months ago, and I’d surely find it.”


Today, you will build a basic Python class, called BandLeader that connects to bandcamp.com, streams music from the “discovery” section of the front page, and keeps track of your listening history.


The listening history will be saved to disk in a CSV file. You can then explore that CSV file in your favorite spreadsheet application or even with Python.


If you have had some experience with web scraping in Python, you are familiar with making HTTP requests and using Pythonic APIs to navigate the DOM. You will do more of the same today, except with one difference.


Today you will use a full-fledged browser running in headless mode to do the HTTP requests for you.


A headless browser is just a regular web browser, except that it contains no visible UI element. Just like you’d expect, it can do more than make requests: it can also render HTML (though you cannot see it), keep session information, and even perform asynchronous network communications by running JavaScript code.


If you want to automate the modern web, headless browsers are essential.


https://realpython.com/modern-web-automation-with-python-and-selenium/

Read More