This is the first post in our new How To…Series. Each month on the first Tuesday of the month, we will post a new blog post to take you through a step-by-step guide on how to address a particular aspect of a tool, platform or device that you might need to address in your testing. The series kicks off with a number of posts by Alec Siminiuc on difference aspects of Selenium and focuses on elements in Selenium webdriver.
We will be addressing different topics, testing tools, platforms over the series. If you have a request for one topic to be addressed then please comment below.
How To…Select Elements In Selenium WebDriver Scripts
How To…Use Explicit Waits In Selenium WebDriver
How To…Manage Exceptions In Selenium WebDriver
How To…Execute Javascript In Selenium
How To…take Screenshots in Selenium
How To…Interact with sliders in Selenium
How to…Interact with Modal Windows
How to…Use Expected Conditions
If you think you would like to help your fellow testers and want to write a How To… post then get in touch: hello [at] testhuddle.com
————————————————————————————————————————————
In Selenium WebDriver test automation scripts, before interacting with elements, they need to be found first.
Finding the elements is done using locators built with the By class.
The By class has a number of static methods for locating elements.
By.className(String classAttribute)
It finds elements based on the value of the class attribute.
By.className works well if the class value of the element is unique.
Code Sample
The following test script finds the search textbox from http://www.vpl.ca using the class name value.
After finding the element, the script verifies with an assertion if the class of the found element is the correct one.
public void testClassNameSelector()
{
String searchBoxClass = “text”;
driver.get(url);
WebElement element = driver.findElement(By.className(searchBoxClass));
String elementClass = element.getAttribute(“class”);
assertTrue(elementClass.equalsIgnoreCase(searchBoxClass) == true);
}
By.id(String idAttribute)
It finds elements based on the value of the id attribute
It works very well if the element has an id attribute.
This is the fastest way of finding an element
Code Sample
The following test script finds the search textbox from http://www.vpl.ca using the id attribute value.
After finding the element, the script verifies with an assertion if the id attribute of the found element is the correct one.
public void testIDSelector()
{
String searchBoxId = “globalQuery”;
driver.get(url);
WebElement element = driver.findElement(By.id(searchBoxId));
String elementId = element.getAttribute(“id”);
assertTrue(elementId.equalsIgnoreCase(searchBoxId) == true);
}
By.name(String nameAttribute)
It finds elements based on the value of the name attribute.
It works well if the element has a name attribute with a unique value.
The following test script finds the search textbox from http://www.vpl.ca using the name attribute value.
Code Sample
After finding the element, the script verifies with an assertion if the name attribute of the found element is the correct one.
public void testNameSelector()
{
String searchBoxName = “q”;
driver.get(url);
WebElement element = driver.findElement(By.name(searchBoxName));
String elementName = element.getAttribute(“name”);
assertTrue(elementName.equalsIgnoreCase(searchBoxName) == true);
By.tagName(String tagName)
It finds elements based on the tag name of the element.
It is not very useful since each web page includes many elements with the same tag.
Code Sample
The following test script finds the first element from http://www.vpl.ca that has the input tag.
After finding the element, the script verifies with an assertion if the tag of the found element is the correct one.
public void testTagNameSelector()
{
String tagName = “input”;
driver.get(url);
WebElement element = driver.findElement(By.tagName(tagName));
String elementTag = element.getTagName();
assertTrue(elementTag.equalsIgnoreCase(tagName) == true);
By.linkText(String linkText)
It finds link elements based on the value of the link text.
It does not work for other element types.
Code Sample
The following test script finds the My VPL link from http://www.vpl.ca.
After finding the element, the script verifies with an assertion if the text of the found element is the correct one.
public void testLinkTextSelector()
{
String linkText = “My VPL”;
driver.get(url);
WebElement element = driver.findElement(By.linkText(linkText));
String elementText = element.getText();
assertTrue(elementText.equalsIgnoreCase(linkText) == true);
By.partialLinkText(String linkText)
It finds link elements based on the value included in the link text.
It does not work for other element types.
Code Sample
The following test script finds the My VPL link from http://www.vpl.ca.
After finding the element, the script verifies with an assertion if the text of the found element is the correct one.
public void testPartialLinkTextSelector()
{
String partialLinkText = “VPL”;
driver.get(url);
WebElement element = driver.findElement(By.partialLinkText(partialLinkText));
String elementText = element.getText();
assertTrue(elementText.equalsIgnoreCase(“My VPL”) == true);
By.cssSelector(String cssSelector)
It finds elements based on a CSS expression.
It is useful if the element to be found does not have id, name, class attributes.
Code Sample
The following test script finds the search textbox from http://www.vpl.ca using the CSS selector.
After finding the element, the script verifies with an assertion if the id of the found element is the correct one.
public void testCSSSelector()
{
String cssSelector = “input#globalQuery”;
driver.get(url);
WebElement element = driver.findElement(By.cssSelector(cssSelector));
String elementId = element.getAttribute(“id”);
assertTrue(elementId.equalsIgnoreCase(“globalQuery”) == true);
By.xpath(String xpathExpression)
It finds elements based on a XPATH expression.
It is useful if the element to be found does not have id, name, class attributes.
Code Sample
The following test script finds the search textbox from http://www.vpl.ca using the xpath selector.
After finding the element, the script verifies with an assertion if the id of the found element is the correct one.
public void testXPATHSelector()
{
String xpathSelector = “//input[@placeholder=’Find books, music, movies and more’]”;
driver.get(url);
WebElement element = driver.findElement(By.xpath(xpathSelector));
String elementId = element.getAttribute(“id”);
assertTrue(elementId.equalsIgnoreCase(“”globalQuery) == true);
Which locators should you use?
Which locator to use in your test automation project depends on the element’s details.
- If the element has an id attribute, use By.id. This is the fastest way of finding an element
- If the element has a name attribute and the name value is unique, use By.name.
- If the element has a class attribute and the class value is unique, use By.className
- Do not use By.tagName
- Use By.linkText and By.partialLinkText for finding link elements. These are weak locators as the text of the link can change.
- In all other cases, use other a CSS or XPATH locator
Complete Test Class
import static org.testng.AssertJUnit.assertTrue;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestSelectors {
WebDriver driver;
String url = “http://www.vpl.ca”;
@Before
public void setUp()
{
driver = new FirefoxDriver();
driver.manage().window().maximize();
}
@After
public void tearDown()
{
driver.quit();
}
public void testIDSelector()
{
String searchBoxId = “globalQuery”;
driver.get(url);
WebElement element = driver.findElement(By.id(searchBoxId));
String elementId = element.getAttribute(“id”);
assertTrue(elementId.equalsIgnoreCase(searchBoxId) == true);
}
public void testNameSelector()
{
String searchBoxName = “q”;
driver.get(url);
WebElement element = driver.findElement(By.name(searchBoxName));
String elementName = element.getAttribute(“name”);
assertTrue(elementName.equalsIgnoreCase(searchBoxName) == true);
}
public void testClassNameSelector()
{
String searchBoxClass = “text”;
driver.get(url);
WebElement element = driver.findElement(By.className(searchBoxClass));
String elementClass = element.getAttribute(“class”);
assertTrue(elementClass.equalsIgnoreCase(searchBoxClass) == true);
}
public void testTagNameSelector()
{
String tagName = “input”;
driver.get(url);
WebElement element = driver.findElement(By.tagName(tagName));
String elementTag = element.getTagName();
assertTrue(elementTag.equalsIgnoreCase(tagName) == true);
}
public void testLinkTextSelector()
{
String linkText = “My VPL”;
driver.get(url);
WebElement element = driver.findElement(By.linkText(linkText));
String elementText = element.getText();
assertTrue(elementText.equalsIgnoreCase(linkText) == true);
}
public void testPartialLinkTextSelector()
{
String partialLinkText = “VPL”;
driver.get(url);
WebElement element = driver.findElement(By.partialLinkText(partialLinkText));
String elementText = element.getText();
assertTrue(elementText.equalsIgnoreCase(“My VPL”) == true);
}
public void testXPATHSelector()
{
String xpathSelector = “//input[@placeholder=’Find books, music, movies and more’]”;
driver.get(url);
WebElement element = driver.findElement(By.xpath(xpathSelector));
String elementId = element.getAttribute(“id”);
assertTrue(elementId.equalsIgnoreCase(“globalQuery”) == true);
}
public void testCSSSelector()
{
String cssSelector = “input#globalQuery”;
driver.get(url);
WebElement element = driver.findElement(By.cssSelector(cssSelector));
String elementId = element.getAttribute(“id”);
assertTrue(elementId.equalsIgnoreCase(“globalQuery”) == true);
Post Views: 4,140