selenium的PageObject設計模式


PageObject設計模式
1. Web自動化測試框架(WebTestFramework)是基於Selenium框架且采用PageObject設計模式進行二次開發形成的框架。 

2. web測試時,建議強烈推薦使用_谷歌或_火狐瀏覽器。

3. PageObject設計模式:是將某個頁面的所有"元素(包含控件)屬性"及"元素操作"封裝在1個類(Class)里面,以page為單位進行管理。

4. 目的: 測試代碼與被測頁面對象代碼分離,后期如果有頁面元素發生了更改,只需要修改相應頁面對象的代碼(即對應Class文件),而不需要修改測試代。

5. 盡量采用xpath方式來尋找頁面元素,而不建議使用name,Link等方法; xpath是基於頁面元素所處區域,一般不會發生變化,測試代碼基本不受干擾。

6. 將頁面元素屬性信息與代碼分離,即與被測對象代碼分離,目的也是為了進一步降低后續因頁面變化帶來的維護成本。

一、公共類,操作瀏覽器相關的

import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class TestBase { protected WebDriver driver; @BeforeTest public void setUp() throws Exception { driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @AfterTest public void tearDown() throws Exception { driver.quit(); } }

二、登錄頁面存放的元素

import org.openqa.selenium.By; import org.openqa.selenium.NotFoundException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; public class LoginPage { private WebDriver driver; public LoginPage(WebDriver driver) { this.driver = driver; } /** * 用戶名 * * @param driver * @param userText */
    public void setPhone(String phoneText) throws NotFoundException { // driver.findElement(By.id("username")).clear(); // driver.findElement(By.id("username")).sendKeys(userText);
        this.setText(driver.findElement(By.id("username")), phoneText); } /** * 密碼 * * @param driver * @param userText */
    public void setPass(String passText) throws NotFoundException { // driver.findElement(By.id("username")).clear(); // driver.findElement(By.id("username")).sendKeys(userText);
        this.setText(driver.findElement(By.id("password")), passText); } /** * 點擊登錄按鈕 * * @param driver */
    public void clickButton() throws NotFoundException { driver.findElement(By.id("butt")).click(); } private void setText(WebElement e,String text) { e.clear(); e.sendKeys(text); } }

三、登錄頁面提供的方法

import org.openqa.selenium.WebDriver; import com.page.LoginPage; public class LoginBuss { private WebDriver driver; public LoginBuss(WebDriver driver) { this.driver = driver; } /** * 定義登陸業務 * * @param driver * @param usename * @param password * @return * @throws InterruptedException */
    public void login(String username, String password) throws Exception { driver.get("xxxxxxxxx");//打開測試網址
 LoginPage login_page = new LoginPage(driver); login_page.setPhone(username);//輸入用戶名
        login_page.setPass(password);// 輸入密碼
        login_page.clickButton();//點擊登錄按鈕
 } }

四、測試用例

import java.lang.reflect.Method; import org.testng.Assert; import org.testng.annotations.Test; import com.bussiness.LoginBuss; public class loginCase extends TestBase{ // 用戶成功登陸
 @Test public void LoginSucc() throws Exception { LoginBuss lb = new LoginBuss(driver); lb.login("xxx", "123abc");// 調用登錄的一個事務
        Thread.sleep(1000); } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM