对象库(UI MAP)

目的:能够使用配置文件存储被测页面上元素的定位方式和定位表达式,做到定位数据和程序的分离。

测试程序写好以后,可以方便不具备编码能力的测试人员进行自定义修改和配置 ;

package dataDriverTest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class ObjectMap {
    public WebDriver driver;
    private Properties properties;

    public ObjectMap(String propFile) {
        properties = new Properties();
        try {
        
String   encoding="utf-8";   
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(propFile), encoding));
FileInputStream in = new FileInputStream(propFile); properties.load(in); in.close(); } catch (FileNotFoundException e) { System.out.println("没有找到 " + propFile + " 文件!无法读取!"); e.printStackTrace(); } catch (IOException e) { System.out.println("读取 " + propFile + "文件失败! "); e.getStackTrace(); } } /* * ElementNameInPropFile 从文件中获取对象的值; locatorType : 存储对象类型; * locatorValue:存储对象的值; */ public By getLocator(String ElementNameInPropFile) throws Exception { // 根据 变量 ElementNameInPropFile ,从属性配置文件中读取对应的配置对象; String locator = properties.getProperty(ElementNameInPropFile); /* * 将配置对象中的定位类型存储到 locatorType 中 ,将定位表达式的值存储到 locatorValue 中; */ System.out.println(locator); String locatorType = locator.split(":")[0]; String locatorValue = locator.split(":")[1]; System.out.println(locatorType + locatorValue); if (locatorType.toLowerCase().equals("id")) { return By.id(locatorValue); } else if (locator.toLowerCase().equals("name")) { return By.name(locatorValue); } else if (locator.toLowerCase().equals("classname") || locatorType.toLowerCase().equals("class")) { return By.className(locatorValue); } else if ((locatorType.toLowerCase().equals("tagname")) || (locatorType.toLowerCase().equals("tag"))) { return By.tagName(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 { throw new Exception("输入的locatorType " + locatorType + "未在程序中被定义!"); } } }

ObjectMap.properties存储的原素的表达式文件内容:

Sohumai.Homepage.username =id:username

Sohumai.Homepage.password =id:password

Sohumai.Homepage.submitbutton =id:submitbutton

测试代码:

package testNGPractice;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import cn.gloryroad.Log;
import dataDriverTest.ObjectMap;
import scr.comm.OpenBrowserInfo;

public class TestObjectMap {
    private ObjectMap objectmap;
    public WebDriver driver;
    private String url = "http://192.168.1.110:7001/CISS/";;

    @Test
    public void test() throws Exception {
        driver.navigate().to(url);

        try {
            objectmap = new ObjectMap("D:\\objectmap.properties");
        } catch (Exception e) {
            e.printStackTrace();

        }
        WebElement username = driver.findElement(objectmap.getLocator("Sohumai.Homepage.username"));
        WebElement password = driver.findElement(objectmap.getLocator("Sohumai.Homepage.password"));
        WebElement submitbutton = driver.findElement(objectmap.getLocator("Sohumai.Homepage.submitbutton"));

        username.sendKeys("wlg3");
        password.sendKeys("88888888");
        submitbutton.click();
    }

    @BeforeMethod
    public void beforeMethod() {
        OpenBrowserInfo.IeDriver();
        driver = new InternetExplorerDriver();
    }

    @AfterMethod
    public void afterMethod() {
        driver.close();
    }

}

 测试public By getLocator(String ElementNameInPropFile) throws Exception {}方法:

package java_practice;

import dataDriverTest.ObjectMap;
import scr.comm.OpenBrowserInfo;

public class T {

    public static void main(String[] args) {
    //    String pfile1="D:\\softerWare\\selenium\\eclipse_workspace\\objectmap.properties";
        String pfile2="C:\\Users\\ty\\Documents\\Tencent Files\\316567803\\FileRecv\\objectMap.properties";
        //String pfile = "D:\\objectmap.properties";
        String uname = "ciss.username";
        String pwd = "ciss.password";
        String a="boc.public.searchBtn";
        String b="boc.public.searchBox";
        OpenBrowserInfo.Log4jInitialize();
        ObjectMap obj = new ObjectMap(pfile2);
        System.out.println("---------------");
        try {
            obj.getLocator(a);
            obj.getLocator(b);
            obj.getLocator(pwd);
            obj.getLocator(uname);
            

            System.out.println("---------------");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

 

posted @ 2016-12-05 16:59  linbo.yang  阅读(1061)  评论(2编辑  收藏  举报