UI对象库:使用配置文件存储测试页面上的定位和定位表达式,做到定位数据和程序的分离。

第一步:实现工具类Object工具类,供测试程序调用。

/**
* 使用配置文件存储测试页面上的定位和定位表达式,做到定位数据和程序的分离
*/
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.By; public class ObjectMap { Properties properties; public ObjectMap(String propFile) {
properties = new Properties();
try {
FileInputStream in = new FileInputStream(propFile);
properties.load(in);
in.close();
} catch (IOException e) {
System.out.println("读取对象文件出错");
e.printStackTrace();
}
} public By getLocator(String ElementNameInpopFile) throws Exception {
// 根据变量ElementNameInpopFile,从属性配置文件中读取对应的配置对象
String locator = properties.getProperty(ElementNameInpopFile); // 将配置对象中的定位类型存储到locatorType变量,将定位表达式的值存储到locatorValue变量中
String locatorType = locator.split(":")[0];
String locatorValue = locator.split(":")[1]; // 在Eclipse中的配置文件均默认为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.className(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);
}
}
}

第二步:对要测试的网页进行分析,把需要定位的元素的定位表达式存放在配置文件中(此处为ObjectMap.properties)

QQ.Login.frame=id:switcher_plogin
QQ.Email.username=id:u
QQ.Email.password=id:p
QQ.Email.login_button=id:login_button

第三步:编写测试类

/**
* 实现程序与数据的分离,主要分为3步:
* 1.从UI对象库文件ObjectMap.properties文件取得测试页面需要操作的页面元素的定位方式和定位表达式
* 2.从ObjectMap类取得该页面元素的实例对象
* 3.返回给测试用例方法中,进行后续处理
*/
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; public class ObjectMapTest {
private WebDriver driver;
private ObjectMap objectMap;
String url; @BeforeMethod
public void beforeMethod(){
url="https://en.mail.qq.com";
System.setProperty("webdriver.chrome.driver", "e:\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod(){
driver.quit();
} @Test
public void test(){
driver.get(url);
//设置10秒超时时间
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); try {
//声明一个objectMap对象,参数是ObjectMap.properties的绝对路径
objectMap = new ObjectMap("F:\\workspace\\TestNGProj\\ObjectMap.properties"); driver.switchTo().frame("login_frame");
//调用objectMap实例的getLocator方法
WebElement frame = driver.findElement(objectMap.getLocator("QQ.Login.frame"));
WebElement username = driver.findElement(objectMap.getLocator("QQ.Email.username"));
WebElement password = driver.findElement(objectMap.getLocator("QQ.Email.password"));
WebElement button = driver.findElement(objectMap.getLocator("QQ.Email.login_button")); frame.click();
username.sendKeys("xxxx@qq.com");
password.sendKeys("xxxx");
button.click();
Thread.sleep(3000);
//断言页面上是否显示了Sign out字符
Assert.assertTrue(driver.getPageSource().contains("Sign out")); } catch (Exception e) {
System.out.println("生成object对象失败");
e.printStackTrace();
}
}
}

Selenium WebDriver UI对象库的更多相关文章

  1. UI对象库-定位元素与程序分离

    1.前言 这几天有人问我,UI自动化测试中使用到的页面定位元素应该存放在哪里比较合适?我想说的是如果你使用的是PO设计模式设计测试用例的话,可以把定位元素存在每一个page页面,一个page存放对应的 ...

  2. 读取ini配置文件 及 UI对象库

    读取ini配置文件 配置项 读取API 写入API 实战:UI 对象库 读取ini配置文件 配置项 在每个 ini 配置文件中,配置数据会被分组(比如下述配置文件中的"config" ...

  3. selenium测试框架使用xml作为对象库

    之前已经写过一篇: selenium测试框架篇,页面对象和元素对象的管理 上次使用的excel作为Locator对象管理,由于excel处理不够方便,有以下缺点: 不能实现分page 加载Locato ...

  4. selenium webdriver(3)---操作页面对象

    页面对象的相关操作可以通过接口文件org.openqa.selenium.WebElement查看,本文只是对象接口的使用方式,具体的实现方式在org.openqa.selenium.remote.R ...

  5. 对象库(UI MAP)

    目的:能够使用配置文件存储被测页面上元素的定位方式和定位表达式,做到定位数据和程序的分离. 测试程序写好以后,可以方便不具备编码能力的测试人员进行自定义修改和配置 : package dataDriv ...

  6. from selenium.webdriver.support.ui import Select

    from selenium.webdriver.support.ui import Select Select(d.find_element_by_id(u'key_开户行')).first_sele ...

  7. python3之selenium.webdriver 库练习自动化谷歌浏览器打开百度自动百度关键字

    import os,time,threading from selenium import webdriver from selenium.webdriver.common.keys import K ...

  8. selenium webdriver(2)---页面对象定位

    webdriver的元素定位很灵活,提供了多种定位方式: Id LinkText PartialLinkText Name TagName Xpath ClassName CssSelector 这些 ...

  9. selenium webdriver (python)大全

    webdriver的简介 硒2.0的主要新功能是集成的webdriver的API.webdriver的设计除了解决一些seleniumr-RC API的一些限制,与webdriver 的整合,将提供一 ...

随机推荐

  1. Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...

  2. 本地部署 Misago Docker + 配置 HTTPS 笔记

    最近答应帮朋友做个论坛网站,想借此机会捡起 Python 在 GitHub 找了一圈,打算借用以 Python+Django 开发的 Misago 这个论坛系统 由于作者在今年更新的 Misago 0 ...

  3. spring-security问题记录

    一.错误信息 Could not decode JSON for additional information: BaseClientDetails 2019-12-03 22:18:37.239 W ...

  4. Elasticsearch7.X 入门学习第九课笔记-----聚合分析Aggregation

    原文:Elasticsearch7.X 入门学习第九课笔记-----聚合分析Aggregation 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. ...

  5. regularexpressionvalidator验证控件使用实例

    一:电话号码验证: 格式如:0755-24256888:带分机格式:0755-24256888-282:手机:11位数字 (^(\d{3,4}-)?\d{6,8}$)|(^(\d{3,4}-)?\d{ ...

  6. Ionic -v1初始项目结构

    界面  

  7. 【问题解决方案】GitHub上克隆项目到本地

    说明: 克隆线上项目到本地,可以直接clone克隆,不必再自己建文件夹啊,初始化啊. 命令: git clone + 想要克隆的项目的地址 END

  8. Dubbo源码学习总结系列七---注册中心

    Dubbo注册中心是框架的核心模块,提供了服务注册发现(包括服务提供者.消费者.路由策略.覆盖规则)的功能,该功能集中体现了服务治理的特性.该模块结合Cluster模块实现了集群服务.Dubbo管理控 ...

  9. 02.Windows2012R2安装360安全卫士失败及无法卸载问题

    问题: Windows 2012 R2 安装360安全卫士失败及无法卸载,导致网络无法通信问题解决. 解决:1.进入 Windows2012R2 安全模式下:2.进行覆盖安装360安全卫士:3.覆盖安 ...

  10. Vue 学习之 vue-router2

    ---恢复内容开始--- 一.路由的安装: npm安装 npm install vue-router --save 执行命令完成vue-router的安装,并在package.json中添加了vue- ...