- 自動化測試步驟
1)取預(yù)期結(jié)果;
2)取實際結(jié)果;
3)斷言:比較1)和2)來判斷測試是否通過;
1 package simplewebtest.test;
2
3 import java.util.concurrent.TimeUnit;
4
5 import org.openqa.selenium.By;
6 import org.openqa.selenium.WebDriver;
7 import org.openqa.selenium.firefox.FirefoxDriver;
8 import org.testng.annotations.Test;
9
10 public class TestBaiduHome {
11
12 @Test
13 public void searchSomething(){
14
15 WebDriver driver=new FirefoxDriver();//打開Firefox; open firefox
16 driver.get("http://www.baidu.com");//打開百度open the url
17 driver.findElement(By.id("kw1")).sendKeys("GitHub");//輸入搜索關(guān)鍵字“GitHub";input search keyword
18 driver.findElement(By.id("su1")).click();//點擊搜索按鈕click the search button
19 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//等頁面加載,10秒內(nèi)不加載成功即報超時。waiting for 10 seconds
20 String aResult=driver.findElement(By.xpath(".//*[@id='4']/h3/a")).getText();//取第四條搜索結(jié)果的標(biāo)題。 get the text of 4th search result
21 assert aResult.contains("GitHub");//做斷言 assertion
22 driver.findElement(By.xpath(".//*[@id='4']/h3/a")).click();//打開第四個搜索結(jié)果。Open the 4th search result on baidu
23 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//等頁面加載,10秒內(nèi)不加載成功即報超時。waiting for 10 seconds
24
25 //獲取所有窗口的handle,然后逐個切換,直到切換到最新窗口 switch to the new window
26 for(String winHandle : driver.getWindowHandles()){
27 driver.switchTo().window(winHandle);
28 }
29
30 String aTitle=driver.getTitle();//取新窗口的title
31 System.out.println("current widnow title is:"+aTitle);//打出來看看
32 assert aTitle.contains("GitHub");//斷言
33
34 }
35 }
-
常用于斷言的三種模式
斷言被用于三種模式: assert 、verify、waitfor
- Assert 失敗時,該測試將終止。
- Verify 失敗時,該測試將繼續(xù)執(zhí)行,并將錯誤記入日顯示屏 。也就是說允許此單個 驗證通過。確保應(yīng)用程序在正確的頁面上。
- Waitfor用于等待某些條件變?yōu)檎?。可用于AJAX應(yīng)用程序的測試。
- 如果該條件為真,他們將立即成功執(zhí)行。如果該條件不為真,則將失敗并暫停測試。直到超過當(dāng)前所設(shè)定的超時時間。 一般跟setTimeout時間一起用
常用的斷言有
| 斷言 | 作用 |
|---|---|
| assertLocation | 判斷當(dāng)前是在正確的頁面 |
| assertTitle | 檢查當(dāng)前頁面的title是否正確 |
| assertValue | 檢查input的值, checkbox或radio,有值為”on”無為”off” |
| assertSelected | 檢查select的下拉菜單中選中是否正確 |
| assertSelectedOptions | 檢查下拉菜單中的選項的是否正確 |
| assertText | 檢查指定元素的文本 |
| assertTextPresent | 檢查在當(dāng)前給用戶顯示的頁面上是否有出現(xiàn)指定的文本 |
| assertTextNotPresent | 檢查在當(dāng)前給用戶顯示的頁面上是否沒有出現(xiàn)指定的文本 |
| assertAttribute | 檢查當(dāng)前指定元素的屬性的值 |
| assertTable | 檢查table里的某個cell中的值 |
| assertEditable | 檢查指定的input是否可以編輯 |
| assertNotEditable | 檢查指定的input是否不可以編輯 |
| assertAlert | 檢查是否有產(chǎn)生帶指定message的alert對話框 |
| waitForElementPresent | 等待檢驗?zāi)吃氐拇嬖?。為真時,則執(zhí)行 |
- 常用的unittest斷言如下
- 相等斷言:assertEqual(self, first, second, msg=None) 常用于title和url的對比來檢查是否與預(yù)期結(jié)果一致;
self.assertEqual(u'用戶 - Delicate Cloud Dashboard', driver.title, 'switch to Admin_Identity_User panel fail')
不等斷言:assertNotEqual(self, first, second, msg=None) 與1相反,此斷言不常用;
True斷言:assertTrue(self, expr, msg=None) 此斷言可用于對尋找某元素的表達式進行判斷,如返回True則通過,否則Fail;
False斷言:assertFalse(self, expr, msg=None) 此斷言可用于對尋找某元素的表達式進行判斷,如返回False則通過,否則Fail;
3和4可以配合使用selenium IDE所生成的[Python](http://lib.csdn.net/base/python "Python知識庫")腳本中is_element_present和is_alert_present來使用;