java - 在 selenium 中测试脚本以验证网页标题

标签 java html selenium testing

我已经编写了一些代码,但它只能在获取网页标题之前起作用。 当标题验证代码时,测试失败。

场景如下:

  1. 访问网站:https://www.google.co.in
  2. 搜索您的姓名:即名字 + 姓氏
  3. 从搜索结果中打开第三个链接并验证页面标题

代码是:

package sample;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class search_demo {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.gecko.driver","C:\\Users\\HP\\Downloads\\geckodriver-v0.26.0-win64\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.navigate().to("http://www.google.com/"); 
        driver.manage().window().maximize();

        // Click on the search text box and send value  
        WebElement element= driver.findElement(By.name("q"));
        element.sendKeys("Nishtha tiwari"); 
        element.submit();

        WebDriverWait wait = new WebDriverWait(driver,20);
        WebElement elem=  wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#rso > div:nth-child(3) > div > div:nth-child(1) > div > div > div.r > a")));
        elem.click(); 

        driver.navigate().to("https://in.linkedin.com/in/nishtha-tiwari-40a95281");
        Thread.sleep(2000);
        String title =driver.getTitle();
        System.out.println("Page title is:" + title);

        String expectedTitle = "LinkedIn: Log In or Sign Up ";


        if (title.equals(expectedTitle))
            System.out.println("Test Passed!");
        else  System.out.println("Test Failed");

        driver.close();
    }

}

最佳答案

当我查看您的代码并检查链接页面的页面源时,我发现标题以结尾 这就是它总是失败的原因。

enter image description here

解决方案:

因此,要摆脱这种情况,您有两种选择。

选项 1:从字符串中删除所有  

String title =driver.getTitle();
title = title.replaceAll("&"+"nbsp;", " "); 
title = title.replaceAll(String.valueOf((char) 160), " ");
System.out.println("Page title is:" + title);

String expectedTitle = "LinkedIn: Log In or Sign Up";

    if (title.trim().equalsIgnoreCase(expectedTitle))
        {
            System.out.println("Test Passed!");
        }
    else  
        {
            System.out.println("Test Failed");
        }

选项 2:使用 contains 代替。

String title =driver.getTitle();
System.out.println("Page title is:" + title);

String expectedTitle = "LinkedIn: Log In or Sign Up";

    if (title.contains(expectedTitle))
        {
            System.out.println("Test Passed!");
        }
    else  
        {
            System.out.println("Test Failed");
        }

关于java - 在 selenium 中测试脚本以验证网页标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59631399/

相关文章:

java - 如何解析时间范围输入?

Java 7u65 和 7u67 windows webstart 缓存路径错误(Could not load file/URL specified)

java - 重命名前保存更改

java - 从现有 Excel 文件进行编辑会删除其数据

javascript - 向动态创建的元素添加 id

java - 解析 Jsoup 不起作用

python - 如何使用 Selenium 和 Python 等待元素包含属性样式 ="display:none;"

javascript - 如何默认显示工具提示而无需在纯 css 中悬停

python - Selenium python : Can not connect to the Service %s"% self. 路径

java - 我需要 ghostdriver 才能在 java 中使用 selenium 和 phantomjs 吗?