自动化测试基础-WebDriver启动浏览器的几种方法总结

在使用相应的WebDriver之前,应确保运行环境的OS和Browser都与WebDriver所需的运行环境相匹配。

一、启动Firefox Browser。

PS:由于Firefox Driver是直接打包在WebDriver Java Client Driver中,所以下载后者后,就不需要再另外下载Firefox Driver。

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

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

Navigation navigation = driver.navigate();

// 进入今日头条首页

navigation.to(“https://www.toutiao.com/”);

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

System.out.println(“start firefox browser…”);

System.setProperty(“webdriver.firefox.bin”, “D:/Program Files/Mozilla Firefox/firefox.exe”); //指定firefox的安装路径

WebDriver driver = new FirefoxDriver();

Navigation navigation = driver.navigate();

navigation.to(“https://www.toutiao.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) {

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(“https://www.toutiao.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 Firefoxfirefox.exe) on port 7055; process output follows:

null

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

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

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

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

public static void main(String[] args) {

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(“https://www.toutiao.com/”);

System.out.println(“start firefox browser succeed…”);

}

二、启动IE Browser。

PS:支持有三种不同的OS平台,包括Windows、Linux、Mac OS。

(1)启动本地IE Browser。

System.setProperty(“webdriver.ie.driver”,

“E:\selenium\IEDriverServer_x64_2.53.0\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(“https://www.toutiao.com/”);

三、启动Chrome Browser。

PS:只能在 Windows OS平台上运行,但要区别32bit版本和64bit版本。

(1)启动本地Chrome Browser。

public static void main(String[] args) {

// TODO Auto-generated method stub

System.setProperty(“webdriver.chrome.driver”,

“E:\chromedriver_win32\chromedriver.exe”);//chromedriver.exe所在本地路径

WebDriver driver = new ChromeDriver();

driver.get(“https://www.toutiao.com/”);

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

driver.findElement(By.id(“su”)).click();

driver.close();

}

note:

public static void main(String[] args) {

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

//设置DesiredCapabilities的属性包含E_ENSURE_CLEAN_SESSION以确保在Browser实例启动前清理会话的脏数据。

DesiredCapabilities capab = DesiredCapabilities.internetExplorer();

capab.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);

WebDriver driver = new InternetExplorerDriver(capab);

driver.get(“https://www.toutiao.com/”);

driver.close();

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年9月12日
下一篇 2019年9月12日

相关推荐