Stabilize Automation with Selenium Webdriver Waits in Python

This tutorial is driving you to the concept of Selenium Webdriver waits (Implicit and Explicit Waits) and how to apply them in Python.

If you’re familiar with automation, placing waits in efficient places is very important to stabilize automation. It can deal with issues of unstable network, data slow loading, and generally situations we can’t predict how much time we need. It’s very important, so we have to learn it.

In general, we need to wait in a specific timeout to ensure web elements loaded completely or ready, before interacting with them. Webdriver API provides two types of wait mechanisms to handle such conditions: implicit wait and explicit wait.

Implicit Wait

An implicit wait instructs the WebDriver to re-load the DOM in a defined amount of time to try to locate an element if it does not exist. The implicit wait is set by the function implicitly_wait with an amount of seconds. Executing the below code, you could see an exception raises since it can’t find an element with id ‘id-not-on-site’ in 5 seconds.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome(executable_path="C:/files/chromedriver.exe")
driver.implicitly_wait(5)
driver.get("http://google.com")
inputElement = driver.find_element_by_id("id-not-on-site")
inputElement.send_keys("goodevops.com")
inputElement.submit()

driver.close()

Explicit Wait

Almost scenarios of waits are uncertain. Explicit wait works more efficient than implicit wait, but it definitely costs you more efforts. You have to figure out signals to wait in a specific amount of time. Don’t worry! Your efforts are rewarded with more quality or stable scripts.

The following are the two Selenium Python classes for explicit waits.
• WebDriverWait
• Expected Conditions

Example 1: the below code snippet is to wait for an alert dialog appears in 10 seconds

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoAlertPresentException
from selenium.common.exceptions import TimeoutException

try:
    WebDriverWait(driver,10).until(EC.alert_is_present())
    alert_dialog = driver.switch_to.alert
    print ("Alert message: ", alert_dialog.text)
    alert_dialog.accept()
except (NoAlertPresentException, TimeoutException) as ex:
    print("Alert not present")
finally:
    driver.quit()

Example 2: this code snippet is to wait for an input with innertext ‘close’ appears in 20. If it appears, click on it will be performed.

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[text()='close']"))).click()

Again, applying waits is very important to stabilize automation scripts. Hope these practices can help you in some ways. Success.

How Can Artificial Intelligence be Applied in Automation Testing of Web Apps?

There are several interesting web app automation scenarios that we can improve using Artificial Intelligence (AI):

  • Increase test execution stability (self-healing automation) by letting AI to automatically locate web elements when the primary locators fail. This feature already appears in some cutting-edge automation tools like Mabl.
  • Increase automation productivity by using Natural Language Processing (NLP) to automatically translate manual test cases into automated test cases. In theory, this capability should be feasible but I haven’t seen any open-source or commercial solutions available on the market yet.
  • Increase test coverage by auto-generate input parameters that could exhaustively test the API under test using advanced algorithms such as Pairwise.
  • Reduce test execution time by only running the tests that are impacted by a certain code commit instead of exhaustively running all of them after every code commit. For instance, check out the Test Impact Analysis feature of Microsoft Azure DevOps.
  • Reduce failure debugging time by auto analyzing test results and assigning them to categories like Environment Issues, Automation Problems, App Defects, etc. An outstanding example of this category is Report Portal.
  • Perform visual validations on web apps using OCR or image-recognition techniques. There are plenty of tools out there such as TestArchitectAppliTools and SikuliX.

AI is just one trend in the industry. Read more about other trends and predictions here: 2019 Test Automation State-of-the-Practice and Trends | LogiGear Magazine

Leave a comment or contact me if you have anything to add on regarding to applying Artificial Intelligence in Automation Testing of Web Apps.

How to Choose the Best Testing Tool for Automated Testing

I’ve noticed that the process of choosing the best automation testing tool for a project is not always clear. So I’d like to shed some more light on it. The process (Choose the Best Testing Tool) should look like this.

