Reading:  

Getting started with Selenium


Selenese Commands

The Selenium API defines dozens of commands that can be categorized into the following:

  • Actions
  • Accessors
  • Assertions

Actions: Actions are commands, to change the state of the application like clicking links or buttons, select an option in a <select> or type a character sequence in a given textbox.

Commonly used Selenium Action commands are:

  • Open: This will open the specified address
  • openWindow (url,windowID): Opens a popup window.
  • select: This will select the specified item in a list.
  • select (selectLocator,optionLocator): Select an option from a drop-down using an option locator.
  • selectWindow (windowID): Selects a popup window using a window locator; once a popup window has been selected, all focus shifts to that window.
  • store (expression,variableName): The name of a variable in which the result is to be stored and expression is the value to store.
  • type: Sends text input to an element.
  • type (locator,value): Sets the value of an input field, similar to user typing action.
  • typeKeys (locator,value): Simulates keystroke events on the specified element, as though you typed the value key-by-key.
  • Click: Clicks the element at the provided location
  • Click (locator): Clicks on a link, button, checkbox or radio button.
  • clickAt (locator,coordString): Clicks on an element with the help of locator and co-ordinates
  • Check: To toggle the value of checkbox.
  • contextMenuAt (locator,coordString): Simulates opening the context menu of the specified element from a specified location
  • dragAndDrop (locator,movementsString): Drags an element and then drops it based on specified target.
  • echo (message): Prints the specified message on console which is used for debugging.
  • fireEvent (locator,eventName): Explicitly simulate an event, to trigger the corresponding "onevent" handler.
  • focus (locator): Move the focus to the specified element.
  • highlight (locator): Changes the background Color of the specified element to yellow Briefly which is useful for debugging purposes.
  • mouseDown (locator): Simulates a user pressing the left mouse button on the specified element.
  • mouseDownAt (locator,coordString): Simulates a user pressing the left mouse button at the specified location on the specified element.
  • mouseUp (locator): Simulates the event that occurs when the user releases the mouse button.
  • mouseUpAt (locator,coordString): Simulates the event that occurs when the user releases the mouse button at the specified location.
  • pause (waitTime): Waits for the specified amount of time (in milliseconds).
  • refresh(): Simulates the user clicking the "Refresh" button on their browser.
  • waitForCondition (script,timeout): Executes the specified JavaScript snippet repeatedly until it evaluates to "true".
  • waitForPageToLoad (timeout): Waits for a new page to load.
  • waitForPopUp (windowID,timeout): Waits for a popup window to appear and load.
  • windowFocus(): Gives focus to the currently selected window.
  • windowMaximize(): Resize currently selected window to take up the entire screen.

 

Accessors:

Accessors search the state of the application and store values in variables. Eg: "storeText". They are also used to generate Assertions automatically. 

  • Store: This will allow storing a value temporarily.
  • Verify: Performs a soft assertion against an expected value.
  • Assert: Performs a hard assertion against an expected value.
  • waitFor: Waits until the value matches the specified value.

The following are more commonly used Selenium Accessors commands:

  • assertErrorOnNext(message): Pings Selenium to expect an error on the next command execution with an expected message.
  • storeAllButtons(variableName): Returns the IDs of all buttons on the page.
  • storeAllFields(variableName): Returns the IDs of all input fields on the page.
  • storeAllLinks (variableName): Returns the IDs of all links on the page.
  • storeAllWindowIds (variableName): Returns the IDs of all windows that the browser knows about in an array.
  • storeAllWindowTitles (variableName): Returns the names of all windows that the browser knows about in an array.
  • storeAllWindowNames (variableName): Returns the titles of all windows that the browser knows about in an array.
  • storeAttribute(attributeLocator, variableName): Gets the value of an element attribute. The value of the attribute may differ across browsers.
  • storeBodyText(variableName): Gets the entire text of the page.
  • storeConfirmation(variableName): Retrieves the message of a JavaScript confirmation dialog generated during the previous action.
  • storeElementIndex(locator,variableName): Get the relative index of an element to its parent (starting from 0).
  • storeLocation(variableName): Gets the absolute URL of the current page.
  • storeSelectedIds(selectLocator,variableName): Gets all element IDs for selected options in the specified select or multi-select element.
  • storeSelectedIndex(selectLocator,variableName): Returns index (option number, starting at 0) for selected option in the specified select element.
  • storeSelectedLabel(selectLocator,variableName): Returns label (visible text) for selected option in the specified select element.
  • storeSelectedValue(selectLocator,variableName): Gets value (value attribute) for selected option in the specified select element.
  • storeSelectOptions (selectLocator,variableName): Gets all labels in the specified select drop-down.
  • storeTable(tableCellAddress,variableName): Gets the text from a cell of a table.
  • storeText(locator,variableName): Gets the text of an element.
  • storeTitle(variableName): Gets the title of the current page.
  • storeValue(locator,variableName): Gets the (whitespace-trimmed) value of an input field.
  • storeChecked(locator,variableName): Returns whether a toggle-button  (checkbox/radio) is checked.
  • storeElementPresent(locator,variableName): Verifies that the specified element is somewhere on the page.
  • storeTextPresent(pattern,variableName): Verifies that the specified text pattern appears somewhere on the rendered page shown to the user.
  • storeVisible(locator,variableName): Determines if the specified element is visible.
  •  

 

