AnndyTuo
2018-07-22
元素层+操作层+业务层
1.1:元素层
获取定位元素
1.2:操作层
对元素进行操作
1.3:业务层
传入参数 进行业务操作
SelectDriver.java
- package PageObject.Base;
- /**
- * Setup1:Base内的封装
- * */
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.chrome.ChromeDriver;
- import org.openqa.selenium.firefox.FirefoxDriver;
- /**
- * PageObject mode的
- *
- * 第一步:封装暖气的driver对象
- *
- * 作用:根据用户输入 选择不同的浏览器
- * */
- public class SelectDriver {
- //这里只默认firefox 和 chrome两种浏览器
- public WebDriver driverName(String browersName){
- if(browersName.equalsIgnoreCase('firefox')){
- return new FirefoxDriver();
- }else{
- return new ChromeDriver();
- }
- }
- }
DriverBase.java
- package PageObject.Base;
- /**
- * Setup1:Base内的封装
- * */
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- /**
- * PageObject第二部: 封装driver
- *
- * 作用:产生driver对象
- * */
- public class DriverBase {
- private WebDriver driver;
- /**
- * 构造方法 创建对象时实例化driver
- * */
- public DriverBase(String browersName){
- SelectDriver selectDriver = new SelectDriver();
- //将SelectDriver中的driver对象赋值给'private WebDriver driver'中的driver 这样driver对象就有值了!!
- this.driver = selectDriver.driverName(browersName);
- }
- /**
- * 封装Element方法
- * */
- public WebElement element(By by){
- return driver.findElement(by);
- }
- /**
- * 封装get方法
- * */
- public void getUrl(String url){
- driver.get(url);
- }
- }
BasePage.java
- package PageObject.Base;
- /**
- * Setup1:Base内的封装
- * */
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebElement;
- /**
- * PageObject第三步:BasePage的封装
- *
- * */
- public class BasePage {
- private DriverBase driver;
- /**
- * 构造方法
- * */
- public BasePage(DriverBase driver){
- this.driver = driver;
- }
- /**
- * 封装Element
- * */
- public WebElement element(By by){
- return driver.element(by);
- }
- /**
- * 封装click操作
- * */
- public void click(WebElement element){
- //判断element是否为空 null和非null的处理方式
- if(element != null){
- element.click();
- }else{
- System.out.println('你要点击的元素不存在');
- }
- }
- /**
- * 封装SendKeys操作
- * */
- public void sendKeys(WebElement element,String context){
- //判断element是否为空 null和非null的处理方式
- if(element != null){
- element.clear();
- element.sendKeys(context);
- }else{
- System.out.println('你要输入的元素不存在 输入内容失败');
- }
- }
- }
ReadProperties.java
- package PageObject.Utils;
- import java.io.BufferedInputStream;
- import java.io.FileInputStream;
- import java.util.Properties;
- public class ReadProperties {
- private String filePath = 'LoginElement.properties';
- private Properties properties;
- /**
- * 构造方法 创建对象时自动返回pro对象 在new该对象的时候会自动加载readProperties()方法
- * */
- public ReadProperties(String filePath){
- this.filePath = filePath;
- //在new该对象的时候会自动加载readProperties()方法
- this.properties = readProperties();
- }
- /**
- * 返回已经加载properties文件的pro对象
- * */
- public Properties readProperties(){
- //创建对象
- Properties pro = new Properties();
- //读取properties文件到缓存
- try {
- BufferedInputStream in = new BufferedInputStream(new FileInputStream(filePath));
- //加载缓存到pro对象 prop.load(in)这么写 不能读取properties配置文件中的中文
- pro.load(new InputStreamReader(in, 'utf-8'));
- } catch (Exception e) {
- e.printStackTrace();
- }
- //返回pro对象
- return pro;
- }
- /**
- * 使用全局的properties对象获取key对应的value值
- * @return
- * */
- public String getValue(String key){
- return properties.getProperty(key);
- }
- }
ByMethon.java
- package PageObject.page;
- import org.openqa.selenium.By;
- import PageObject.Utils.ReadProperties;
- public class ByMethon {
- /**
- * 封装by 静态方法 直接调用
- * @throws IOException
- * */
- public static By bystr(String PropertiesKey){
- String PropertiesPath = 'LoginElement.properties';
- //创建ReadProperties对象
- ReadProperties properties = new ReadProperties(PropertiesPath);
- String value = properties.getValue(PropertiesKey);
- //对value进行拆分
- String LocateMethon = value.split('>')[0];
- String LocateEle = value.split('>')[1];
- //System.out.println(LocateMethon+'========='+LocateEle);
- if(LocateMethon.equalsIgnoreCase('id')){
- return By.id(LocateEle);
- }else if(LocateMethon.equalsIgnoreCase('name')){
- return By.name(LocateEle);
- }else if(LocateMethon.equalsIgnoreCase('tagNmae')){
- return By.tagName(LocateEle);
- }else if(LocateMethon.equalsIgnoreCase('linkText')){
- return By.linkText(LocateEle);
- }else if(LocateMethon.equalsIgnoreCase('xpath')){
- return By.xpath(LocateEle);
- }else if(LocateMethon.equalsIgnoreCase('cssSelector')){
- return By.cssSelector(LocateEle);
- }else{
- return By.partialLinkText(LocateEle);
- }
- }
- }
loginPage.java
- package PageObject.page;
- import org.openqa.selenium.WebElement;
- import PageObject.Base.BasePage;
- import PageObject.Base.DriverBase;
- public class loginPage extends BasePage{
- /**
- * 构造方法
- * */
- public loginPage(DriverBase driver) {
- super(driver);
- }
- /**
- * 获取登录按钮的element
- * super类BasePage有封装一个element方法 此处element可以直接使用
- * */
- public WebElement findLoginButtion(){
- //return super.element(ByMethon.bystr('userCountBox'));
- return element(ByMethon.bystr('userCountBox'));
- }
- /**
- * 获取'账号登录'的位置的element
- * */
- public WebElement checkCountLogin(){
- return element(ByMethon.bystr('countLogin'));
- }
- /**
- * 获取登录框的element
- * */
- public WebElement loginBox(){
- return element(ByMethon.bystr('loginUser'));
- }
- /**
- * 获取密码框的element
- * */
- public WebElement passwordElement(){
- return element(ByMethon.bystr('loginPassword'));
- }
- /**
- * 获取登录按钮的element
- * */
- public WebElement loginButtion(){
- return element(ByMethon.bystr('loginButtion'));
- }
- }
loginPageHandle.java
- package PageObject.handle;
- /**
- * 操作层
- * */
- import PageObject.Base.DriverBase;
- import PageObject.page.loginPage;
- /**
- * 封装登录页面的操作 操作loginPage下的方法
- * */
- public class loginPageHandle {
- private DriverBase driver;
- private loginPage loginpage;
- /**
- * 构造方法
- * */
- public loginPageHandle(DriverBase driver){
- this.driver = driver;
- //loginpage放在这里的目的是使用driver,保证driver的一致性
- loginpage = new loginPage(driver);
- }
- /**
- * 封装寻找登录框按钮的操作
- * */
- public void findLoginButtion(){
- loginpage.click(loginpage.findLoginButtion());
- }
- /**
- * 封装点击账号登录的方法
- * */
- public void countLogin(){
- loginpage.click(loginpage.checkCountLogin());;
- }
- /**
- * 封装登录框的操作
- * */
- public void userLogin(String username){
- loginpage.sendKeys(loginpage.loginBox(), username);
- }
- /**
- * 封装密码输入
- * */
- public void passwordLogin(String password){
- loginpage.sendKeys(loginpage.passwordElement(), password);
- }
- /**
- * 封装点击登录的操作
- * */
- public void loginAction(){
- loginpage.click(loginpage.loginButtion());
- }
- }
loginBuss.java
- package PageObject.busession;
- /**
- * 业务层
- * */
- import PageObject.Base.DriverBase;
- import PageObject.handle.loginPageHandle;
- public class loginBuss {
- private loginPageHandle loginPgeHandle;
- /**
- * 构造方法 实例化loginPageHandle
- * */
- public loginBuss(DriverBase driver){
- loginPgeHandle = new loginPageHandle(driver);
- }
- /**
- * 执行操作业务
- * */
- public void bussLogin(String username,String password){
- loginPgeHandle.findLoginButtion();
- loginPgeHandle.countLogin();
- loginPgeHandle.userLogin(username);
- loginPgeHandle.passwordLogin(password);
- loginPgeHandle.loginAction();
- }
- }
2.2.6:执行用例
CaseBase.java
- package PageObject.Case;
- import PageObject.Base.DriverBase;
- /**
- * 生成driver对象 所有的case都集成这个类
- * */
- public class CaseBase {
- /**
- * 初始化driver
- * */
- public DriverBase initDriver(String browers){
- DriverBase driver = new DriverBase(browers);
- return driver;
- }
- }
loginCase.java
- package PageObject.Case;
- import org.junit.Test;
- import PageObject.Base.DriverBase;
- import PageObject.Utils.ReadProperties;
- import PageObject.busession.loginBuss;
- /**
- * 测试登录操作
- * */
- public class loginCase extends CaseBase{
- private DriverBase driver;
- private loginBuss loginBuss;
- /**
- * 构造方法初始化loginBuss
- * */
- public loginCase(){
- this.driver = initDriver('firefox');
- loginBuss = new loginBuss(driver);
- }
- @Test
- public void LoginTest() throws Exception{
- //读取配置文件
- ReadProperties readProperties = new ReadProperties('LoginElement.properties');
- //这个driver来自于DriverBase DriverBase类下封装了gerUrl方法
- String URL = readProperties.getValue('URL');
- driver.getUrl(URL);
- Thread.sleep(3000);
- //获取username 和 password
- String username = readProperties.getValue('LoginInfo').split('>')[0];
- String password = readProperties.getValue('LoginInfo').split('>')[1];
- loginBuss.bussLogin(username, password);
- }
- }
LoginElement.properties
- #URL
- URL=https://www.csdn.net/
- #用户名和密码
- LoginInfo=你的账户>你的密码
- #页面获取登录框的位置
- userCountBox=xpath>.//*[@id='csdn-toolbar']/div/div/ul/li[5]/a[1]
- #获取'账号登录'的位置
- countLogin=xpath>html/body/div[3]/div/div/div[2]/div/h3/a
- #输入账号的位置
- loginUser=id>username
- #输入密码框的位置
- loginPassword=xpath>.//*[@id='password']
- #登录按钮的位置
- loginButtion=xpath>.//*[@id='fm1']/input[8]
联系客服