Wednesday, 27 August 2014

Web Elements and Locators

  •  In WebDriver automation testing all about  related with web elements as it is an web application automation tool.
  • WebElement is nothing but, all DOM objects appeared in the web page. To perform the functions with DOM objects/ web elements  all we need to locate those elements exactly.


Syntax:

                WebElement element=driver.findElement(By.<Locator>);


  •    As we have seen in the above statement we want to specify some locator to identify the WebElement.


  • 'By' is the class, in that class we have different types of  static methods to identify elements. Those are,


  1. By.className
  2. By.cssSelector
  3. By.id
  4. By.linkText
  5. By.name
  6. By.partialLinkText
  7. By.tagName
  8. By.xpath 

1. By.className
See below example
Example:1
  <td class=name> </td>
WebElement td=driver.findElement(By.className("name"));

2.By.cssSelector
CSS selector is the one the best ways to locate some complex elements in the page.

See below some examples for easy understanding
Example:1
 <td class=name> </td>
driver.findElement(By.cssSelector("td.name"));     In css selector we can denote class name with dot (.)
                             (or)
driver.findElement(By.cssSelector("[class=name]"))    We can specify the attribute name and its value.


Example:2
<input id=create>
driver.findElement(By.cssSelector("#create")).sendKeys("test");    shortcut for denoting id is #
                                  ( or )
driver.findElement(By.cssSelector("[id=create]")).sendKeys("test")


Example:3
<td value=dynamicValue_13232><td>
driver.findElement(By.cssSelector("[value*=dynamicValue]"))     * is for checking contained value
(here value contains dynamicValue)


Example:4
          <div value=testing name=dynamic_2307></div>
driver.findElement(By.cssSelector("[value=testing][name*=dynamic]"));


If you want to include more attribute values in locator criteria use css locator as above.



3. By.id
See below example
Example:1
  <td id=newRecord> </td>
WebElement td=driver.findElement(By.id("newRecord"));


here we can specify the id attribute value directly.

4. By.linkText
See below example
Example:1
  <a onclick=gotonext()>Setup </a>
WebElement link=driver.findElement(By.linkText("Setup"));


This is the best locator for locating links (anchor tags) in your web page.

5. By.partialLinkText
See below example
Example:1
  <a onclick=gotonext()>very long link text </a>
WebElement link=driver.findElement(By.partialLinkText("very"));
                               (or)
WebElement link=driver.findElement(By.partialLinkText("long link"));


This is the locator for locating links (anchor tags) using partial text it contains .

6. By.name
See below example
Example:1
  <td name=WebDriver> </td>
WebElement td=driver.findElement(By.name("WebDriver"));

7. By.tagName
See below example
Example:1
  <td class=name> </td>
WebElement td=driver.findElement(By.tagName("td"));


If you want to get the entire text of web page use below logic.


driver.findElement(By.tagName("body")).getText();

8. By.xpath
                 Xpath is a really good way to navigate your site there are no IDS on elements that you need to work with or is near the element you want to work with.

(//): is used to search in entire structure

<html>
  <body>
<form id="loginForm">
<input name="username" type="text" />
<input name="password" type="password" />
 <input name="continue" type="submit" value="Login" />
 <input name="continue" type="button" value="Clear" />
 </form>
</body>
<html>
xpath=/html/body/form[1]  - Absolute path (would break if the HTML was changed only slightly)
//form[1] - First form element in the HTML
xpath=//form[@id='loginForm'] - The form element with attribute named ‘id’ and the value ‘loginForm


To know FAQ's on Selenium Please ClickHere
To know SQL's Queries Required For Testers Please ClickHere

           




No comments:

Post a Comment