6 Ways to Make The Most of Selenium IDE

Selenium IDE is an indispensable tool for automated testing. If you’re a web developer or QA, you need to have Selenium tests to cover the basic functionality of your web site, and anything that you have to retest repeatedly.

One of the nice things about Selenium is that it allows you to record your actions in a browser, so you don’t have to type in the test script by hand. Still, though, there is almost always tweaking you need to do to get reliable tests. Here are my top tips:

  1. deleteAllVisibleCookies. Your tests will fail if they expect the user to be logged out, but they’re really logged in. To make sure they’re logged out, use the deleteAllVisibleCookies command to do exactly that.
  2. Watch out for click and clickAndWait. The clickAndWait command will click a link or button, then wait for the page to finish loading before executing the next step. The click command will click and then immediately execute the next step. So you always want to use clickAndWait when clicking a link that goes to a new page, or submitting a form. Usually Selenium IDE records these clicks as clickAndWait – but sometimes, for no particular reason I can discern, it records them as a click. You will probably need to change those to clickAndWait.
  3. Generate random user names. You don’t want your test scripts to rely on data in the database if possible, so that means registering new users instead of logging in as existing ones. But you can’t use the same username each time you register. The way I get around this is using the store command to get the current time from JavaScript, and use that as part of the username. Use command “store,” target “javascript{“test-” + new Date().getTime + “@test.com”}”, value “testEmail”. When I ran that just now, it generated “test-1270654433712@test.com”. That number will always be unique, so you’ll never get a “user already exists” error. It may seem tedious to register a new user for each of your test cases, but it doesn’t have to be – just copy and paste the registration code.
  4. waitForText for Ajax. If you’re using Ajax, you can’t clickAndWait, because the page doesn’t load. And you can’t just keep executing immediately, because the next step in the test case will usually run before your Ajax request completes. Running your tests at less than maximum speed can help, but that’s not very efficient. It’s better to add a waitForText command (or another waitFor… command) after everything you do that generates an Ajax request. Test for something that will show you the Ajax request finishes, before continuing. In my application, for example, sometimes I test that values are populated in a select dropdown, and sometimes I test that an “OK” icon has appeared on the screen.
  5. Test all the cases. Sometimes, Selenium is most useful when you have a lot of different combinations of behaviors. For example, my application interfaces via SSO (single sign-on) to another application. There are multiple places in the application that it interfaces, and there are three kinds of user that have different SSO abilities, leading to 22 different cases. As you can imagine, it’s easy for a fix for one feature to break another feature. So I wrote up the test cases in an Excel spreadsheet, then created a Selenium test for each of the 22 cases. Now I can rerun the test suite at any time to make sure all of the cases still work.
  6. Test what you can automate, not what you can’t. Not everything is easy to automate in Selenium. Don’t expect it to take the place of all your QA testing. And don’t give up on Selenium just because it can’t take the place of all your QA testing. Use it to cover as many cases as you reasonably can, and run it regularly to certify code updates, merges, and environments. Then continue to do manual QA as well.
About these ads

13 Responses to 6 Ways to Make The Most of Selenium IDE

  1. And most importantly:

    7) Don’t use the Selenium IDE. Use Selenium RC and write your tests in a proper programming language, not HTML. Here’s an excellent set of reasons why:

    http://adhockery.blogspot.com/2009/09/testing-grails-apps-with-selenium-rc.html

    • Josh Justice says:

      Robert, it sounds like your absolute statement may be a bit unfounded. Selenium RC sounds like it has an entirely different goal than Selenium IDE: unit testing, not integration testing. I use JUnit for unit testing, and it works just fine. But when I want to walk through every screen in my app and make sure they’re functioning properly, it’s much more efficient to write a Selenium IDE case. Anyone who thinks of that as simply “data scraping” is forgetting that, ultimately, to the user the screen is the application. It doesn’t matter if the data makes it into the database: if it doesn’t make it back to the screen when it’s supposed to, that’s a bug. So I would argue that there is a need for both unit backend testing and integration front end testing. I’m by no means a testing expert, and I’m sure there’s more to it than that. I’m just saying I don’t think you can dismiss Selenium IDE so easily.

  2. No, Selenium RC is definitely not for unit testing. No test that goes across so many levels can possibly be called a unit test.

    I have some sympathy for the view that you should test solely fromt he user’s perspective in a functional test, but even so you absolutely should not be using Selenium IDE once you’ve grasped the basics of Selenium. I’ve done so on two projects now, and after a year and when you’ve got 300 long HTML tests produced in the IDE you’ll be crying at the pain in maintaining and refactoring them.

    But don’t just take it from me – here’s another good explanation:

    http://adam.goucher.ca/?p=1198

    The Selenium developers agree, too.

  3. Shiblee Mehdi says:

    Thanks for sharing your practical experience.

    You are correct on ‘2. Watch out for click and clickAndWait.’. Is is disturbing to me that sometimes selenium does this and I need to change that.

  4. Saimadhu says:

    Josh,
    I need to automated my SSO test cases.
    Case:
    1. User logged in to the application.
    2. Now for SSO, User has to open a new browser instance and enter linked application url.

    When i try opening a browser session the ‘session id is different’ than the one i got in step 1. So user not logged in automatically to linked application.

    How to solve this issue? Is there any way to open a tab using selenium?

  5. simone says:

    I would be very interested to know how you read the excel sheet with selenium ide. i use xml but it is a pain as I have many variables to enter, so I loose the ocerview I would hace in excel.

  6. John Sherwood says:

    Josh, your suggestion #3 was exactly what I was looking for. I did have to modify it with single quotes instead of the double quotes you used:

    javascript{‘test-‘ + new Date().getTime + ‘@test.com’}

    The double quotes gave me a nasty error in version 1.8.0.

  7. Himanshu says:

    Josh, John:

    I stored javascript{‘test-’ + new Date().getTime + ‘@test.com’} in a variable (say testEmail) but while using this variable in a textfield used for storing email only “testEmail” is appearing. Pls guide what exact command should be used ?

    See the content from log:

    [info] Executing: |store | “javascript{‘test-’ + new Date().getTime + ‘@test.com’}” | testEmail |
    [info] Executing: |type | id=user_name | test1908u |
    [info] Executing: |getEval | id=user_email | testEmail |
    [info] script is: id=user_email
    [error] Threw an exception: user_email is not defined

  8. Corey says:

    Thanks for these Josh! I’m just gettting started with Selenium & testing sessions in an app, so #1 was especially helpful. Thanks!

  9. Johanna says:

    Thanks for this breakdown, Josh, really great. Point 1 doesn’t work when the cookie is HttpOnly, though, as I discovered. This is often the case with Drupal sites. Instead I’m using the Selenium IDE extension Sideflow to add an if statement checking if the user is logged in or not. I wrote about it here: http://wunderkraut.com/blog/using-selenium-ide-and-sideflow-to-log-in-to-a-drupal-7-site/2013-05-16

  10. Peter Kehl says:

    Hi Josh,

    There is more you can do in Selenium IDE now. You may be interested in SeLite. It’s a framework for Selenium IDE that allows your test to access (read and write to) a test database (isolated from the database of the tested application).

    See https://code.google.com/p/selite/wiki/ProjectHome and https://code.google.com/p/selite/wiki/DrupalTutorial.

  11. Saman says:

    Use this in Selenium IDE
    javascript{“test-’ + new Date().getTime() + ‘@test.com”}

  12. Saman says:

    Use this in Selenium IDE
    javascript{“test-” + new Date().getTime() + “@test.com”}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 525 other followers

%d bloggers like this: