This is a quick cheatsheet for various Selenium (IDE-based) commands. There's nothing too jaw-dropping here; just a collection of tips that I've found useful.
General Usage
Here is a suggested development technique with caveats. There are lots of ways to accomplish the same thing; this is just my own approach:
To confirm there's a DIV that contains an onClick handler with a given name and some template text of a given value:
...use this:
The @onclick can be replaced with any attribute of the given DIV tag. The bracketed "contains" conditions can be chained, and are AND'd together.
To check/uncheck a checkbox in a table with a given ID that contains a cell with the given template text:
...use this:
Avoid the annoying 30-second delay after clicking a link that use a JavaScript command as the HREF:
Set the timeout as needed:
This will still get logged in the Selenium IDE as an ERROR, which unfortunately will result in the test being marked as failed. This of course makes it difficult to determine that your tests are in fact all green, since these failures show up as red. But I've just gotten into the habit of clearing the log before I start a test suite, and just ignoring the "[error] Timed out after 1000ms" log messages. If any other log messages are at ERROR level, these tell me there is something to look into; else, I consider my test suite to have passed.
To select an item from a menu:
...use this:
To enter a carriage-return character in an input field with a given ID:
So the number 13 is the HTML code for the carriage return character. Here's a reference of more character codes.
To click on a link with the given template text:
...use this:
To consume an alert:
General Usage
Here is a suggested development technique with caveats. There are lots of ways to accomplish the same thing; this is just my own approach:
- Start by creating a directory that is named by the use case you're testing.
- Create a test suite in that directory, naming it TestSuite.html. Using this convention will help distinguish the test suite from its test cases, all of which should use an HTML suffix.
- Create test cases one by one, immediately give the test case a title via the Properties menu item (right click over the test case object), copy that name onto your clipboard before hitting OK, and then immediately save it to the use case directory by pasting that name into the file-save dialog and appending .html.
- Do not expect to be able to re-use general purpose test cases from other use-case directories (e.g. a login sequence) - I've had to duplicate these in each use case that I use it in. This might be just me doing something wrong; I might need to revisit it to confirm. Obviously it sure would be nice if we could reuse things this way.
- Do not expect to "Add Test Case" using an existing test case which you then alter, without altering the other uses of it within your test suite. Using my techniques here, one test case == one HTML file; modifying it will modify all instances of it within that use case.
- Save often - ideally save test cases as soon you alter them, and save the test suite as soon as you add/delete test cases to/from it. If you change test suites, the IDE will not warn you that you have unsaved changes.
To confirm there's a DIV that contains an onClick handler with a given name and some template text of a given value:
<div onclick="doSomething()">foo</div>
...use this:
assertElementPresent //div[@onclick[contains(., 'doSomething') ]][contains(., 'foo')]
The @onclick can be replaced with any attribute of the given DIV tag. The bracketed "contains" conditions can be chained, and are AND'd together.
To check/uncheck a checkbox in a table with a given ID that contains a cell with the given template text:
<table id="myID"> <tr> <td nowrap="nowrap"> <input type="checkbox">check me</input> </td> </tr> </table>
...use this:
click //table[@id='myID']//td[contains(.,'check me')]//input
Avoid the annoying 30-second delay after clicking a link that use a JavaScript command as the HREF:
<a href="javascript:doSomething()">Do Something</a>
Set the timeout as needed:
setTimeout 1000
This will still get logged in the Selenium IDE as an ERROR, which unfortunately will result in the test being marked as failed. This of course makes it difficult to determine that your tests are in fact all green, since these failures show up as red. But I've just gotten into the habit of clearing the log before I start a test suite, and just ignoring the "[error] Timed out after 1000ms" log messages. If any other log messages are at ERROR level, these tell me there is something to look into; else, I consider my test suite to have passed.
To select an item from a menu:
select menu label=three
...use this:
select menu label=three
To enter a carriage-return character in an input field with a given ID:
keyPress inputFieldID 13
So the number 13 is the HTML code for the carriage return character. Here's a reference of more character codes.
To click on a link with the given template text:
<a href="foobar">Click Me</a>
...use this:
click //a[contains(text(),'Click Me')]
To consume an alert:
storeAlert
No comments:
Post a Comment