5.1对象库(UI Map)

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

  被测网页的网址:

  http://www.baidu.com

  Java语言版本的API实例代码 

  首先实现ObjectMap工具类,供测试程序调用  

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 ElementNameInpropFile) throws Exception{
//根据变量ElementNameInpropFile,从属性配置文件中读取对象 String locator = properties.getProperty(ElementNameInpropFile);
//将配置文件中的值存储至locatorType及locatorValue
String locatorType = locator.split(":")[0];
String locatorValue = locator.split(":")[1];
//输出locatorType变量值和locatorValue变量值,验证是否赋值正确
System.out.println("获取的定位类型:"+locatorType+"\t获取的定位表达式"+locatorValue);
//根据localtorType的变量值内容判断返回何种定位方式的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.className(locatorValue);
else if(locatorType.toLowerCase().equals("partiallinktext"))
return By.name(locatorValue);
else if((locatorType.toLowerCase().equals("cssselector")) || (locatorType.toLowerCase().equals("css")))
return By.className(locatorValue);
else if(locatorType.toLowerCase().equals("xpath"))
return By.name(locatorValue);
else
throw new Exception("输入的locator type 未在程序中被定义:"+locatorType);
}
public static void main(String[] args) { } }

  

建立ObjectMap.properties存储元素表达式的资源文件

Baidu.HomePage.search = id:kw
Baidu.HomePage.button = id:su

