一、摘要

本篇博文将介绍自动化测试实现页面元素、页面对象及测试代码分离在自动化框架中的实现

二、解析页面元素定位信息

首先,将页面元素与实际的代码分离,首先我们将页面元素定位信息和定位表达式保存在属性文件中,例如我们PaaS平台提供Mysql服务的页面,在工程中新建一个名为MysqlService.properties的文件,文件中保存内容类似如下:

[MySQL数据库服务]
[MySQL数据库服务-列表]
paas.mysql.refreshbutton=xpath>//*[@id='app']/section/section/main/div[2]/div/div[5]/button
paas.mysql.createnewinstance=xpath>//*[@id='app']/section/section/main/div[2]/div/div[4]/button
paas.mysql.searchinstancenameinput=xpath>//*[@id="app"]/section/section/main/div[2]/div/div[1]/label[1]/div/input
paas.mysql.searchinstancenamebutton=xpath>//*[@id='app']/section/section/main/div[2]/div/div[1]/label[2]/button
paas.mysql.searchspacename=xpath>//*[@id='app']/section/section/main/div[2]/div/div[3]/label[2]/div/div[1]/input
paas.mysql.operation=xpath>//*[@id='app']/section/section/main/section/div[1]/div[3]/table/tbody/tr[1]/td[6]/div/div/span
paas.mysql.operationrestart=xpath>/html/body/ul/li[1]
paas.mysql.operationrelease=xpath>/html/body/ul/li[2]
paas.mysql.operationmanage=xpath>/html/body/ul/li[3]
paas.mysql.operationlog=xpath>/html/body/ul/li[4]
paas.mysql.operationmonitor=xpath>/html/body/ul/li[5]
paas.mysql.confirmrestart=xpath>/html/body/div[1]/div/div[3]/button[2]
paas.mysql.cancelrestart=xpath>/html/body/div[1]/div/div[3]/button[1]
paas.mysql.releaseconfirmbutton=xpath>/html/body/div[1]/div/div[3]/button[2]
paas.mysql.releasecancelbutton=xpath>/html/body/div[1]/div/div[3]/button[1] [MySQL数据库服务-创建]
paas.newinstance.instancename=xpath>//*[@id='app']/section/section/main/div[3]/div/div[2]/main/form/div[2]/div/div[1]/input
paas.newinstance.description=xpath>//*[@id='app']/section/section/main/div[3]/div/div[2]/main/form/div[10]/div/div/textarea
paas.newinstance.standard5.6=xpath>//*[@id='app']/section/section/main/div[3]/div/div[2]/main/form/div[4]/div/div/label[1]/span
paas.newinstance.standard5.7=xpath>//*[@id='app']/section/section/main/div[3]/div/div[2]/main/form/div[4]/div/div/label[2]/span
paas.newinstance.instancestandard=xpath>//*[@id='app']/section/section/main/div[3]/div/div[2]/main/form/div[5]/div/div/div[1]/input
paas.newinstance.1c1gb=xpath>/html/body/div[2]/div[1]/div[1]/ul/li[1]
paas.newinstance.1c2gb=xpath>/html/body/div[2]/div[1]/div[1]/ul/li[2]
paas.newinstance.2c8gb=xpath>/html/body/div[2]/div[1]/div[1]/ul/li[3]

三、解析定位元素属性文件

满足selenium的8中定位方式

