《手把手教你》系列基礎篇(九十三)-java+ selenium自動化測試-框架設計基礎-POM設計模式實現-上篇(詳解教程)
- 2022 年 4 月 22 日
- 筆記
- java+selenium自動化測試
1.簡介
上一篇介紹了POM的基礎理論知識和非POM方式寫腳本,這篇介紹利用頁面工廠類(page factory)去實現POM,通過查看PageFactory類,我們可以知道它是一個初始化一個頁面實例的功能,在實例化該頁面對象時候,也會一起實例化該頁面的元素定位。
2.項目實戰
在這裡宏哥以百度首頁登錄的例子,如果用POM實現,在測試腳本中實際程式碼就幾行。
2.1程式碼設計
1.先新建一個pageObjects包,然後在pageObjects包新建一個百度主頁類:BaiduHomePage,程式碼設計如下圖所示:
2.再次新建一個testSuites包,然後在testSuites包下新建一個測試類:TestWithPOM
2.2參考程式碼
1.BaiduHomePage
package pageObjects; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; /** * @author 北京-宏哥 * * @公眾號:北京宏哥 * * 《手把手教你》系列基礎篇(八十七)-java+ selenium自動化測試-框架設計基礎-POM設計模式實現-上篇(詳解教程) * * 2022年3月20日 */ public class BaiduHomePage { // 元素定位 // 登錄按鈕 @FindBy(xpath="//*[@id='u1']/a[1]") WebElement login_link; // 輸入用戶名框 @FindBy(xpath="//*[@id='TANGRAM__PSP_11__userName']") WebElement inputBox_username; // 輸入密碼 @FindBy(xpath="//*[@id='TANGRAM__PSP_11__passwordWrapper']/input[2]") WebElement inputBox_password; // 登錄按鈕 @FindBy(id = "TANGRAM__PSP_11__submit") WebElement login_submitBtn; // 業務邏輯和操作方法 // 登錄方法 public void login(String username, String password) throws InterruptedException { login_link.click(); Thread.sleep(3000); inputBox_username.sendKeys(username); inputBox_password.sendKeys(password); login_submitBtn.click(); } }
2.TestWithPOM
package testSuites; import org.testng.annotations.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.PageFactory; import org.testng.annotations.BeforeClass; import pageObjects.BaiduHomePage; /** * @author 北京-宏哥 * * @公眾號:北京宏哥 * * 《手把手教你》系列基礎篇(八十七)-java+ selenium自動化測試-框架設計基礎-POM設計模式實現-上篇(詳解教程) * * 2022年3月20日 */ public class TestWithPOM { WebDriver driver; @BeforeClass public void setUp() throws Exception{ System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe"); driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("//www.baidu.com/"); Thread.sleep(2000); } @Test public void testLogin() throws InterruptedException{ BaiduHomePage hp = PageFactory.initElements(driver, BaiduHomePage.class); hp.login("user1", "123456"); } }
2.3運行程式碼
1.運行程式碼,右鍵Run AS->TestNG Suite,控制台輸出,如下圖所示:
2.運行程式碼後電腦端的瀏覽器的動作,如下小影片所示:
3.非POM實現
下面跟隨宏哥看一下不用POM怎麼實現登錄百度首頁。
3.1程式碼設計
3.2參考程式碼
package testSuites; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; /** * @author 北京-宏哥 * * @公眾號:北京宏哥 * * 《手把手教你》系列基礎篇(八十七)-java+ selenium自動化測試-框架設計基礎-POM設計模式實現-上篇(詳解教程) * * 2022年3月20日 */ public class TestWithoutPOM { WebDriver driver; @BeforeClass public void setUp() throws Exception{ System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe"); driver = new ChromeDriver(); driver.manage().window().maximize(); } @Test public void testBaidu() throws InterruptedException { driver.get("//www.baidu.com/"); Thread.sleep(2000); Thread.sleep(3000); // click login link // 元素定位 // 登錄按鈕 driver.findElement(By.xpath("//*[@id='u1']/a[1]")).click(); Thread.sleep(2000); // 輸入用戶名框 driver.findElement(By.xpath("//*[@id='TANGRAM__PSP_11__userName']")).clear(); driver.findElement(By.xpath("//*[@id='TANGRAM__PSP_11__userName']")).sendKeys("user1"); Thread.sleep(2000); // 輸入密碼 driver.findElement(By.xpath("//*[@id='TANGRAM__PSP_11__passwordWrapper']/input[2]")).clear(); driver.findElement(By.xpath("//*[@id='TANGRAM__PSP_11__passwordWrapper']/input[2]")).sendKeys("123456"); // 登錄按鈕 driver.findElement(By.id("TANGRAM__PSP_11__submit")).click(); } @AfterClass public void tearDown(){ driver.quit(); } }
3.3運行程式碼
1.運行程式碼,右鍵Run AS->TestNG Suite,控制台輸出,如下圖所示:
2.運行程式碼後電腦端的瀏覽器的動作,如下小影片所示:
4.小結
好了今天主要介紹和講解了百度首頁登錄使用POM和不使用POM。二者的優缺點一目了然,宏哥在這裡就不多說了,今天就到這裡了,感謝您耐心的閱讀!!!