zoukankan      html  css  js  c++  java
  • java读取properties文件,并在配置文件中设置默认浏览器驱动

    java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properties,文件中,可以用"#"来作注释

    Properties类的重要方法

    Properties 类存在于胞 Java.util 中,该类继承自 Hashtable
    1. getProperty ( String  key) ,   用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。

    2. load ( InputStream  inStream) ,从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String  key) 来搜索。
    3. setProperty ( String  key, String  value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。 

    4. store ( OutputStream  out, String  comments) ,   以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。

    5. clear () ,清除所有装载的 键 - 值对。该方法在基类中提供。

    1、新建PropUtil类

    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Map;
    import java.util.Properties;
    import java.util.concurrent.ConcurrentHashMap;
    
    public class PropUtil {
        private static Map<String, Properties> propFileMap = new ConcurrentHashMap<String, Properties>();    //键值对,key是string类型,value是properties类型
    public static Properties getProperties() {
            return getProperties("/config.properties");
        }
        public static Properties getProperties(String fileName) {
            Properties prop = propFileMap.get(fileName);   //获取key对应的value值
            if (prop == null) {
                prop = new Properties();
            }
            InputStream is = null;
            try {
                is = PropUtil.class.getResourceAsStream(fileName);
                prop.load(is);   //从输入流中读取属性列表
                propFileMap.put(fileName, prop);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (is != null)
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
            return prop;
        }
    }

    2、新建DriverManage类,根据字段值来判断使用哪个浏览器驱动

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    
    public class DriverManage {
        public static WebDriver driver = null;
        
        public static WebDriver getDriver(String runDriver) throws Exception {
            switch (Integer.parseInt(runDriver)) {
            case 1:
                driver = new FirefoxDriver();            
                break;
    
            case 2:
                driver = new InternetExplorerDriver();
                break;
    
            case 3:
                driver = new ChromeDriver();
                break;
            }
            return driver;
        }
    
    }

    三、在/src/main/resources目录下新建config.properties,设置启动浏览器的值为1,则启动火狐浏览器

    browser = 1

    四、新建TestBase类,设置一些浏览器的操作,从配置文件中读取数据

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    import Hnjb.Hnjb.DriverManage;
    import Hnjb.Hnjb.PropUtil;
    
    public class TestBase {
        protected WebDriver driver;
        protected String browser;
        
        @BeforeTest
        public void setUp() throws Exception {
            Properties prop = PropUtil.getProperties();
            browser = prop.getProperty("browser");
            driver = DriverManage.getDriver(browser);
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        }
        
        @AfterTest
        public void tearDown() throws Exception {
    
            driver.quit();
        }
    
    }

    如果想要别的浏览器驱动,则修改配置文件的值即可

  • 相关阅读:
    23Flutter FloatingActionButton实现类似闲鱼App底部导航凸起按钮:
    Emgu.CV 播放视频-本地文件/RTSP流
    SDL 截图、录像、录像播放
    直接操作 SDL_Overlay YUV叠加上的像素
    SDL 显示解码后的yuv12数据
    SDL绑定播放窗口 及 视频窗口缩放
    SDL鼠标事件
    SDL文字和图形
    SDL第一个程序:加载一张图片
    SDL简介(网络汇总)
  • 原文地址:https://www.cnblogs.com/fulucky/p/8065450.html
Copyright © 2011-2022 走看看