Java+Appium自动化测试框架(二) 定位方式

[复制链接]

1275

主题

1275

帖子

1013

积分

金牌会员

Rank: 6Rank: 6

积分
1013
分享到:
发表于 2022-5-12 15:25:44 | 显示全部楼层 |阅读模式
package com.appium.test;
/**
* @author YuFeifei
* @version 2017年11月15日 上午11:41:21
* 类说明
* 根据读取的配置文件,将key和value拆分
* 再通过split将value分成定位类型(locatorType)和元素(locatorValue)
*/
import org.openqa.selenium.By;
public class GetByLocatorTest {

public static By getLocator(String key){
ProUtilTest properties = new ProUtilTest("./configs/test1.properties");
/**属性locator 是通过key获取的value*/
String locator = properties.getProp(key);
/**属性locatorType 获取的value中通过split分离出的>前面的数据==id、name等*/
String locatorType = locator.split(">")[0];
/**属性locatorType 获取的value中通过split分离出的>后面的数据==元素*/
String locatorValue = locator.split(">")[1];

System.out.println("获取的定位类型:" + locatorType + "\t获取的元素是:" + locatorValue);

/**根据定位类型,返回定位方式*/
if (locatorType.toLowerCase().equals("id"))//toLowerCase()将大写字符转换为小写
return By.id(locatorValue);
else if (locatorType.toLowerCase().equals("name"))
return By.name(locatorValue);
else if (locatorType.toLowerCase().equals("classname"))
return By.className(locatorValue);
else if (locatorType.toLowerCase().equals("tagname"))
return By.tagName(locatorValue);
else if (locatorType.toLowerCase().equals("linktext"))
return By.linkText(locatorValue);
else if (locatorType.toLowerCase().equals("cssselector"))
return By.cssSelector(locatorValue);
else if (locatorType.toLowerCase().equals("xpath"))
return By.xpath(locatorValue);
else
try{
throw new Exception("输入的locatorType未在预设程序中被定义:" + locatorType + "请检查GetByLocatorTest这个类");
}catch (Exception e){
e.printStackTrace();
}
return null;

}
/**测试*/
public static void main(String agrs[]){
GetByLocatorTest test2 = new GetByLocatorTest();
System.out.println(test2.getLocator("LG_NAME_PHONE"));

}

}

使用高级回帖 (可批量传图、插入视频等)快速回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则   Ctrl + Enter 快速发布  

发帖时请遵守我国法律,网站会将有关你发帖内容、时间以及发帖IP地址等记录保留,只要接到合法请求,即会将信息提供给有关政府机构。
快速回复 返回顶部 返回列表