比如界面有一个按钮,id号是test。如果进行对象化的话,就是test.click就可以了。不用每次都要去创建test对象。如果id号变了,我们也只需要改一下test的名称就行了。

使用Yaml需要用到下载jyaml的jar包。下载地址是http://www.java2s.com/Code/Jar/j/Downloadjyaml13jar.htm

TestBaidu.yaml

baidu_button:
type:id
value:su
baidu_input:
type:id
value:kw

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdgAAACTCAIAAACiU5/VAAAOJElEQVR4nO2dvW8cxxXACbhwJ+QPEJCegAGXrJJC6k2XhgJ1AqgyhQQ1TiEYCBBAgAtVZOM0lot0EYwUShy7Ne3aUm0RhD8gUwkYmUfyUpx9Ws7HmzdvZnd2l78fHoS72fl4e7f3u9Hs3nJjCS349Mk+QRDEKjZaGwkA4LKDiAEAGrPx6tWrkvaFzQEAABEDADQGEQMANCZPxPfu3XNKus03NjY2NooWnZU9lA/UR1cAADa0Ir73K06503wYEZcP1FNXAAAGas6IlwNKbTwilptjeQBIgogRMQA0BhFPWMQscAPMg15EvPEr7mAXiW2N5tqp0K3mtMrVk5Cz3LNyd4R8YhW6u5lsrt9TABgn9UXsqCr4OFYSK/S78s2l6STWs5Bnsmd5LPl7RX4qv3oAMBt6X5pYl5SIONm2UMRCSU8iTu5myR4BwLQYTsTdEuF/3IhYOS4AzIZBRaz57zYiVo4LALOBpQlEDACNaX+yTmkc/cm62KJHDP3JumDPwjeNvDXruyTWOYIGmAHZP3F2fuhccvmaUy1YGGy+jLhY2Cog56zpWR5O2Brb39grkzUuAEwF7r4GANAYRAwA0JiZizi2GML/6AFgPMxcxAAA4wcRAwA0BhEDADSm8uVrs0S5pszSMwDYUInY/x3HugQROzX7TgYA5se4RDx1kU09fwBoAiKuydTzB4AmGE/WzU/EVVZ4ETEAGLCIuI+TdfKvLfy7Pfi3YljG78kgb3I6sTVcJ5DeVQCAi4xFxL9kI/pOftotESrbXBlr5fSMiAHAQLaInfXiIZcmulPgZMOsyrbEqvQMAFD5fsSl2fQjYp8qiSFiAK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lO42GiP2IzYhlO/vND0MaVYpV30QYDhETMw67iFckr5o41Bm5zs4gYi9yJ7mx5sGayTcuWEFo5Qzn5IyLiblGtT+VpBlMMHKdnelNi7Hl4Msj4lylyltjiw/JfnAxMcuw/Kmk4E1/skb1jeyYzj/55txQOLwzEVf6/QTrCFZNitjpofvUfzzYuxt0rkHEGkU65ZrJsmb5OLZYjI6JOYXlTyUtQ7fBtA2/NnJQjsHHsZJYoV8uPM21pNA2tqntJDp3eSHXdxUVqVy5JogZhH1pInmyLjsVndRiJbFCuTdlz8mc9V8k41/NIAhi4JiGiLvlwjKCTcQ+uTnL4/r/EgRBdGNEIj4UbeUv9caaJ8srzk+7i7/y7iBigiBiMQ0Rj39pQugKERMEIYfxOuJaJ+sCCekMG/SaRsT+4oN+SSSZWO4pR4IgiMPcnzjXunxNSkixOuyv5yYXebubgi6WV4eDW2M5xHYHERMEEYze70dMEARByIGICYIgGgciJgiCaByD/hVnAADwQcQAAI1BxAAAjUHEAACNQcQAAI1BxAAAjUHEAACNQcQAAI1BxAAAjUHEAACNQcQAAI1BxAAAjUHEAACNQcQAAI1BxAAAjUHEAACNQcQAAI1BxAAAjUHEAACNQcQAAI1BxAAAjUHEAACNQcQAAI1BxAAAjUHEAACNQcQAAI1BxAAAjUHEAACNQcQAAI1BxAAAjfk/VvxyYHX+fKQAAAAASUVORK5CYII=" alt="" />

解析yaml类 YamlUtil.java

