Showing posts with label Selenium. Show all posts
Showing posts with label Selenium. Show all posts

Wednesday, November 17, 2010

Components of Selenium

Selenium IDE: It is the plugin to Firefox to record and play tests in Firefox and also exports tests in different language.

Selenium-IDE has a recording feature, which will keep account of user actions as they are performed and store them as a reusable script to play back.

It also has context menu integrated with the Firefox browser, which allows the user to pick from the list of assertions and verifications for the selected location.

Exported test can be run in any browser and any platform using "selenium remote control".
Selenium RC: Selenium-RC provides an API (Application Programming Interface) and library for each of its supported languages: HTML, Java, C#, Perl, PHP, Python and Ruby.

Allows playing of scripts which are exported to different platform or OS.

This ability to use Selenium RC with a high level languague to develop test cases also allows the automated testing to be integrated with the project's automated build environment.
Selenium Grid: Selenium-Grid allows the Selenium-RC solution to scale for test suites or test suites to be run in multiple environments.

With selenium-Grid multiple instances of Selenium RC are running on various operating system and browser configurations, each of these when launching register with a hub. When tests are sent to the hub they are then redirected to an available Selenium-RC, which will launch the browser and run the test.

This allows running the tests in parallel, with the entire test suite theorietically taking only as long as to run as the longest individual test.

Tuesday, October 12, 2010

Regular Expressions

There are three types of String Matching Pattern in Selenium.

1. glob:pattern - "Glob" is a kind of regular expression used in command line shells.
In glob pattern * represents any sequence of characters. '?' represents a single character.
2. regexp:regexp -Uses Javascript regular expressions
3. exact:string-Match a string exactly

Element Locators in Selenium

Element locators tell selenium which HTML element a command refers to.
Format of element locator is:
locatortype = argument

1. Locating by identifier
identifier = id
Selects an element with specified @id attribute.If no match is found, it will take the element with @name = id.

2. Locating by Name
name = name
Selects the first element with specified @name attribute. If multiple elements have the same value for a name attribute, then you can use filters to further refine your location strategy (matching the value attribute).

3. Locating by id
id = id
Selects the element with @id attribute.

4. Locating by DOM
The Document Object Model represents an HTML document and can be accessed using JavaScript.
dom = javascriptExpression
Example:
dom=document.forms['myForm'].myDropdown

5. Locating by XPath
xpath = xpathExpression
Locates an element with XPath expression.

XPath is the language used for locating nodes in an XML document. As HTML can be an implementation of XML (XHTML), Selenium users can leverage this powerful language to target elements in their web applications.
One of teh main reasons for using XPath is when you don't have a suitable id or name attribute for the element you wish to locate. You can use XPath to either locate the element in absolute terms, or relative to an element that does have an id or name attribute.
Absolute XPaths contain the location of all elements from the root and as a result are likely to fail with only the slightest adjustment to the application.
In Relatiion XPath, we can find a nearby element with an id or name attribute (ideally a parent element), you can locate you target element based on the relationship. This is less likely to change and can make your tests more robust.

Since only xpath locators start with "//", it is not necessary to include the xpath=label when specifying an XPath locator.

Example:
xpath=//img[@alt='The image alt text']
xpath=//table[@id='table1']//tr[4]/td[2]
xpath=//a[contains(@href,'#id1')]

6. Locating Hyperlinks by link Text
link = textPattern
Selects a link with specified text. If two links with the same text are present, then the first match will be used.

7. Locating by CSS
CSS (Cascading Style Sheets) is a language for describing the rendering of HTML and XML documents. CSS uses Selectors for binding style properties to elements in the document. These selectors can be used by Selenium as another locating strategy.
css = cssSelectorSyntax

8. ui = uiSpecifierString

Without an explicit locator prefix, selenium uses the following default strategies:
1. dom, for locators starting with document.
2. xpath, for locators starting with //

Wednesday, September 15, 2010

Introduction to Selenium

Selenium is open source testing tool which supports record and playback. You can export the recorded test to most languages - html, java, .net, perl, ruby etc.

Features of Selenium IDE


  1. Easy record and playback
  2. Statements use IDs, Names and XPath.
  3. Autocomplete for all Selenium commands
Selenium uses a language called selenese.

Selenium commands should be executed in Selenium IDE or Selenium RC.

Selenese consists of three types of statements:
1. Actions
2. Accessors
3. Element Locators.

Actions statements are commands used for performing action on a web application.
Example:
Open a URL
Click a link, button
Type text in the text field

Accessors statements are commands used for verification/validation.

Example:
store(locator, variable)
verify(locator, pattern)

Monday, September 13, 2010

Action Commands in Selenium

Selenium commands consists of two parameters:
1. Locator: for identifying a UI element within a page.
2. Text pattern: for verifying or asserting expected page content.

The following are the commands used:
1. Open Statement
2. Check Statement
3. Click Statement
4. Select Statement
5. Type Statement
6. Wait For Page to Load
7. Set Time Out Statement
8. Store Commands
8.1. Store Element Present
8.2. Store Text
8.3. Store Eval

1. Open Command -- Tells the Browser to Open the URL
Syntax: open(String url)
private static final String BASE_URL = http://www.google.com/
selenium.open(BASE_URL)
2. waitForPageToLoad -- waits for a new page to load
Syntax: waitForPageToLoad(String timeoutinMilliSeconds)
public static final String MAX_TIME = "60000"
selenium.waitForPageToLoad(MAX_TIME)