1. Define a list of criteria

Define a list of criteria that your ideal Test Automation framework should meet such as supported automation platforms (desktop, web or mobile?), usability, maintainability, stability, ease of debugging test failures, ease of integrating test automation into a CI/CD pipeline, first-class Docker support, budget/pricing, etc.

2. Weigh the criteria you selected

Which criteria are more desirable and which annoying problems you can live with in the long run? Look around (especially for tool listing articles), and pick at least 3 good Test Automation frameworks that closely match your criteria considering your current situation.

3. Try

Try building a workable framework if you picked open-source solutions like Selenium, Protractor, Cypress, etc. or ask for a demo if you‘re evaluating commercial solutions like UFT, TestComplete, TestArchitect, Ranorex, etc. When you’re at it, pay special attention to the criteria you already listed out. Some call this stage a POC (Proof of Concept). Rate each tool on the scale from 1 to 10 for every criterion you selected. Then compute the score of each tool considering the weight of each criterion. Make sure you’re being fair and your rankings are transitive (if toolX > toolY and toolY > toolZ, toolX must be superior to toolZ).

4. Pick the best tool

Voila! Pick the best tool that got the highest score. No brainer!

Hopefully, this weighted-decision method can help you pick the best software testing tool for automated testing. Leave a comment or contact me if you have anything to add on on how to Choose the Best Testing Tool.

Keeping an active desktop session

Credit: this post heavily uses/reuses the content from Arnon Axelrod’s post. For some reasons, I couldn’t leave him a comment because of reCAPTCHA issue. So I decided to create this post and cover part he didn’t. I strongly recommend you to visit his post for full story.

Some test automation tools require an active desktop session to operate normally. I have been struggling with other approaches: autologin, relogin, unlock locked screen, etc. All of them led to same dead end. I already have a post for “How To Unlock Desktop Screen After Remote Access Disconnected” however it requires running cmd command as administrator which is not so convenience.

What locks the desktop?
1. System locked (Windows Key + L) – So far, I don’t see any better solution than using VNC.
2. Remote Desktop session disconnected – See solution below

In my opinion, Arnon Axelrod’s approach or solution is so elegent for this very common test automation problem. What we need to do is keeping an active desktop session by getting cmd command triggered automatically upon RDC disconnected.

In brief, the solution is composed of few pieces
1. A Task Scheduler’s task that is triggered upon disconnecting form Remote Desktop
2. The query session command
3. The TSCon command
4. A windows script that combines the 2 commands

for /f “skip=1 tokens=2” %s in (‘query user %USERNAME%’) do (tscon.exe %s /dest:console)

Where is Software Testing Heading?

