准備環境,提前安裝好Jenkins及git,maven插件
1、首先我們新建一個maven的工程,並且在pom.xml中配置好我們依賴的一些jar包
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>test</groupId> <artifactId>com.test</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.6</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-api</artifactId> <version>3.4.0</version> </dependency> </dependencies> <build> <finalName>Test</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <configuration> <forkMode>once</forkMode> <argLine>-Dfile.encoding=UTF-8</argLine> </configuration> </plugin> </plugins> </build> </project>
2、編寫我們selenium腳本
import org.openqa.selenium.By; import org.openqa.selenium.TimeoutException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class NewTest { private WebDriver driver; protected DesiredCapabilities capabilities; @BeforeTest public void beforeTest() throws Exception { capabilities = DesiredCapabilities.chrome(); capabilities.setBrowserName("chrome"); System.setProperty("webdriver.chrome.driver", getClass().getResource("/chromedriver.exe").getPath()); ChromeOptions options = new ChromeOptions(); options.addArguments("--start-maximized"); capabilities.setCapability(ChromeOptions.CAPABILITY, options); driver = new ChromeDriver(capabilities); driver.manage().window().maximize(); } @AfterTest public void afterTest(){ try { //等待5秒查看執行效果 Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } driver.quit(); } @Test public void testClass() { driver.get("http://www.baidu.com"); System.out.println("heloo"); By inputBox = By.id("kw"); By searchButton = By.id("su"); //智能等待元素加載出來 intelligentWait(driver, 10, inputBox); //智能等待元素加載出來 intelligentWait(driver, 10, searchButton); driver.findElement(inputBox).sendKeys("中國地圖"); driver.findElement(searchButton).click(); } /**這是智能等待元素加載的方法*/ public void intelligentWait(WebDriver driver,int timeOut, final By by) { try { (new WebDriverWait(driver, timeOut)).until(new ExpectedCondition<Boolean>(){ public Boolean apply(WebDriver driver) { WebElement element = driver.findElement(by); return element.isDisplayed(); } }); } catch (TimeoutException e) { Assert.fail("超時L !! " + timeOut + " 秒之后還沒找到元素 [" + by + "]", e); } } }
3、配置testng.xml文件
<?xml version="1.0" encoding="UTF-8"?> <suite name="Suite" parallel="false"> <test name="Test"> <classes> <class name="NewTest"> </class> </classes> </test> </suite>
chromedriver.exe驅動放在resources目錄下
工程配置已經結束了,我們來進行jenkins的一些配置吧。進入jenkins的系統配置
4、Jenkins中環境的配置
4.1 系統管理-->系統設置-->配置全局屬性,及maven項目配置
這個地方一定要配置,因為如果不配置成utf-8的話,jenkins從git上拉下來的文件編碼格式不是utf-8的格式,這樣子就會導致文件中的一些中文直接變成了亂碼,到時候直接影響到腳本的運行
進行maven的項目配置
這里是配置maven的編碼以及防止oom,還有是maven的本地倉庫以及maven的安裝地址
4.2 全局工具配置maven,git,jdk ,進入系統管理-->Global Tool Configuration 里面
4.3 新建一個projce后,在構建中新建一個構建步驟 invoke-top-level Maven targets
這里只需要配置正確pom就可以了
4.4 配置git源碼管理路徑,這里使用的是https的方式
4.5 在構建中進行配置
源碼管理這里我以Git為例子,Repository URL填寫項目的地址,可以使https地址,也可以是SHH地址。
注意,如果使用https地址,步驟需要點擊Add->填寫網站的用戶名和密碼,要使用SHH地址,在這之前我們必須在本機上生成了ssh密鑰,並且在平台上設定過公鑰才可以使用。
這里我使用的是碼雲平台上的項目,參考文章 本地電腦和 Git @ OSC 之間SHH連接教程
只是項目的SHH地址:git@git.oschina.net:higher/webtest.git
如有需要的朋友可以自行下載,https地址為: https://git.oschina.net/higher/webtest.git
**a.SHH方式 **
如果出現如下的錯誤,請檢查公鑰配置
如果不能解決可以參考我的另外的文章Jenkins 與 git 的配置及解決方式 點擊查看
**b.https方式 **
4.6 構建后操作設置
需要顯示一下測試報告,采用publish html report方式,實際上就是讀取一個html文件,顯示在jenkins里面的Html Report中 ;【HTML directory to archive】是生成的報告地址,轉換到這個地址中【
C:\Users\Vague\.jenkins\jobs\test1\htmlreports\HTML_Report】
添加publish html reports后,會在項目主頁生成一個html reports 的報告入口,如下
5、運行結果
ps:這次搭建自動化測試環境也有不少踩坑的地方,總結避免再次掉坑啊
1、Jenkins maven 構建亂碼,修改file.encoding系統變量編碼為UTF-8,參考我的這篇文章
2、構建成功卻沒有運行測試用例:一是測試用例沒有放到src/test/java目錄下,二是測試類沒有按規則命名,三是因為testng.xml沒有放到項目根目錄下面