SELENIUM TESTING
Monday, January 27, 2014
WHAT ARE THE SELENIUM COMMON COMMANDS?
- open : Opens a page using a URL.
- click/clickAndWait : Performs a click operation, and optionally waits for a new page to load.
- verifyTitle/assertTitle : Verifies an expected page title.
- verifyTextPresent : Verifies expected text is somewhere on the page.
- verifyElementPresent : Verifies 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 present on the page.
- verifyTable : Verifies a table’s expected contents.
- waitForPageToLoad : Pauses execution until an expected new page loads. Called automatically when clickAndWait is used.
- waitForElementPresent : Pauses execution until an expected UI element, as defined by its HTML tag, is present on the page.
ELEMENT LOCATORS IN SELENIUM
ID (id) Locator: Select the element with the specified @id attribute.
For example,
is the id locator in below html code:
NAME (name) Locator: Select the first element with the specified @name attribute.
For example,
- id=name
- <input type="text" name="name" id="name" />
NAME (name) Locator: Select the first element with the specified @name attribute.
For example,
is the name locator in below html code:
CSS (cssSelectorSyntax) Locator: Select the element using css selectors.
For example,
is the css locator in below html code:
DOM (javascriptExpression) Locator: Find an element using JavaScript traversal of the HTML Document Object Model. DOM locators must begin with "document.".
For example,
is the dom locator in below html code:
Xpath (xpathExpression) Locator: Locate an element using an XPath expression.
For example,
is the xpath locator of “link22” element in below html code:
- namename=name
- <input type="text" name="name" id="name" />
CSS (cssSelectorSyntax) Locator: Select the element using css selectors.
For example,
- css=a[href="#id3"]
- <a href="#id3">Test Link 3</a>
DOM (javascriptExpression) Locator: Find an element using JavaScript traversal of the HTML Document Object Model. DOM locators must begin with "document.".
For example,
- dom=document.forms['myForm'].myName
- <form name="myForm" id="myForm" action="#" method="post">
- <input type="text" name=" myName " id="myName" />
- </form>
Xpath (xpathExpression) Locator: Locate an element using an XPath expression.
For example,
- xpath=//table[@id='table1']//tr[2]/td[2]
- <table id="table1">
- <tbody>
- <tr><td>link11</td><td>link12</td></tr>
- <tr><td>link21</td><td>link22</td></tr>
- </tbody>
- </table>
ABSOLUTE XPATH LOCATOR
This locator is start with root element.
For example, “xpath=//table[@id='table1']/tr/td” is the absolute xpath locator for “link11” element in below html code:
For example, “xpath=//table[@id='table1']/tr/td” is the absolute xpath locator for “link11” element in below html code:
- <table id="table1" border="1" cellspacing="10" cellpadding="10">
- <tbody>
- <tr><td>link11</td><td>link12</td></tr>
- <tr><td>link21</td><td>link22</td></tr>
- </tbody>
- </table>
RELATIVE XPATH LOCATOR
This locator is start with current element.
For example, “xpath=//td[4]” is the relative xpath locator for “link22” element in below html code:
For example, “xpath=//td[4]” is the relative xpath locator for “link22” element in below html code:
- <table id="table1" border="1" cellspacing="10" cellpadding="10">
- <tbody>
- <tr><td>link11</td><td>link12</td></tr>
- <tr><td>link21</td><td>link22</td></tr>
- </tbody>
- </table>
XPATH LOCATOR’S EXAMPLE IN SELENIUM
We have already learned about Xpath locator in previous posts.
Following are different example of Xpath locator:
Xpath Locator based on id of element:
For example,
is the xpath based on id for Name textbox in above html code.
Xpath Locator based on name of element:
For example,
is the xpath based on name for Email Address textbox in above html code.
Xpath Locator based on class of element:
For example,
is the xpath based on class for Submit button in above html code.
Xpath Locator based on value of element:
For example,
is the xpath based on value for Submit button in above html code.
Xpath Locator based on containing text by element:
For example,
is the xpath based containing text for Name label in below html code.
Following are different example of Xpath locator:
- <div class="MsoListParagraphCxSpFirst">
- <table id="table1"><o:p></o:p></div>
- <div class="MsoListParagraphCxSpMiddle">
- <form method="POST"
- action="#"><o:p></o:p></div>
- <div class="MsoListParagraphCxSpMiddle">
- <tbody><o:p></o:p></div>
- <div class="MsoListParagraphCxSpMiddle">
- <tr><td>Name: </td><td><input
- type="text" id="name" size="20"></td></tr><o:p></o:p></div>
- <div class="MsoListParagraphCxSpMiddle">
- <tr><td>Email Address: </td><td><input
- type="text" name="email" size="20"></td></tr><o:p></o:p></div>
- <div class="MsoListParagraphCxSpMiddle">
- <tr><td><input
- type="submit" value="Submit"
- class="button"></td></tr><o:p></o:p></div>
- <div class="MsoListParagraphCxSpLast">
- </tbody><o:p></o:p></div>
- </table>
Xpath Locator based on id of element:
For example,
- xpath=//input[@id='name']
Xpath Locator based on name of element:
For example,
- xpath=//input[@name='email']
Xpath Locator based on class of element:
For example,
- xpath=//input[@class='button']
Xpath Locator based on value of element:
For example,
- xpath=//input[@value='Submit ']
Xpath Locator based on containing text by element:
For example,
- xpath=//p[contains(text(),'Name')]
Subscribe to:
Posts (Atom)