/*
* @FileName GetElementUtil: this util is use for getting page element
* @author davieyang
* @create 2018-08-21 16:37
*/
package util;
import org.openqa.selenium.By; import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties; public class GetElementUtil {
private Properties properties;
/**
* 用于读取存储页面元素的属性文件
* @param propFile 属性文件的绝对路径,应定义为常量
*/
public GetElementUtil(String propFile){
properties = new Properties();
try{
FileInputStream in = new FileInputStream(propFile);
properties.load(in);
in.close();
}catch (IOException e){
System.out.println("读取对象文件出错");
e.printStackTrace();
}
} /**
* @param elementName 存储在属性文件中的元素名称"pass.spacemanagement.releasebutton"
* @return 根据传入的页面元素名获取元素,返回元素定位
* @throws Exception "输入的locator Type 未在程序中定义:" + locatorType
*/
public By getLocator(String elementName) throws Exception{
//根据变量ElementNameInproFile,从属性配置文件中读取对应的配置对象
String locator = properties.getProperty(elementName);
//将配置对象中的定位类型存到locatorType变量,将定位表达式的值存到locatorValue变量
String locatorType = locator.split(">")[0];
String locatorValue = locator.split(">")[1];
/**
* 配置文件均默认为ISO-8859-1编码存储,使用getBytes方法可以将字符串编码转换为UTF-8
* 以此来解决读取中文为乱码的问题
*/
locatorValue = new String(locatorValue.getBytes("ISO-8859-1"), "UTF-8");
//输出locatorType变量值和locatorValue变量值,验证是否赋值正确
System.out.println("获取的定位类型:" + locatorType + "\t获取的定位表达式" + locatorValue);
//根据locatorType的变量值内容判断返回何种定位方式的By对象
if(locatorType.toLowerCase().equals("id"))
return By.id(locatorValue);
else if(locatorType.toLowerCase().equals("name"))
return By.name(locatorValue);
else if(locatorType.toLowerCase().equals("classname")||(locatorType.toLowerCase().equals("class")))
return By.className(locatorValue);
else if(locatorType.toLowerCase().equals("tagname")||(locatorType.toLowerCase().equals("tag")))
return By.tagName(locatorValue);
else if(locatorType.toLowerCase().equals("linktext")||(locatorType.toLowerCase().equals("link")))
return By.linkText(locatorValue);
else if(locatorType.toLowerCase().equals("partiallinktext"))
return By.partialLinkText(locatorValue);
else if(locatorType.toLowerCase().equals("cssselector")||(locatorType.toLowerCase().equals("css")))
return By.cssSelector(locatorValue);
else if(locatorType.toLowerCase().equals("xpath"))
return By.xpath(locatorValue);
else
throw new Exception("输入的locator Type 未在程序中定义:" + locatorType);
}
}

四、将页面元素封装成对象

/*
* @FileName StoreManagement: 封装Mysql页面对象
* @outhor davieyang
* @create 2018-08-08 11:12
*/
package pageobject.resourcemanagement;
import util.GetElementUtil;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import static constants.Constants.MysqlService_Property;
import static util.JavaScriptToDo.highLightElement; public class MySQLService {
private static WebElement element = null;
/**指定页面元素定位表达式配置文件的绝对路径
*
*/
private static GetElementUtil getElementUtil = new GetElementUtil(MysqlService_Property);
private WebDriver driver;
public MySQLService(WebDriver driver){
this.driver = driver;
} /**返回MySQL数据库服务页面“刷新”按钮的页面元素对象
*
* @param driver 浏览器驱动
* @return
* @throws Exception 获取定位信息失败
*/
public static WebElement refresh_Button(WebDriver driver) throws Exception{
//调用GetElementUtil中的getLocator方法获取配置文件中关于用户名的定位方式和定位表达式
element = driver.findElement(getElementUtil.getLocator("paas.mysql.refreshbutton"));
highLightElement(driver, element);
return element;
} /**返回MySQL数据库服务页面“创建实例”按钮的页面元素对象
*
* @param driver 浏览器驱动
* @return
* @throws Exception 获取定位信息失败
*/
public static WebElement create_New_Instance_Button(WebDriver driver) throws Exception{
//调用GetElementUtil中的getLocator方法获取配置文件中关于用户名的定位方式和定位表达式
element = driver.findElement(getElementUtil.getLocator("paas.mysql.createnewinstance"));
highLightElement(driver, element);
return element;
} /**
*
* @param driver
* @return
* @throws Exception
*/
public static WebElement search_Instance_Name_Input(WebDriver driver) throws Exception{
//调用GetElementUtil中的getLocator方法获取配置文件中关于用户名的定位方式和定位表达式
element = driver.findElement(getElementUtil.getLocator("paas.mysql.searchinstancenameinput"));
highLightElement(driver, element);
return element;
}
/**返回MySQL数据库服务页面MySQL名称检索控件的页面元素对象
*
* @param driver 浏览器驱动
* @return
* @throws Exception 获取定位信息失败
*/
public static WebElement search_Instance_Name_Button(WebDriver driver) throws Exception{
//调用GetElementUtil中的getLocator方法获取配置文件中关于用户名的定位方式和定位表达式
element = driver.findElement(getElementUtil.getLocator("paas.mysql.searchinstancenamebutton"));
highLightElement(driver, element);
return element;
} /**返回MySQL数据库服务页面运行空间检索控件页面元素对象
*
* @param driver 浏览器驱动
* @return
* @throws Exception 获取定位信息失败
*/
public static WebElement search_Space_Name(WebDriver driver) throws Exception{
//调用GetElementUtil中的getLocator方法获取配置文件中关于用户名的定位方式和定位表达式
element = driver.findElement(getElementUtil.getLocator("paas.mysql.searchspacename"));
highLightElement(driver, element);
return element;
}
}

