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