Вы можете сделать это легко с помощью Assertion, используя платформу Selenium Testng.
Шаги:
1.Создать сеанс браузера Firefox
2. Инициализировать ожидаемое имя заголовка.
3. Перейдите на "www.google.com" [По вашему требованию вы можете изменить] и подождите некоторое время (15 секунд), чтобы полностью загрузить страницу.
4. Получитефактическое название заголовка с помощью «driver.getTitle ()» и сохраните его в переменной String.
5. Примените утверждение, как показано ниже, Assert.assertTrue (actualGooglePageTitlte.equalsIgnoreCase (ОжидаемыйGooglePageTitle), "Имя заголовка страницы не соответствует или проблема в сетке загрузки");
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();
}
}
}