selenium多浏览器支持

编写配置文件如下:

#配置需要使用的浏览器
#(Firefox ,Chrome or IE)
#browser = Firefox
#browser = Chrome
browser = IE

#配置需要测试的网站
webSite = http://zc.qq.com/chs/index.html

#报告名称
reportName = 注册QQ号测试

#浏览器驱动为位置
#browserLocation = F:/selenium/Mozilla Firefox/Mozilla Firefox/firefox.exe
#browserLocation = F:/Google/chromedriver.exe
browserLocation  = C:/Program Files/Internet Explorer/IEDriverServer.exe

#浏览器启动时需要加载的插件的位置
#fireBugLocation = webdriver.firefox.bin
#fireBugLocation = webdriver.chrome.driver
fireBugLocation  = webdriver.ie.driver

浏览器启动文件如下:

package SecondTest;

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class SetUp {
    private static Logger log = Logger.getLogger(SetUp.class);//日志
    
      public WebDriver lanuch() throws Exception{
          Properties config = new Properties();
          WebDriver dr = null;
          InputStreamReader fn = new InputStreamReader(new FileInputStream
                  (System.getProperty("user.dir")+"/src/SecondTest/config.properties"),"UTF-8");
          config.load(fn);
          String browser = config.getProperty("browser");//读取要使用的浏览器
          String fileBugLoacation = config.getProperty("fireBugLocation");//读取浏览器插件位置
          String browserLocation = config.getProperty("browserLocation");//读取浏览器驱动位置
          String weburl = config.getProperty("webSite");//读取测试网站
          
          if(browser.equals("Firefox")){//使用火狐浏览器
              System.setProperty //环境配置
              (fileBugLoacation, browserLocation);
              log.info("打开Firefox浏览器");
              dr=new FirefoxDriver();//打开浏览器
              dr.get(weburl);
          }else if(browser.equals("Chrome")){//使用谷歌浏览器
              System.setProperty
              (fileBugLoacation,browserLocation);
              ChromeOptions opt = new ChromeOptions();

                opt.setBinary("C:/Users/Administrator/AppData/Local/Google/Chrome/Application/chrome.exe");
              //谷歌浏览器地址

              log.info("打开Chrome浏览器");
              dr = new ChromeDriver(opt);
              dr.get(weburl);
          }else{//使用IE浏览器
                System.setProperty
              (fileBugLoacation,browserLocation);
              log.info("打开IE浏览器");
              dr = new InternetExplorerDriver();
              dr.get(weburl);
          }
              log.info(dr);
              dr.manage().window().maximize();
              return dr;
      }
}

原文地址:https://www.cnblogs.com/Mr-xiao/p/6993020.html