網(wǎng)站建設(shè)優(yōu)化文檔網(wǎng)站外包
目錄
自動(dòng)化測(cè)試定義
為什么選擇selenium來作為我們web自動(dòng)化測(cè)試的工具?
自動(dòng)化測(cè)試定位元素
使用cssSelector定位
使用XPath 定位
操作測(cè)試對(duì)象?
模擬手動(dòng)從鍵盤輸入
點(diǎn)擊對(duì)象
獲取頁(yè)面文本
?清除對(duì)象輸入的文本內(nèi)容
添加等待(三種方式)
強(qiáng)制等待sleep()
隱式等待
顯式等待
打印信息
瀏覽器操作
瀏覽器窗口大小設(shè)置
瀏覽器前進(jìn)、后退設(shè)置
鍵盤事件
鼠標(biāo)事件
上傳文件操作
屏幕截圖
自動(dòng)化測(cè)試定義
為什么選擇selenium來作為我們web自動(dòng)化測(cè)試的工具?
- 首先selenium是免費(fèi)開源的。
- 支持多瀏覽器例如,chrome,firefox,edge,ie等。
- 支持多系統(tǒng)例如:Linux,windows,macos等。
- 支持多語言,例如java,python等。
- selenium提供了很多可使用的API。
自動(dòng)化測(cè)試定位元素

