【selenium3+JAVA】界面自动化测试教程(一)——浏览器启动之firefox浏览器的启动

前言

由于使用selenium3,故启动firefox浏览器时已经不能像以前那样不需要driver就可以启动了,需要添加driver,driver名称为geckodriver.exe,下载地址:https://github.com/mozilla/geckodriver/releases

启动firefox浏览器

1、设置驱动

如前言所述,由于selenium3更新,导致现在启动firefox也需要设置驱动。但是由于以前的firefox启动时时不需要的,所以现在对于不同版本的firefox浏览器启动方式有所不同。本教程使用windows10,故linux和mac上启动firefox有何不同还需要研究;
对于47以上版本的firefox浏览器,使用如下代码:

System.setProperty("webdriver.gecko.driver", ".\\Tools\\geckodriver.exe")

对于47版本及以下版本的firefox浏览器,则使用如下代码:

System.setProperty("webdriver.firefox.marionette", ".\\Tools\\geckodriver.exe");

2、简单启动firefox

此处启动firefox不需要加任何参数,简单的启动方式如下:

public static void main( String[] args )
    {
    	System.setProperty("webdriver.gecko.driver", ".\\Tools\\geckodriver.exe");
    	FirefoxDriver firefox = new FirefoxDriver();
    	firefox.get("https://www.baidu.com/");
    }

3、firefox定制启动之带FirefoxOptions

基本方法如下:

FirefoxOptions options = new FirefoxOptions();
FirefoxDriver firefox = new FirefoxDriver(options);

如上的options

(1)、添加启动参数

此项和chrome浏览器类似,基本使用方法为:

FirefoxOptions options = new FirefoxOptions();
options.addArguments("-devtools");
FirefoxDriver firefox = new FirefoxDriver(options);

当然addArguments方法可以添加一个命令列表,也可以添加多个命令,如下:

FirefoxOptions options = new FirefoxOptions();
options.addArguments("-devtools", "-tray");//连续添加多个
List<String> list = new ArrayList<String>();
list.add("-safe-mode");
options.addArguments(list);//添加命令列表
FirefoxDriver firefox = new FirefoxDriver(options);

如下为一些可用的启动命令及其含义:

  • -headless – 无边框模式启用 Firefox。最低要求的版本是Linux 为 Firefox 55,Win 和 Mac OS X 则是 Firefox 56。和直接设定headless模式效果形同
  • -new-tab URL – 在 Firefox 新建一个标签页打开指定的 URL。
  • -new-window URL – 在 Firefox 新建一个窗口打开指定的 URL。
  • -private – 始终在隐私浏览模式启动 Firefox。
  • -private-window – 新建一个隐私模式窗口。
  • -private-window URL – 新建一个隐私模式窗口打开指定的 URL。如果已经有隐私模式窗口打开,则在此窗口打开这个 URL。
  • -search term – 使用 Firefox 默认的搜索引擎进行搜索。
  • -url URL – 新建一个标签页或窗口打开指定的 URL。可以不添加 -url 字段,如指定多个 URL,则 URL 之间空格分隔。(一些恶意软件经常会篡改这个参数来每次启动时打开特定页面)。
  • -safe-mode – 安全模式启动 Firefox,或者按住 Shift 键打开 Firefox 也可以。
  • -devtools – 启动 Firefox 时打开开发工具。
  • -inspector URL – 使用 DOM 查看器查看指定的页面。
  • -jsconsole – 启动 Firefox 时打开浏览器控制台。
  • -tray – 启用 Firefox 时最小化。
    如果要看详细的启动参数,则可以在如下链接中去看:
    英文版:https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options
    中文版(不及英文版新):https://developer.mozilla.org/zh-CN/docs/Mozilla/Command_Line_Options

(2)、Headless模式启动

这个headless模式与chrome浏览器的该模式相同,即不打开浏览器GUI但是可以正常运行脚本,属于隐藏启动模式,使用代码如下:

public static void main( String[] args )
    {
    	System.setProperty("webdriver.gecko.driver", "D:\\test\\driver\\geckodriver.exe");
    	FirefoxOptions options = new FirefoxOptions();
    	options.setHeadless(true);//此处设置headless模式
    	FirefoxDriver firefox = new FirefoxDriver(options);
    	firefox.get("https://www.baidu.com/");
    	String title = firefox.getTitle();
    	System.out.println(title);
    	firefox.quit();
    }

结果:
headless

(3)、设定firefox.exe启动

和chrome不一样firefox.exe可以安装多个不同版本的在同一台电脑上,所以有时候我们可以启动不同的火狐浏览器,方法为设定firefox.exe的路径即可,使用方法setBinary,如下所示:

public static void main( String[] args )
    {
    	System.setProperty("webdriver.gecko.driver", "D:\\test\\driver\\geckodriver.exe");
    	FirefoxOptions options = new FirefoxOptions();
    	options.setBinary("C:\\Program Files\\Mozilla Firefox\\firefox.exe");//设定启动的获取浏览器的firefox.exe的路径;
    	FirefoxDriver firefox = new FirefoxDriver(options);
    	firefox.get("https://www.baidu.com/");
    	String title = firefox.getTitle();
    	System.out.println(title);
    	firefox.quit();
    }

(4)、增加性能设定

此处类似chrome浏览器的添加设置,使用基本方法如下:

FirefoxOptions options = new FirefoxOptions();
options.addPreference(key, value);
FirefoxDriver firefox = new FirefoxDriver(options);

(5)、添加用户设置

此处可以添加用户自定义的设置,基本使用方法如下:

FirefoxOptions options = new FirefoxOptions();
ProfilesIni allProfile = new ProfilesIni();
FirefoxProfile profile = allProfile.getProfile("default");//此处default为用户自定义设置的名称,获取其他设置的名称;
FirefoxDriver firefox = new FirefoxDriver(options);

此处如果使用default则是使用用户自定义设置,类似chrome的options.addArguments("--user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data");

(6)、添加代理

此处与chrome添加代理相同,此处不再复数;

(7)、启动时附加组件

如果想要在浏览器启动时附加组件,则需要使用添加profile的方法,先自定义一个新的profile,而后在selenium启动firefox时配置加载此profile即可;

FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile(profileDir);
options.setProfile(profile);
FirefoxDriver firefox = new FirefoxDriver(options);

猜你喜欢

转载自blog.csdn.net/df0128/article/details/82795523