Automation Using Selenium Webdriver

Wednesday, 2 November 2016

Checking text on web page using WebDriver

While testing any web application, we need to make sure that it is displaying correct text under given elements. Selenium WebDriver provides a method to achieve this. We can retrieve the text from any WebElement using getText() method.

Example
Lets create a test that check for the proper text in flipkart menu tab.

Code:
@Test
public void testFlipkartMenuText() {
    //Get Text from first item and store it in String variable
    String electronics = driver.findElement(By.cssSelector("li[data-key='electronics']>a>span")).getText();
 
    //Compare actual & required texts
    assertEquals("Electronics",electronics); 
}
The WebElement class' getText() method returns value of the innerText attribute
of the element.

P.S : Given script may not work on flipkart site. I've written this code just to give some basic idea of using getText() method

No comments:

Post a Comment