一、概述與入門
1、Selenium介紹
? 使用前需要下載瀏覽器對應(yīng)的Driver,Selenium提供了EdgeDriver和ChromiumDriver兩種驅(qū)動類。需要安裝與本機瀏覽器版本相同的驅(qū)動。
? EdgeDriver下載地址:Microsoft Edge WebDriver - Microsoft Edge Developer
? ChromiumDriver下載地址:CNPM Binaries Mirror (npmmirror.com)
2、導(dǎo)入Maven庫
(1)Selenium
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.4.0</version>
</dependency>
(2)TestNG[用于單元測試]
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
3、TestNG的使用
? 參考博客:https://blog.csdn.net/lovedingd/article/details/106784561
? TestNG是一個java中的開源自動化測試框架,其靈感來自JUnit和NUnit,TestNG還涵蓋了JUnit4整個核心的功能,但引入了一些新的功能,使其功能更強大,使用更方便。
? 其常用注解有:BeforeTest、Test、AfterTest、BeforeClass、AfterClass等
4、第一個Selenium的程序
? 我們編寫了一個單元測試類,通過@Test注解來標注測試方法。
public class TestCase {
@Test
public void openURL(){
//獲取driver路徑
Path driverPath = Paths.get("src","driver","msedgedriver.exe");
//設(shè)置程序環(huán)境
System.setProperty("webdriver.edge.driver",driverPath.toAbsolutePath().toString());
WebDriver driver = new EdgeDriver();
driver.get("https://www.baidu.com");
}
}
? 這里要注意msedgedriver.exe的位置是src/driver/msedgedriver.exe。
二、元素定位方式
? 通過EdgeDriver的實例化對象的findElement()方法來獲取相應(yīng)的元素,并執(zhí)行操作。
1、id屬性定位
? 這里通過By.id(“string”)的方式來獲取元素,并執(zhí)行了sendKeys(“文本內(nèi)容”)和click()兩種操作。
@Test
public void getElementById(){
webDriver.get("https://www.baidu.com");
webDriver.findElement(By.id("kw")).sendKeys("未來村村長");
webDriver.findElement(By.id("su")).click();
}
2、name屬性定位
webDriver.findElement(By.name("wd")).sendKeys("未來村村長");
3、class屬性定位
webDriver.findElement(By.className("s_ipt")).sendKeys("未來村村長");
4、XPath定位?
? XPath 是一門在 XML 文檔中查找信息的語言。XPath 用于在 XML 文檔中通過元素和屬性進行導(dǎo)航。其常用表達式如下:
表達式 | 描述 |
---|---|
nodename | 選取此節(jié)點的所有子節(jié)點 |
/ | 從根節(jié)點選取 |
// | 從任意位置進行匹配 |
@ | 選取屬性 |
[] | 表示謂語,用于查詢某個指定值的節(jié)點 |
* | 匹配任何元素節(jié)點 |
@* | 匹配任何屬性節(jié)點 |
webDriver.findElement(By.xpath("http://*[@id=\"kw\"]")).sendKeys("未來村村長");
? 瀏覽器檢查-選中元素標簽-右鍵-復(fù)制-復(fù)制xpath可快速獲取XPath路徑。
三、事件
1、鼠標滑動
? 首先需要實例化Actions對象,然后傳入WebDriver對象。先找到相應(yīng)的元素,存儲到WebElement對象中,然后通過actions實例化對象去模擬鼠標事件[moveToElement],再執(zhí)行相應(yīng)動作,最后需要perform來開始執(zhí)行動作。
public void getElementById(){
webDriver.get("https://www.baidu.com");
Actions actions = new Actions(webDriver);
WebElement element1 = webDriver.findElement(By.xpath("http://*[@id=\"s-top-left\"]/div/a"));
actions.moveToElement(element1);
WebElement element2 = webDriver.findElement(By.xpath("http://*[@id=\"s-top-more\"]/div[3]/a[3]/img"));
actions.moveToElement(element2).click();
actions.perform();
}
2、瀏覽器窗口切換
? 瀏覽器窗口切換首先需要通過webDriver的getWindowHandle()方法獲取窗口定位字符串,然后通過webDriver的switchTo().window(定位字符串)進行切換。
@Test
public void getElementById(){
webDriver.get("https://www.baidu.com");
String baidu = webDriver.getWindowHandle();
webDriver.findElement(By.xpath("http://*[@id=\"s-top-left\"]/a[3]")).click();
Set<String> windows = webDriver.getWindowHandles();
webDriver.switchTo().window(baidu);
}
3、iframe窗口切換
? 當(dāng)網(wǎng)頁內(nèi)嵌了iframe時,需要先切換到相應(yīng)的iframe才能執(zhí)行操作。iframe頁面也是一個WebElement元素,可以通過findElement()方法獲取,然后通過switchTo().frame(iframe)進行切換。文章來源:http://www.zghlxwxcb.cn/news/detail-531973.html
WebElement iframe = webDriver.findElement(By.id("iframe-id"));
webDriver.switchTo().frame(iframe);
4、設(shè)置等待時間
(1)最長等待時間
? 當(dāng)頁面加載較慢時,我們可以先進行等待,該等待使后序操作執(zhí)行回旋,直到執(zhí)行成功則放棄等待時間。文章來源地址http://www.zghlxwxcb.cn/news/detail-531973.html
webDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
(2)確定等待時間
WebDriverWait wait = new WebDriverWait(webDriver,Duration.ofSeconds(10));
WebElement element = wait.until(ExceptedConditions.presenceOfElementLocated(By.id("xxx")));
到了這里,關(guān)于【測試開發(fā)之路】Java & Selenium自動化的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!