Selenium2(WebDriver)启动浏览器、设置profile、加载插件

一、Driver下载地址

http://docs.seleniumhq.org/download/

二、启动firefox浏览器(不需要下载驱动,原生支持)

1、firefox安装在默认路径下:

 //启动默认安装路径下的ff
public void StartFireFoxByDefault(){
    System.out.println("start firefox browser...");
    WebDriver driver = new FirefoxDriver();
    Navigation navigation = driver.navigate();
    navigation.to("http://www.baidu.com/");
    System.out.println("start firefox browser succeed...");        
}

2、firefox未安装在默认路径下:

public static void StartFireFoxNotByDefault(){
    System.out.println("start firefox browser...");
    System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe");  
    WebDriver driver = new FirefoxDriver();
    Navigation navigation = driver.navigate();
    navigation.to("http://www.baidu.com/");
    System.out.println("start firefox browser succeed...");        
}

3、启动firefox时加载插件:

  首先,要知道我们为什么需要加载插件?原因是webdriver在启动浏览器时,启动的一个干净的没有任务、插件及cookies信息的浏览器(即使你本机的firefox安装了某些插件,webdriver启动firefox也是没有这些插件的),但是有可能被测系统本身需要插件或者需要调试等等,此时可以用如下方法在启动firefox时加载插件,下面示例加载firebug插件:

 public static void StartFireFoxLoadPlugin(){
    System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe");
    File file = new File("files/firebug-2.0.7-fx.xpi");
    FirefoxProfile profile = new FirefoxProfile();
    try {
        profile.addExtension(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    profile.setPreference("extensions.firebug.currentVersion", "2.0.7");
    //active firebug extensions
    profile.setPreference("extensions.firebug.allPagesActivation", "on");    
    WebDriver driver = new FirefoxDriver(profile);
    driver.get("http://www.baidu.com");
    System.out.println("start firefox browser succeed...");    
}

4、启动firefox时设置profile:

  上面提到过webdriver启动firefox时是启动一个完全新的浏览器,我们除了可以使用上面提到的方法定制插件,webdriver还可以对profile进行定制(在firefox地址栏中输入about:config,可以查看firefox的参数),下面设置代理和默认下载路径:

public static void StartFireFoxByProxy(){
    String proxyIp = "10.17.171.11";
    int proxyPort = 8080;
    System.out.println("start firefox browser...");
    System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe");
     
    FirefoxProfile profile = new FirefoxProfile();
    //设置代理参数
    profile.setPreference("network.proxy.type", 1);
    profile.setPreference("network.proxy.http", proxyIp);
    profile.setPreference("network.proxy.http_port", proxyPort);

    //设置默认下载路径
    profile.setPreference("browser.download.folderList", 2);
    profile.setPreference("browser.download.dir", "D:\\");

    WebDriver driver = new FirefoxDriver(profile);
    driver.get("http://www.baidu.com");

    System.out.println("start firefox browser succeed...");    
}

 5、启动本机器的firefox配置: 

  每次启动如果都像上面那样在代码里面配置profile比较麻烦,可以使用下面的方法启动本机器的firefox的配置,换句话说就是我们可以事先配置本机的firefox然后用webdriver启动它,这样本机上的firefox安装了什么插件都可以直接使用了,不需要在配置profile:

public static void StartLocalFirefox(){
    System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe");
    ProfilesIni pi = new ProfilesIni();
    FirefoxProfile profile = pi.getProfile("default");
    WebDriver driver = new FirefoxDriver(profile);
    driver.get("http://www.baidu.com/");
}

6、如果在机器B上要启动机器A上的firefox配置,可以先导出A的配置,然后加载:

1、将A机器上的Profiles文件夹”C:\Users\cloudchen\AppData\Local\Mozilla\Firefox\Profiles”给拷贝出来到某个目录

2、代码:

public static void StartFireFoxByOtherConfig(){
    System.out.println("start firefox browser...");
    System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla Firefox/firefox.exe");        
    File file = new File("files\\lg6mie1i.default");//profiles文件目录,这里我是放在工程目录下的files文件夹下
    FirefoxProfile profile = new FirefoxProfile(file);    
    WebDriver driver = new FirefoxDriver(profile);
    driver.get("http://www.baidu.com");        
    System.out.println("start firefox browser succeed...");    
}

PS:如果插件或其它东东未加载成功,可以检查下profile文件夹下是否包含插件信息。

三、启动chrome浏览器

 1、启动chrome需要chromedriver的驱动:

public static void StartChrome(){
    System.out.println("start firefox browser...");        
    System.setProperty("webdriver.chrome.driver", "files\\chromedriver.exe");//指定驱动路径
    WebDriver driver = new ChromeDriver();
    driver.get("http://www.baidu.com/");
    System.out.println("start firefox browser succeed...");        
}

  另,如果不想用setProperty的方式,可以将chromedriver.exe 放在”C:\Windows\System32”路径下或者path可以找到的路径下并重启电脑即可。

2、加载插件:

public static void StartChromeLoadPlugin(){
    System.out.println("start firefox browser...");
    System.setProperty("webdriver.chrome.driver", "files\\chromedriver.exe");
    File file = new File ("files\\youtube.crx");
    ChromeOptions options = new ChromeOptions();
    options.addExtensions(file);
    WebDriver driver = new ChromeDriver(options);
    driver.get("http://www.baidu.com/");
    System.out.println("start firefox browser succeed...");    
}

四、启动IE浏览器

1、IE启动和chrome类似也需要下载相应的驱动:

public static void StartIE(){
    System.out.println("start firefox browser...");        
    System.setProperty("webdriver.ie.driver", "files\\IEDriverServer.exe");
    WebDriver driver = new InternetExplorerDriver();
    driver.get("http://www.baidu.com/");
    System.out.println("start firefox browser succeed...");        
}

2、IE下没有插件加载

3、IE的放大比例为要设置100%

4、启动IE时,需关闭如下图中4个区域的保护模式:

5、对于第4点提到的关闭保护模式,还可以使用代码关闭:

//启动IE浏览器并关闭保护模式
public static void StartIEAndCloseProtectedMode(){
    System.out.println("start firefox browser...");        
    System.setProperty("webdriver.ie.driver", "files\\IEDriverServer.exe");
    DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
    dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);

    //IE默认启动保护模式,要么手动在浏览器的设置中关闭保护模式,要么在代码中加上这一句,即可
    dc.setCapability("ignoreProtectedModeSettings", true);
    WebDriver driver = new InternetExplorerDriver(dc);
    driver.get("http://www.baidu.com/");
    System.out.println("start firefox browser succeed...");        
}
感谢作者的分享,原文路径:http://www.cnblogs.com/puresoul/p/4251536.html

猜你喜欢

转载自blog.csdn.net/yangfengjueqi/article/details/85250683
今日推荐