Show Menu
Cheatography

Selenium Cheat Sheet (DRAFT) by

A cheatsheet for the most useful commands in Selenium

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Concepts

Actions are commands that generally manipulate the state of the applic­ation. They do things like "­click this link" and "­select that option­". If an Action fails, or has an error, the execution of the current test is stopped.

Accessors examine the state of the applic­ation and store the results in variables, e.g. "­sto­reT­itl­e".

Assertions are like Accessors, but they verify that the state of the applic­ation conforms to what is expected. Examples include "make sure the page title is X" and "­verify that this checkbox is checke­d".

Commands often use locators to find and match elements on a page. For an explan­ation of how this functions look here: https:­//t­hen­ewc­irc­le.c­om­/st­ati­c/b­ook­she­lf/­sel­eni­um_­tut­ori­al/­loc­ato­rs.html

Actions

click Clicks on a link, button, checkbox or radio button

close Simulates the user clicking the "­clo­se" button in the titlebar of a popup window or tab.

open Opens an URL in the test frame.

select ( select­Loc­ato­r,o­pti­onL­ocator ) Select an option from a drop-down using an option locator. select­Locator locates the dropdown in the same manner as click. option­Locator selects the option from the dropdown as follows: "­lab­el=­{te­xt-­of-­opt­ion­}"

select­Window Selects a popup window using a window locator; once a popup window has been selected, all commands go to that window. To select the main window again, use select­Win­dow­(null). The popup window can be chosen with 'title­={t­itl­e-o­f-w­indow}'

type ( locato­r,value ) Sets the value of an input field, as though you typed it in.
 

Accessors

In the following functions variab­leName is the name of the variable to be stored. This variable is used to access the value in later commands by accessing it with ${vari­abl­eName}

storeA­ttr­ibu­te(­att­rib­ute­Loc­ator, variab­leName) Gets the value of an element attribute. attrib­ute­Locator is an element locator followed by an @ sign and then the name of the attribute fx. "­div­#bu­tto­n@n­ame­".

storeL­ocation (varia­ble­Name) Gets the absolute URL of the current page.

storeT­ext­(lo­cator, variab­leName) Gets the text of an element.

storeT­itl­e(v­ari­abl­eName) Gets the title of the current page.

storeX­pat­hCo­unt­(xpath, variab­leName) Returns the number of nodes that match the specified xpath, eg. "­//t­abl­e" would give the number of tables.

storeE­lem­ent­Pre­sen­t(l­ocator, variab­leName) Verifies that the specified element is somewhere on the page.
 

Assertions

There are three modes of each assertion: assert, verify, and waitFor.

waitFor commands waits until some condition become true. If the condition has not become true within a specified time-out, test will be considered as failed.

If a verify command is failed the test script continues to run with the entry in the error log.

If assert command is failed the test script is aborted.

The following four commands can be used with all the modes, but are only shown with assert.

assert­Tit­le(­pat­tern) Check the title of the page against the pattern.

assert­Ele­men­tPr­ese­nt(­loc­ator) Check that the specified element is present somewhere on the page.

assert­Che­cke­d(l­ocator) Check that the specified element is checked or not.

assert­Ale­rt(­pat­tern) Check the message of a JavaScript alert and click OK.

Useful references