Selenium2学习-042-Selenium3启动Firefox Version 48.x浏览器(ff 原生 geckodriver 诞生)

  

今天又被坑了一把,不知谁把 Slave 机的火狐浏览器版本升级为了 48 的版本,导致网页自动化测试脚本无法启动火狐的浏览器,相关的网页自动化脚本全线飘红(可惜不是股票,哈哈哈...),报版本不兼容的错误(当前 selenium-server 版本为 2.53.1,火狐升级后版本为 48.0.2)。

查看了一下,发现 Selenium 3 也在众所期望中登场了,从其官网的更新历史可知其主要特性如下所示:

  • 1、全面支持JDK8。毕竟JDK8是Oracle官方支持的版本,同时若想使用 java binding则必须使用JDK8了。预计selenium-standalone-server后续也会运行在 JDK8 上;
  • 2、取消 Selenium RC 的支持,全面回归 WebDriver 协议;
  • 3、通过 Mozilla 官方的 geckodriver 支持 Firefox。虽然之前谷大爷实现原生支持了火狐,但毕竟不是火狐自己实现的。同时新版本的火狐也更新了其新的引擎,google原生的驱动未实现新引擎的支持,所以 geckodriver 的出现也是一个必然。在 Selenium 3 中系统特性 webdriver.firefox.marionette 也进行了强制设定。
  • 4、通过微软官方的ms webdriver支持 Edge 浏览器,注意仅支持 Version 9+。又是一个官方版本哦 ^_^,
  • 5、支持 Mac OS(Safari 10+),支持官方的 safaridriver,由此看出 Apple 官方也买了 webdriver 协议的单哦 ^_^。是否由此可见 WebDriver 协议一统江湖的美好前景了,哈哈哈。。。

 

Selenium 3.x 在启动 Firefox 48.x 的时候,必须下载 geckodriver.exe 驱动,并将其路径配置在 Path 变量中。

 

下面是Selenium3启动Firefox 48.x 的简单示例,供各位参考。

 1 /**
 2  * Aaron.ffp Inc.
 3  * Copyright (c) 2004-2016 All Rights Reserved.
 4  */
 5 package ffp.demo.webdriver;
 6 
 7 import org.openqa.selenium.Alert;
 8 import org.openqa.selenium.By;
 9 import org.openqa.selenium.WebDriver;
10 import org.openqa.selenium.firefox.FirefoxDriver;
11 import org.openqa.selenium.os.WindowsUtils;
12 import org.openqa.selenium.remote.DesiredCapabilities;
13 import org.testng.annotations.Test;
14 
15 /**
16  * <strong>Selenium3 启动 Firefox 48</strong><br>
17  * <br>
18  * @author Aaron.ffp
19  * @version V1.0.0: ffp-demo ffp.demo.webdriver DemoSelenium3.java, 2016-09-17 17:49:55.933 Exp $
20  */
21 public class DemoSelenium3 {
22     String url = "http://localhost:8080/ffp-demo/res/main/webdriver/DemoAlert.html";
23     
24     private WebDriver driver;
25   
26   @Test
27   public void test_alert() throws InterruptedException {
28       DesiredCapabilities capabilities = DesiredCapabilities.firefox();
29       capabilities.setCapability("marionette", true);
30       
31       this.driver = new FirefoxDriver(capabilities);
32       
33       this.driver.manage().window().maximize();
34       this.driver.get(this.url);
35       
36       this.driver.findElement(By.xpath("//input[@class='alert']")).click();
37       Alert alert = this.driver.switchTo().alert();
38       
39       System.out.println(alert.getText());
40       
41       Thread.sleep(5000);
42       
43       alert.accept();
44       
45       this.driver.switchTo().defaultContent();
46       
47       Thread.sleep(5000);
48       
49       this.driver.findElement(By.xpath("//input[@class='alert']")).click();
50       
51       Thread.sleep(5000);
52       
53       WindowsUtils.killByName("geckodriver.exe");
54   }
55 }

 

  执行日志如下所示:

九月 17, 2016 6:42:22 下午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
1474108943366    Marionette    INFO    Listening on port 59022
1474108944547    Marionette    INFO    startBrowser dd016983-5b27-4343-bcbb-4ef21d391ba8
1474108944558    Marionette    INFO    sendAsync dd016983-5b27-4343-bcbb-4ef21d391ba8
九月 17, 2016 6:42:24 下午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Detected dialect: W3C
1474108944887    Marionette    INFO    sendAsync dd016983-5b27-4343-bcbb-4ef21d391ba8
1474108944995    Marionette    INFO    sendAsync dd016983-5b27-4343-bcbb-4ef21d391ba8
1474108945360    Marionette    INFO    sendAsync dd016983-5b27-4343-bcbb-4ef21d391ba8
1474108945383    Marionette    INFO    sendAsync dd016983-5b27-4343-bcbb-4ef21d391ba8
1474108945433    Marionette    INFO    sendAsync dd016983-5b27-4343-bcbb-4ef21d391ba8
这是一个:弹出框(警示、提示)
1474108950462    Marionette    INFO    sendAsync dd016983-5b27-4343-bcbb-4ef21d391ba8
1474108955570    Marionette    INFO    sendAsync dd016983-5b27-4343-bcbb-4ef21d391ba8
1474108955577    Marionette    INFO    sendAsync dd016983-5b27-4343-bcbb-4ef21d391ba8
1474108955590    Marionette    INFO    sendAsync dd016983-5b27-4343-bcbb-4ef21d391ba8
PASSED: test_alert

  

写在后续:

关于是否要升级?可根据各自当前的自动化测试需求进行决定,若是需要测试Edge、Safari及Firfox高版本(Version 48+),那么升级就成了一种必要;否则,我个人觉得当前无需升级。升级与否,见仁见智吧。

 

相应的脚本源码文件分享链接:https://yunpan.cn/ckrsnINJhm3cw  访问密码 3ae9

 

至此,Selenium2学习-042-Selenium3启动Firefox Version 48.x浏览器(ff 原生 geckodriver 诞生) 顺利完结,希望此文能够给初学 Selenium 的您一份参考。

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

 

posted @ 2016-09-18 00:03  范丰平  Views(10319)  Comments(5Edit  收藏  举报