java+testng+selenium的自動化測試實例
前言
這是用testng框架加selenium做的一個UI自動化測試的項目
Java代碼
package com.justin; /** ?* @author justin-zhu ?* <p> ?* 2022年02月23日 16:48 ?*/ import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.*; import java.util.concurrent.TimeUnit; public class HelloTestNG { ? ? private WebDriver driver; ? ? @BeforeMethod ? ? public void setBefore(){ ? ? ? ?? ? ? ? ? System.setProperty("webdriver.chrome.driver","C:\\Users\\betalpha-qa\\code\\testcode\\TestNG-Demo\\src\\main\\resources\\chromedriver.exe"); ? ? ? ? //打開瀏覽器,使其最大化,并隱性等待兩秒鐘 ? ? ? ? driver = new ChromeDriver(); ? ? ? ? driver.manage().window().maximize(); ? ? ? ? driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS); ? ? } ? ? @AfterMethod ? ? public void setAfter(){ ? ? ?? ?//結(jié)束驅(qū)動程序進程,關(guān)閉瀏覽器 ? ? ? ? driver.quit(); ? ? } ? ? @Test(groups = {"login"}) ? ? public void login() throws InterruptedException { ? ? ? ? //輸入網(wǎng)址(輸入本地項目的URL,下面為本地項目的登陸界面) ? ? ? ? driver.get("http://192.168.0.188/webapp/session/login"); ? ? ? ? driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); ? ? ? ? //使用其方法獲取瀏覽器類型,并斷言(如果斷言失敗,不會執(zhí)行下面代碼) ? ? ? ? String browserType = driver.getTitle(); ? ? ? ? Assert.assertEquals("Google", browserType); ? ? ? ? //獲取賬號框定位 ? ? ? ? WebElement userName = driver.findElement(By.xpath("http://*[@id=\"app\"]/div/div/form/div/div[2]/div/div[1]/div/input")); ? ? ? ? //獲取密碼框定位 ? ? ? ? WebElement password = driver.findElement(By.xpath("http://*[@id=\"app\"]/div/div/form/div/div[3]/div/div[1]/div/div/span/input")); ? ? ? ? //獲取驗證碼框定位 ? ? ? ? WebElement authCode = driver.findElement(By.xpath("http://*[@id=\"app\"]/div/div/form/div/div[4]/div/div/div[1]/div/input")); ? ? ? ? WebElement loginButton = driver.findElement(By.xpath("http://*[@id=\"app\"]/div/div/form/div/div[5]/div/div/div/button")); ? ? ? ? //輸入賬號密碼登錄,并點擊登錄 ? ? ? ? userName.sendKeys("jusitn@qq.com"); ? ? ? ? password.sendKeys("123456"); ? ? ? ? authCode.sendKeys("1234"); ? ? ? ? driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS); ? ? ? ? loginButton.click(); ? ? ? ? driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); ? ? ? ? //獲取登錄界面的title,驗證登錄成功 ? ? ? ? WebElement title = driver.findElement(By.xpath("http://*[@id=\"logo\"]/div/div/div[1]/h1")); ? ? ? ? String actual = title.getText(); ? ? ? ? Assert.assertEquals(actual, "指數(shù)研發(fā)與管理平臺"); ? ? } ? ?@Test(description = "定位百度一下") ? ? public void testBaiDu(){ ? ? ? ? //輸入網(wǎng)址 ? ? ? ? driver.get("https://www.baidu.com/"); ? ? ? ? driver.manage().timeouts().implicitlyWait(2,TimeUnit.SECONDS); ? ? ? ? //定位到百度一下按鈕 ? ? ? ? WebElement name = driver.findElement(By.id("su")); ? ? ? ? String text = name.getAttribute("value"); ? ? ? ? Assert.assertEquals(text, "百度一下"); ?? ?} ?? ? ?? ?@Test(groups = {"fast"}) ? ? public void aFastTest(){ ? ? ? ? System.out.println("Fast test"); ? ? } ? ? @Test(groups = {"slow"}) ? ? public void aSlowTest(){ ? ? ? ? System.out.println("Slow test"); ? ? } }
配置文件
要想上面的test能跑起來,還需要再pon.xml文件里面添加以下依賴
<!-- https://mvnrepository.com/artifact/org.testng/testng --> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.testng</groupId> ? ? ? ? ? ? <artifactId>testng</artifactId> ? ? ? ? ? ? <version>6.14.3</version> ? ? ? ? ? ? <scope>test</scope> ? ? ? ? </dependency>
這是testng框架的依賴,有了這個依賴testng的注釋才會生效
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server --> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.seleniumhq.selenium</groupId> ? ? ? ? ? ? <artifactId>selenium-server</artifactId> ? ? ? ? ? ? <version>3.14.0</version> ? ? ? ? </dependency>
如果你只在本地運行代碼,那么有selenium-java就夠了;但是如果要在遠程調(diào)用,就需要配置該selenium-server依賴
?? ??? ?<dependency> ? ? ? ? ? ? <groupId>org.seleniumhq.selenium</groupId> ? ? ? ? ? ? <artifactId>selenium-chrome-driver</artifactId> ? ? ? ? ? ? <version>2.42.2</version> ? ? ? ? </dependency>
這是想要再界面上操作元素配置的依賴
擴展
testng用例可以直接運行java代碼,也可以配置testng.xml文件進行用例的執(zhí)行
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name = "testProj"> ? ? <test name = "testDemo1"> <!-- ? ? ? ?.XML中指定組內(nèi)的某些方法,include為執(zhí)行,exclude為不執(zhí)行--> ? ? ? ? <groups> ? ? ? ? ? ? <run> ? ? ? ? ? ? ? ? <exclude name="fast"/> ? ? ? ? ? ? ? ? <exclude name="slow"/> ? ? ? ? ? ? ? ? <include name="login"/> ? ? ? ? ? ? </run> ? ? ? ? </groups> <!-- ? ? ? ?.XML指明測試類,按照類名執(zhí)行--> ? ? ? ? <classes> ? ? ? ? ? ? <class name="com.justin.HelloTestNG"/> ? ? ? ? </classes> <!-- ? ? ? ?.XML指定包名,執(zhí)行包內(nèi)的所有測試類--> <!-- ? ? ? ?<packages>--> <!-- ? ? ? ? ? ?<package name="com.justin"></package>--> <!-- ? ? ? ?</packages>--> ? ? </test> ? ?? ? ? <listeners> ? ? ? ? <listener class-name="org.uncommons.reportng.HTMLReporter"></listener> ? ? ? ? <listener class-name="org.uncommons.reportng.JUnitXMLReporter"></listener> ? ? </listeners> </suite>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
將springboot項目生成可依賴的jar并引入到項目中的方法
SpringBoot項目默認打包的是可運行jar包,也可以打包成不可運行的jar包,本文給大家介紹將springboot項目生成可依賴的jar并引入到項目中的方法,感興趣的朋友一起看看吧2023-11-11Java如何獲取resources下的文件路徑和創(chuàng)建臨時文件
這篇文章主要介紹了Java如何獲取resources下的文件路徑和創(chuàng)建臨時文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12Java實現(xiàn)基于JDBC操作mysql數(shù)據(jù)庫的方法
這篇文章主要介紹了Java實現(xiàn)基于JDBC操作mysql數(shù)據(jù)庫的方法,結(jié)合實例形式分析了java使用JDBC實現(xiàn)針對mysql數(shù)據(jù)庫的連接、查詢、輸出等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12SpringBoot整合Vue實現(xiàn)微信掃碼支付以及微信退款功能詳解
最近公司要在微信公眾號上做一個活動預(yù)報名,活動的門票等需要在微信中支付,下面這篇文章主要給大家介紹了關(guān)于SpringBoot整合Vue實現(xiàn)微信掃碼支付以及微信退款功能的相關(guān)資料,需要的朋友可以參考下2022-05-05