WebDriver总结-不同浏览器的启动方式

简介:

启动Firefox Browser。

1这种情况适用于Firefox安装在了默认路径下

        WebDriver driver = new FirefoxDriver();//直接new一个FirefoxDriver

        Navigation navigation = driver.navigate();

        // 进入百度首页

        navigation.to("http://www.baidu.com");

2 这种情况适用于Firefox未安装在默认路径下

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

       System.setProperty("webdriver.firefox.bin",     //指定firefox的安装路径

                "D:/Program Files/Mozilla Firefox/firefox.exe");  

        WebDriver driver = new FirefoxDriver();

        Navigation navigation = driver.navigate();

        navigation.to("http://www.baidu.com/");


3这种情况可以加载出Firefox的插件。

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

import java.io.File;

import java.io.IOException;


import org.openqa.selenium.Alert;

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 TestDemo {


    public static void main(String[] args) {

        // TODO Auto-generated method stub

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

        System.setProperty("webdriver.firefox.bin", 

                "C:/Program Files (x86)/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...");

    }


}

--------------------------------------

上述代码并未调通报如下异常

start firefox browser...

Exception in thread "main" org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary(C:\Program Files (x86)\Mozilla Firefox\firefox.exe) on port 7055; process output follows: 

null

Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'

System info: host: 'XL-20150414QGDQ', ip: '192.168.80.6', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_80'

Driver info: driver.version: FirefoxDriver

    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:128)

    at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:271)

    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:119)

    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:218)

    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:211)

    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:207)

    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:124)

    at TestDemo.main(TestDemo.java:27)

Caused by: org.openqa.selenium.firefox.UnableToCreateProfileException: java.io.FileNotFoundException: \files\firebug-2.0.7-fx.xpi (系统找不到指定的路径。)

Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'

System info: host: 'XL-20150414QGDQ', ip: '192.168.80.6', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_80'

Driver info: driver.version: FirefoxDriver

    at org.openqa.selenium.firefox.FirefoxProfile.layoutOnDisk(FirefoxProfile.java:427)

    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:85)

    ... 7 more

Caused by: java.io.FileNotFoundException: \files\firebug-2.0.7-fx.xpi (系统找不到指定的路径。)

    at java.io.FileInputStream.open(Native Method)

    at java.io.FileInputStream.<init>(Unknown Source)

    at org.openqa.selenium.firefox.internal.FileExtension.obtainRootDirectory(FileExtension.java:80)

    at org.openqa.selenium.firefox.internal.FileExtension.writeTo(FileExtension.java:59)

    at org.openqa.selenium.firefox.FirefoxProfile.installExtensions(FirefoxProfile.java:443)

    at org.openqa.selenium.firefox.FirefoxProfile.layoutOnDisk(FirefoxProfile.java:421)

    ... 8 more


    4用第(3)种情况未调通。

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

    public static void main(String[] args) {

        // TODO Auto-generated method stub

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

        System.setProperty("webdriver.firefox.bin", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe");

        ProfilesIni pi = new ProfilesIni();

        FirefoxProfile profile = pi.getProfile("default");

        WebDriver driver = new FirefoxDriver(profile);

        driver.get("http://www.baidu.com");

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

    }




启动IE Browser。

PS:Firefox已自带外其他浏览器均需从Selenium官网http://docs.seleniumhq.org/download/下载各自的Driver。

1启动本地IE Browser。

        System.setProperty("webdriver.ie.driver",

                "E:\\selenium\\IEDriverServer.exe");//IEDriverServer.exe所在本地路径

        DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();

        ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);

        WebDriver driver = new InternetExplorerDriver(ieCapabilities);

        driver.get("http://www.baidu.com");


启动Chrome Browser。


1启动本地Chrome Browser。

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver",

                "E:\\selenium\\chromedriver.exe");//chromedriver.exe所在本地路径

        WebDriver driver = new ChromeDriver();

        driver.get("http://www.baidu.com");

        driver.findElement(By.id("kw")).sendKeys(Keys.chord(Keys.SHIFT,"webdriver"));

        driver.findElement(By.id("su")).click();

        driver.close();

    }


本文转自 honzhang 51CTO博客,原文链接:http://blog.51cto.com/hongz/1768256


相关文章
|
8天前
|
Java 测试技术 定位技术
《手把手教你》系列技巧篇(二十三)-java+ selenium自动化测试-webdriver处理浏览器多窗口切换下卷(详细教程)
【4月更文挑战第15天】本文介绍了如何使用Selenium进行浏览器窗口切换以操作不同页面元素。首先,获取浏览器窗口句柄有两种方法:获取所有窗口句柄的集合和获取当前窗口句柄。然后,通过`switchTo().window()`方法切换到目标窗口句柄。在项目实战部分,给出了一个示例,展示了在百度首页、新闻页面和地图页面之间切换并输入文字的操作。最后,文章还探讨了在某些情况下可能出现的问题,并提供了一个简单的本地HTML页面示例来演示窗口切换的正确操作。
35 0
|
Web App开发 数据采集 缓存
曲鸟全栈UI自动化教学(四):Selenium工作原理及Webdriver对浏览器的配置和操作
曲鸟全栈UI自动化教学(四):Selenium工作原理及Webdriver对浏览器的配置和操作
380 0
曲鸟全栈UI自动化教学(四):Selenium工作原理及Webdriver对浏览器的配置和操作
|
移动开发 前端开发 JavaScript
Selenium WebDriver API 学习笔记(三):浏览器控制
Selenium WebDriver API 学习笔记(三):浏览器控制
116 0
|
测试技术 API Python
Selenium WebDriver API 学习笔记(二):浏览器控制
Selenium WebDriver API 学习笔记(二):浏览器控制
110 0
|
Web App开发 安全
接口框架中WebDriver启动IE、Firefox和Chrome浏览器
接口框架中WebDriver启动IE、Firefox和Chrome浏览器
接口框架中WebDriver启动IE、Firefox和Chrome浏览器
|
缓存 数据安全/隐私保护 Go
|
6月前
|
Web App开发 前端开发 JavaScript

热门文章

最新文章