• java+Selenium+TestNg搭建自动化测试架构(2)实现跨浏览器功能


    1.切换浏览器类:其中包含了切换浏览器的方法,以及关闭浏览器,设置等待时间,以及重写的断言方法

    package com.rrx.framework;

    import java.io.IOException;
    import java.util.concurrent.TimeUnit;

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    import org.testng.Assert;
    import org.testng.Reporter;
    import org.testng.annotations.Parameters;

    public class BorwserEngin {
    public String browserName;
    public String URL;
    public WebDriver driver;

    public void initConfigDate() throws IOException {
    browserName = PropertiesEngine.getProperties("browserName");
    URL = PropertiesEngine.getProperties("URL");
    //System.out.println(browserName);
    }
    @Parameters("Browser")
    public WebDriver getDriver() throws IOException {
    initConfigDate();
    Logger.getLogger().info("浏览器名称"+browserName);
    Logger.getLogger().info("URL:"+URL);
    System.out.println(browserName);
    if (browserName.equalsIgnoreCase("Firefox")) {
    System.setProperty("webdriver.firefox.bin", "");
    driver = new FirefoxDriver();
    } else if (browserName.equalsIgnoreCase("Chrome")) {
    System.setProperty("webdriver.chrome.driver",
    "C:\Users\Administrator\workspace\SeleniumKuangJia\driver\chromedriver.exe");
    driver = new ChromeDriver();
    } else if (browserName.equalsIgnoreCase("IE")) {
    System.setProperty("webdriver.ie.driver", "");
    driver = new InternetExplorerDriver();
    }
    driver.get(URL);
    return driver;
    }

    /**
    * //关闭浏览器并且推出
    */
    public void tearDown() {
    driver.quit();
    }
    /**
    * 隐式时间等待方法
    * imlicitlyWait是隐式等待,一般在查找元素的时候使用。例如,我设置一个查找元素最大时间为10秒,使用了
    imlicitlyWait后,如果第一次没有找到元素,会在10秒之内不断循环去找元素,知道超过10秒,报超时错误。
    */
    public void callTime(int time){

    driver.manage().timeouts().implicitlyWait(time, TimeUnit.SECONDS);
    }

    /**
    * 断言方法
    */
    public static void assertEqual(Object object,Object object2){
    try {
    Assert.assertEquals(object, object2,"不相同");
    } catch (Error e) {

    Reporter.log(""+e);//断言错误是把日志打印在测试报告中
    Logger.getLogger().info(e);//打印在logger日志中

    }

    }
    }

    2.日志的输出采用long4j和Reporter两种方法。Reporter会将日志打印到测试报告中。如果不知道具体方法的可以查看我之前的博客。

  • 相关阅读:
    Android TextView内容过长加省略号
    PowerDesigner物理模型用法总结
    [K/3Cloud] 在设计时复制已有表单菜单或菜单项快速建立菜单
    cocos2d-x 多分辨率适配详解(转载),以前北京团队设计的游戏,也是用这套方案
    xcode 开发ios兼容性问题的上下黑边 和 coco2d-x 游戏分辨率适配 ResolutionPolicy::FIXED_WIDTH 都会引起上下黑边问题!!!
    孟岩的c++ 的学习方法,这何尝有不是做人做事的方法呢?
    Mac OS X 启用 Web 服务器
    android 64位的so文件 报错
    mac 用 brew
    C/C++语言参数传递----函数/方法 参数的指针引用传递
  • 原文地址:https://www.cnblogs.com/liwei09k1/p/8109783.html
Copyright © 2020-2023  润新知