Monday, August 24, 2009

Selenium troubleshooting

Problem 1: Something on the page isn't found because it isn't ready yet
Solution: Use clickAndWait

Problem 2: It won't record my password or another text field
Solution: Click off the text field (in other words, click elsewhere) after you type it. Usually Selenium will successfully record at that point. If you are still having trouble, try entering the command yourself directly into the Selenium script.

Problem 3: I can't get a dropdown menu to appear for a type-ahead field, perhaps because it's JavaScript
Solution: Use the "fireEvent" command on the HTML field. The value "focus" will cause, for example, the JavaScript focus event to be triggered. If the dropdown menu looks for that event, then firing it will cause the dropdown to appear. Also, use the "typeKeys" command to enter the text in the type-ahead field that informs the dropdown.

Problem 4: I can't figure out how to identify an element (link, button, table cell...) reliably
Solution: Ask a developer if there is a way to include an id or name for the element that can be uniquely identified (i.e. this is a "testability" issue). Failing that, use XPath targets. For example, use clickAndWait with a target that looks like //a[@href='myurl'] (that is an XPath query). You can even use Firebug to locate XPath for a given element. With Firebug installed and enabled (on Windows press F12 to enable), click Inspect, highlight the HTML element on the page, click it to select it, right-click its representation in the script windows in the debugger, and click Copy XPath. You may need to edit the XPath before entering it into the Selenium target. Selenium always requires an extra / (frontslash) at the beginning of XPath targets.

Problem 5: Running Java Selenium tests via Selenium RC, clicking my link doesn't work in IE (or vice versa, in Firefox)
Solution: Firefox and IE handle ampersands (&) differently, among other things. Don't use the "click" command on links that contain ampersands.

Selenium scripting conventions

Conventions
These are suggested guidelines, or rules of thumb, for writing or editing Selenium scripts, based on real-world experience.

Base URL
Set a standard Base URL for your scripts. Assuming you have multiple environments, such as development, test, staging, and production, you may wish to configure your Base URL with an environment-specific variable.

Record but Edit
Selenium recording is great but does not do everything we need to do. After you record your script, be prepared to edit it some by hand.

HTML Format
Maintain each test in HTML format. This should be the "gold" copy of the test. The idea here is that you can edit HTML tests in Selenium IDE. The other formats, such as Java, can be generated automatically from HTML, but there is no robust automated way to go the other direction, for example from Java to HTML. Therefore once you leave HTML, there is no easy way back. If you modify your tests after translating them to Java or another language, you may be able to automate that process in order to make the entire system work easier for you.

Create Examples or Templates
Create some initial example or template tests and have the entire automation team vet them. Once best practices are in place, it will be easier for all to maintain tests, regardless of the original author. Just as with general programming, communication is key. Even with templates, hold regular code reviews, especially at the beginning of your automation effort.

Feature IDs
At the top of each script, where applicable, enter a comment "FeatureID=Xxxxx" where Xxxxx is a unique identifier of the main feature covered. This sort of convention will make it easier for you to integrate with test coverage reporting tools in the future.

Author's Name
Also at the top of each script, enter a comment "Author=Name" where Name is your name or handle. This makes it easier to resolve questions about how the script works or what its purpose is.

Setup and Cleanup
Test independence helps ensure the robustness of the execution environment. Therefore it is a good idea to have each script attempt to setup any environment it needs and likewise clean up after itself when finished. One example of such behavior is to start with a logout and end with a logout. Likewise, the script should also undo any data changes it makes. However, this approach is still no substitute for "harder" data cleanup and preparation. Before any test suite executes, the entire test environment should always begin with a known configuration and data set. That is generally something you will have to do outside Selenium.

Minimize Assumptions
Even though scripts should clean up after themselves, it is also wise not to assume the system is in the exact state that it was last time you recorded the script. Try to minimize the assumptions the script makes. For example, only sign in as a user you know will be available at all times.

Use clickAndWait
Instead of the "click" command, use the "clickAndWait" command. This waits for the new page to load fully before continuing. Unfortunately when recording scripts, Selenium usually places the "click" command everywhere, so you have to change them by hand.

Use check and uncheck
Selenium, by default, also uses the "click" command for interacting with checkboxes. Unless you specifically want a test that toggles a checkbox and doesn't care what specific value it ends up with, use the "check" and "uncheck" commands instead of "click".

Test Something
It's important to include "assertXxxxXxxx" statements in your script. These are the actual meat of the test. "assertTextPresent", "assertTextNotPresent", and "assertElementPresent", are your friends.

Test the Test
Run your script a couple times after you record (and edit) it to make sure it runs repeatedly. If you have difficulty making your script repeatable, you may wish to raise the issue with your team to see if anyone else has discovered a solution.

If and when you convert your test from HTML to Java, especially if you run it remotely via Selenium RC on a different browser, test again. Browser differences can easily break tests if you are not careful.

Basics of web test automation using Selenium

What is Selenium?
Selenium is a JavaScript-based framework for recording and running automated tests in a web browser.

Why Selenium?
Use Selenium to automate functional regression tests.

Functional means user-facing tests, not just back-end or code-level tests. Regression tests means tests that can be used to measure the progress of the product against past behavior, to make sure it does not regress (worsen in quality for core features). To put it in other terms, Selenium can help save time and/or improve quality (including predictability).

Two Approaches
There are two general approaches for using Selenium to improve testing:
  1. Manual testing: You can simply run Selenium scripts as part of a manual, or semi-automated, testing process, using just the Selenium IDE (available for Firefox only). If you don't have an automated build, this approach may be a good way to get started testing faster right away.
  2. Automated testing: Selenium test scripts can also be launched programmatically, from a first-class programming language such as Java. They can run in the context of a JUnit test suite, for example as part of a build process. This is the more automated approach. In the long run, it is a good idea to transition tests to become more automated. Another advantage of this approach is it enables using Selenium RC, a remote-control test execution environment that lets you run tests on remote machines in essentially any browser.
Automate Which Tests?
Which tests should you automate with Selenium? The answer is not always obvious. A high-level answer is that you should automate tests that give the biggest bang for the buck. Good reasons to automate a certain test include:
  • The test has identified bugs in the past.
  • The test should be run frequently.
  • The test is boring or difficult to run manually.
  • The test is for a high-priority part of the application under test (AUT).
  • Leaving a bug in this particular area of the application would be costly.
Especially at the beginning of product development or automated test development, automating core feature tests probably makes more sense than very new, unusual, or experimental features.

Download IDE
Selenium IDE is a plugin for Firefox that you use to create and run Selenium tests. It is one of several tools that can work together. It makes sense to start with Selenium IDE because you can record, edit, and run tests using it. Download here:
http://seleniumhq.org/download/

Generally the version at the top is best.
Copyright 2011 by William Cain