zoukankan      html  css  js  c++  java
  • selenium2 用Yaml文件进行元素管理 (五)

    比如界面有一个按钮,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

    解析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();
        }
    }
  • 相关阅读:
    717. 1比特与2比特字符
    697. 数组的度
    674. 最长连续递增序列
    665. 非递减数列
    661. 图片平滑器
    643. 子数组最大平均数 I
    plink计算两个SNP位点的连锁不平衡值(LD)
    GWAS群体分层校正,该选用多少个PCA
    PyCharm的安装和应用
    GWAS后续分析:多基因风险评分(Polygenic Risk Score)的计算
  • 原文地址:https://www.cnblogs.com/milanmi/p/4636503.html
Copyright © 2011-2022 走看看