3. Type- Types text in the text field
Syntax: type(String locator, String value)
We can use selenium.type commands in one of the following ways
selenium.type("name=q","testing");
selenium.type("xpath=//input[@name='q']","testing");
selenium.type("//*[@name='q']","testing");

4. Click--performs a Click operation, optionally waits for a new page to load
(button, link, checkbox or radio button.)

Syntax: Click(String locator)
selenium.click("link=Images");

5. Check
Syntax: check(String locator)
selenium.check("id=checkBoxInput");

6. Select - select options from the drop-down listbox
Syntax: select(String locator, String value)

7. setTimeout --- Overrides Default Timeout value of Selenium
Syntax: setTimeout("50000");

8. Store Commands and Selenium Variables
Selenium variables can be used to store values passed to your test program from the command-line, from another program, or from a file.

It takes two parameters, the text value to be stored and a selenium variables.

Use the standard variable naming conventions of only alphanumeric characters when choosing a name for your variable

store paul@gmail.com userName

To access the value of a variable, enclose the variable in curly brackets ({}) and precede it with a dollar sign like this.

verifyText //div/p ${userName}

8.1. storeElementPresent
It stores a boolean value - "true" or "false" - depending on whether the UI element is found.

8.2. storeText
It uses a locater to identify specific page test. The text, if found, is stored in the variable.

8.3. storeEval
StoreEval allows the test to store the result of running the script in a variable.

We can get information from a page using the following commands.
1. Get Title
2. Get Text
3. getSelectedValue Statement
4. Is Something Selected Statement
5. Get Title Statement
6. Get Text
7. Get Value
8. Is Editable
9. Is Element Present
10. Get Selected Label
11. getSelectedValue Statement
12. isChecked
13. isSomethingSelected Statement
14. verify Title
15. verify Text Present
16. verify Element Present
17. verify text
18. verify Table

Get Title -- Gets the title of the current page
Syntax: getTitle()
Example: private status final String TEST_PAGE_TITLE = "My Page"
assertEquals(TEST_PAGE_TITILE, selenium.getTitle());

getText -- Gets the Text of the element
Syntax: getText(String locator)
Example: assertEquals("Text in the fields","selenium.getValue("id=textInput"));

Get Value: Gets the value from the OObject
getValue(String locator)
Example: assertEquals("option two",selenium.getValue("id=selectWithLabelsOnly"));

Is Editable
Syntax: isEditable(String locator)

Is Element Present
Syntax: isElementPresent(String locator)
assertTrue(selenium.isElementPresent("id=textInput"));

Get Selected Label
getSelectedLabel(String locator)
assertEquals("option two",selenium.getSelectedLabel("id=selectWithLabelsOnly"));

getSelectedValue Statement
Syntax: getSelectedValue(String locator)
Example: assertEquals("2",selenium.getSelectedValue("id=selectWithLabelsAndValues"));

isSomethingSelected Statement
isSomethingSelected(String locator)
Example: assertTrue(selenium.isSomethingSelected("id=selectwithLabelsOnly"));

isChecked
Syntax: isChecked(String locator)
Example:
assertTrue(selenium.isChecked("id=checkBoxInput"));
assertTrue(selenium.isChecked("name=radioButton value=a"));

getAlert()


verifyTitle/AssertTitle verifies an expected page title


verifyTextPresent This command is used to verify specific text is somewhere on the page
. It takes a single argument the text pattern to be verified.
Example: selenium.verifyTextPresent("Testing")

verifyElementPresent This command verifies whether an expected UI element, as defined by its HTML tag, is present on the page


verifyText verifies expected text and it's corresponding HTML tag are preset on the page.


verifyTable verifies a table's expected contents.

Assertion Commands in Selenium

Adding assertion commands
When selenium is in recording mode, right click on the application and select verify and/or assert commands from context menu.

There are three modes of Assertion commands used in Selenium
1. Assert: When an assert fails, the test is aborted.
Example: assertText

2. Verify: When a Verify fails, the test will continue execution.
Example: verifyText

3. WaitFor: WaitFor Commands waits for a particular condition to get true.
Example:
waitForText
waitForPageToLoad

Start Selenium Server from Command prompt

To start selenium from command prompt use
java -jar selenium-server.jar

Selenium - Get Browser Details

//To Get the name of the browser
System.out.println("Name of the browser used"+selenium.getEval("navigator.appName;"));

//To Get the code name of the browser using Selenium
System.out.println("Code name of the browser"+selenium.getEval("navigator.appCodeName;"));

// To Get the browser version using Selenium
System.out.println("browser version of the selenium"+selenium.getEval("navigator.appVersion;"));

//To Get the Operating System Details using Selenium
System.out.println(" "+selenium.getEval("navigator.userAgent;"));

//To Get whether the cookies are enabled in the browser
System.out.println("Cookies are enabled: "+selenium.getEval("navigator.cookieEnabled;"));

//To Get the Language Details of the Browser
System.out.println("Language used by the browser:"+selenium.getEval("navigator.userLanguauge;"));

//To Get Default language of the Operating System
System.out.println("Default Language used by the Operating System: "+selenium.getEval("navigator.systemLanguage;"));