Selenium2启动浏览器且加载插件

一、SELENIUM2启动浏览器

注意: SELENIUM2在启动浏览器时,都是启动一个干净的没有任务 插件及cookies信息的浏览器,即使是你之前的浏览器有设置过代理,到自动化启动时,也是没有代理的模式。

1.启动firefox浏览器:建议使用32.x版本的火狐浏览器,要不有可能会出问题。

image

启动不在默认安装路径的firefox浏览器:

image

2.启动chrome浏览器:

需要chromedriver.exe的支持,驱动下载地址(http://docs.seleniumhq.org/download/)

image

如果不想用setProperty的方式,也可以将chromedriver.exe 放在”C:WindowsSystem32”路径下并重启电脑即可

image

3.启动IE浏览器:
需要IEDriverServer.exe 的支持,且 IE的exe文件分64位与32位,请根据自已的机器选择相应的exe文件。
启动代码如下:

image

二、SELENIUM2启动浏览器时加载插件

加载插件的作用:有的是为了调试,也有的是被测试的系统本身需要插件

Firefox下加载firebug插件:

image

Chrome下加载crx插件:

image

且IE下没有插件加载。


三、SELENIUM2启动firefox时的profile设置

在firefox地址栏中输入about:config,可以看到有哪些可以设置

举例:导出文件的地址设置

image


3.启用默认情况下被firefox禁用的功能,以本地事件例,很简单直接设置为true就可以了。

image

特别重要:启动时,每次都是一个干净的firefox,如果要启动本机器的firefox的配置:

image

如果在机器B上要启动机器A上的firefox配置,则先导出A的配置,然后加载:

将A机器上的Profiles文件夹”C:UserscloudchenAppDataLocalMozillaFirefoxProfiles”给拷贝出来

image


四、SELENIUM2启动chrome时的profile设置

特别重要:将user data文件夹” C:UserscloudchenAppDataLocalGoogleChromeUser Data”拷贝出来
image
请自己验证一下,加不加以下代码的区别:

image


五、SELENIUM2启动IE时的设置

特别重要:启动IE时,需关闭保护模式。 4个红框标记的区域中的保护模式的checkbox 都要uncheck

image


也可以代码关闭:

image

完整代码如下:

package com.selenium.test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class launchBrowser {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		launchChrome();
	}

	public static void launchFireFox() {
		FirefoxProfile firefoxProfile = new FirefoxProfile();

		firefoxProfile.setPreference("browser.download.folderList", 2);
		firefoxProfile.setPreference(
				"browser.download.manager.showWhenStarting", false);
		firefoxProfile.setPreference("browser.download.dir", "d:\test");
		firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
				"text/csv");

		String proxyIp = "child-prc.intel.com";
		int proxyPort = 913;
		firefoxProfile.setPreference("network.proxy.type", 1);
		firefoxProfile.setPreference("network.proxy.http", proxyIp);
		firefoxProfile.setPreference("network.proxy.http_port", proxyPort);
		firefoxProfile.setPreference("network.proxy.ssl", proxyIp);
		firefoxProfile.setPreference("network.proxy.ssl_port", proxyPort);

		firefoxProfile
				.setPreference("network.proxy.share_proxy_settings", true);
		firefoxProfile
				.setPreference("network.proxy.no_proxies_on", "localhost");

		WebDriver driver = new FirefoxDriver(firefoxProfile);
		// WebDriver driver = new FirefoxDriver();
		Navigation navigation = driver.navigate();
		navigation.to("https://baidu.com");
		// driver.close();
		// driver = null;
	}

	public static void launchChrome() {
		System.setProperty("webdriver.chrome.driver", "files/chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		Navigation navigation = driver.navigate();
		navigation.to("https://www.baidu.com");
		driver.close();
		driver = null;
	}

	public static void launchIE() {
		System.setProperty("webdriver.ie.driver", "files/iedriver64.exe");
		WebDriver driver = new InternetExplorerDriver();
		Navigation navigation = driver.navigate();
		navigation.to("https://www.baidu.com");
		driver.close();
		driver = null;
	}
}

最后打个广告,不要介意哦~

最近我在Dataguru学了《软件自动化测试Selenium2》网络课程,挺不错的,你可以来看看!要是想报名,可以用我的优惠码 G863,立减你50%的固定学费!

链接:http://www.dataguru.cn/invite.php?invitecode=G863

原文地址:https://www.cnblogs.com/yajing-zh/p/4897728.html