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();
    }
}

 

posted @ 2015-07-16 13:55  米蓝  阅读(1710)  评论(0编辑  收藏  举报