Tuesday, September 10, 2019

Post#15.Locators Part-1 (ID,Name,Class,TagName,LinkText,Partial LinkText)


Locators:

Locators allow us to find elements on a page that can be used in our tests.

With the help of element locators we try to locate an element on AUT uniquely.


The different types of locator are:

1.      ID
2.      Name
3.      Class Name
4.      Tag Name
5.      Link Text
6.      Partial Link Text
7.      CSS Selector
8.      XPath

Locating elements by ID

ID’s are unique for each element so it is common way to locate elements using ID Locator. As per W3C, ID’s are supposed to be unique on a page and it makes ID’s are the most reliable locator. ID locators are the fastest and safest locators out of all locators.
1.      Open Mozilla Firefox and navigate to Gmail application.
2.      Open Firebug and inspect the “enter your email” input box. Take a note of its ID. Follow the below screenshot to do so.
3.      Copy the below mentioned script and execute in your system.
Syntax: findElement(By.id("Email"))


package pack31;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Locators {

              public static void main(String[] args) {
                            
                             WebDriver driver = new FirefoxDriver();
                             driver.get("https://www.google.com/gmail/");
                          driver.findElement(By.id("Email")).sendKeys("Sujoy@gmail.com");

              }
}

Locating elements by Name

We sometimes use Name locator to identify the elements on our webpage. Locating elements using Name is same as locating elements using ID locator.
These are not unique on a page. If there are multiple elements with the same Name locator then the first element on the page is selected. Test may fail, if another element with the same Name locator is present on the web page or added by the developers in the later stages.
Syntax: findElement(By.name("Email"))
1.      Open Mozilla Firefox and navigate to Gmail application.
2.      Open Firebug and inspect the enter your email input box. Take a note of its Name. Follow the below screenshot to do so.
3.      Copy the below mentioned script and execute in your system.

Class Name Locator:
Class Name locator gives the element which matches the values specified in the attribute name “class”.
1.      Open Mozilla Firefox and navigate to Facebook application.
2.      Open Firebug and inspect the Email input box. Take a note of its Class. Follow the below screenshot to do so.
3.      Copy the below mentioned script and execute in your system.

Syntax: findElement(By.className("Email"))

package pack2;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Locators {

              public static void main(String[] args) {
                             // TODO Auto-generated method stub
                             WebDriver driver = new FirefoxDriver();
                             driver.get("https://www.facebook.com/");
              driver.findElement(By.className("inputtext")).sendKeys("Sujoy@gmail.com");
                             }
}

Tag Name Locator:
Tag Name locator is used to find the elements matching the specified Tag Name. It is very helpful when we want to extract the content within a Tag.
Syntax:
findElement(By.tagName("HTML Tag Name"))

Script Explanation: 
1.      Opens Firefox Browser
2.      Navigates to Google.com
3.      Here we used, tagName(“a”) to get the links and used findElements method to store the list of all links on the google.com page
4.      Prints the size of total no. of links
5.      Print s the list of all the links available on the page
package pack1;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Locators {
              public static void main (String [] args){           
                             WebDriver driver = new FirefoxDriver();
                             driver.get("http://www.google.com");
                             List <WebElement> list = driver.findElements(By.tagName("a"));
                             System.out.println("Number of links: "+list.size());
                             for(int i = 0; i < list.size(); i++)
{
                             System.out.println(list.get(i).getText());
                             }
              }
}

Locating elements by Link Text /Partial Link Text

If there are multiple elements with the same link text then the first one will be selected. This Link Text locator works only on links (hyperlinks) so it is called as Link Text locator.

Example:
package pack2;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Locators {

              public static void main(String[] args) {
                            
                             WebDriver driver = new FirefoxDriver();
                             driver.get("https://www.google.com/gmail/");
                             driver.findElement(By.linkText("Create account")).click();
              }
}

Partial Link Text:
In some situations, we may need to find links by a portion of the text in a Link Text element. it contains. In such situations, we use Partial Link Text to locate elements.
Syntax: findElement(By.partialLinkText("partialLinkText"))
Script:
package pack3;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Locators {

              public static void main(String[] args) {
                            
                             WebDriver driver = new FirefoxDriver();
                             driver.get("https://www.google.com/gmail/");
                             driver.findElement(By.partialLinkText("Create")).click();
}
}

No comments:

Post a Comment