简体   繁体   English

如何在selenium webdriver中验证标题

[英]How to verify the title in selenium webdriver

I tried to verify title in home page, I have tried below these two methods but I got error. 我试图在主页验证标题,我尝试了下面这两种方法,但我得到了错误。

1. 1。

WebDriverWait wait=new WebDriverWait(driver,30);       
wait.until(ExpectedConditions.titleContains("This is demo site for"));

ERROR: 错误:

Timed out after 30 seconds waiting for title to contain "This is demo site for". Current title: "Home page"
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'

2. 2。

String actualTitle=driver.getTitle();
String expectedTitle="This is demo site for";
assertEquals(actualTitle, expectedTitle);

ERROR: 错误:

org.junit.ComparisonFailure: expected:<[Home page]> but was:<[This is demo site for]>

HTML: HTML:

<div class="page-title">
  <h2>
    This is demo site for
    <img alt="" src="image link here"></img>
  </h2>
</div>

The above one is DOM of that page title. 以上是该页面标题的DOM。 I don't know how to use this element, please any one can help me. 我不知道如何使用这个元素,请任何人都可以帮助我。

You seem to confuse real page title ( <title>This is demo site for</title> ) that should be present in <head> with just a simple header string located somewhere in the <body> of page 您似乎混淆了实际页面标题( <title>This is demo site for</title> ),它应该出现在<head> ,只有一个简单的标题字符串位于页面的<body>

Try following: 试试以下:

String actualTitle = driver.findElement(By.xpath('//div[@class="page-title"]/h2'))getText();
String expectedTitle = "This is demo site for";
assertEquals(actualTitle, expectedTitle);

My solution to verify title in elements: 我在元素中验证标题的解决方案:

public String verifyTitle(String locator, String data) {    
   String txt = getElement(locator).getText();
        if (txt.equals(data)) {
            System.out.println("Title name present");
                }

or another way: 或另一种方式:

public String verifyElementTitle(String locator, String data) {
    String expectedTxt = data;
    String actualTxt = getElement(locator).getText();
    if (actualTxt.equals(expectedTxt)) {
        System.out.println("Title name present");
        return Constants.PASS;
    } else
        System.out.println("Title mismach or not present");
    return Constants.FAIL;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM