10分鐘玩轉(zhuǎn)Selenium自動(dòng)化
大家好袭异,今天向大家介紹一款一直很火的自動(dòng)化測(cè)試工具Selenium钠龙。我們將教您如何在10分鐘內(nèi)完成Selenium的環(huán)境搭建,玩轉(zhuǎn)POM御铃,跑起DEMO碴里,走向人生巔峰。
Selenium automates browsers. That's it!
-- Selenium官網(wǎng)介紹
搭建項(xiàng)目
- 下載并安裝Java JDK
- 下載并安裝IntelliJ IDEA
- 在IntelliJ IDEA中創(chuàng)建新的Maven project: File > New > Project > Maven
- 將“Project SDK”關(guān)聯(lián)至本機(jī)JDK上真,路徑一般為“C:\Program Files\Java\jdkxxxx”
- 設(shè)置groupId與artifactId:
<groupId>SeleniumTest</groupId>
<artifactId>Test</artifactId>
- 在項(xiàng)目左側(cè)面板的pom.xml文件中添加Selenium和JUnit依賴項(xiàng)
<dependencies>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Selenium -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>3.7.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>3.7.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.7.1</version>
</dependency>
</dependencies>
直到這一步咬腋,如果前面都配置正確,依賴會(huì)自動(dòng)進(jìn)行下載谷羞。如果沒(méi)有自動(dòng)下載帝火,則在IDEA的左側(cè)Maven Projects面板找到并激活Plugins > Install > install。
[圖片上傳失敗...(image-87476b-1510301274662)]
一旦項(xiàng)目成功完成裝載湃缎,在"src/test/java"目錄下創(chuàng)建"com.sogo"目錄犀填,并在其下創(chuàng)建"com.sogo.tests"和"com.sogo.webpages"目錄。
[圖片上傳失敗...(image-f66c06-1510301274662)]
我們以后把Page Object/Page Factory類放入"com.sogo.webpages"目錄嗓违,把測(cè)試用例放在"com.sogo.tests"目錄下九巡。接下來(lái),我們創(chuàng)建Page Object類蹂季。
主頁(yè) Page Object
首先之先冕广,在"com.sogo.webpages"下創(chuàng)建一個(gè)叫做"HomePage"的類疏日。
package com.sogo.webpages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
public class HomePage {
private WebDriver driver;
//Page URL
private static String PAGE_URL="https://weixin.sogo.com";
//Locators
//Find Weixin Button
@FindBy(how = How.LINK_TEXT, using = "特朗普訪華")
private WebElement weixinButton;
//Constructor
public HomePage(WebDriver driver){
this.driver=driver;
driver.get(PAGE_URL);
//Initialise Elements
PageFactory.initElements(driver, this);
}
public void clickTrumpVisitToChina(){
weixinButton.click();
}
}
找到元素定位器
在搜狗微信搜索的主頁(yè)里我們關(guān)注的是“特朗普訪華”的消息,我們通過(guò)文本的匹配找到這個(gè)元素撒汉,也就是上文代碼做的事情沟优。
除了需要將網(wǎng)頁(yè)建模為頁(yè)面對(duì)象類之外,查找和識(shí)別網(wǎng)頁(yè)元素通常也會(huì)比較麻煩睬辐。我們可以通過(guò)使用Google Chrome自帶的元素檢查器挠阁,找到關(guān)注的任意元素的相關(guān)信息。
在“特朗普訪華”右鍵溯饵,點(diǎn)擊檢查元素侵俗,右鍵點(diǎn)擊Copy > Copy Xpath,我們就可以找到對(duì)應(yīng)的頁(yè)面元素路徑丰刊。
[圖片上傳失敗...(image-a8e0af-1510301274662)]
@(1.4 微信公眾號(hào))
WebElement hyperlink;
具體信息頁(yè)面Page Object
在這個(gè)頁(yè)面中隘谣,我們關(guān)心的有兩點(diǎn):1. 頁(yè)面是否成功加載;2.進(jìn)入具體信息頁(yè)面
package com.sogo.webpages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class DetailedInfo {
private WebDriver driver;
@FindBy(xpath = "http://*[@id=\"topwords\"]/li[1]/a")
private WebElement hyperlink;
@FindBy(linkText = "特朗普訪華亮點(diǎn)有哪些啄巧?")
private WebElement hyperlink2;
//Constructor
public DetailedInfo (WebDriver driver){
this.driver = driver;
//Initiallize Elements
PageFactory.initElements(driver, this);
}
public boolean isPageOpened(){
return hyperlink.getText().toString().contains("特朗普訪華");
}
public void clickOnOpen(){
hyperlink2.click();
}
}
寫一個(gè)測(cè)試用例
Page Object類代表了各個(gè)網(wǎng)頁(yè)與用戶的一些動(dòng)作寻歧,接下來(lái),在tests目錄下我們可以寫一些測(cè)試用例與注入檢查其生效性秩仆。
package com.sogo.tests;
import com.sogo.webpages.DetailedInfo;
import com.sogo.webpages.HomePage;
import org.junit.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.net.URL;
import java.util.concurrent.TimeUnit;
public class WhereIsTrumpTest {
WebDriver driver;
@Before
public void setup(){
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void testTrump(){
HomePage home = new HomePage(driver);
home.clickTrumpVisitToChina();
DetailedInfo detailedInfo = new DetailedInfo(driver);
Assert.assertTrue(detailedInfo.isPageOpened());
detailedInfo.clickOnOpen();
}
@After
public void close(){
driver.close();
}
}
這樣熄求,一個(gè)完整的測(cè)試框架和Demo就搭建完成了,Trump也被我們找到了逗概,你學(xué)會(huì)了嗎?