在测试类中调用ObjectMap工具类的方法实现测试逻辑

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class testSohuMailLoginByObjectMap {
WebDriver driver;
String url = "https://www.baidu.com/";
private ObjectMap objectMap;
@Test
public void testSohuMailLogin()throws Exception {
try {
//声明一个objectMap对象实例,路径为ObjectMap的绝对路径
objectMap = new ObjectMap("E:\\project\\WebDriverAPIProj\\src\\cn\\api1\\ObjectMap.properties");
} catch (Exception e) {
System.out.println("生成ObjectMap对象失败");
}
WebDriverWait wait = new WebDriverWait(driver, 10);
//调用objectMap的getLocator方法获取元素位置
WebElement search = driver.findElement(objectMap.getLocator("Baidu.HomePage.search"));
WebElement button = driver.findElement(objectMap.getLocator("Baidu.HomePage.button"));
//在搜索框中输入要搜索的内容
search.sendKeys("selenium");
//单击搜索按钮
button.click();
//断言判断页面上是否显示了selenium
Assert.assertTrue(driver.getPageSource().contains("selenium"));
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to(url);
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

  5.2操作富文本框(方法一)

  目的:能定位到页面的富文本框,使用JavaScript语句实现富文本框中的HTML格式内容输入

  被测网页的网址:

  https://mail.sohu.com

  Java语言版本的API实例代码 

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class testMailSendMail {
WebDriver driver;
String url = "https://mail.sohu.com";
@Test
public void testQQMailWriteEMail() throws InterruptedException {
//设置显示等待15秒 用于判断元素是否加载成功 防止因加载问题找不到元素
WebDriverWait wait = new WebDriverWait(driver, 15);
//使用显示等待出现用户名输出框
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='theme']/form/div[1]/div[1]/input")));
//找到用户名输入框
WebElement userName = driver.findElement(By.xpath("//*[@id='theme']/form/div[1]/div[1]/input"));
//找到密码框
WebElement password = driver.findElement(By.xpath("//*[@id='theme']/form/div[2]/div[1]/input"));
//找到登录按钮
WebElement loginButton = driver.findElement(By.xpath("//*[@id='theme']/form/div[5]/input"));
//输入用户名
userName.sendKeys("******@sohu.com");
//输入密码
password.sendKeys("****");
//单击登录按钮
loginButton.click();
//显示等待写信按钮的出现
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='addSkinClass']/div[4]/div/ul/li[1]")));
//找到写信按钮
WebElement writeMailButton = driver.findElement(By.xpath("//*[@id='addSkinClass']/div[4]/div/ul/li[1]"));
//单击写信按钮
writeMailButton.click();
//显示等待写信界面出现
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='subject']/input")));
//找到接收人输入框
WebElement receipiets = driver.findElement(By.xpath("//*[@id='mailContent']/div/div[1]/div[1]/div[1]/div[1]/div/span/input"));
//找到主题输入框
WebElement subject = driver.findElement(By.xpath("//*[@id='mailContent']//div[@class='subject']/input"));
//输入接收人
receipiets.sendKeys("cyw894452087@sohu.com");
//选中主题框
subject.click();
//输入主题
subject.sendKeys("发给自己的测试邮件");
//跳转至富文本框的iframe中
driver.switchTo().frame("ueditor_0");
//声明JavaScriptExecutor对象来执行JavaScript脚本
JavascriptExecutor js = (JavascriptExecutor)driver;
//获取富文本框的编辑对象使用编辑对象的innerHtml属性设定任意HTML格式的文字内容
js.executeScript("document.getElementsByTagName('body')[0].innerHTML ='<b>邮件要发送的内容<b>'");
//返回默认的页面
driver.switchTo().defaultContent();
//找到发送按钮
WebElement sendMailButton = driver.findElement(By.xpath("//*[@id='mailContent']/div/div[2]/span[1]"));
//单击发送
sendMailButton.click();
Thread.sleep(1000);
//断言判断页面上是否存在发送成功
Assert.assertTrue(driver.getPageSource().contains("发送成功"));
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

  Java语言版本的API实例代码(方法二)

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod; import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class testSohuMailSendMail {
WebDriver driver;
String url = "https://mail.sohu.com";
@Test
public void testSendMail() throws InterruptedException {
//设置显示等待15秒
WebDriverWait wait = new WebDriverWait(driver, 15);
//显示等待用户名输入框出现后执行之后语句
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@placeholder='请输入您的邮箱']")));
//找到用户名输入框
WebElement userName = driver.findElement(By.xpath("//input[@placeholder='请输入您的邮箱']"));
//找到密码输入框
WebElement password = driver.findElement(By.xpath("//input[@placeholder='请输入您的密码']"));
//找到登录按钮元素
WebElement loginButton = driver.findElement(By.xpath("//input[@value='登 录']"));
//输入邮箱名
userName.sendKeys("****@sohu.com");
//输入密码
password.sendKeys("****");
//单击登录
loginButton.click();
//显示等待页面跳转出现写邮件
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//li[text()='写邮件']")));
//找到写邮件元素
WebElement writeMailButton = driver.findElement(By.xpath("//li[text()='写邮件']"));
//单击写邮件
writeMailButton.click();
//显示等待写信窗口出现
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='subject']/input")));
//找到接收方输入框元素
WebElement recipients = driver.findElement(By.xpath("//input[@class='noshadow ng-pristine ng-valid']"));
//找到主题输入框元素
WebElement subject = driver.findElement(By.xpath("//div[@class='subject']/input"));
//输入接收方邮箱
recipients.sendKeys("cyw894452087@sohu.com");
//调用函数,按下tab键跳至主题输入框
pressTabKey();
//输入主题
subject.sendKeys("发送给自己的邮件");
subject.click();
Thread.sleep(1500);
//调用函数跳转至富文本框编辑区域
pressTabKey();
Thread.sleep(1500);
//调用粘贴函数将内容粘贴至富文本框中
setAndctrlVClipboardData("邮件正文内容");
//找到发送元素
WebElement sendMailButton = driver.findElement(By.xpath("//span[text()='发送']"));
//点击发送文件
sendMailButton.click();
Thread.sleep(1500);
//断言判断页面上是否出现发送成功字样
Assert.assertTrue(driver.getPageSource().contains("发送成功"));
}
public void setAndctrlVClipboardData(String string) {
StringSelection stringSelection = new StringSelection(string);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
//调用keyPress方法实现按下ctrl键
robot.keyPress(KeyEvent.VK_CONTROL);
//调用keyPress方法实现按下v键
robot.keyPress(KeyEvent.VK_V);
//调用keyRelease方法松开v键
robot.keyRelease(KeyEvent.VK_V);
//调用keyRelease方法松开ctrl键
robot.keyRelease(KeyEvent.VK_CONTROL);
}
public void pressTabKey() {
Robot robot =null;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

两种方法比较如下:

方法1优点:可以支持HTML格式的文字内容作为富文本框的文字输入内容

方法1的缺点:因为各种网页产品的富文本框实现机制不同,定位到富文本框的文本编辑区域较难,对fram的进出方式和html代码的含义需比较熟悉,对定位能力要求较高

方法2的优点:不管何种类型的富文本框,只要找到它上面的紧邻元素,通过按tab键均可进入富文本框编辑区域

方法2的缺点:不能在富文本框编辑区域进行HTML格式的文本输入

以上两种方法各有利弊,只要能稳定的完成富文本框文字内容的输入即可

WebDriver高级应用实例(5)的更多相关文章

  1. WebDriver高级应用实例(10)

    10.1控制HTML5语言实现的视频播放器 目的:能够获取html5语言实现的视频播放器视频文件的地址.时长.控制进行播放暂停 被测网页的网址: http://www.w3school.com.cn/ ...

  2. WebDriver高级应用实例(9)

    9.1封装操作表格的公用类 目的:能够使自己编写操作表格的公用类,并基于公用类进行表格中的元素的各类操作 被测网页的网址的HTML代码: <html> <body> <t ...

  3. WebDriver高级应用实例(8)

    8.1使用Log4j在测试过程中打印日志 目的:在测试过程中,使用Log4j打印日志,用于监控和后续调试测试脚本 被测网页的网址: http://www.baidu.com 环境准备: (1)访问ht ...

  4. WebDriver高级应用实例(7)

    7.1在测试中断言失败的步骤进行屏幕截图 目的:在测试过程中,在断言语句执行失败时,对当前的浏览器进行截屏,并在磁盘上新建一个yyyy-mm-dd格式的目录,并在断言失败时新建一个已hh-mm-ss格 ...

  5. WebDriver高级应用实例(6)

    6.1精确比较网页截图图片 目的:对于核心界面进行截屏,并且使用测试过程中的截图和以前测试过程中的截图进行比较.确认页面是否发生了改变 被测网页的网址: http://www.baidu.com Ja ...

  6. WebDriver高级应用实例(4)

    4.1操作web页面的滚动条 被测网页的网址: http://v.sogou.com Java语言版本的API实例代码 import org.testng.annotations.Test; impo ...

  7. WebDriver高级应用实例(3)

    3.1自动化下载某个文件 被测网页的网址: https://pypi.org/project/selenium/#files Java语言版本的API实例代码 import java.util.Has ...

  8. WebDriver高级应用实例(2)

    2.1在日期选择器上进行日期选择 被测网页的网址: https://www.html5tricks.com/demo/Kalendae/index.html Java语言版本的API实例代码 impo ...

  9. WebDriver高级应用实例(1)

    1.1使用JavaScriptExecutor单击元素 被测网页的网址: http://www.baidu.com Java语言版本的API实例代码 import org.testng.annotat ...

随机推荐

  1. 2018.12.19 atcoder Iroha and a Grid(组合数学)

    传送门 组合数学好题. 给你一个hhh行www列的网格,其中左下角aaa行bbb列不能走,问从左上角走到右下角有多少种走法(每次只能向右或者向下) 我们考虑分步计数. 我们一共能走的区域是总网格区域去 ...

  2. 2018.11.08 NOIP模拟 景点(倍增+矩阵快速幂优化dp)

    传送门 首先按照题意构造出转移矩阵. 然后可以矩阵快速幂求出答案. 但是直接做是O(n3qlogm)O(n^3qlogm)O(n3qlogm)的会TTT掉. 观察要求的东西发现我们只关系一行的答案. ...

  3. 2018.11.06 bzoj1097: [POI2007]旅游景点atr(最短路+状压dp)

    传送门 预处理出不能在每个点停留之后才停留的点的状态. 对kkk个点都跑一次最短路存下来之后只需要简单状压一下就能过了吐槽原题空间64MB蒟蒻无能为力 然后用fillfillfill赋极大值的时候当m ...

  4. ACM-ICPC 2018 徐州赛区网络预赛 J Maze Designer(最大生成树,倍增lca)

    https://nanti.jisuanke.com/t/31462 要求在一个矩形中任意选两个点都有唯一的通路,所以不会建多余的墙. 要求满足上述情况下,建墙的费用最小.理解题意后容易想到首先假设全 ...

  5. c#文件比较Code

    我想我们很多时候想比较一个文件里面是否有改动,比如一个dll库是新加了一个方法或修改了其中的方法实现,不能通过可视化的工具来比较的时候,可以用这个小工具来比较, 以下是比较文件的代码. using S ...

  6. MySQL 安装与使用(一)

    操作系统:CentOS release 5.10 (Final) 文件准备: MySQL-server-community-5.1.73-1.rhel5.i386.rpm MySQL-client-c ...

  7. css 特殊使用技巧

    1  border颜色设置 border-color: transparent black black black;   分别设置四条边框的颜色  上边transparent 透明无色 2  阴影 t ...

  8. urllib.parse.parse_qsl 的一个小问题

    最近在使用urllib时发现的一个问题,记录一下. 首先请分别执行下面这两句代码: 1."你好".encode("utf8").decode("gbk ...

  9. (记忆化搜索) FatMouse and Cheese(hdu 1078)

    题目大意:   给n*n地图,老鼠初始位置在(0,0),它每次行走要么横着走要么竖着走,每次最多可以走出k个单位长度,且落脚点的权值必须比上一个落脚点的权值大,求最终可以获得的最大权值   (题目很容 ...

  10. Java案例:超市库存管理系统

    案例介绍: 模拟真实的库存管理逻辑,完成超市管理系统的日常功能实现,见下图 案例需求分析: 根据案例介绍,我们进行分析,首先需要一个功能菜单,然后输入功能序号后,调用序号对应的功能方法,实现想要的操作 ...