When we start making some web application, We start with some UI and Every UI has menu, Check box, Button, Combo Box , Radio button, Text Field and other element of Web Application.
But most of the time we need to verify that Text or value associated with above mentioned elements of Web Application are correct or not and for this we commonly use two function
1- getText()
2- assertEquals().
getText() helps in retrieving the Text from an element by using WebElement class.This method returns the value of inner text attribute of Element.
So why not take a look on this how it works
package com.testng.src.com.testng; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.safari.SafariDriver; import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class Verify { WebDriver driver; @BeforeMethod public void launch() { System.setProperty("webdriver.chrome.driver", "E:\DD MISHRA\workspace\chromedriver_win_26.0.1383.0\chromedriver.exe"); driver = new ChromeDriver(); } @Test public void verify()throws Exception { driver.get("http://google.co.in"); WebElement element = driver.findElement(By.xpath("//span[text()='Google Search']")); String strng = element.getText(); System.out.println(strng); Assert.assertEquals("Google Search", strng); } }
Recommended Paid Courses to Lean Selenium and Locators:
- Selenium WebDriver with Java -Basics to Advanced+Frameworks
- Selenium WebDriver With Java – Novice To Ninja + Interview
In this script
1- I have used xpath to find the Google Search button
2- By using findElement method we could find Google Search Button
3- Once we get the element, By using getText() method we could find the text on Button
4- By using assertEquals(), we verify that Google Search Text is available on Google Search Button. If text retrieved from getText() match with the String inserted in to asserEquals() method then test is passed otherwise it is failed.
Also Read : Mobile emulation in Google Chrome using Selenium WebDriver
Result after execution of this code
Started ChromeDriver port=22048 version=26.0.1383.0 log=E:DD MISHRAworkspaceDataDrivenWebdriverchromedriver.log Google Search PASSED: verify =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 1, Failures: 0, Skips: 0 ===============================================
There are other assert methods that could be used to verify the text in place of assertEquals()
assertTrue(strng.contains(“Search”));
assertTrue(strng.startsWith(“Google”));
assertTrue(strng.endsWith(“Search”));
If this text pattern match then test get passed other wise test get failed.
Leave a Reply