Selenium Webdriver 学习总结-Advanced Usage-Cookie、Profile(七)

晒酷学院qq群:979438600

一、如何使用Cookie代码示例:

import org.openqa.selenium.Cookie;
mport org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import java.util.Set;

public class DemoCookies {
	@Test 
	public void cookies() {
		WebDriver driver = new FirefoxDriver();
		driver.get("http://bbs.shareku.com/");
		
		// 设置一个cookie,cookie是以键值对的形式存在域中
		Cookie cookie = new Cookie("ray", "male");
		driver.manage().addCookie(cookie);
		
		// 输出当前站点所有可用的
		cookie Set allCookies = driver.manage().getCookies();
		for (Cookie loaded : allCookies) {
 			System.out.println(String.format("Cookie path:%s \n%s-->%s", loaded.getPath(), loaded.getName(), loaded.getValue()));
 }
 
		// 删除Cookie的三种方式
		// 通过Cookie的key删除指定的cookie
		driver.manage().deleteCookieNamed("ray");
		
		// 通过Cookie对象删除指定Cookie
		driver.manage().deleteCookie(allCookies.iterator().next());
		
		// 删除所有Cookie
		driver.manage().deleteAllCookies(); 			
		driver.quit();
	}
}

二、Firefox用户配置文件

您对 Firefox 做的所有设置,比如您的主页、工具栏、保存的密码以及书签、插件配置等,都被保存在一个指定的用户配置文件夹中。您的用户配置文件夹和 Firefox 的程序各自独立,这样一旦您的 Firefox 出现问题,您的所有信息仍旧是安全的。也就是说,您可以卸载 Firefox 后仍保留您的设置和其他信息。

用户配置文件详细信息,参考:http://support.mozilla.org/zh-CN/kb/用户配置文件

1、webdriver如何处理profile

当我们初始化Firefox WebDriver时,可以使用一个已存在的Profile或一个新的Profile,WebDriver每次使用前都会复制一份(win7 默认存放路径C:\Users\ADMINI~1\AppData\Local\Temp\anonymous5354649999399361803webdriver-profile),如果没有指定firefox profile,webdriver会创建一个空的Profile并使用它,所以我们在每次webdriver启动的浏览器中都看不到我们之前对firefox默认的配置信息(比如,缺少firebug组件、书签信息等)。

2、webdriver使用已存在的firefoxProfile

@Test   
public void firefoxProfile() {   
	ProfilesIni allProfiles = new ProfilesIni();   
	FirefoxProfile profile = allProfiles.getProfile("webdriver");   
	WebDriver driver = new FirefoxDriver(profile);   
	driver.get("http://bbs.shareku.com/");   
	driver.quit();   
}

以上代码启动firefox时,使用的是我自定义的名为“webdriver”的profile文件,默认情况下的profile文件名为“default”。

如何自定义profile文件:
a.打开dos控制台,将当前目录切换到firefox.exe文件所在目录。
b.打开用户配置文件管理器,命令行中输入: firefox.exe -p ,回车。
c.在弹出对话框中,选择“创建配置文件”,选择【下一步】,紧接着命名,我这里命名为“webdriver”,然后【结束】。
d.创建完成,回过来执行以上代码,可以发现webdriver使用我们指定的Profile启动firefox。
默认情况,配置文件安装目录位于:C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox\Profiles目录下。

3、使用外部的Profile启动firefox

该profile未在本机的firefox中注册,比如我们直接从其它机器上拷贝过来的Profile
代码示例:

@Test   
public void unRegistedProfile() {   
	File profileDir = new File("D:/tmp/webdriver");  //webdriver为之前定义的Profile文件  
	FirefoxProfile profile = new FirefoxProfile(profileDir);   
	WebDriver driver = new FirefoxDriver(profile); 
	driver.get("http://bbs.shareku.com/");
	driver.quit();  
}

4、定制我们的匿名Profile

前面已经讲过,firefoxDriver会创建一个匿名的Profile,以下教大家如何定制我们的匿名Profile
a) 启动带有firebu组件的firefox

@Test
public void testFirefoxProfile() {
	File file = new File("src/test/resources/firebug.xpi");
	FirefoxProfile profile = new FirefoxProfile();
	try {
		//在webdriver自己创建的Profile中添加firebug组件
		profile.addExtension(file);
	} catch (IOException e) {
		e.printStackTrace();
}

//指定firefox版本,否则启动webdriver实例后,会打开firebug页面
profile.setPreference("extensions.firebug.currentVersion", "1.11.4");    
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://bbs.shareku.com");
//driver.quit();
}

执行上述代码,会发现启动的浏览器带有firebug组件,同样方法可以添加更多的组件,如果不是必须的话,建议大家尽量减少这样的配置(影响firefox driver启动速度)

5、对浏览器的偏好设置

FirefoxProfile中的setPreference方法可以更改浏览器中首选项的任何设置,另外FirefoxDriver也提供了附加的配置(详细参考:http://code.google.com/p/selenium/wiki/DesiredCapabilities 中的FirefoxProfile settings部分)
关于firefox首选项配置,可以通过在浏览器地址栏中输入about:config ,回车,在这里可以修改我们关注的东西,这里修改后会应用于当前系统,如果我们只想在启动浏览器时,个性化部分配置,可以通过FirefoxProfile完成。

举例说明:
a) 自定义webdriver启动时显示指定页面

FirefoxProfile pro = new FirefoxProfile(); 
pro.setPreference("hello", "webdriver"); 
pro.setPreference("browser.startup.homepage", "http://bbs.shareku.com/"); 
WebDriver driver = new FirefoxDriver(pro); 
// driver.quit();

以上代码执行后,浏览器启动后会显示晒库论坛主页。

**b) 默认情况下WebDriver在执行get、click等方法后会等待页面加载完成,脚本才会继续向下执行,这里可以通过WebDriver提供 **
“webdriver.load.strategy”来修改webdriver忽略此等待。

FirefoxProfile pro = new FirefoxProfile(); 
pro.setPreference("hello", "webdriver"); 
pro.setPreference("webdriver.load.strategy", "unstable"); 
WebDriver driver = new FirefoxDriver(pro); 

该方法谨慎使用,会造成很多意向不到的错误。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值