package com.trackingio.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

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.openqa.selenium.safari.SafariDriver;



public class CfReader {
	
	public static String confPath = "./config/conf.properties";
	public static WebDriver driver;

	//获取driver类型
	public static String driverType = readConfValue("browser.type");

	//获取浏览器driver path
	public static String firefoxDriverPath  = readConfValue("browser.firefoxDriverPath");
	public static String internetExplorerDriverPath = readConfValue("browser.internetExploerPath");
	//获取主账号id和pwd
	public static String priUsr = readConfValue("adminUsr");
	public static String priPwd = readConfValue("priPwd");
	//获取测试url
	public static String baseURl = readConfValue("baseURL");
	
	
	//读取配置文件数据
	public static String readConfValue(String key) {

		
		Properties prop = new Properties();
		
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(confPath));
			
			prop.load(in);
			
			String value = prop.getProperty(key);
			
			return value;
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			return null;
		}
		
	}
	
	//获取配置文件中配置的浏览器driver
	public static WebDriver getWebDriver() {

		String key = driverType;

		switch(key)
		{
		case "0": getInternetExplorerDriver();
		case "1": getFirefoxDriver();
		case "2": driver = new ChromeDriver();
		case "3": driver = new SafariDriver();
		}
			
		return driver;
		
	}
	
	//获取firefox驱动
	public static WebDriver getFirefoxDriver() {
		
		//System.setProperty("executable_path ",firefoxPath );

		System.setProperty("webdriver.gecko.driver",firefoxDriverPath);
		
		driver = new FirefoxDriver();
		
		return driver;
	}
	//获取IE驱动
	public static WebDriver getInternetExplorerDriver() {
		
		System.setProperty("webdriver.ie.driver",internetExplorerDriverPath);
		
		driver = new InternetExplorerDriver();
		
		return driver;
	}
	

}
