一、API操作代碼實體
1.1 訪問網址
@Test
public void visitURL() {
String baseUrl = "http://www.sogou.com";
driver.get(baseUrl);
// 或者
driver.navigate().to(baseUrl);
}
1.2 回傳上/下一頁,重繪
@Test
public void visitURL() {
String url1 = "http://www.sogou.com";
String url2 = "http://www.baidu.com";
driver.navigate().to(url1);
driver.navigate().to(url2);
// 回傳上一頁
driver.navigate().back();
// 前進下一頁
driver.navigate().forward();
// 重繪
driver.navigate.refresh();
}
1.3 操作瀏覽器視窗
@Test
public void operateBrowser() {
Point point = new Point(150, 150);
Dimension dimension = new Dimension(500, 500);
// 設定瀏覽器在螢屏上的位置
driver.manage().window().setPosition(point);
// 設定瀏覽器視窗大小
driver.manage().window().setSize(dimension);
// 獲取瀏覽器在螢屏的位置
System.out.println(driver.manage().window().getPosition());
// 獲取視窗的大小
System.out.println(driver.manage().window().getSize());
// 視窗最大化
driver.manage().window().maximize();
}
1.4 獲取頁面title屬性/源代碼/URL
@Test
public void testweb() {
driver.get("http://www.sogou.om");
// 獲取頁面的title屬性
String title = driver.getTitle();
// 獲取頁面源代碼
String pageSource = driver.getPageSource();
// 斷言頁面源代碼包含“購物”關鍵字
Assert.assertTrue(pageSource.contains("購物"));
// 獲取當前頁面的URL
String currentPageUrl = driver.getCurrentUrl();
}
1.5 清除文本框,再輸入內容
@Test
public void testweb() {
WebElement input = driver.findElement(By.id("test"));
// 清除文本框
input.clear();
// 輸入指定內容
String text = "測驗內容";
input.sendKeys(text);
}
1.6 單擊按鈕/雙擊元素
@Test
public void testweb() {
WebElement button = driver.findElement(By.id("button"));
// 單擊按鈕
button.click();
// 雙擊元素
WebElement inputBox = driver.findElement(By.id("inputBox"));
Actions builder = new Actions(driver);
builder.doubleClick(inputBox).build().perform();
}
1.7 操作單選下拉串列
@Test
public void testweb() {
Select dropList = new Select(driver.findElement(By.name("fruit"));
// 斷言下拉串列不支持多選
Assert.assertFalse(dropList.isMultiple());
// 斷言當前選中文本
Assert.assertEquals("桃子", dropList.getFirstSelectedOption().getText());
// 選中第四項
dropList.selectByIndex(3);
// 通過value值進行選中操作
dropList.selectByValue("山楂");
// 通過選項文字進行選中操作
dropList.selectByVisibleText("荔枝");
}
1.8 操作多選選擇串列
@Test
public void testweb() {
Select dropList = new Select(driver.findElement(By.name("fruit"));
// 斷言支持多選
Assert.assertTrue(dropList.isMultiple());
// 可通過索引、value、visibleText進行選中操作,與單選相同
// 取消所有選中狀態
dropList.deselectAll();
// 取消操作與選中操作相似,只是在方法前面加上de
// 通過索引取消選中第四項
dropList.deselectByIndex(3);
}
1.9 執行js
package cn.amnotgcs;
import org.testng.annotations.Test;
import org.testng.reporters.Files;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import java.io.File;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
public class NewTest {
@Test
public void f() {
WebDriver driver = new EdgeDriver();
driver.get("http://www.sogou.com");
JavascriptExecutor js = (JavascriptExecutor) driver;
String title = (String) js.executeScript("return document.title");
Assert.assertTrue(title.contains("搜狗"));
String searchButtonText = (String) js.executeScript("var button = document.getElementById('stb');return button.value");
System.out.println(searchButtonText);
driver.quit();
}
@BeforeMethod
public void BeforeMethod() {
}
}
1.10 模擬滑鼠鍵盤操作
@Test
public void testweb() {
driver.get("http://www.sogou.com");
Actions action = new Actions(driver);
// 模擬鍵盤操作
action.keyDown(Keys.CONTROL);
action.keyDown(Keys.SHIFT);
action.keyUp(Keys.CONTROL);
actoin.keyDown(Keys.SHIFT).sendKeys("abcdef").perform();
// 模擬滑鼠右鍵
action.contextClick(driver.findElement(By.id("query"))).perform();
// 滑鼠懸浮
WelElement link1 = driver.findElement(By.id("linkaaa");
action.moveToElement(link1).perform();
// 滑鼠按住不動
action.clickAndHold(link1).perform();
// 釋放滑鼠
action.release(link1).perform();
}
1.11 獲取元素屬性
// 獲取元素的value屬性
<元素>.getAttribute("value");
// 獲取元素css的width屬性
<元素>.getCssValue("width");
1.12 等待
// 隱式等待
// 沒有立刻找到元素時,會每隔一段時間重新查找,超過最長等待時間則報錯
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// 顯式等待
// 顯式等待更節約時間,推薦使用
// 設定最長等待時間
WebDriverWait wait = new WebDriverWait(driver, 10);
// 判斷p標簽是否在頁面中
wait.until(ExpectedConditions.presenceOFElementLocated(By.xpath("//p")));
// 判斷物件中是否包含某些關鍵字
wait.until(ExpectedConditions.textToBePresentInElement(p, "某些關鍵字"));
// 自定義的顯式等待
@Test
public void testweb() {
try{
// 判斷food元素是否包含“愛吃”關鍵字
Boolean containTextFlag = (new WebDriverWait(driver, 10))
.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver d) {
return d.findElement(By.id("food")).getText().contains("愛吃");
}
});
} catch(NoSuchElementException e) {
Assert.fail("沒找到");
}
}
其他顯示等待方法:
| 等待條件 | WebDriver方法 |
|---|---|
| 元素是否可用或可被單擊 | elementToBeClickAble(By locator) |
| 元素處于選中狀態 | elementToBeSelected(WebElement element) |
| 元素存在 | presenceOfElementLocated(By locator) |
| 元素包含某些文字 | textToBePresentInElement(By locator) |
| 元素頁面值 | textToBePresentInElementValue(Bylocator String) |
| 標題 | titleContains(String title) |
二、新視窗及彈窗處理
2.1 使用標題識別新視窗
2.2 使用頁面文字識別新視窗
2.3 操作JavaScript的Alert/confirm/prompt彈窗
2.4 操作frame
2.5 操作cookies
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/162994.html
標籤:其他
下一篇:Git常用命令
