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.

1 comment:

  1. I have a problem in locating the row number of a city.
    suppose i have a form to add a new city for any country. in the list
    of added city there are two columns, one column contains the city name
    and other column have the respective delete action link
    and if i have to delete a city say testcity i need to know the
    repective row number for that testcity so that i can loacte the action
    delete link for the respective testcity.
    how can i get the location of the test city? this was the problem
    because row above the testcity can be deleted by others and the
    location of it changes. which may cause the test to fail though other
    record in the list with the previous location of testcity gets
    deleted.
    help me with this thank you

    ReplyDelete