Automation Testing

Selenium Automation 101 How to use waitFor Commands

By Test Guild
  • Share:
Join the Guild for FREE

“Don't wait. The time will never be just right.” Napoleon Hill

Selenium Element not found

Are you testing a web application using Selenium and keep getting an [error] Element .. not found error message? One way to work around this and similar issues is to use a waitFor command.

Why the Pause command stinks

You may think, “Wouldn't it just be easier to use pause commands throughout my script?” In rare instances, a pause command may be the way to go, but in most instances, it's a very poor choice.

When using a pause command in your test, you have to try and make the test wait just a little bit longer than what you believe the longest anticipated response time will be.

The problem with this, of course, is that the response time could be considerably longer than anticipated – which means you end up bumping up your wait to be even longer than the longest expected delay.

Those long pause times will considerably slow your test run time, which is not cool. The solution is to use a flexible method to synchronize with longer and shorter delays.

WaitFor

WaitFor commands are not hard waits; meaning that they will, by default, wait up to 30 seconds to find an element, but if they find an element before 30 seconds, they continue without waiting the remainder of the time. (If you're familiar with HP's Quick Test Professional, it's similar to an objects exist method or WinRunner's generic obj_wait_info statements.)

Why use Synchronization Points?

In test automation, this concept is often referred to as synchronization. Adding synchronization points in your script facilitates your test when you need to wait for a response from an application. This is important — because when testing a web application, server response times can vary for any number of reasons.

To help make your scripts more reliable, you should use synchronization points like one of the many waitFor commands available in Selenium.

For this sample test application, I use AJAX to populate it. More often than not, an Element not found error occurs when running a test against this application. This is due to issues with the element not being loaded into the DOM because it's in the process of being rendered in your browser. One way to work around this issue is to use a waitFor command.

Let's check out a WaitFor quick example:

Selenium has a bunch of waitFor commands. In this example, we'll use the waitForElement, but you can use whichever command works best for your situation. To make this quick I'll use the Firefox Selenium IDE, but you'd use the same waitFor methods if you were coding using the Selenium APIs.

Selenium IDE

  • In Firefox, start the Selenium IDE.
  • For the Base URL, navigate to:
    • https://testguild.com/HpSupport.html
  • From the HPSupport.html page, enter the following values:
    • Select your tool & version = QTP11
    • Is this supported = DELPHI_ADDIN
  • Right click on the first row, first cell, Delphi Controls CLX and select Show All Available Commands>asserText css=td Delphi Controls CLX.


  • Stop recording. Your IDE Table results should look like this:


  • Click on “Play current test case”.
  • You should see the error [error] Element css=td not found. (If you don't see this error, it's ok-you'll still want to add a waitFor, however. It's a good practice since response time will vary for each test run.)


Add a waitForElementPresent command

Now, let's add a waitForElementPresent command and rerun our test.

  • Right click on the assertText command and select “Insert New Command” from the menu.


  • Next, enter to following:
    • Command = waitForElementPresent
    • Target = css=td
  • Click on the Find button, this should highlight the first cell in the first row.


  • Rerun the test by clicking on”Play current test case” –the run should pass and your log should be error free!


It's Elementary

That's the basics on how to use the waitFor commands in Selenium. Stay tuned for more Selenium 101 posts in the coming year.
To get started using Selenium WebDriver in Java or .Net check out my other post:

 

Optimized Bio with Podcast Longevityfor

Maximum Credibility & SEO

A bearded man with blue glasses and a black-and-white jacket smiles at a microphone in a studio setting.

About Joe Colantonio

Joe Colantonio is the founder of TestGuild, an industry-leading platform for automation testing and software testing tools. With over 25 years of hands-on experience, he has worked with top enterprise companies, helped develop early test automation tools and frameworks, and runs the largest online automation testing conference, Automation Guild.

Joe is also the author of Automation Awesomeness: 260 Actionable Affirmations To Improve Your QA & Automation Testing Skills and the host of the TestGuild podcast, which he has released weekly since 2014, making it the longest-running podcast dedicated to automation testing. Over the years, he has interviewed top thought leaders in DevOps, AI-driven test automation, and software quality, shaping the conversation in the industry.

With a reach of over 400,000 across his YouTube channel, LinkedIn, email list, and other social channels, Joe’s insights impact thousands of testers and engineers worldwide.

He has worked with some of the top companies in software testing and automation, including Tricentis, Keysight, Applitools, and BrowserStack, as sponsors and partners, helping them connect with the right audience in the automation testing space.

Follow him on LinkedIn or check out more at TestGuild.com.

  1. Hi, im using Selenium IDE (2.4.0) and i have one issue with this method, when the element that im searching is not present when the waitFor command start it works fine, but if the element its already present in the browser render, the command waitFor stay waiting for it, and it ends with a timeout, and returns the timeout error, the script continue but its already a fail.

    Do you know if there are any solution for this?, thank you.

Comments are closed.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Solving the Top 6 Mobile Automation Challenges Fast (DevAssure Guide)

Posted on 02/27/2025

Are you tired of struggling with the mobile test automation setups? Does managing ...

Top Software Testing Conferences (2025 Guide)

Posted on 01/19/2025

Why a list of top software testing conferences? Many testers ask me what ...

Top 8 Automation Testing Trends Shaping 2025

Posted on 01/11/2025

As we enter 2025, here are some automation testing trends I think will ...