Did you know you can tell Selenium IDE to execute certain commands only if a specific element exists on the page? The if element exists trick makes your web app testing scripts far more interesting. Here is a quick example using Selenium IDE 3 — the “new” Selenium IDE.

Log out a standard user

In this use case a WordPress multisite standard user is logged in. We can only execute our commands if we are a super admin. When trying to open a URL that is only available to the super admin an error page shows up. Thankfully this page also comes with an easily identifiable body ID set to error-page.

A WordPress error page when attempting to access a restricted area.

We are going to use this identifier to create some web app testing logic to switch back to a multisite super admin.

This example is based on some links provided by the User Switching WordPress plugin and assumes our Super Admin switched to a standard user and can easily switch back. Those details are not important our Selenium IDE if element exists logic; they are provide to give some context of the commands you’ll see inside our if construct.

If element exists example

The first couple of lines are not important to this example.

Line 3: We start by trying to open the restricted access page. We can tell that we have an error page by looking for a <body> tag with an id set to error-page.

Line 4: Create a variable we can test. The simplest option is to store the number of items on the page that match a specific xpath. In our case the xpath //body[@id=’error-page’] is very specific and should return a value of 1 if we are showing an error page or a 0 if not. We store the value in a Selenium IDE variable named error.

Line 5: Echo the error count for our example only. Not important, but you can see at the bottom of the window in the log it is indeed 1 when on an error page.

Line 6: The start of the if element exists magic. If our error variable is greater than 0 that means we are on an error page. All the commands up until the end command (line 9) will be run only if we are on an error page.

NOTE: In Selenium IDE 3 variables are referenced via ${…}

Line 7 and 8: In our case go to the user profile and click a switch user link. These lines are not important to this example. You add whatever commands you want run here — as many as you need and end that block of commands with the END statement as you see on line 9 in our example.

Selenium IDE commands in this example

open

  • open url
  • Opens a URL and waits for the page to load before proceeding. This accepts both relative and absolute URLs.
  • arguments:
    • url – The URL to open (may be relative or absolute).

echo

  • echo message
  • Prints the specified message into the third table cell in your Selenese tables. Useful for debugging.
  • arguments:
  • message –

store xpath count

  • store xpath count xpath, variable name
  • Gets the number of nodes that match the specified xpath, eg. “//table” would give the number of tables.
  • arguments:
  • xpath – The xpath expression to evaluate.
  • variable name – The name of a variable (without brackets). Used to either store an expression’s result in or reference for a check (e.g., with ‘assert’ or ‘verify’).

if

  • if conditional expression
  • Create a conditional branch in your test. Terminate the branch with the end command.
  • arguments:
  • conditional expression – JavaScript expression that returns a boolean result for use in control flow commands.

end

  • end
  • Terminates a control flow block for if, while, and times.

Add some if element exists to your web scripts

That’s it. Pretty darn easy, right? No extra extensions or plugins needed.

Go out there and make some great web app testing or automation scripts.

Share Your Insight

%d bloggers like this: