自動化測試相關(guān)理論
什么是自動化測試
將人工的測試手段進(jìn)行轉(zhuǎn)換,讓代碼去執(zhí)行。
自動化分類:
- 單元測試
- 接口測試
- UI自動化測試
selenium
selenium 是什么
selenium 是web應(yīng)用中基于UI的自動化測試框架。
selenium 特點
支持多平臺、多瀏覽器、多語言、有豐富的API
工作原理
實操(selenium + Junit)
selenium + Java 環(huán)境搭建
public class Main {
public static void main(String[] args) {
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*"); //允許所有請求
WebDriver webDriver = new ChromeDriver(options);
webDriver.get("https://www.baidu.com"); //打開百度首頁
//找到百度搜索輸入框 通過CSS 選擇器查找定位元素
// WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));
//通過 xpath 查找定位元素
WebElement element = webDriver.findElement(By.xpath("http://*[@id=\"kw\"]"));
//輸入軟件測試
element.sendKeys("軟件測試");
}
}
selenium 常用api
定位元素
定位元素:findElement
通過CSS 選擇器查找定位元素By.cssSelector()
WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));
通過 xpath
查找定位元素
WebElement element = webDriver.findElement(By.xpath("http://*[@id=\"kw\"]"));
CSS選擇器語法
- id選擇器:#id
- 類選擇器:.class
- 標(biāo)簽選擇器:標(biāo)簽名
- 后代選擇器:父級選擇器 子級選擇器
xpath定位元素
絕對路徑:/html/head/title 【不常用】
相對路徑:雙斜杠開頭 //
- 相對路徑 + 索引:
//form/span[1]/input
- 相對路徑 + 屬性值:
//input[@class="s_ipt"]
- 相對路徑 + 通配符:
//*[@*="su"]
- 相對路徑 + 文本匹配:
//a[text()="新聞]"
CSS選擇器和xpath定位元素哪個更好?
CSS選擇器定位元素效率更高
常用的操作測試對象
- click:點擊對象
- sendKeys:在對象上模擬按鍵輸入
- clear:清除對象輸入的文本內(nèi)容
- submit:提交
- 如果點擊的元素放在form標(biāo)簽中,此時使用submit實現(xiàn)的效果和click是一樣的
- 如果點擊的元素放在非form標(biāo)簽中,此時使用submit會報錯
- getText:用于獲取元素的文本信息.無法獲取元素對應(yīng)的屬性值,只能獲取文本內(nèi)容
- getAttribute:可以獲取元素對應(yīng)的屬性值
click、sendKeys
private static void test01() throws InterruptedException {
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*"); //允許所有請求
WebDriver webDriver = new ChromeDriver(options);
webDriver.get("https://www.baidu.com"); //打開百度首頁
//找到百度搜索輸入框 通過CSS 選擇器查找定位元素
// WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));
//通過 xpath 查找定位元素
WebElement element = webDriver.findElement(By.xpath("http://*[@id=\"kw\"]"));
//輸入軟件測試
element.sendKeys("軟件測試");
// 找到百度一下按鈕 點擊
webDriver.findElement(By.cssSelector("#su")).click();
sleep(3000);
// 校驗
// 找到搜索結(jié)果
int flg = 0;
List<WebElement> elements = webDriver.findElements(By.cssSelector("a em"));
for (int i = 0; i < elements.size(); i++) {
// System.out.println(elements.get(i).getText());
//如果返回的結(jié)果有軟件測試,就證明測試通過,否則測試不通過
if (elements.get(i).getText().contains("軟件測試")) {
flg = 1;
System.out.println("測試通過");
break;
}
}
if (flg == 0) {
System.out.println("測試不通過");
}
}
submit
//test03()會報錯,因為這里使用submit點擊了百度新聞的超鏈接,這個超鏈接沒有在form表單中,
private static void test03() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver = new ChromeDriver(options);
webDriver.get("https://www.baidu.com");
// webDriver.findElement(By.xpath("http://a[text()=\"新聞\"]")).submit();
webDriver.findElement(By.xpath("http://a[text()=\"新聞\"]")).click();
}
clear
//clear 清除對象輸入的文本按鈕
private static void test02() throws InterruptedException {
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*"); //允許所有請求
WebDriver webDriver = new ChromeDriver(options);
webDriver.get("https://www.baidu.com"); //打開百度首頁
//找到百度搜索輸入框,輸入Java
webDriver.findElement(By.cssSelector("#kw")).sendKeys("java");
//點擊了百度一下按鈕
webDriver.findElement(By.cssSelector("#su")).click();
// webDriver.findElement(By.cssSelector("#su")).submit();
sleep(2000);
// 清空百度搜索輸入框中的數(shù)據(jù)
webDriver.findElement(By.cssSelector("#kw")).clear();
}
getText、getAttribute
private static void test04() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver = new ChromeDriver(options);
webDriver.get("https://www.baidu.com");
//getText 無法獲取元素對應(yīng)的屬性值,只能獲取文本內(nèi)容
//getAttribute 可以獲取元素對應(yīng)的屬性值
// String buttomValue = webDriver.findElement(By.cssSelector("#su")).getText();
String buttomValue = webDriver.findElement(By.cssSelector("#su")).getAttribute("value");
// System.out.println(buttomValue);
if (buttomValue.equals("百度一下")) {
System.out.println("測試通過");;
} else {
System.out.println(buttomValue);
System.out.println("測試不通過");
}
}
等待
- 強(qiáng)制等待:
sleep()
- 智能等待
- 隱式等待:
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
參數(shù)1是時間,參數(shù)2是單位 - 顯示等待:
- 隱式等待:
WebDriverWait wait = new WebDriverWait(webDriver, 3000);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#s-hotsearch-wrapper > div > a.hot-title > div > i:nth-child(1)")));
隱式等待:等待的是所有的元素
顯示等待:等待的是一定的條件(程序員自己設(shè)定的條件)
假設(shè)等待3天時間
強(qiáng)制等待:一直等待 等待的時間為3天
隱式等待:最長等待3天時間,如果在3天之內(nèi)獲取到頁面上的元素,此時執(zhí)行下面的代碼;
如果等待3天時間還是沒有找到這個元素,此時報錯
信息獲取
-
getTitle()
:打印標(biāo)題 -
getCurrentUrl()
:打印url
private static void test05() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
WebDriver webDriver = new ChromeDriver(options);
webDriver.get("https://www.baidu.com");
String url = webDriver.getCurrentUrl();
String title = webDriver.getTitle();
if (url.equals("https://www.baidu.com/") && title.equals("百度一下,你就知道")) {
System.out.println("當(dāng)前頁面:" + url);
System.out.println("當(dāng)前頁面title:" + title);
System.out.println("測試通過");
} else {
System.out.println("測試不通過");
}
}
瀏覽器操作
瀏覽器前進(jìn)后退
private static void test07() throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
//打開百度首頁
webDriver.get("https://www.baidu.com");
//搜索521
webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
webDriver.findElement(By.cssSelector("#su")).click();
//強(qiáng)制等待3秒
sleep(3000);
//瀏覽器后退
webDriver.navigate().back(); // 后退
sleep(3000);
//強(qiáng)制等待3秒
webDriver.navigate().refresh(); //刷新
//瀏覽器前進(jìn)
sleep(3000);
webDriver.navigate().forward(); // 前進(jìn)
}
瀏覽器滾動條
瀏覽器滾動條的控制需要依靠js腳本
((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
瀏覽器窗口大小
private static void test07() throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
//打開百度首頁
webDriver.get("https://www.baidu.com");
//搜索521
webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
webDriver.findElement(By.cssSelector("#su")).click();
//強(qiáng)制等待3秒
sleep(3000);
((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
sleep(3000);
webDriver.manage().window().maximize(); //瀏覽器最大化
sleep(3000);
webDriver.manage().window().fullscreen(); //全屏
sleep(3000);
webDriver.manage().window().setSize(new Dimension(600, 1000)); //設(shè)置窗口大小
}
鍵盤操作
//進(jìn)行 全選 剪切 粘貼
private static void test08() throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
//打開百度首頁
webDriver.get("https://www.baidu.com/");
//搜索521
webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
// ctrl + A
webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "A");
sleep(3000);
// ctrl + X
webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "X");
sleep(3000);
// ctrl + V
webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL, "V");
sleep(3000);
// webDriver.findElement(By.cssSelector("#su")).click();
}
鼠標(biāo)右擊
private static void test09() throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://www.baidu.com");
webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
webDriver.findElement(By.cssSelector("#su")).click();
sleep(3000);
//找到圖片按鈕
WebElement webElement = webDriver.findElement(By.cssSelector("#searchTag > div > div > a.c-color-t.c-line-clamp1.tags_2yHYj.tag-selected_1iG7R > span"));
//鼠標(biāo)右擊
Actions actions = new Actions(webDriver);
sleep(3000);
actions.moveToElement(webElement).contextClick().perform();
}
定位一組元素
webdriver 可以很方便的使用findElement 方法來定位某個特定的對象,不過有時候我們卻需要定位一
組對象,這時候就需要使用findElements 方法。
定位一組對象一般用于以下場景:
- 批量操作對象,比如將頁面上所有的checkbox 都勾上
- 先獲取一組對象,再在這組對象中過濾出需要具體定位的一些對象。比如定位出頁面上所有的
checkbox,然后選擇最后一個
//對應(yīng)test01.html
private static void page01() {
WebDriver webDriver = new ChromeDriver();
webDriver.get("http://localhost:63342/20230512TestCode/src/main/page/test01.html?_ijt=79ohoqao534c22adm6ri7dfaku&_ij_reload=RELOAD_ON_SAVE");
webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS); //隱式等待
List<WebElement> webElements = webDriver.findElements(By.cssSelector("input"));
for (int i = 0; i < webElements.size(); i++) {
// 如果每個元素的 type 值等于 checkbox ,進(jìn)行點擊
// getAttribute 獲取頁面元素上的屬性值
if (webElements.get(i).getAttribute("type").equals("checkbox")) {
webElements.get(i).click();
} else {
//否則什么也不操作
}
}
}
多層窗口定位 (frame切換)
webDriver.switchTo().frame("f1");
獲取frame中的元素
// 多層窗口定位
private static void page02() {
WebDriver webDriver = new ChromeDriver();
webDriver.get("http://localhost:63342/20230512TestCode/src/main/page/test02.html?_ijt=rss48aadnhjt4mgv70r81np9u5&_ij_reload=RELOAD_ON_SAVE");
webDriver.switchTo().frame("f1"); //定位一個frame 值是frame的id
webDriver.findElement(By.cssSelector("body > div > div > a")).click();
}
下拉框
//下拉框
private static void page03() throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("http://localhost:63342/20230512TestCode/src/main/page/test03.html?_ijt=u5qdt3mv62m6hpml2pun3pkni4&_ij_reload=RELOAD_ON_SAVE");
WebElement webElement = webDriver.findElement(By.cssSelector("#ShippingMethod"));
Select select = new Select(webElement);
// select.selectByIndex(1); // 通過下標(biāo)選擇
// sleep(3000);
select.selectByValue("3.20"); // 通過value值選擇
}
對alert操作
// 針對alert操作
private static void page04() throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("http://localhost:63342/20230512TestCode/src/main/page/test04.html?_ijt=puu6ngd14ds8dnofliuvvs3360&_ij_reload=RELOAD_ON_SAVE");
webDriver.findElement(By.cssSelector("button")).click();
sleep(3000);
//alert 彈窗取消
webDriver.switchTo().alert().dismiss();
sleep(3000);
//點擊按鈕
webDriver.findElement(By.cssSelector("button")).click();
//在alert彈窗中輸入
webDriver.switchTo().alert().sendKeys("sss");
sleep(3000);
//alert彈窗確認(rèn)
webDriver.switchTo().alert().accept();
}
文件上傳
//文件上傳
private static void page05() throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("http://localhost:63342/20230512TestCode/src/main/page/test05.html?_ijt=ole7f30h4kb7q9vgs1pmb1k6j0&_ij_reload=RELOAD_ON_SAVE");
sleep(3000);
webDriver.findElement(By.cssSelector("input")).sendKeys("D://log.txt"); //文件路徑
}
關(guān)閉瀏覽器
關(guān)閉瀏覽器quit和close的區(qū)別:
- quit關(guān)閉了整個瀏覽器,close只關(guān)閉了當(dāng)前的頁面
- quit會清空緩存;close不會清空緩存
private static void test10() throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://www.baidu.com/");
webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)"));
sleep(4000);
webDriver.quit();
// webDriver.close();
}
切換窗口
//切換窗口
private static void test11() throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://www.baidu.com/");
//點擊跳轉(zhuǎn)到新界面
webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
sleep(3000);
// 通過 getWindowHandles 獲取所有的窗口句柄
// 通過 getWindowHandle 獲取的get打開的頁面窗口句柄
// System.out.println(webDriver.getWindowHandle());
Set<String> handles = webDriver.getWindowHandles();
String target_handle = "";
for (String handle : handles) {
target_handle = handle; //拿到handles的最后一個元素
}
webDriver.switchTo().window(target_handle); //
//在新的界面輸入框輸入
//之所以找不到元素,是因為元素默認(rèn)是在get打開的這個頁面找元素,需要對頁面進(jìn)行切換
webDriver.findElement(By.cssSelector("#ww")).sendKeys("新聞聯(lián)播");
webDriver.findElement(By.cssSelector("#s_btn_wr")).click(); //點擊搜索
}
截圖
添加maven依賴 commons-io文章來源:http://www.zghlxwxcb.cn/news/detail-470439.html
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
//截圖
private static void test12() throws InterruptedException, IOException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://www.baidu.com/");
webDriver.findElement(By.cssSelector("#kw")).sendKeys("軟件測試");
webDriver.findElement(By.cssSelector("#su")).click();
sleep(3000);
File file = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File("D://20230521.png"));
}
QUESTION
在自動化測試的時候,驗證碼無法獲取,如何操作?
可以在自動化測試賬號加一個白名單,設(shè)置該賬號登陸的時候不需要驗證碼驗證文章來源地址http://www.zghlxwxcb.cn/news/detail-470439.html
到了這里,關(guān)于自動化測試selenium的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!