Ethon

为什么要有方法,因为懒惰是一种美德。

   :: 首页  :: 新随笔  ::  ::  :: 管理

UI对象库:使用配置文件存储测试页面上的定位和定位表达式,做到定位数据和程序的分离。

第一步:实现工具类Object工具类,供测试程序调用。

/**
 * 使用配置文件存储测试页面上的定位和定位表达式,做到定位数据和程序的分离
 */
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.By;

public class ObjectMap {

    Properties properties;

    public ObjectMap(String propFile) {
        properties = new Properties();
        try {
            FileInputStream in = new FileInputStream(propFile);
            properties.load(in);
            in.close();
        } catch (IOException e) {
            System.out.println("读取对象文件出错");
            e.printStackTrace();
        }
    }

    public By getLocator(String ElementNameInpopFile) throws Exception {
        // 根据变量ElementNameInpopFile,从属性配置文件中读取对应的配置对象
        String locator = properties.getProperty(ElementNameInpopFile);
        
        // 将配置对象中的定位类型存储到locatorType变量,将定位表达式的值存储到locatorValue变量中
        String locatorType = locator.split(":")[0];
        String locatorValue = locator.split(":")[1];
        
        // 在Eclipse中的配置文件均默认为ISO-8859-1编码存储,使用getBytes方法可以将字符串编码转换为UTF-8编码,以此来解决在配置文件读取中文乱码的问题
        locatorValue = new String(locatorValue.getBytes("ISO-8859-1"), "UTF-8");
        // 输出locatorType变量值和locatorValue变量值,验证是否赋值正确
        System.out.println("获取的定位类型:" + locatorType + "\t 获取的定位表达式:" + locatorValue);
        
        // 根据locatorType的变量值内容判断返回何种定位方式的By对象
        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 {
            throw new Exception("输入的 locator type 未在程序中被定义:" + locatorType);
        }
    }
}

 

 第二步:对要测试的网页进行分析,把需要定位的元素的定位表达式存放在配置文件中(此处为ObjectMap.properties)

QQ.Login.frame=id:switcher_plogin
QQ.Email.username=id:u
QQ.Email.password=id:p
QQ.Email.login_button=id:login_button

 

第三步:编写测试类

/**
 * 实现程序与数据的分离,主要分为3步:
 * 1.从UI对象库文件ObjectMap.properties文件取得测试页面需要操作的页面元素的定位方式和定位表达式
 * 2.从ObjectMap类取得该页面元素的实例对象
 * 3.返回给测试用例方法中,进行后续处理
 */
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class ObjectMapTest {
    private WebDriver driver;
    private ObjectMap objectMap;
    String url;
    
    @BeforeMethod
    public void beforeMethod(){
        url="https://en.mail.qq.com";
        System.setProperty("webdriver.chrome.driver", "e:\\chromedriver.exe");
        driver = new ChromeDriver();
    }
    
    @AfterMethod
    public void afterMethod(){
        driver.quit();
    }
    
    @Test
    public void test(){
        driver.get(url);
        //设置10秒超时时间
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        
        try {
            //声明一个objectMap对象,参数是ObjectMap.properties的绝对路径
            objectMap = new ObjectMap("F:\\workspace\\TestNGProj\\ObjectMap.properties");
            
            driver.switchTo().frame("login_frame");
            //调用objectMap实例的getLocator方法
            WebElement frame = driver.findElement(objectMap.getLocator("QQ.Login.frame"));
            WebElement username = driver.findElement(objectMap.getLocator("QQ.Email.username"));
            WebElement password = driver.findElement(objectMap.getLocator("QQ.Email.password"));
            WebElement button = driver.findElement(objectMap.getLocator("QQ.Email.login_button"));

            frame.click();
            username.sendKeys("xxxx@qq.com");
            password.sendKeys("xxxx");
            button.click();
            Thread.sleep(3000);
            //断言页面上是否显示了Sign out字符
            Assert.assertTrue(driver.getPageSource().contains("Sign out"));

        } catch (Exception e) {
            System.out.println("生成object对象失败");
            e.printStackTrace();
        }
    }
}

 

posted on 2019-05-14 17:14  Ethon  阅读(339)  评论(0编辑  收藏  举报