五、测试代码

package testscript;
import org.apache.log4j.xml.DOMConfigurator;
import org.openqa.selenium.*;
import org.testng.Assert;
import org.testng.annotations.*;
import util.KeyActionsUtil;
import static util.KeyActionsUtil.*;
import java.util.List;
import static appmodule.MysqlService.linkToMysqlPage;
import static util.KeyBoardUtil.pressTabKey;
import static util.LogUtil.info;
import static pageobject.resourcemanagement.MySQLService.*;
import static util.ScrollBarUtil.scrolltoBottom;
import static util.WaitElementUtil.sleep;
// @Listeners({util.TestReport.class})
public class Test_Mysql { static {
DOMConfigurator.configure("log4j.xml");
}
@BeforeClass
public void setUp()throws Exception {
WebDriver driver = KeyActionsUtil.initBrowser("chrome");
linkToMysqlPage(driver, "yangdawei", "alex005x");
sleep(2000);
} @Test(priority = 0, description = "测试创建mysql数据库服务1CPU2G")
public void test_CreateMysqlInstance() throws Exception {
create_New_Instance_Button(driver).click();
info("点击创建实例按钮...");
sleep(1000);
info("等待3秒...");
instance_Name_in_Create_Instance_Dialog(driver).sendKeys("automationtest");
info("输入实例名:automationtesta");
sleep(1000);
info("等待3秒...");
//页面存在相同属性的元素,取所有放到list里,用序号操作
List<WebElement> radios = driver.findElements(By.className("el-radio-button__inner"));
radios.get(1).click();
sleep(1000);
info("选择数据库版本5.7...");
instance_Standard_in_Create_Instance_Dialog(driver).click();
info("点击实例规格...");
sleep(2000);
info("等待2秒...");
one_Core_two_GB(driver).click();
info("选择1CPU2GB...");
storage_Space_in_Create_Instance_Dialog(driver).clear();
info("清空存储空间字段...");
storage_Space_in_Create_Instance_Dialog(driver).sendKeys("1");
info("输入1G....");
scrolltoBottom(driver);
sleep(2000);
pressTabKey();
outsideaccess_Checkbox_in_Create_Instance_Dialog(driver).click();
info("选择外部链接...");
password_in_Create_Instance_Dialog(driver).sendKeys("111111");
info("输入密码111111...");
repassword_in_Create_Instance_Dialog(driver).sendKeys("111111");
info("确认密码111111...");
description_in_Create_Instance_Dialog(driver).sendKeys("automationtest");
info("描述信息输入automationtest");
sleep(2000);
submit_Button_in_Create_Instance_Dialog(driver).sendKeys(Keys.ENTER);
info("确认创建...");
sleep(2000);
refresh_Button(driver).click();
Assert.assertTrue(driver.getPageSource().contains("automationtest"));
Assert.assertTrue(driver.getPageSource().contains("创建中"));
}
@Test(priority = 1, description = "重启mysql服务")
public void test_RestartMysqlInstance()throws Exception {
operation_Button(driver).click();
info("点击列表里最后一列的...");
sleep(2000);
info("等待3秒...");
operation_Restart_Button(driver).click();
info("点击下拉菜单中的重启按钮...");
sleep(2000);
info("等待3秒...");
restart_Confirm_Button(driver).click();
info("点击确定按钮...");
sleep(2000);
info("等待3秒...");
Assert.assertTrue(driver.getPageSource().contains("重启请求成功"));
Assert.assertTrue(driver.getPageSource().contains("重启中"));
} @Test(priority = 2, description = "管理mysql服务页面")
public void test_Review_Basic_Mysql_Info()throws Exception{
operation_Button(driver).click();
info("点击列表里最后一列的...");
sleep(2000);
info("等待3秒...");
operation_Manage_Button(driver).click();
info("点击下拉菜单里的管理按钮...");
sleep(2000);
info("等待三秒");
assertString(driver,"基本信息");
}
@Test(priority = 3, description = "管理mysql服务页面")
public void test_Review_Mysql_Link()throws Exception{
database_Link_Tab(driver).click();
sleep(2000);
Assert.assertTrue(driver.getPageSource().contains("210.13.50.105"));
} @Test(priority = 4,description = "查看Mysql日志")
public void test_ReviewLog()throws Exception{
operation_Button(driver).click();
info("点击列表里最后一列的...");
sleep(2000);
info("等待3秒...");
operation_Log_Button(driver).click();
info("点击下拉菜单中的日志按钮...");
sleep(2000);
info("等待3秒...");
extend_Button_in_Log_Page(driver).click();
info("点击展开按钮...");
sleep(2000);
info("等待3秒...");
datefrom_in_Log_Page(driver).click();
info("点击第一个日期空间,弹出下拉...");
sleep(2000);
info("等待3秒...");
datefrom_by_Date_in_Log_Page(driver).clear();
datefrom_by_Date_in_Log_Page(driver).sendKeys("2018-09-01");
info("输入日期”2018-09-01");
sleep(2000);
info("等待3秒...");
datefrom_Sure_Button_in_Log_Page(driver).click();
info("点击确定按钮...");
sleep(2000);
info("等待3秒...");
search_Button_in_Log_Page(driver).click();
info("点击筛选按钮...");
sleep(2000);
info("等待3秒...");
Assert.assertTrue(driver.getPageSource().contains("Initializing database")); } @Test(priority = 5, description = "查看Mysql服务监控")
public void test_MonitorMysqlService()throws Exception{
operation_Button(driver).click();
info("点击列表里最后一列的...");
sleep(3000);
info("等待3秒...");
operation_Monitor_Button(driver).click();
info("点击下拉菜单里的监控按钮...");
sleep(3000);
info("等待3秒...");
} @Test(priority = 6, description = "释放mysql服务")
public void test_ReleaseMysqlService()throws Exception{
operation_Button(driver).click();
info("点击列表里最后一列的...");
sleep(3000);
info("等待3秒...");
operation_Release_Button(driver).click();
info("点击下拉菜单里的释放按钮...");
sleep(3000);
info("等待3秒...");
release_Confirm_Button(driver).click();
info("点击确定按钮...");
sleep(3000);
info("等待3秒...");
Assert.assertTrue(driver.getPageSource().contains("操作成功"));
Assert.assertTrue(driver.getPageSource().contains("删除中"));
} @AfterClass
public void afterMethod(){
driver.quit();
}
}