package com.test.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map; import org.ho.yaml.Yaml;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement; public class YamlUtil {
private String yamlfile;
private WebDriver driver;
private Map<String,Map<String,String>> ml;
public YamlUtil(WebDriver driver,String yamlFilePath){
this.yamlfile =yamlFilePath;
this.driver=driver;
getYamlFile();
}
public void getYamlFile(){
File f = new File(this.yamlfile); try {
ml= Yaml.loadType(new FileInputStream(f.getAbsolutePath()), HashMap.class);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//创建by对象
private By getBy(String key){
By by = null;
if (ml.containsKey(key)){
Map<String,String> m = ml.get(key);
String type = m.get("type");
String value = m.get("value");
switch(type){
case "id":
by = By.id(value);
break;
case "name":
by = By.name(value);
break;
case "xpath":
by = By.xpath(value);
break;
case "class":
by = By.className(value);
break;
case "linkText":
by = By.linkText(value);
break;
case "cssSelector":
by= By.cssSelector(value);
break;
}
}
else
{
System.out.println("Locator "+key+" is not exist in "+yamlfile);
}
return by;
}
//根据名称 返回webElement对象
public WebElement getElement(String key){
By by = this.getBy(key);
WebElement element = driver.findElement(by);
return element;
}
}

调用:

package info.milan.webdriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; import com.test.util.YamlUtil; import common.Assertion; public class day10 {
public WebDriver Driver;
public YamlUtil myYaml;
//case,suit单元用例里面,存在多个case可以成为一个簇
//每个class执行之前调用
@BeforeClass
public void Bclass(){
///浏览器初始化
Driver = new FirefoxDriver();
Driver.manage().window().maximize();
//加载yaml文件
myYaml = new YamlUtil(Driver,"yamlFile/TestBaidu.yaml");
}
//每个用例执行之前调用
@BeforeMethod
public void setup(){
Driver.navigate().to("https://www.baidu.com");
}
//每个用例执行完毕之后调用
@AfterMethod
public void teardown(){
}
//每个class执行之后调用
@AfterClass
public void Aclass(){
///浏览器关闭
Driver.close();
Driver.quit();
}
//测试用例数据
@DataProvider(name="logOutDataPro")
public Object[][]loginOutData(){
return new Object[][]{{"1"},{"很长很长的观坚持"},{"特殊字符+!@¥"}};
}
//执行测试用例
@Test(dataProvider="logOutDataPro")
public void baidu(String info){
//获取webElement
WebElement baiduInput = myYaml.getElement("baidu_input");
baiduInput.clear();
baiduInput.sendKeys(info);
WebElement baiduButton = myYaml.getElement("baidu_button");
baiduButton.click();
}
}

最新文章

  1. 1Z0-053 争议题目解析481
  2. 关于git中git pull --rebase中的一些坑
  3. mysql SELECT FOR UPDATE语句使用示例
  4. [CareerCup] 10.3 Integer not Contain in the File 文件中不包含的数
  5. Qt4过渡至Qt5
  6. 【转发】Linux下清除系统日志方法
  7. dubbo 解决Multicast java.net.SocketException: No such device
  8. LinkButton(按钮)
  9. uniquery 配合 mssql 自带存储过程实现分页
  10. 在linux后台执行脚本
  11. Ubuntu Server修改IP、DNS、hosts
  12. java实现代理domino web邮件下载
  13. Cocos2D场景中对象引用为nil时的判断
  14. C#设计模式(5)——建造者模式
  15. PAT甲级1057 Stack【树状数组】【二分】
  16. 通过GUID确保winform运行唯一实例
  17. 基于bootstrap表单登录(带验证码)
  18. js获取本机id
  19. 快捷切换hosts的小工具:SwitchHosts!
  20. 201621123018《Java程序设计》第4周学习报告

热门文章

  1. Extjs的js函数
  2. House Robber
  3. Fragment的基本用法
  4. CDOJ 1071 秋实大哥下棋 线段树
  5. jquery判断浏览器版本插件,jquery-browser.js
  6. PHP系列笔记——Zend_Controller工作流程
  7. 在ireport中使用checkbox
  8. 设计模式 - 观察者模式(Observer Pattern) 详细解释
  9. Django数据库操作性能相关
  10. JSR-303校验类型
  11. Micropython 如何用Turnipbit做一个自动浇水装置
  12. 第一章 Python基本语法元素分析(二)
  13. 近期待学习&amp;目标内容
  14. poj1947(树形背包)
  15. list、map、数组 转换
  16. js_加入收藏夹功能
  17. [VS 2015] VS2015 完整ISO镜像包
  18. DIV+CSS实战(四)
  19. P1796 汤姆斯的天堂梦
  20. Python面试题之Python中的lambda map filter reduce zip