[技術(shù)探索]Web自動(dòng)化測(cè)試之Selenium

Selenium原理

Selenium 是一個(gè)用于測(cè)試 Web 應(yīng)用程序的自動(dòng)化工具 ,用于模擬用戶操作,主要功能:功能測(cè)試,瀏覽器兼容性測(cè)試等

Selenium WebDriver 簡(jiǎn)稱 WebDriver,即 Selenium 2。

Selenium 安裝配置

指定瀏覽器驅(qū)動(dòng),打開(kāi)瀏覽器

IE:
System.setProperty("webdriver.ie.driver", "<DRIVER_PATH>/IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();

Firefox:
System.setProperty("webdriver.gecko.driver", "<DRIVER_PATH>/geckodriver.exe"); 
System.setProperty("webdriver.firefox.bin","E:/Mozilla Firefox/firefox.exe"); 
WebDriver driver = new FirefoxDriver();

Google:
System.setProperty("webdriver.chrome.driver", "<DRIVER_PATH>/chromedriver.exe"); 
WebDriver driver = new ChromeDriver();

driver.manage.window.maximize()窗口最大化

Selenium元素定位

driver.findElement(By.屬性)

屬性名包括以下內(nèi)容

  • name("值")
  • tagName("標(biāo)簽名")
  • xpath("http://標(biāo)記名[@屬性名='屬性值']")
  • id
  • className : CSS class 屬性的值
  • linkText : <a href>和</a>之間的內(nèi)容
  • partialLinkText : <a href>和</a>之間的部分文本值

driver.findElement(By.屬性)用于查詢符合要求的第一個(gè)元素對(duì)象,主要包括于文本框、密碼框、命令按鈕、單選按鈕

driver.findElements(By.屬性).get(Index)
  • 查詢符合屬性要求的所有對(duì)象;
  • 適用于單選按鈕、復(fù)選框、鏈接等
  • 使用 get(下標(biāo))可以選擇某個(gè),從 0 開(kāi)始。

Selenium元素操作

文本框操作
  • sendKeys("數(shù)據(jù)") 發(fā)送數(shù)據(jù)到瀏覽器
  • clear() 清空文本框數(shù)據(jù)
  • getAttribute("屬性名") 獲取元素屬性值
  • getText() 獲取文本值 (類似于JS的innerText)
文件上傳操作

input類型文件上傳
driver.findElement(By.id("file")).sendKeys("<File_local_path>")

非input類型文件上傳

單復(fù)選框操作
  • click() 單擊/選中
  • isSelected()
下拉列表

new Select(driver.findElement(By.name("Select控件名"))).

  • selectByIndex(下標(biāo)) 根據(jù)下標(biāo)選擇某項(xiàng),從 0 開(kāi)始
  • selectByVisibleText("數(shù)據(jù)") 根據(jù)列表框中顯示的文本選擇某項(xiàng)
  • selectByValue("值") 根據(jù)值選擇某項(xiàng)
frame 框架
  • driver.switchTo().defaultContent(); 切換到主文檔
  • driver.switchTo().frame(frameName);切換到frameName的子文檔
多Tab窗口
  • driver.getWindowHandle(): 獲得當(dāng)前窗口句柄。
  • driver.getWindowHandles(): 返回的所有窗口的句柄到當(dāng)前會(huì)話。
  • driver.switchTo().window(handle): 用于切換到相應(yīng)的窗口,與上一節(jié)的switchTo().frame()類似,前者用于不同窗口的切換, 后者用于不同表單之間
元素狀態(tài)
  • isEnable()用于存儲(chǔ)input、select等元素的可編輯狀態(tài)
  • isSelected()用于存儲(chǔ)input、select等元素的選中狀態(tài)
  • isDisplayed()源于判斷元素是否在頁(yè)面中可獲取
元素操作之對(duì)話框處理

Alert 警告框

Alert alert = driver.switchTo().alert();
alert.accept();

Confirm 確認(rèn)框

Alert confirm = driver.switchTo().alert();
String text1 = confirm.getText(); //獲取confirm上的文本
System.out.println(text1);
confirm.dismiss(); //關(guān)閉confirm 

prompt 輸入框

Alert prompt = driver.switchTo().alert();
prompt.sendKeys("hello world")//輸入值,如果支持輸入的話
prompt.accept(); //關(guān)閉prompt

Modal 模態(tài)框

WebElement  el = driver.switchTo().activeElement();

getText() 得到它的文本值,accept() 相當(dāng)于點(diǎn)擊它的"確認(rèn)", dismiss() 相當(dāng)于點(diǎn)擊"取消"或者叉掉對(duì)話框,activeElement()用戶獲取當(dāng)前獲得焦點(diǎn)的元素

元素操作之鼠標(biāo)事件

Action action = new Action(driver);

  • 左擊action.click().perform()
  • 右擊 action.contextClick().perform()
  • 雙擊 action.doubleClick().perform()
  • 懸停 action.moveToElement(driver).perform()
執(zhí)行Javascript操作
import org.openqa.selenium.JavascriptExecutor;

String js = "document.getElementById(\"objId\").click()";//定義一個(gè)js    
JavascriptExecutor JE = (JavascriptExecutor)driver;
JE.executeScript(js)

等待函數(shù)

隱式等待

driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
隱式等待采用全局設(shè)置,所有的 findElement 方法都會(huì)隱式等待,上述方法針對(duì)執(zhí)行腳本的所有對(duì)象均會(huì)等待3s.

顯示等待
Thread.sleep(1000);

Maven+ Selenium

安裝Maven,配置Maven環(huán)境變量,設(shè)置本地倉(cāng)庫(kù)

(忽略。。。)

新建Maven工程
  • 新建一個(gè)Maven工程,F(xiàn)ile>New>Other,搜索Maven


    image.png

    image.png

    image.png

    image.png

    新建的Maven工程的JRE System Library可能是jre1.5的,需要在D:\Applications\apache-maven-3.6.1\conf\settings.xml文件的<profiles>標(biāo)簽對(duì)中加入

<profile>
<id>jdk-1.8</id>
<activation>
    <activeByDefault>true</activeByDefault>
    <jdk>1.8</jdk>
</activation>
<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
導(dǎo)入selenium+TestNG
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
        </dependency>
    </dependencies> 

測(cè)試用例設(shè)計(jì)

代碼重構(gòu)&業(yè)務(wù)分層

日志收集與報(bào)告生成

項(xiàng)目CI/CD持續(xù)集成

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容