TestNG 强大的测试框架(3)-Page Object
开始学习UI自动化时,我们把所有的元素定位、操作步骤以及睡眠等都一股劲的写到TestNG的@Test方法里,给后期维护代码提高了难度,可读性也比较低。本章引进Page Object, page object 是selenium2.0里提供的分离元素、操作的一种框架;它使用注解@FindBy查找元素;@CacheLookup缓存元素以供下次直接调用,缩短项目运行时间;WebElement定义元素变量;结合构造器里的PageFactory.initElements(driver, this)将元素映射到元素变量上(driver是浏览器驱动,this指向的是自己的类)。
下面讲一个简单的page Object+TestNG项目
1. java项目结构,(1),common 公共类;(2),page 页面元素、操作;(3),TestNG 测试框架 运行入口

2.common.java,这个的类的所有方法都有可能被任何页面引用到
package com.common; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class Common { /* * 暂停时间 */ public void PauseTime(int ms){ try { Thread.sleep(ms); } catch (InterruptedException e) { e.printStackTrace(); } } /* * 页面加载 */ public void LoadPage(WebDriver driver){ driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); } /*等待页面元素加载完成 */ public void WaitTimeOut(WebDriver driver,By by){ WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.presenceOfElementLocated(by)); } /* *退出浏览器 */ public void Exit(WebDriver driver){ driver.quit(); } /* * 访问页面 */ public void Load(WebDriver driver,String url){ driver.get(url); } }
3.login.java 封装页面元素以及页面操作。
package com.page;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.CacheLookup; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; public class Login { //登录按钮 @FindBy(id="umengLoginBtn") @CacheLookup WebElement UMENG_LOGIN_BTN; //用户名 @FindBy(xpath=".//*[@id='ump']/div[1]/div/form/div[1]/ul/li[1]/div/label/input") @CacheLookup WebElement UMENG_USER; //密码 @FindBy(xpath=".//*[@id='ump']/div[1]/div/form/div[1]/ul/li[2]/label/input") @CacheLookup WebElement UMENG_PWD; //提交 @FindBy(id="submitForm") @CacheLookup WebElement SUBMIT_LOGIN; /*构造器,初始化元素,将元素映射到定义好的变量上 * */ public Login(WebDriver driver){ PageFactory.initElements(driver, this); } /* * 登录 * @element 元素类的对象 */ public void loginbtn(){ UMENG_LOGIN_BTN.click(); } /* * 清空用户名、密码并重新输入 * @usr 用户名 * @pwd 密码 * @element 元素类的对象 */ public void caozuo(String usr,String pwd){ UMENG_USER.clear(); UMENG_PWD.clear(); UMENG_USER.sendKeys(usr); UMENG_PWD.sendKeys(pwd); } /* * 提交 * @element 元素类的对象 */ public void submit(){ SUBMIT_LOGIN.click(); }
4.NewTest.java TestNG 测试框架 运行入口
package com.TestNG; import org.testng.annotations.Test; import com.common.Common; import com.page.Login; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.DataProvider; import org.testng.annotations.BeforeTest; import org.testng.annotations.AfterTest; public class NewTest { private String url = "http://mobile.umeng.com/apps"; private By by = By.xpath(".//*[@id='ump']/div[1]/h2/span[2]"); private Login login = null; private Common comm = null; private WebDriver driver = null; @Test(dataProvider = "dp") public void f(String usr, String pwd) { comm.LoadPage(driver);//加载页面 comm.PauseTime(1000);//等待1秒 login.loginbtn();//登录按钮,弹出登录窗口 comm.WaitTimeOut(driver, by); login.caozuo(usr, pwd); //输入用户名、密码 login.submit();//提交登录 } @DataProvider public Object[][] dp() { return new Object[][] { new Object[] { "xxx", "xxx" }, }; } @BeforeTest public void beforeTest() { comm = new Common(); driver = new ChromeDriver();//打开谷歌浏览器 login = new Login(driver); comm.Load(driver,url); //访问友盟首页 } @AfterTest public void afterTest() { comm.Exit(driver); //退出 } }
浙公网安备 33010602011771号