码迷,mamicode.com
首页 > 其他好文 > 详细

自动化框架学习二,判断定位方式

时间:2017-11-14 22:30:37      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:自动化学习

/**判断定位方式工具类*/
public class GetByLoctorTest {
    
    /**
     *读取配置文件
     *@param key String 定位方式
     * */
    public static By getLocator(String key){
        
        //ProUtilTest类是读取配置文件的(自定义),使用构造方法时,需传入配置文件的路径
        //文件的路径可以单独写一个类,进行配置
        ProUtilTest properties = new ProUtilTest("configs/login.properties");
        
        //从属性配置文件中读取相应的配置对象
        //locator=name>登录
        String locator = properties.getPro(key);
        
        //将配置对象中的定位类型存在locatorType变量,将定位表达式的值存入到locatorValue变量
        //[0]为>的左边 [1]为>的右边
        String locatorType = locator.split(">")[0];//取出>前的name
        String locatorValue = locator.split(">")[1];//取出登录
        
        //输出locatorType变量值和locatorValue变量值,验证是否赋值成功
        System.out.println("获取的定位类型:"+locatorType+"获取的定位表达式:"+locatorValue);
        
        //根据locatorType的变量值内容判断,返回何种定位方式的By对象
        //toLowerCase() 方法用于把字符串转换为小写
        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{
            try{
                throw new Exception("输入的 locator type 未在程序中被定义:" + locatorType);    
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        return null;
        
    }
}

自动化框架学习二,判断定位方式

标签:自动化学习

原文地址:http://13478818.blog.51cto.com/13468818/1981816

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!