Reputation: 285
How can I verify some text in the title tag using Selenium WebDriver and java?
Upvotes: 13
Views: 153953
Reputation: 806
It could be done by getting the page title by Selenium and do assertion by using TestNG.
Import Assert class in the import section:
import org.testng.Assert;
Create a WebDriver object:
WebDriver driver=new FirefoxDriver();
Apply this to assert the title of the page:
Assert.assertEquals("Expected page title", driver.getTitle());
Upvotes: 3
Reputation: 31
You can do it easily by Assertion using Selenium Testng framework.
Steps:
1.Create Firefox browser session
2.Initialize expected title name.
3.Navigate to "www.google.com" [As per you requirement, you can change] and wait for some time (15 seconds) to load the page completely.
4.Get the actual title name using "driver.getTitle()" and store it in String variable.
5.Apply the Assertion like below, Assert.assertTrue(actualGooglePageTitlte.equalsIgnoreCase(expectedGooglePageTitle ),"Page title name not matched or Problem in loading grid");
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.myapplication.Utilty;
public class PageTitleVerification
{
private static WebDriver driver = new FirefoxDriver();
@Test
public void test01_GooglePageTitleVerify()
{
driver.navigate().to("https://www.google.com/");
String expectedGooglePageTitle = "Google";
Utility.waitForElementInDOM(driver, "Google Search", 15);
//Get page title
String actualGooglePageTitlte=driver.getTitle();
System.out.println("Google page title" + actualGooglePageTitlte);
//Verify expected page title and actual page title is same
Assert.assertTrue(actualGooglePageTitlte.equalsIgnoreCase(expectedGooglePageTitle
),"Page title not matched or Problem in loading url page");
}
}
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Utility {
/*Wait for an element to be present in DOM before specified time (in seconds ) has
elapsed */
public static void waitForElementInDOM(WebDriver driver,String elementIdentifier,
long timeOutInSeconds)
{
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds );
try
{
//this will wait for element to be visible for 15 seconds
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath
(elementIdentifier)));
}
catch(NoSuchElementException e)
{
e.printStackTrace();
}
}
}
Upvotes: 1
Reputation:
You can do it easily by using JUnit or TestNG framework. Do the assertion as below:
String actualTitle = driver.getTitle();
String expectedTitle = "Title of Page";
assertEquals(expectedTitle,actualTitle);
OR,
assertTrue(driver.getTitle().contains("Title of Page"));
Upvotes: 18
Reputation: 4536
In java you can do some thing like:
if(driver.getTitle().contains("some expected text"))
//Pass
System.out.println("Page title contains \"some expected text\" ");
else
//Fail
System.out.println("Page title doesn't contains \"some expected text\" ");
Upvotes: 4
Reputation: 2437
If you're using Selenium 2.0 / Webdriver you can call driver.getTitle() or driver.getPageSource() if you want to search through the actual page source.
Upvotes: 3