使用Java使用Selenium WebDriver获取页面标题

使用Java使用Selenium WebDriver获取页面标题,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,如何使用Selenium WebDriver和java验证标题标记中的某些文本?如果使用Selenium 2.0/WebDriver,可以调用 或者,如果您想搜索实际的页面源代码。在java中,您可以执行以下操作: if(driver.getTitle().contains("some expected text")) //Pass System.out.println("Page title contains \"some expected text\" "); else

如何使用Selenium WebDriver和java验证标题标记中的某些文本?

如果使用Selenium 2.0/WebDriver,可以调用
或者,如果您想搜索实际的页面源代码。

在java中,您可以执行以下操作:

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\" ");

通过使用JUnit或TestNG框架,您可以很容易地做到这一点。按如下方式执行断言:

String actualTitle = driver.getTitle();
String expectedTitle = "Title of Page";
assertEquals(expectedTitle,actualTitle);
或者


这可以通过Selenium获取页面标题和使用TestNG进行断言来实现

  • 在导入部分导入断言类:

    `import org.testng.Assert;`
    
  • 创建WebDriver对象:

    WebDriver=newfirefoxdriver()

  • 应用此选项可断言页面的标题:

    Assert.assertEquals(“预期的页面标题”,driver.getTitle())


  • 通过使用SeleniumTestNG框架进行断言,您可以很容易地做到这一点

    步骤:

    1.创建Firefox浏览器会话

    2.初始化预期的标题名称

    3.导航到“www.google.com”[根据您的要求,您可以更改]并等待一段时间(15秒)以完全加载页面

    4.使用“driver.getTitle()”获取实际的标题名,并将其存储在字符串变量中

    5.应用如下断言, Assert.assertTrue(actualGooglePageTitlte.equalsIgnoreCase(expectedGooglePageTitle),“页面标题名称不匹配或加载网格中存在问题”)



    可能重复-1:不要为了获得可见性而重新提问。相反,最糟糕的是,尽管这个问题在其他帖子上被回答了好几次,但还是有人很快地试图回答。。不知道为什么人们不先检查db问题,还是ppl搜索太痛苦,而直接问别人让别人帮你搜索会更容易
    `import org.testng.Assert;`
    
    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();
    }           
    }
    }