Selenium2——profile设置、启动Firefox浏览器

来源:互联网 发布:js事件处理函数 编辑:程序博客网 时间:2024/05/20 18:15

自学Selenium2 ( WebDriver ),理论和实践的差距还是很大的,所以学习任何编程语言、工具,实践是最好的老师。

进入正题,这篇文章讲述,在自动化测试时,对Firefox浏览器的profile设置、启动有所不同,需根据自己情况进行相应修改。


1 Firefox的profie设置

自动化测试时,有可能会遇到下载文件的情况,如下图;目前Selenium2还无法处理这样的对话框,但可通过对Firefox的profile预先进行设置达到自动下载的效果。


1.1 创建FirefoxProfile对象

FirefoxProfile profile = new FirefoxProfile();

1.2 设置下载路径

// 设置是否询问下载位置(可忽略);默认true——不询问,直接下载到指定路径,具体设置见browser.folderList,false——询问profile.setPreference("browser.download.useDownloadDir",true);// 指定下载位置profile.setPreference("browser.download.downloadDir", "c:\\OutputFolder");// 设置下载方式;0——下载到桌面,默认1——下载到Firefox默认位置,2——下载到指定位置profile.setPreference("browser.download.folderList", 2);// 当一个下载开始时,设置是否显示下载管理器;默认true——显示,flase——不显示profile.setPreference("browser.download.manager.showWhenStarting",false);


2 设置无需确认即可下载的文件格式

profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream, application/vnd.ms-excel, text/csv, application/zip,application/xml");

注意:

MIME类型是设置某种拓展名的文件用一种应用程序打开的方式类型。

常见文件的MIME类型:

.txttext/plain 或 text/x-log.pdfapplication/pdf.csvtext/csv.xlsapplication/vnd.ms-excel.docapplication/msword.zipapplication/zip.xmlapplication/xml.rarapplication/octet-stream.exeapplication/octet-stream.gifimage/gif.jpegimage/jpeg.htmltext/html

有时,需要的文件无法在搜索引擎上查询到其对应文件类型的MIME类型,可在浏览器中查看,如Firefox浏览器的工具栏 -> 选项 -> 应用程序。


3 启动Firefox时加载插件

WebDriver启动的是一个干净的没有任务、插件、cookies信息的Firefox浏览器(即使本机Firefox安装某些插件),但在自动化测试中可能需要插件(如Firebug)进行调试。

注意:需要下载firebug.xpi,且最好使用非Firefox浏览器下载,不然提示直接安装到Firefox;最好不要在Firebug官网中下载,因为提示你需要使用Firefox浏览器。

// 定义插件所在位置File file = new File("E:\\Firefox\\files\\firebug-2.0.17.xpi");// 创建一个FirefoxProfile对象profileFirefoxProfile profile = new FirefoxProfile();try{// 将Firebug加载到profile对象中profile.addExtension(file);}catch (IOException e){e.printStackTrace();}// 设置Firebug的当前版本号profile.setPreference("extensions.firebug.currentVersion","2.0.17");// 激活Firebugprofile.setPreference("extensions.firebug.allPagesActivation","on");


4 启动Firefox浏览器

4.1 Firefox安装在默认路径下

直接创建一个FirefoxDriver对象。

WebDriver driver = new FirefoxDriver();

4.2 Firefox未安装在默认路径下

需要指定Firefox的可执行程序firefox.exe的路径,再创建FirefoxDriver对象。

System.setProperty("webdriver.firefox.bin", "E:\\Firefox\\firefox.exe");WebDriver driver = new FirefoxDriver();


结合起来,就是如下代码:

File file = new File("E:\\Firefox\\files\\firebug-2.0.17.xpi");FirefoxProfile profile = new FirefoxProfile();try{profile.addExtension(file);} catch (IOException e){e.printStackTrace();}profile.setPreference("extensions.firebug.currentVersion","2.0.17");profile.setPreference("extensions.firebug.allPagesActivation","on");profile.setPreference("browser.download.downloadDir","C:\\Output");profile.setPreference("browser.download.folderList",2);profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream, application/vnd.ms-excel, text/csv, application/zip,application/xml");System.setPreperty("webdriver.firefox.bin","E:\\Firefox\\firefox.exe");WebDriver driver = new FirefoxDriver();


0 0