Skip to content

Instantly share code, notes, and snippets.

@lazypower
Created May 16, 2012 04:24
Show Gist options
  • Save lazypower/2707373 to your computer and use it in GitHub Desktop.
Save lazypower/2707373 to your computer and use it in GitHub Desktop.
Handy Selenium Syntax

echo

Outputs the exact phrase put into the statement. Eg: echo "Now checking validation" will cause a new line, the phrase "Now checking validation" to be printed, another new line and then the output of the test. Handy when reviewing logs to separate multiple test cases.

open

tells the browser to open a specific page. This is useful if you don’t want the test to navigate to a page and instead just want it to go straight there. This can be a relative path so you can use it on both a testing and live environment.

Type and TypeKeys

type will place text in an input. Typekeys will actually mimic entering one key at a time so any onkeyup() functionality you might have going on will work. We have autocomplete search boxes that required TypeKeys.

verifyTextPresent

Verifies that text appears on the page. It can be dynamic text that was loaded with ajax. If it’s on the page in the active window at the time this command is called, it’ll find it.

verifyValue

Verifies the value of an input field. Provide the id of the input as the target and the value is the text you’re expecting.

click

tell the test to click something on the page. The target can be referenced with the element ID or xPath as with the other commands.

clickAndWait

Useful for form submissions. Many times when you’re in record mode and click a submit button, selenium will only record a “click” command. This will break your test commands following the form submission where you verify the presence of a success message or form values because the next page hasn’t loaded yet. The “wait” portion will wait to continue the testing when the next page has fully loaded.

select

select an option in a select box by providing the id of the select box and the option value.

pause

Tell the test to pause for a while. Enter a target of 3000 for 3 seconds. I started using this command frequently at first, especially when testing ajax. However, as I went on I found better alternatives since a TCP/IP connection isn’t always reliable and doesn’t get returned on time, every time.

waitForVisible

This command tells the test to wait until an element, often returned by ajax, is visible before continuing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment