基於Java的Selenium學習筆記——Assert


用assert來判斷一個表達式,返回true或者false,若表達式返回false,則會導致AssertionError。

注意Eclipse中assert默認是關閉的,需手動開啟,如下圖:

第一個簡單的斷言腳本,判斷頁面title是否與預期一致:

package com.selenium.test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class AssertTitle {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.setProperty("webdriver.chrome.driver", ".\\lib\\chromedriver.exe");
		//啟動Chrome瀏覽器
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		String url = "https://www.baidu.com";
		driver.get(url);
		String current_title = driver.getTitle();
		System.out.println(current_title);
		String expected_title = "百度一下,你就知道";
		//判斷當前頁面title與預期結果是否一致			
		assert expected_title == current_title;
		System.out.println("Pass");
					
		//退出瀏覽器
		driver.quit();
		
		
		

	}

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM