webdriver启动chrome、firefox前,读取个人配置文件

webdriver在打开chrome、firefox浏览器时,默认是创建新用户,每次打开都是一个全新的浏览器。对于浏览器需要使用npapi插件,会有困扰。通过加载个人配置文件的方式,在第一次打开浏览器时,手动设置npapi插件是否加载,后续运行直接使用之前的配置信息,不会反复提示。

chrome:
ChromeOptions options = new ChromeOptions();
options.addArguments(“–user-data-dir=C:/Users/xxx/AppData/Local/Google/Chrome/User Data/Default”);
WebDriver driver = new ChromeDriver(options);

firefox:
FirefoxProfile fp = new FirefoxProfile();
ProfilesIni allProfiles = new ProfilesIni();
fp = allProfiles.getProfile(“default”);
WebDriver driver = new FirefoxDriver(fp);

selenium2.0操作chrome浏览器

selenium自带的webdriver库,没有chrome driver,需要另外下载导入后才能操作chrome浏览器。

杯具的chrome,被bs了,firefox浏览器就没有这个问题,可以直接调用。

1. http://code.google.com/p/chromium/downloads/list,下载对应的chrome driver

2. 在使用WebDriver driver = new ChromeDriver()前,先设置属性System.setProperty(“webdriver.chrome.driver”,path);
path为chromedriver存放的路径,如e:\\chromedriver.exe。
也可以把路径设置在环境变量中,直接WebDriver driver = new ChromeDriver();

css selectors

用于selenium中控件定位。杯具的xpath在ie下识别很慢,换用css定位吧。
http://www.w3.org/TR/css3-selectors/

Pattern Meaning Described in section First defined in CSS level
* any element Universal selector 2
E an element of type E Type selector 1
E[foo] an E element with a “foo” attribute Attribute selectors 2
E[foo=”bar”] an E element whose “foo” attribute value is exactly equal to “bar” Attribute selectors 2
E[foo~=”bar”] an E element whose “foo” attribute value is a list of whitespace-separated values, one of which is exactly equal to “bar” Attribute selectors 2
E[foo^=”bar”] an E element whose “foo” attribute value begins exactly with the string “bar” Attribute selectors 3
E[foo$=”bar”] an E element whose “foo” attribute value ends exactly with the string “bar” Attribute selectors 3
E[foo*=”bar”] an E element whose “foo” attribute value contains the substring “bar” Attribute selectors 3
E[foo|=”en”] an E element whose “foo” attribute has a hyphen-separated list of values beginning (from the left) with “en” Attribute selectors 2
E:root an E element, root of the document Structural pseudo-classes 3
E:nth-child(n) an E element, the n-th child of its parent Structural pseudo-classes 3
E:nth-last-child(n) an E element, the n-th child of its parent, counting from the last one Structural pseudo-classes 3
E:nth-of-type(n) an E element, the n-th sibling of its type Structural pseudo-classes 3
E:nth-last-of-type(n) an E element, the n-th sibling of its type, counting from the last one Structural pseudo-classes 3
E:first-child an E element, first child of its parent Structural pseudo-classes 2
E:last-child an E element, last child of its parent Structural pseudo-classes 3
E:first-of-type an E element, first sibling of its type Structural pseudo-classes 3
E:last-of-type an E element, last sibling of its type Structural pseudo-classes 3
E:only-child an E element, only child of its parent Structural pseudo-classes 3
E:only-of-type an E element, only sibling of its type Structural pseudo-classes 3
E:empty an E element that has no children (including text nodes) Structural pseudo-classes 3
E:link
E:visited
an E element being the source anchor of a hyperlink of which the target is not yet visited (:link) or already visited (:visited) The link pseudo-classes 1
E:active
E:hover
E:focus
an E element during certain user actions The user action pseudo-classes 1 and 2
E:target an E element being the target of the referring URI The target pseudo-class 3
E:lang(fr) an element of type E in language “fr” (the document language specifies how language is determined) The :lang() pseudo-class 2
E:enabled
E:disabled
a user interface element E which is enabled or disabled The UI element states pseudo-classes 3
E:checked a user interface element E which is checked (for instance a radio-button or checkbox) The UI element states pseudo-classes 3
E::first-line the first formatted line of an E element The ::first-line pseudo-element 1
E::first-letter the first formatted letter of an E element The ::first-letter pseudo-element 1
E::before generated content before an E element The ::before pseudo-element 2
E::after generated content after an E element The ::after pseudo-element 2
E.warning an E element whose class is “warning” (the document language specifies how class is determined). Class selectors 1
E#myid an E element with ID equal to “myid”. ID selectors 1
E:not(s) an E element that does not match simple selector s Negation pseudo-class 3
E F an F element descendant of an E element Descendant combinator 1
E > F an F element child of an E element Child combinator 2
E + F an F element immediately preceded by an E element Adjacent sibling combinator 2
E ~ F an F element preceded by an E element General sibling combinator 3

selenium环境搭建 PHP Client

用久了QTP一直想换种自动化测试工具,跟猴哥一起考虑了很久。最后选定了selenium & PHP Client。原本想用2.0,可惜没有官方的php包,找到个民间的,怕以后版本更新会有问题。而且现在selenium 2.0还在rc,不想当小白老鼠。于是乎选择了selenium-php-client-driver-1.0.1
装完后,在我的台式机XP系统上,跑demo很顺利。但是在win7上居然启动不了firefox浏览器。不知道是不是兼容性问题

服务器端:通过java起rc
客服端:通过phpunit起php脚本

1.安装xampp

2.加载channel,用于下载安装phpunit。有些机子上只要加载第一个channel即可。
c:\xampp\php>pear channel-discover pear.phpunit.de
””””c:\xampp\php>pear channel-discover pear.symfony-project.com
””””c:\xampp\php>pear channel-discover components.ez.no

3.phpunit对PEAR版本有要求。可能需要升级PEAR,现在最新版是1.9.3
一次不能升级到位,得分多次升级到相应版本,再继续往上升级
PS:查看PEAR的命令:pear -V ””””’ V 要大写
c:\xampp\php>pear upgrade -a PEAR-1.3.3
c:\xampp\php>pear upgrade -f PEAR-1.5.4        ”””’ 升级1.5.4和1.8.0需要强制升级,用-a参数会报错
c:\xampp\php>pear upgrade -f PEAR-1.8.0
c:\xampp\php>pear upgrade -a PEAR

4.通过pear下载安装phpunit
c:\xampp\php>pear install phpunit/PHPUnit

PS:这步不是100%成功。在我的台式机上(XP系统),一次就成功。
在本本(win7)上死活装不了,一直报
“No releases available for package “pear.phpunit.de/PHPUnit”
install failed”
最后还是把台式机上的phpunit拷过来,才搞定的。

5.把php client中的PEAR文件夹内的testing文件夹,放到C:\xampp\php\PEAR

6.启动rc
D:\selenium-remote-control-1.0.1\selenium-server-1.0.1>java -jar selenium-server

7.运行demo
c:\xampp\php>phpuint D:\selenium-remote-control-1.0.1\selenium-php-client-driver-1.0.1\GoogleTest.php