一、概述
WebDriver加载浏览器,我们需要掌握主流的三个浏览器的使用,谷歌、火狐和IE。
二、基本目录结构
其中files下存放的是相应使用的浏览器对应的driver。lib下存储的是selenium2使用的jar包。
三、三种浏览器的启动方式
1、FireFox火狐浏览器启动
实现代码如下:
package com.webdriver.selenium2;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FireFoxDemo {
public static void main(String[] args) {
//实例化火狐浏览器
WebDriver driver = new FirefoxDriver();
//打开导航栏
Navigation na = driver.navigate();
//导航到百度
na.to("http://www.baidu.com");
//关闭浏览器
driver.close();
driver = null;
}
}
请注意selenium_webdriver是默认使用firefox来进行浏览器加载的,所以FireFox是在标准目录下,如果我们使用其他的浏览器进行加载webdriver,比如Chrom。我们此时需要先设置其chromdriver.exe的路径,然后在加载dirver。具体代码实现时要加上下面这行代码:
System.setProperty("Webdriver.chrome.driver", "files/chromedriver.exe");
2、动态加载插件
我们知道,每打开一个网页,都会有网络的更新 变化,我们实现自动化的时候,肯定希望可以即时监控网络资源:具体指:
试想一下,每打开一个网页的时候,可以即时Export网络资源文件,自动化是多么每秒的一件事情。基于这样的想法我们需要加载插件:
首先:将firebug.xpi(这个存在与firebug安装的根目录下)放到我们工程的files目录下。
其次:补充个使用firefox时非常有用的知识,叫做:about:config。
此时如果我们想加载firebug插件,就选择extensions.firebug.currentVersion使用FirefoxProfile类下的setPreference()方法进行配置加载。具体代码实现如下:
package com.webdriver.selenium2;
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class Demo {
public static void main(String[] args) {
myplug();
}
public static void myplug(){
File file = new File("files:\\firebug-2.0.19-fx.xpi");
FirefoxProfile fp = new FirefoxProfile();
//因为不能保证插件是否可以成功安装,所以需要捕获异常
try {
fp.addExtension(file);
} catch (IOException e) {
System.out.println("IO异常"+e);
}
fp.setPreference("extensions.firebug.currentVersion","2.0.19" );
WebDriver driver = new FirefoxDriver();
Navigation na = driver.navigate();
na.to("http://www.baidu.com");
}
}
当然越将工作简单化,自动化做的就会越成功,如果不用代码加载浏览器插件,可以通过浏览器自身配置,直接配置得当,更是好的方法。
package com.webdriver.selenium2;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
public class Demo01 {
public static void main(String[] args) {
firefox();
}
public static void firefox(){
//实例化浏览器默认的所有配置
ProfilesIni pf = new ProfilesIni();
//加载浏览器默认的所有配置
FirefoxProfile fpf = pf.getProfile("default");
//实例化火狐浏览器时进行加载
WebDriver driver = new FirefoxDriver(fpf);
//打开导航栏
Navigation na = driver.navigate();
//导航到百度
na.to("http://www.baidu.com");
//关闭浏览器
driver.close();
driver = null;
}
}
此时我们再次调用firefox,希望启动浏览器的同时,插件不单只是加载进来,还应该是启动状态,此时about:config(这个非常有用)
找到这个配置extensions.firebug.allPagesActivation,将它的值设置为on,再次启动firefox时,firebug就会跟着自动启动,如果把这个配置的值设置为off,再次调用的时候就不会自动启动。
3、网络资源导出扩展
继续我们自动导出网络资源的话题,还是在about:config中进行相应的配置。我们需要配置的内容如下:
extensions.firebug.netexport.alwaysEnableAutoExport,需要把这个值置为true
extensions.firebug.netexport.defaultLogDir,这个用来配置导出的文件的存储路径
extensions.firebug.netexport.saveFiles,这个值设置为true.