使用Java語言Spring框架實作的收銀臺專案,用戶完成注冊登錄后進入首頁,可以進行購買商品和瀏覽商品訂單的功能,收銀員可以對商品進行上架,更新商品,雙方都能夠瀏覽到商品資訊,
一,測驗介紹
使用Java語言實作Web自動化測驗,對各頁面的元素進行查找確認是否存在,對頁面中各功能按按鈕進行測驗,使用junit簡化測驗,直觀顯示哪些代碼通過哪些不通過,顯示不通過的原因,
相關技術堆疊
Java、Maven、seleniumWeb自動工具、junit單元測驗框架
二,收銀臺專案的主要功能:

三,Web自動化測驗
1)設計測驗用例
我的學習交流群:769146372 群里有技術指導一起交流學習~

二)撰寫測驗用例代碼

頁面測驗
對注冊頁面進行測驗,首先檢查注冊頁面元素是否正常展示,之后輸入用戶名和密碼,點擊注冊按鈕成功跳轉到登錄頁面為注冊成功
package com.webAutoTest.tests; import com.webAutoTest.common.CommonDriver; import org.junit.jupiter.api.*; import org.openqa.selenium.By; import org.openqa.selenium.chrome.ChromeDriver; /** * @author hu * @date 2022/9/12 15:00 */ @TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class RegisterTest { private static ChromeDriver driver = CommonDriver.getDriver(); @BeforeAll public static void getUrl(){ driver.get("http://127.0.0.1:8080/"); driver.findElement(By.cssSelector("body > div.導航欄 > a:nth-child(2)")).click(); } /** * 檢查頁面元素是否正確顯示 */ @Test @Order(1) public void checkHTMLElement(){ String registerText = driver.findElement(By.cssSelector("body > div.內容區域 > form > h2")).getText(); String nameText = driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(2) > input[type=text]")).getAttribute("placeholder"); String passwordText = driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(3) > input[type=text]")).getAttribute("placeholder"); Assertions.assertEquals("注冊",registerText); Assertions.assertEquals("用戶名",nameText); Assertions.assertEquals("密碼",passwordText); } /** * 檢查頁面能否注冊成功 * 成功跳轉到登錄頁面 */ @Test @Order(2) public void checkRegister(){ driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(2) > input[type=text]")).sendKeys("xiaoliu"); driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(3) > input[type=text]")).sendKeys("123"); driver.findElement(By.cssSelector("body > div.內容區域 > form > button")).click(); //檢查是否跳轉成功 String text = driver.findElement(By.cssSelector("body > div.內容區域 > form > h2")).getText(); Assertions.assertEquals("登錄",text); } }
進入專案首頁,對首頁個元素進行檢查,對各按鈕功能進行檢查,點擊按鈕是否跳轉到相應頁面
package com.webAutoTest.tests; import com.webAutoTest.common.CommonDriver; import org.junit.jupiter.api.*; import org.openqa.selenium.By; import org.openqa.selenium.chrome.ChromeDriver; /** * @author hu * @date 2022/9/12 11:14 */ @TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class FrontPageTest { private static ChromeDriver driver = CommonDriver.getDriver(); /** * 跳轉url */ @BeforeAll private static void getUrl(){ // driver.get("http://127.0.0.1:8080/login.html"); driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(2) > input[type=text]")).sendKeys("xiaohu"); driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(3) > input[type=text]")).sendKeys("123"); driver.findElement(By.cssSelector("body > div.內容區域 > form > button")).click(); } /** *校驗首頁功能是否正常展示 */ @Test @Order(1) public void checkFrontPage(){ String registerText = driver.findElement(By.cssSelector("body > div.導航欄 > a:nth-child(2)")).getText(); String updateRootText = driver.findElement(By.xpath("/html/body/div[1]/a[2]")).getText(); String restProductText = driver.findElement(By.xpath("/html/body/div[1]/a[3]")).getText(); String browseProductText = driver.findElement(By.xpath("/html/body/div[1]/a[4]")).getText(); String updateProductText = driver.findElement(By.cssSelector("body > div.導航欄 > a:nth-child(6)")).getText(); String browseOrderText = driver.findElement(By.cssSelector("body > div.導航欄 > a:nth-child(7)")).getText(); String buyText = driver.findElement(By.cssSelector("body > div.導航欄 > a:nth-child(8)")).getText(); Assertions.assertEquals("注冊賬號",registerText); Assertions.assertEquals("切換賬號",updateRootText); Assertions.assertEquals("上架商品",restProductText); Assertions.assertEquals("瀏覽商品",browseProductText); Assertions.assertEquals("更新商品",updateProductText); Assertions.assertEquals("瀏覽訂單",browseOrderText); Assertions.assertEquals("購買商品",buyText); } /** * 檢查首頁功能是否正確 */ @Test @Order(2) public void checkPageRight(){ //注冊賬號 driver.findElement(By.cssSelector("body > div.導航欄 > a:nth-child(4)")).click(); //從上架商品頁面元素對應的文本,校驗文本是否符合預期 String restProduct = driver.findElement(By.cssSelector("body > div.內容區域 > form > h2")).getText(); Assertions.assertEquals("上架商品",restProduct); } }
測驗商品上架頁面,檢查頁面元素是否正確展示,使用引數化對商品進行上架操作,檢查上架功能是否正常
package com.webAutoTest.tests; import com.webAutoTest.common.CommonDriver; import com.webAutoTest.common.ParamsUtil; import com.webAutoTest.model.Goods; import org.junit.jupiter.api.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.stream.Stream; /** * @author hu * @date 2022/9/12 11:57 */ public class RestProductTest { private static ChromeDriver driver = CommonDriver.getDriver(); @BeforeAll public static void getUrl() throws InterruptedException { // driver.get("http://127.0.0.1:8080/login.html"); // Thread.sleep(2); // driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(2) > input[type=text]")).sendKeys("xiaohu"); // driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(3) > input[type=text]")).sendKeys("123"); // Thread.sleep(2); // driver.findElement(By.cssSelector("body > div.內容區域 > form > button")).click(); driver.findElement(By.cssSelector("body > div.導航欄 > a:nth-child(4)")).click(); } @BeforeEach public void getUrlIn(){ driver.findElement(By.cssSelector("body > div.導航欄 > a:nth-child(4)")).click(); } /** * 校驗商品頁面是否正常展示 */ @Test public void checkRestProduct(){ String restText = driver.findElement(By.cssSelector("body > div.內容區域 > form > button")).getText(); String productNameText = driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(2) > input[type=text]")).getAttribute("placeholder"); String unitText = driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(5) > input[type=text]")).getAttribute("placeholder"); Assertions.assertEquals("添加",restText); Assertions.assertEquals("名稱",productNameText); Assertions.assertEquals("單位",unitText); } /** * 添加商品之后會跳轉到瀏覽商品頁面 */ @ParameterizedTest @MethodSource public void addGoods(String goodsName,int count,String introduce,String unit,int price,int discount){ driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(2) > input[type=text]")). sendKeys(goodsName); driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(3) > input[type=text]")). sendKeys(count+""); driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(4) > input[type=text]")). sendKeys(introduce); driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(5) > input[type=text]")). sendKeys(unit); driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(6) > input[type=text]")). sendKeys(price+""); driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(7) > input[type=text]")). sendKeys(discount+""); driver.findElement(By.cssSelector("body > div.內容區域 > form > button")).click(); //是否跳轉到瀏覽商品頁面 String element = driver.findElement(By.cssSelector("body > div.內容區域 > div.展示串列 > h2")).getText(); Assertions.assertEquals("瀏覽商品",element); } public static Stream<Arguments> addGoods(){ Goods goods = ParamsUtil.getGoodsName(); Goods goods1 = ParamsUtil.getGoodsName(); return Stream.of(Arguments.arguments(goods.getName(),goods.getCount(),goods.getIntroduce() ,goods.getUnit(),goods.getPrice(),goods.getDiscount()), Arguments.arguments(goods1.getName(),goods1.getCount(),goods1.getIntroduce() ,goods1.getUnit(),goods1.getPrice(),goods1.getDiscount())); } }
測驗商品瀏覽頁面,檢查頁面元素是否存在,檢查下架功能是否正常
package com.webAutoTest.tests; import com.webAutoTest.common.CommonDriver; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.openqa.selenium.By; import org.openqa.selenium.chrome.ChromeDriver; /** * @author hu * @date 2022/9/12 13:54 */ public class BrowseProductTest { private static ChromeDriver driver = CommonDriver.getDriver(); /** * 跳轉到瀏覽商品頁面 */ @BeforeAll public static void getUrl(){ /* driver.get("http://127.0.0.1:8080/login.html"); driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(2) > input[type=text]")).sendKeys("xiaohu"); driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(3) > input[type=text]")).sendKeys("123"); driver.findElement(By.cssSelector("body > div.內容區域 > form > button")).click();*/ driver.findElement(By.cssSelector("body > div.導航欄 > a:nth-child(5)")).click(); } /** * 瀏覽頁面元素是否展示元素 */ @Test public void checkHTMLElement(){ String countText = driver.findElement(By.cssSelector("body > div.內容區域 > div.展示串列 > table > thead > tr > th:nth-child(1)")).getText(); String introduceText = driver.findElement(By.cssSelector("body > div.內容區域 > div.展示串列 > table > thead > tr > th:nth-child(3)")).getText(); Assertions.assertEquals("編號",countText); Assertions.assertEquals("介紹",introduceText); } /** * 檢查下架按鈕是否能正常使用 */ @Test public void checkPull(){ // driver.findElement(By.cssSelector("body > div.內容區域 > div.展示串列 > table > tbody > tr:nth-child(1) > td:nth-child(8) > a")).click(); } }
測驗更新商品頁面,檢查頁面元素正確顯示,對更新功能進行測驗
package com.webAutoTest.tests; import com.webAutoTest.common.CommonDriver; import org.junit.jupiter.api.*; import org.openqa.selenium.By; import org.openqa.selenium.chrome.ChromeDriver; /** * @author hu * @date 2022/9/12 15:34 */ @TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class UpdateTest { private static ChromeDriver driver = CommonDriver.getDriver(); @BeforeAll public static void getUrl(){ driver.findElement(By.cssSelector("body > div.導航欄 > a:nth-child(6)")).click(); } /** * 檢查頁面元素是否正確顯示 */ @Test @Order(1) public void checkHTMLElement(){ String updateText = driver.findElement(By.cssSelector("body > div.內容區域 > form > h2")).getText(); String idText = driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(2) > input[type=text]")).getAttribute("placeholder"); Assertions.assertEquals("更新商品",updateText); Assertions.assertEquals("商品 id",idText); } /** * 更新商品功能是否正常 */ @Test @Order(2) public void checkUpdateRight(){ driver.findElement(By.cssSelector("body > div.導航欄 > a:nth-child(5)")).click(); String idText = driver.findElement(By.cssSelector("body > div.內容區域 > div.展示串列 > table > tbody > tr:nth-child(1) > td:nth-child(1)")).getText(); driver.findElement(By.cssSelector("body > div.導航欄 > a:nth-child(6)")).click(); driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(2) > input[type=text]")).sendKeys(idText); driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(3) > input[type=text]")).sendKeys("西瓜"); driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(4) > input[type=text]")).sendKeys("50"); driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(5) > input[type=text]")).sendKeys("大西瓜"); driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(6) > input[type=text]")).sendKeys("斤"); driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(7) > input[type=text]")).sendKeys("2"); driver.findElement(By.cssSelector("body > div.內容區域 > form > div:nth-child(8) > input[type=text]")).sendKeys("70"); driver.findElement(By.cssSelector("body > div.內容區域 > form > button")).click(); //更新成功跳轉到瀏覽商品 String browseText = driver.findElement(By.cssSelector("body > div.內容區域 > div.展示串列 > h2")).getText(); Assertions.assertEquals("瀏覽商品",browseText); } }
對購買商品頁面進行測驗,檢查頁面元素是否正常顯示,購買功能是否正常,成功跳轉到支付頁面,失敗回到購買頁面
package com.webAutoTest.tests; import com.webAutoTest.common.CommonDriver; import org.junit.jupiter.api.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.openqa.selenium.By; import org.openqa.selenium.chrome.ChromeDriver; /** * @author hu * @date 2022/9/12 16:09 */ @TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class BuyTest { private static ChromeDriver driver = CommonDriver.getDriver(); @BeforeAll public static void getUrl(){ driver.findElement(By.cssSelector("body > div.導航欄 > a:nth-child(8)")).click(); } /** * 檢查頁面元素是否正確顯示 */ @Test @Order(1) public void checkHTMLElement(){ String text = driver.findElement(By.cssSelector("body > div.內容區域 > form > h2")).getText(); Assertions.assertEquals("購買商品",text); } /** * 檢查購買功能 */ @Test @Order(2) public void checkBuyRight(){ driver.findElement(By.cssSelector("body > div.導航欄 > a:nth-child(5)")).click(); String idText = driver.findElement(By.cssSelector("body > div.內容區域 > div.展示串列 > table > tbody > tr:nth-child(3) > td:nth-child(1)")).getText(); driver.findElement(By.cssSelector("body > div.導航欄 > a:nth-child(8)")).click(); driver.findElement(By.cssSelector("body > div.內容區域 > form > div > input[type=text]")).sendKeys(idText + "-" + 10); driver.findElement(By.cssSelector("body > div.內容區域 > form > button")).click(); //購買成功跳轉到支付頁面 String text = driver.findElement(By.cssSelector("body > div.內容區域 > div.展示區域 > a.btn.btn-confirm")).getText(); Assertions.assertEquals("確認",text); //購買失敗跳轉到購買頁面 } }
測驗購買訂單頁面,檢查頁面元素是否正確展示,對支付功能進行測驗,成功跳轉到支付頁面進行支付操作
package com.webAutoTest.tests; import com.webAutoTest.common.CommonDriver; import org.junit.jupiter.api.*; import org.openqa.selenium.By; import org.openqa.selenium.chrome.ChromeDriver; /** * @author hu * @date 2022/9/12 16:01 */ public class BrowseOrder { private static ChromeDriver driver = CommonDriver.getDriver(); @BeforeAll public static void getUrl(){ driver.findElement(By.cssSelector("body > div.導航欄 > a:nth-child(7)")).click(); } /** * 檢查頁面元素是否正確顯示 */ @Test public void checkHTMLElement(){ String orderText = driver.findElement(By.cssSelector("body > div.內容區域 > div.展示串列 > h2")).getText(); String informationText = driver.findElement(By.cssSelector("body > div.內容區域 > div.展示串列 > table > thead > tr > th:nth-child(1)")).getText(); Assertions.assertEquals("瀏覽訂單",orderText); Assertions.assertEquals("資訊",informationText); } }
測驗支付頁面,進入瀏覽訂單頁面,點擊訂單中的未支付,進入支付頁面,對支付功能進行測驗,支付成功跳轉到瀏覽訂單頁面,取消支付清除此訂單并跳轉到瀏覽商品頁面
package com.webAutoTest.tests; import com.webAutoTest.common.CommonDriver; import org.junit.jupiter.api.*; import org.openqa.selenium.By; import org.openqa.selenium.chrome.ChromeDriver; /** * @author hu * @date 2022/9/12 17:06 */ public class PayTest { private static ChromeDriver driver = CommonDriver.getDriver(); @BeforeAll private static void getUrl() { driver.findElement(By.cssSelector("body > div.內容區域 > div.展示串列 > table > tbody > tr:nth-child(1) > td:nth-child(1) > p:nth-child(2) > a")).click(); ; } /** * 對功能進行測驗 */ @Test public void CheckHTMLElement() { //取消支付訂單清除,跳轉到商品瀏覽頁面 driver.findElement(By.cssSelector("body > div.內容區域 > div.展示區域 > a.btn.btn-confirm")).click(); //檢查是否跳轉到商品瀏覽頁面 String browseProductText = driver.findElement(By.cssSelector("body > div.內容區域 > div.展示串列 > h2")).getText(); Assertions.assertSame("瀏覽商品",browseProductText); //確認支付跳轉到訂單瀏覽頁面 driver.findElement(By.cssSelector("body > div.內容區域 > div.展示串列 > table > tbody > tr:nth-child(1) > td:nth-child(1) > p:nth-child(2) > a")).click(); driver.findElement(By.cssSelector("body > div.內容區域 > div.展示區域 > a.btn.btn-confirm")).click(); //檢查是否跳轉到訂單瀏覽頁面 String browseOrderText = driver.findElement(By.cssSelector("body > div.內容區域 > div.展示串列 > h2")).getText(); Assertions.assertEquals("瀏覽訂單",browseOrderText); } }
三)測驗結果
程序中觀察測驗資料,執行緒等待,共通過測驗用例7,耗時25s

測驗用例全部通過
總結
1.使用selenium4web自動化工具和Junit5單元測驗框架,通過注解,提升測驗效率,
2.使用單例模式,將ChromeDriver私有化,保證所有的測驗都使用同一個實體物件,減少創建和銷毀物件的時間,
3.使用測驗套件,一次執行所有的測驗用例,
4.使用隱式等待和強制等待,提升自動化測驗用例的穩定性,
為什么使用強制等待,不使用顯示等待:
- 顯示等待書寫麻煩
- 顯示等待和隱式等待容易出現問題
- 彈窗無法定位
5.使用螢屏截圖,方便定位問題的出處,
都是測驗黨,不定期分享干貨(只有軟體測驗行業的)
現在我邀請大家加入自己建的軟體測驗學習社群:【 769146372 】,備注“博客園樂卻思蜀”, 大家可以一起探討交流軟體測驗,共同學習軟體測驗技術、面試等軟體測驗方方面面,還會有免費直播課,識訓更多測驗技巧,
我們一起進階Python自動化測驗/測驗開發,走向高薪之路,
如果我的博客對你有幫助、如果你喜歡我的博客內容,請 “點贊” “評論” “收藏” 一 鍵三連哦!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/507172.html
標籤:其他
