Use of Selenium common API - Get information assertion

Whether doing functional testing or automated testing, the final step required to get the actual results compared to the expected. This comparison is called assertions.
We usually can be asserted by obtaining title, URL and text information. The method already mentioned in the foregoing text, it is text information for acquiring the tag between pairs.

  • getTitle (): used to obtain the title of the current page.
  • getCurrentUrl (): user obtain the URL of the current page.
  • getText () Gets a page of text information.

Here likewise Baidu, for example, describes how to obtain such information.

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
 
 
public class AssertDemo {
 
  public static void main(String[] args) throws InterruptedException {
 
      WebDriver driver = new ChromeDriver();
      driver.get("https://www.baidu.com");
 
      System.out.println("Search before================");
 
      //获取当前的 title 和 url
      System.out.printf("title of current page is %s\n", driver.getTitle());
      System.out.printf("url of current page is %s\n", driver.getCurrentUrl());
 
      //百度搜索
      WebElement search = driver.findElement(By.id("kw"));
      search.sendKeys("Selenium");
      search.sendKeys(Keys.ENTER);
      Thread.sleep(2000);
 
      System.out.println("Search after================");
 
      //获取当前的 title 和 url
      System.out.printf("title of current page is %s\n", driver.getTitle());
      System.out.printf("url of current page is %s\n", driver.getCurrentUrl());
 
      //获取第一条搜索结果的标题
      WebElement result = driver.findElement(By.xpath("//div[@id='content_left']/div/h3/a"));
      System.out.println(result.getText());
 
      driver.quit();
  }
}

Print Results:

Search before================
title of current page is 百度一下, 你就知道
url of current page is https://www.baidu.com/
 
Search after================
title of current page is Selenium_百度搜索
url of current page is
https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd=Selenium&rsv_pq=9be
4680700a485c1&rsv_t=e925U%2F%2B9SBTqmRI%2BuARg0%2BTCzrrZWn4jOBJkb1OS2vUjMrZsq5VblQ7toD8
&rqlang=cn&rsv_enter=1&rsv_sug3=8&rsv_sug2=0&inputT=155&rsv_sug4=155
Selenium - Web Browser Automation

Guess you like

Origin www.cnblogs.com/zhizhao/p/11303202.html