Just like other vibrant industries, software testing is changing every day. As a tester, what should you learn to stay on top of your game? Below are some trends you might want to take a look at in 2018. Sharpen the saw!

  • Blockchain app testing: Unless you’ve been living under a rock for the past few years, you’d probably have heard of buzzwords like Bitcoin, Ethereum and Blockchain. Blockchain is taking the world by storm. More and more investments are made on developing Blockchain-based applications, translated: more software testing needed.
    Tip: “Mastering Bitcoin: Unlocking Digital Cryptocurrencies” of Andreas Antonopoulos is a very good start. This book provides basic understanding about Bitcoin and Blockchain through very good examples.
  • Smart product testing: Devices equipped with sensors (think smart toys like Anki Overdrive), voice-based & AI-powered devices (think Amazon Alexa) are taking the center stage. Millions of Amazon Echo and Google Home devices have been sold. Gartner predicts that the market for voice-based wireless speakers will reach $2 billion by 2020. Testers nowadays face a wildly different problem that requires a wildly different skill set.
    Tip: Learn how to program an Alexa “skill” (another name for “apps” on Alexa Marketplace). You just need to sign up for an AWS account (free) and start writing a Lambda function (guide).
  • More test automation: Manual testers have found themselves in a shrinking job market. The industry demands more technical skills like the ability to churn out automated tests, preferably platform-agnostic.
    Tip: If you’re a manual tester, it’s still not too late to learn test automation. Here’s a general guideline: http://qr.ae/TbSswT
  • Wiring automated tests into the pipelines: In today’s DevOps world, bug fixes & new product increments are directly pushed out to end-users, in the continuous fashion. Shit hits the fan if those pieces of code are continuously untested. But the path to Continuous Testing is not always straightforward. Unexpected plumbing landmines are always lurking around the corner.
    Tip: You or your colleagues won’t need to be coding experts to wire automated tests to your pipelines. Almost all ALM tools provide a command-line interface.
  • Service-oriented testing: This is absolutely not a brand new trend in 2018. The trend started long ago: the number of API-level tests keeps growing while UI-level tests keep shrinking. API tests obviously dig deeper, run faster & more reliably (think Fowler pyramid).
    Tip: Learn how to use REST endpoints via tools like POSTMAN and curl first. Then you can write API tests using RestAssured.
  • Involvement of non-engineering testers: More and more non-engineering testers (or some might call “test analysts” or “domain experts”) are taking part in software testing although they don’t possess a strong technical background. This trend calls for an effective scripting language that is readable & writable for non-technical testers, yet executable for the test runners.
    Tip: Focus on the business flows and business logics of your app instead of code. Leave the coding / test implementation to automation engineers. Besides, it’s really useful to get acquainted to the Keyword-Driven method, a solution for non-technical testers to collaborate effectively with technical ones.

Obviously these trends will pivot as the industry progresses. To stay up to date, I’d recommend keeping up-to-date via several information channels below. It’s only my personal ranking / preferences so don’t shoot the messenger.

  1. Automation Awesomeness
  2. Software Development & Testing Insights | TechWell
  3. LogiGear Magazine
  4. StickyMinds
  5. Community Articles | SoftwareTestPro
  6. SD Times – Software Development News
  7. DevOps.com
  8. James Bach – Satisfice, Inc.
  9. Asktester Blog
  10. Testing Excellence

Some testable smart products:
testable smart products

Install MariaDB / MySQL on Raspbian / Debian

Install MariaDB (or MySQL) on Raspbian / Debian is pretty easy with the supported package:
sudo apt-get install mariadb-server (or mysql-server)

And you can set password for the root account as well as configure others by one simple command:
sudo mysql_secure_installation (for both MariaDB and MySQL)

But the problem is that you can’t connect to the database from any system. You’ll get the “Access denied” error all the time. The only way to access is from the terminal of that server:
sudo mysql -u root -p

I don’t know why the authors make them to be difficult to use at the first time. To remove the restrictions, it requires many steps:

1. Login from terminal:
sudo mysql -u root -p

2. The plugin of the root account in the mysql.user table is set to ‘unix_socket’, which means you can only access using the terminal. Use the following commands to remove it:
USE mysql;
UPDATE user SET unix_socket = '' WHERE User='root';
FLUSH PRIVILEGES;

3. Now you can use any SQL tool (phpMyAdmin, SQL Workbench…) to access the database. But that tool must be used on the same server because the Host field of root account in mysql.user is ‘localhost’. So you may need to remove it.
UPDATE user SET Host = '' WHERE User='root';
FLUSH PRIVILEGES;

4. Now, try to access again. You still can’t access the database from another machine. Why? Because the database socket doesn’t bind to an IP address. This setting is missing from the config file. Let’s find the config file first:
sudo find / -name my.cnf

The file is usually located at: /etc/mysql/my.cnf. Open it to modify using nano:
sudo nano /etc/mysql/my.cnf

Add to that file a new section:
[mysqld]
bind-address=::

5. That’s it. Restart the services and enjoy it.
sudo service mysqld restart
sudo service mysql restart

Leave a comment or contact me if you have any questions or suggestions