Java&Selenium自动化测试实现页面元素、页面对象及测试代码分离的更多相关文章

  1. 《手把手教你》系列技巧篇(四十五)-java+ selenium自动化测试-web页面定位toast-上篇(详解教程)

    1.简介 在使用appium写app自动化的时候介绍toast的相关元素的定位,在Web UI测试过程中,也经常遇到一些toast,那么这个toast我们这边如何进行测试呢?今天宏哥就分两篇介绍一下. ...

  2. 《手把手教你》系列技巧篇(四十六)-java+ selenium自动化测试-web页面定位toast-下篇(详解教程)

    1.简介 终于经过宏哥的不懈努力,偶然发现了一个toast的web页面,所以直接就用这个页面来夯实一下,上一篇学过的知识-处理toast元素. 2.安居客 事先声明啊,宏哥没有收他们的广告费啊,纯粹是 ...

  3. 《手把手教你》系列技巧篇(十四)-java+ selenium自动化测试-元素定位大法之By xpath上卷(详细教程)

    1.简介 按宏哥计划,本文继续介绍WebDriver关于元素定位大法,这篇介绍定位倒数二个方法:By xpath.xpath 的定位方法, 非常强大.  使用这种方法几乎可以定位到页面上的任意元素. ...

  4. 《手把手教你》系列技巧篇(十五)-java+ selenium自动化测试-元素定位大法之By xpath中卷(详细教程)

    1.简介 按宏哥计划,本文继续介绍WebDriver关于元素定位大法,这篇介绍定位倒数二个方法:By xpath.xpath 的定位方法, 非常强大.  使用这种方法几乎可以定位到页面上的任意元素. ...

  5. 《手把手教你》系列技巧篇(十六)-java+ selenium自动化测试-元素定位大法之By xpath下卷(详细教程)

    1.简介 按宏哥计划,本文继续介绍WebDriver关于元素定位大法,这篇介绍定位倒数二个方法:By xpath.xpath 的定位方法, 非常强大.  使用这种方法几乎可以定位到页面上的任意元素. ...

  6. 《手把手教你》系列技巧篇(十七)-java+ selenium自动化测试-元素定位大法之By css上卷(详细教程)

    1.简介 CSS定位方式和xpath定位方式基本相同,只是CSS定位表达式有其自己的格式.CSS定位方式拥有比xpath定位速度快,且比CSS稳定的特性.下面详细介绍CSS定位方式的使用方法.xpat ...

  7. 《手把手教你》系列技巧篇(十八)-java+ selenium自动化测试-元素定位大法之By css中卷(详细教程)

    1.简介 按计划今天宏哥继续讲解倚天剑-css的定位元素的方法:ID属性值定位.其他属性值定位和使用属性值的一部分定位(这个类似xpath的模糊定位). 2.常用定位方法(8种) (1)id(2)na ...

  8. 《手把手教你》系列技巧篇(十九)-java+ selenium自动化测试-元素定位大法之By css下卷(详细教程)

    1.简介 按计划今天宏哥继续讲解css的定位元素的方法.但是今天最后一种宏哥介绍给大家,了解就可以了,因为实际中很少用. 2.常用定位方法(8种) (1)id(2)name(3)class name( ...

  9. 《手把手教你》系列技巧篇(四十八)-java+ selenium自动化测试-判断元素是否可操作(详解教程)

    1.简介 webdriver有三种判断元素状态的方法,分别是isEnabled,isSelected 和 isDisplayed,其中isSelected在前面的内容中已经简单的介绍了,isSelec ...

随机推荐

  1. python+opencv实现车牌定位

    写在前面 HIT大三上学期视听觉信号处理课程中视觉部分的实验三,经过和学长们实验的对比发现每一级实验要求都不一样,因此这里标明了是2019年秋季学期的视觉实验三. 由于时间紧张,代码没有进行任何优化, ...

  2. 【VS开发】Return与Exit的区别

    1. exit用于结束正在运行的整个程序,它将参数返回给OS,把控制权交给操作系统:而return 是退出当前函数,返回函数值,把控制权交给调用函数. 2. exit是系统调用级别,它表示一个进程的结 ...

  3. 最新 4399java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.4399等10家互联网公司的校招Offer,因为某些自身原因最终选择了4399.6.7月主要是做系统复习.项目复盘.Leet ...

  4. 实现base标签中有绝对路径

    1.首先在jsp页面中写一段神奇的JAVA代码 <% String path = request.getContextPath(); String basePath = request.getS ...

  5. centos7服务搭建常用服务配置之二:Rsync+sersync实现数据实时同步

    目录 1.RSYNC数据备份 1.1 rsync服务简介 1.2 rsync特点和优势 1.3 rysnc运行模式简介 1.4 数据同步方式 2 Rsync实验测试 2.1 实验环境说明 2.2 服务 ...

  6. jquery 实时监听输入框值变化的完美方案

    只需要同时绑定 oninput 和 onpropertychange 两个事件,但是这并不完美 $('.input-form :input').bind('input propertychange', ...

  7. [转帖]SPARC简介

    https://www.cnblogs.com/chaohm/p/5674886.html 1.    概述 SPARC(Scalable Processor ARChitecture,可扩展处理器架 ...

  8. HDU1401(双向BFS)

    题意:http://acm.hdu.edu.cn/showproblem.php?pid=1401 给你8*8的棋盘和4个棋子初始位置.最终位置,问你能否在8次操作后达到该状态. 思路: 双向BFS, ...

  9. php 求两个数组的差集应该注意的事情

    对于 phper 来说 array_diff 这个函数应该知道它的用途,获取两个数组的差集,我理解中的差集是这样的 但是执行下代码会发现结果并不是 <?php $a = [1,2,3,4,5]; ...

  10. 网络编程[第二篇]基于udp协议的套接字编程

    udp协议下的套接字编程 一.udp是无链接的    不可靠的 而上篇的tcp协议是可靠的,会有反馈信息来确认信息交换的完成与否 基于udp协议写成的服务端与客户端,各司其职,不管对方是否接收到信息, ...