使用cssSelector定位
????????CSS 使用選擇器來為頁(yè)面元素綁定屬性,CSS 的比較靈活可以選擇控件的任意屬性,通過findElement(By.cssSelector())函數(shù)來使用。
? ? ? ? 以百度輸入框?yàn)槔?#xff1a;
- 獲取百度瀏覽器的網(wǎng)址:https://www.baidu.com/? ?
- 找到百度輸入框的cssSelector的定位元素(使用chrome的F12開發(fā)者模式,點(diǎn)擊百度輸入框,復(fù)制selector),輸入關(guān)鍵詞“迪麗熱巴”
- 找到百度的點(diǎn)擊按鈕并點(diǎn)擊
代碼如下
package com.javazidonghau;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class firstAutoTest {//第一個(gè)簡(jiǎn)單的自動(dòng)化示例public void dilireba() throws InterruptedException {//創(chuàng)建一個(gè)驅(qū)動(dòng)對(duì)象來打開瀏覽器ChromeDriver driver = new ChromeDriver();Thread.sleep(3000);//輸入百度瀏覽器網(wǎng)站driver.get("https://www.baidu.com");Thread.sleep(3000);//找到百度輸入框,并輸入關(guān)鍵詞"迪麗熱巴"driver.findElement(By.cssSelector("#kw")).sendKeys("迪麗熱巴");Thread.sleep(3000);//找到百度一下按鈕,并點(diǎn)擊driver.findElement(By.cssSelector("#su")).click();Thread.sleep(3000);//釋放掉驅(qū)動(dòng)對(duì)象并關(guān)閉瀏覽器driver.quit();}
}
?
使用XPath 定位
//找到百度輸入框,并輸入關(guān)鍵詞"楊洋"driver.findElement(By.xpath("//*[@id=\"kw\"]")).sendKeys("楊洋");Thread.sleep(3000);//找到百度一下按鈕,并點(diǎn)擊driver.findElement(By.xpath("//*[@id=\"su\"]")).click();Thread.sleep(3000);//釋放掉驅(qū)動(dòng)對(duì)象并關(guān)閉瀏覽器driver.quit();
?
操作測(cè)試對(duì)象?
? ? ? ?前面講到了不少知識(shí)都是定位元素,定位只是第一步,定位之后需要對(duì)這個(gè)元素進(jìn)行操作。是鼠標(biāo)點(diǎn)擊還是鍵盤輸入,或者清除元素的內(nèi)容,或者提交表單等。這個(gè)取決于定位元素需要進(jìn)行的下一步操作。
? ? ? ? web自動(dòng)化測(cè)試中比較常用的操作對(duì)象的方法有下面幾個(gè):
- sendKeys()? ? ?模擬手動(dòng)從鍵盤輸入
- click()? ? ? 點(diǎn)擊對(duì)象
- getText()? ? ?獲取頁(yè)面文本
- clear()? ? ?? 清除對(duì)象輸入的文本內(nèi)容
模擬手動(dòng)從鍵盤輸入
driver.findElement(By.xpath("//*[@id=\"kw\"]")).sendKeys("楊洋");
sendKeys方法可以模擬我們用戶從鍵盤輸入
點(diǎn)擊對(duì)象
driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
click() 方法可以用于點(diǎn)擊一個(gè)按鈕
獲取頁(yè)面文本
String s1= driver.findElement(By.xpath("//*[@id=\"hotsearch-content-wrapper\"]/li[1]/a/span[2]")).getText();System.out.println(s1);
????????getText()方法可以用來獲取頁(yè)面的文本信息,但不能獲取定位元素的屬性值,如果想要獲取屬性值就可以使用getAttribute()來進(jìn)行獲取
舉例:獲取百度熱搜第一個(gè)文本頁(yè)面?
package com.javazidonghau;import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;public class firstAutoTest {public void dilireba() throws InterruptedException {//創(chuàng)建一個(gè)驅(qū)動(dòng)對(duì)象來打開瀏覽器ChromeDriver driver = new ChromeDriver();Thread.sleep(3000);//輸入百度瀏覽器網(wǎng)站driver.get("https://www.baidu.com");//找到百度頁(yè)面的對(duì)應(yīng)文本的xpath定位String s1= driver.findElement(By.xpath("//*[@id=\"hotsearch-content-wrapper\"]/li[1]/a/span[2]")).getText();Thread.sleep(3000);System.out.println(s1);//釋放掉驅(qū)動(dòng)對(duì)象并關(guān)閉瀏覽器driver.quit();}
}
?清除對(duì)象輸入的文本內(nèi)容
driver.findElement(By.xpath("//*[@id=\"kw\"]")).clear();
clear()可以清除輸入框輸入的文本信息
添加等待(三種方式)
強(qiáng)制等待sleep()
Thread.sleep(3000);//3000毫秒即3秒
sleep()表示設(shè)置一個(gè)固定的等待時(shí)間,如果我們的頁(yè)面已經(jīng)提前全部加載完畢,也必須等待固定設(shè)
置的時(shí)間才能進(jìn)行下一步。如果涉及過多的測(cè)試用例,則會(huì)十分影響自動(dòng)化測(cè)試效率。
舉例:未設(shè)置等待時(shí)間發(fā)生異常
public class firstAutoTest {//第一個(gè)簡(jiǎn)單的自動(dòng)化示例public void dilireba() throws InterruptedException {//創(chuàng)建一個(gè)驅(qū)動(dòng)對(duì)象來打開瀏覽器ChromeDriver driver = new ChromeDriver();//輸入百度瀏覽器網(wǎng)站driver.get("https://www.baidu.com");//找到百度輸入框,并輸入關(guān)鍵詞"楊洋"driver.findElement(By.cssSelector("#kw")).sendKeys("楊洋");driver.findElement(By.cssSelector("#su")).click();//Thread.sleep(3000);driver.findElement(By.cssSelector("#\\\\34 > div > div:nth-child(1) > h3"));//釋放掉驅(qū)動(dòng)對(duì)象并關(guān)閉瀏覽器driver.quit();}
}
????????當(dāng)在輸入和點(diǎn)擊操作完成后,未設(shè)置一個(gè)等待時(shí)間又開始定位下一個(gè)元素,此時(shí)發(fā)現(xiàn)運(yùn)行異常報(bào)錯(cuò),報(bào)錯(cuò)原因是程序執(zhí)行的速度很快,當(dāng)上一個(gè)操作的頁(yè)面渲染還沒有結(jié)束,下一個(gè)操作又要開始定位元素導(dǎo)致未找到定位元素,于是發(fā)生異常。
? ? ? ? 解決方案:設(shè)置一個(gè)等待時(shí)間
public class firstAutoTest {//第一個(gè)簡(jiǎn)單的自動(dòng)化示例public void dilireba() throws InterruptedException {//創(chuàng)建一個(gè)驅(qū)動(dòng)對(duì)象來打開瀏覽器ChromeDriver driver = new ChromeDriver();//輸入百度瀏覽器網(wǎng)站driver.get("https://www.baidu.com");//找到百度輸入框,并輸入關(guān)鍵詞"楊洋"driver.findElement(By.cssSelector("#kw")).sendKeys("楊洋");driver.findElement(By.cssSelector("#su")).click();Thread.sleep(3000);//強(qiáng)制等待driver.findElement(By.cssSelector("#\\\\34 > div > div:nth-child(1) > h3"));//釋放掉驅(qū)動(dòng)對(duì)象并關(guān)閉瀏覽器driver.quit();}
}
? ? ? ??
隱式等待
? ? ? ? sleep()當(dāng)遇到許多測(cè)試用例時(shí),會(huì)大大降低測(cè)試效率,此時(shí)隱式等待的作用就比設(shè)置sleep()比較智能。隱式等待設(shè)置的等待時(shí)間范圍內(nèi),輪詢等待元素,如果元素出現(xiàn)之后就立即結(jié)束進(jìn)行下一步,不需要等待時(shí)間結(jié)束。
? ? ? ? 隱式等待節(jié)省了大量的等待時(shí)間,元素展示之后,就可以直接執(zhí)行下一步。如果在規(guī)定的時(shí)間范圍內(nèi),元素仍然沒有出現(xiàn),則會(huì)拋出一個(gè)異?!綨oSuchElementException】,停止運(yùn)行
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(3000));
public class firstAutoTest {//第一個(gè)簡(jiǎn)單的自動(dòng)化示例public void dilireba() throws InterruptedException {//創(chuàng)建一個(gè)驅(qū)動(dòng)對(duì)象來打開瀏覽器ChromeDriver driver = new ChromeDriver();//輸入百度瀏覽器網(wǎng)站driver.get("https://www.baidu.com");//找到百度輸入框,并輸入關(guān)鍵詞"楊洋"driver.findElement(By.cssSelector("#kw")).sendKeys("楊洋");driver.findElement(By.cssSelector("#su")).click();// Thread.sleep(3000); //強(qiáng)制等待driver.manage().timeouts().implicitlyWait(Duration.ofMillis(3000));//隱式等待driver.findElement(By.cssSelector("#\\\\34 > div > div:nth-child(1) > h3"));//釋放掉驅(qū)動(dòng)對(duì)象并關(guān)閉瀏覽器driver.quit();}
}
顯式等待
????????顯示等待需要使用到?selenium 里中的一個(gè)類 ExpectedConditions,以及?until 方法。
//點(diǎn)擊后出現(xiàn)彈窗,顯示登錄成功
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(3));
//出現(xiàn)彈窗之后,才開始停止等待
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#\\34 > div >div:nth-child(1) > h3")));
???顯示等待通過使用WebDriverWait類來傳入兩個(gè)參數(shù):瀏覽器驅(qū)動(dòng)driver和等待時(shí)間Duration.ofSeconds()
until方法中 ExpectedConditions表示預(yù)期條件,presenceOfElementLocated()表示定位元素
public class firstAutoTest {//第一個(gè)簡(jiǎn)單的自動(dòng)化示例public void dilireba() throws InterruptedException {//創(chuàng)建一個(gè)驅(qū)動(dòng)對(duì)象來打開瀏覽器ChromeDriver driver = new ChromeDriver();//輸入百度瀏覽器網(wǎng)站driver.get("https://www.baidu.com");//找到百度輸入框,并輸入關(guān)鍵詞"楊洋"driver.findElement(By.cssSelector("#kw")).sendKeys("楊洋");driver.findElement(By.cssSelector("#su")).click();// Thread.sleep(3000); //強(qiáng)制等待// driver.manage().timeouts().implicitlyWait(Duration.ofMillis(3000));//隱式等待//傳入驅(qū)動(dòng)和等待的時(shí)間WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(3));//顯示等待//等待的條件wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#\\34 > div > div:nth-child(1) > h3")));driver.quit();}
}
打印信息
public class firstAutoTest {public void dilireba() throws InterruptedException {//創(chuàng)建一個(gè)驅(qū)動(dòng)對(duì)象來打開瀏覽器ChromeDriver driver = new ChromeDriver();String s1 =driver.getTitle();//獲取當(dāng)前標(biāo)題:百度一下,你就知道String s2 = driver.getCurrentUrl();//獲取當(dāng)前的url:https://www.baidu.comSystem.out.println(s1);System.out.println(s2);}
}
瀏覽器操作
瀏覽器窗口大小設(shè)置
public void window() throws InterruptedException {driver.manage().window().maximize();//將窗口設(shè)置為最大Thread.sleep(2000);driver.manage().window().minimize();//將窗口設(shè)置為最小Thread.sleep(2000);driver.manage().window().setSize(new Dimension(500,500));//自定義大小}
瀏覽器前進(jìn)、后退設(shè)置
driver.navigate().back();//后退Thread.sleep(3000);driver.navigate().forward();//前進(jìn)Thread.sleep(3000);
鍵盤事件
????????從鍵盤輸入就需要使用sendKeys(),可以來輸入鍵盤的按鍵和組合鍵。WebElement 提供了所有的Keys()方法,需要導(dǎo)入此包:
import org.openqa.selenium.WebElement;
public void Test() throws InterruptedException {ChromeDriver driver = new ChromeDriver();driver.get("https://www.baidu.com");WebElement web= driver.findElement(By.cssSelector("#kw"));web.sendKeys("我們都要好好的");Thread.sleep(2000);web.sendKeys(Keys.ESCAPE);//回退鍵web.sendKeys(Keys.ENTER);//回車鍵web.sendKeys(Keys.SPACE);//空格鍵web.sendKeys(Keys.TAB);//TAB鍵web.sendKeys(Keys.CONTROL,"A");//ctrl+A 全選web.sendKeys(Keys.CONTROL,"C");//ctrl+c 復(fù)制web.sendKeys(Keys.CONTROL,"V");//ctrl+V 粘貼web.sendKeys(Keys.CONTROL,"X");//ctrl+x 剪切Actions action=new Actions(driver);action.sendKeys(Keys.F5);//按F5鍵刷新Thread.sleep(2000);driver.quit();}
?
鼠標(biāo)事件
上傳文件操作
public void test1() throws InterruptedException {ChromeDriver driver = new ChromeDriver();//進(jìn)入百度網(wǎng)站driver.get("https://www.baidu.com");driver.findElement(By.xpath("//span[@class='soutu-btn']")).click();//上傳按鈕為input標(biāo)簽,定位上傳按鈕WebElement element = driver.findElement(By.xpath("//input[@class='upload-pic']"));Thread.sleep(2000);//輸入文件路徑,上傳文件element.sendKeys("D:\\download\\test.jpg");Thread.sleep(2000);driver.quit();
}
屏幕截圖
public void dilireba() throws InterruptedException, IOException {//創(chuàng)建一個(gè)驅(qū)動(dòng)對(duì)象來打開瀏覽器ChromeDriver driver = new ChromeDriver();Thread.sleep(3000);//輸入百度瀏覽器網(wǎng)站driver.get("https://www.baidu.com");Thread.sleep(3000);//找到百度輸入框,并輸入關(guān)鍵詞"楊洋"driver.findElement(By.cssSelector("#kw")).sendKeys("楊洋");Thread.sleep(3000);//找到百度一下按鈕,并點(diǎn)擊driver.findElement(By.cssSelector("#su")).click();Thread.sleep(3000);//屏幕截圖File file = driver.getScreenshotAs(OutputType.FILE);//將截圖文件保存在指定路徑下File filename = new File("./src/test/my.png");FileUtils.copyFile(file,filename);//在Maven需要導(dǎo)入FileUtils的依賴driver.findElement(By.cssSelector("#head_wrapper"));//釋放掉驅(qū)動(dòng)對(duì)象并關(guān)閉瀏覽器driver.quit();}
?