Assertions:

Assertions are like Accessors, but they verify that the state of the application is as expected. Selenium Assertions can be used in 3 modes:

  • Assert
  • Verify and
  • waitFor. 

The following are more commonly used Selenium Assertions commands:

  • waitForErrorOnNext(message): Waits for error. Used with the accessor assertErrorOnNext.
  • verifySelected(selectLocator,optionLocator): Verifies that the selected option of a drop-down satisfies the option Specified.
  • waitForSelected(selectLocator,optionLocator): Waits for getting the option selected; used with the accessor assertSelected.
  • waitForNotSelected(selectLocator,optionLocator): Waits for not getting the option selected; used with accessor the assertSelected.
  • verifyAlert (pattern): Verifies the alert text.
  • waitForAlert (pattern): Waits for the alert; used with the accessor storeAlert.
  • verifyAllButtons(pattern): Verifies the button.
  • waitForAllButtons(pattern): Waits for the button to load; used with the accessor storeAllButtons.
  • verifyAllLinks (pattern): Verifies all links; used with the accessor storeAllLinks.
  • waitForAllLinks (pattern): Waits for all links; used with the accessor storeAllLinks.
  • verifyAllWindowIds(pattern): Verifies the window id; used with the accessor storeAllWindowIds.
  • waitForAllWindowIds(pattern): Waits the window id; used with the accessor storeAllWindowIds.
  • verifyAttribute(attributeLocator,pattern): Verifies an attribute of an element; used with the accessor storeAttribute.
  • waitForAttribute(attributeLocator,pattern): Waits for an attribute of an element; used with accessor storeAttribute.
  • verifyBodyText(pattern): Verifies the body text;
  • waitForBodyText(pattern): Waits for the body text; used with the accessor storeBodyText.
  • waitForConfirmation(pattern): Waits for confirmation; used with the accessor storeConfirmationPresent. 

Locators

Selenium uses locators to find and match the elements of web page that it needs to interact with.

There are 8 locators in Selenium are:

  • Identifier: works with the id and name attributes of your html tags.
  • Id: Select the element with the specified "id" attribute.
  • Name: Select the first element with the specified "name" attribute.
  • Link: This is intended to select links only and selects the anchor element containing the specified text: link=the text of the link.
  • DOM: This is works by locating elements that matches the javascript expression refering to an element in the DOM of the page.
  • XPath: To locate an element using an XPath expression. XPath is the standard navigation tool for XML; and an HTML document is also an XML document (xHTML).
  • CSS: The CSS locator uses CSS selectors to find the elements in the page.

Description

This tutorial will get you started with Selenium. This tutorial is subdivided into 11 chapters. 

  1. Overview
  2. IDE
  3. Environment Setup
  4. Remote Control
  5. Selenese Commands
  6. Webdriver
  7. Locators
  8. User Interactions
  9. Test design techniques
  10. TestNG
  11. Grid

 

 

 

* freelancer contributed



Environment

A PC capable of running selenium

Prerequisites

A basic idea of what Software Testing is.

Audience

People who wish to get started with Selenium

Learning Objectives

You will learn ins and outs of Selenium which includes downloading, installing and using Selenium.

Author: Subject Coach
Added on: 10th Feb 2015

You must be logged in as Student to ask a Question.

None just yet!