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

QQ群:136924235

论坛:http://bbs.shareku.com

一、如何使用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、前面已经讲过,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);

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

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

时间: 2024-08-13 21:04:35

Selenium Webdriver 学习总结-Advanced Usage-Cookie、Profile(七)的相关文章

selenium webdriver学习(九)------------如何操作cookies(转)

selenium webdriver学习(九)------------如何操作cookies 博客分类: Selenium-webdriver Web 测试中我们经常会接触到Cookies,一个Cookies主要属性有”所在域.name.value.有效日期和路径",下面来讲一下怎么操作Cookies. Java代码   import java.util.Set; import org.openqa.selenium.Cookie; import org.openqa.selenium.WebD

Selenium webdriver 学习总结-元素定位

Selenium webdriver 学习总结-元素定位 webdriver提供了丰富的API,有多种定位策略:id,name,css选择器,xpath等,其中css选择器定位元素效率相比xpath要高些,使用id,name属性定位元素是最可靠,效率最高的一种办法. 1.工具选择:在我们开发测试脚本的过程中各个浏览器给我们也提供了方便定位元素的工具,我比较喜欢使用firefox的firebug工具,也是目前很多开发测试人员比较热衷的选择,原因是firefox是唯一能够集成selenium IDE

Selenium Webdriver 学习总结-Jenkins配置(八)

这周单位要做一个人脸美化的项目,查资料遇到这位大牛的博客,地址如下:点击打开链接 我的代码也是在他的基础上进行修改的,但是他对图像的RGB三个通道平等调节,为了适应我的需求,我改成了针对三个通道分别调节.废话不多说,开始上源码 void ImageAdjust(Mat& src, Mat& dst, vector<double> low_in, vector<double> high_in, vector<double> low_out, vector&

selenium webdriver 学习总结-数据驱动(六)

QQ群:136924235 论坛:http://bbs.shareku.com webdriver可以结合junit中的Parameterized运行器完成数据驱动的目的,数据驱动的方式很多,可以结合csv文件,excel文件,jdbc等,下面我将结合csv,jdbc来展示如何完成数据驱动测试. 一.先给大家介绍一下如何使用Parameterized运行器,两种方式实现. 1.第一种方式,通过构造方法初始化测试数据 代码示例: package junit.parameters; import o

selenium webdriver学习(七)------------如何处理alert、confirm、prompt对话框( 转)

selenium webdriver学习(七)------------如何处理alert.confirm.prompt对话框 博客分类: Selenium-webdriver alertpromptconfirmseleniumwebdriver alert.confirm.prompt这样的js对话框在selenium1.X时代也是难啃的骨头,常常要用autoit来帮助处理. 试用了一下selenium webdriver中处理这些对话框十分方便简洁.以下面html代码为例: Html代码  

selenium webdriver学习(五)------------iframe的处理(转)

selenium webdriver学习(五)------------iframe的处理 博客分类: Selenium-webdriver 如何定位frame中元素 有时候我们在定位一个页面元素的时候发现一直定位不了,反复检查自己写的定位器没有任何问题,代码也没有任何问题.这时你就要看一下这个页面元素是否在一个iframe中,这可能就是找不到的原因之一.如果你在一个default content中查找一个在iframe中的元素,那肯定是找不到的.反之你在一个iframe中查找另一个iframe元

selenium webdriver学习(一)------------快速开始(转载JARVI)

selenium webdriver学习(一)------------快速开始 博客分类: Selenium-webdriver selenium webdriver 学习 selenium webdriver学习历程(一)------------快速开始 学习selenium已经两年了,从1.X到2.X,一直在关注它.中间由于工作原因中断了一段时间,但是一直无法割舍,最近又去官网看了一下,更新还挺快的.selenium1.X的时代将被取代,selenium-webdriver的大航海时代开始了

selenium webdriver学习(八)------------如何操作select下拉框(转)

selenium webdriver学习(八)------------如何操作select下拉框 博客分类: Selenium-webdriver 下面我们来看一下selenium webdriver是如何来处理select下拉框的,以http://passport.51.com/reg2.5p这个页面为例.这个页面中有4个下拉框,下面演示4种选中下拉框选项的方法.select处理比较简单,直接看代码吧:) Java代码   import org.openqa.selenium.By; impo

selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面(转)

selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面 博客分类: Selenium-webdriver 元素拖放drag and drop Q群里有时候会有人问,selenium  webdriver怎么实现把一个元素拖放到另一个元素里面.这一节总一下元素的拖放. 下面这个页面是一个演示拖放元素的页面,你可以把左右页面中的条目拖放到右边的div框中. http://koyoz.com/demo/html/drag-drop/drag-drop.