Loving Coding & Visual Design

Selenium自动化测试

selenium-webdriver
http://selenium.googlecode.com/git/docs/api/javascript/index.html

下载安装扩展组件:
Chrome chromedriver(.exe)
http://chromedriver.storage.googleapis.com/index.html

Internet Explorer IEDriverServer.exe
http://selenium-release.storage.googleapis.com/index.html

PhantomJS phantomjs(.exe)
http://phantomjs.org/download.html

Opera operadriver(.exe)
https://github.com/operasoftware/operachromiumdriver/releases

Safari SafariDriver.safariextz
http://selenium-release.storage.googleapis.com/index.html

安装selenium-webdriver
cnpm install selenium-webdriver

//////////////////////////////////////
install the standalone server

Open a command prompt, navigate to the file location and enter the following command:

java -jar selenium-server-standalone-[version].jar

To run in IE, the command should be as follows:

java -jar selenium-server-standalone-[version].jar -Dbwebdriver.ie.driver=IEDriverServer.exe

写入bat,执行出错:Exception in thread "main" java.lang.NoClassDefFoundError
直接在命令行运行可以。java -jar selenium-server-standalone-2.45.0.jar


//////////////////////////////////////
https://www.npmjs.com/package/selenium-standalone
It will install a selenium-standalone command line that will be able to install selenium server and start firefox, chrome, internet explorer or phantomjs for your tests.

npm install selenium-standalone@latest -g
selenium-standalone install (dos下会下载失败,通过浏览器下载后,复制到C:\Program Files\nodejs\node_modules\selenium-standalone\.selenium\)
selenium-standalone start

//////////////////////////////////////////////
windows7下出错:node-gyp rebuild AttributeError: 'module' object has no attribute 'F_GETFD' gyp ERRor (可以忽略)

下载chromedriver_win32.zip等组件,解压chromedriver.exe到selenium目录,将selenium目录加入path。Mac下可编辑~./.bash_profile:
export PATH=/selenium/web:$PATH

然后运行 . .bash_profile
echo $PATH
看看是否有selenium目录
//////////////////////////////////////////////
windows7下调用IEDriver出错:UnknownError: Unexpected error launching Internet Explorer. Protected Mode setti
ngs are not the same for all zones. Enable Protected Mode must be set to the sam
e value (enabled or disabled) for all zones.

IE浏览器->internet安全选项->取消保护模式

//////////////////////////////////////////////
Error: Could not locate Firefox on the current system

////////////////////////////////
centos7 64下node selenium.js 出错Error: spawn EACCES
把chromedriver chmod 755.

/////////////////////////////////
centos7 64下node selenium-server.js 出错:
The path to the driver executable must be set by the webdriver.chrome.driver system property.

办法
chmod 755 /chromedriver
java -jar selenium-server-standalone-2.35.0.jar -Dwebdriver.chrome.driver="./chromedriver"

/////////////////////////////////
centos7 64下node selenium-server.js 出错:
error while loading shared libraries: libgconf-2.so

原因:Linux based EC2 instances lack gtk+, which is a must to launch any GUI enabled applications. - See more at: http://itsallabtamil.blogspot.com/2013/02/setting-up-chrome-firefox-ec2-selenium-java.html#sthash.Ga4egJnu.dpuf

办法:yum install libgconf-2.so.4 -y
/////////////////////////////////
通过server网址访问出错:HTTP ERROR: 403 Forbidden for Proxy RequestURI=/session

var driver = new webdriver.Builder()
//.forBrowser('firefox')
.usingServer("http://localhost:4444")
.withCapabilities(webdriver.Capabilities.chrome())
.build();

办法:用这个路径 http://localhost:4444/wd/hub

http://localhost:4444/wd/hub/static/resource/hub.html
//////////////////////////////////////////////
最后node test.js
测试脚本:

var webdriver = require('selenium-webdriver'),
By = require('selenium-webdriver').By,
until = require('selenium-webdriver').until;

var driver = new webdriver.Builder()
.forBrowser('chrome')
.build();

driver.get('https://www.baidu.com/');
driver.findElement(By.id('kw')).sendKeys('zhaiduo');
driver.findElement(By.id('su')).click();
driver.wait(until.titleIs('zhaiduo_百度搜索'), 3000);
driver.quit();


运行后,chrome浏览器自动打开,并访问相应网址,自动执行脚本中的各种事件。

///////////////

语法参考:

driver.navigate("file:///race_condition.html")
el = driver.find_element_by_tag_name("p")
assert el.text == "Hello from JavaScript!"

def document_initialised(driver):
return driver.execute_script("return initialised")

driver.navigate("file:///race_condition.html")
WebDriverWait(driver).until(document_initialised)
el = driver.find_element_by_tag_name("p")
assert el.text == "Hello from JavaScript!"

from selenium.webdriver.support.ui import WebDriverWait

driver.navigate("file:///race_condition.html")
el = WebDriverWait(driver).until(lambda d: return d.find_element_by_tag_name("p"))
assert el.text == "Hello from JavaScript!"

https://seleniumhq.github.io/docs/wd.html#driver_requirements

/////////练习

微博通过oauth自动登录,刷新微博

/////////扩展

可以做成自动化同步微博工具。
引用截屏插件


























更多参考:
https://github.com/chibimagic/WebDriver-PHP

http://code.google.com/p/php-webdriver-bindings/



下一页


最 近 文 章

  1. Google的中美办公室对比 - Wed, 06 Sep 2006 05:12:48 +0000
  2. Apach1.3.31+PHP4.3.3安装出错:can't load of php4apache.dll - Tue, 05 Sep 2006 16:12:32 +0000
  3. 广州暂住人员如何申请港澳游 - Sat, 02 Sep 2006 16:25:54 +0000
  4. CSS样例:英文首字母特大写 - Fri, 25 Aug 2006 05:06:15 +0000
  5. 微软说:任何人都可以为XBOX开发游戏 - Wed, 23 Aug 2006 05:35:01 +0000
  6. 来自kiko.com的互联网创业经验 - Tue, 22 Aug 2006 05:17:16 +0000
  7. Google工具栏的安全浏览功能 - Thu, 17 Aug 2006 08:57:48 +0000
  8. mac 终端常见问题 - Mon, 30 Nov -0001 00:00:00 +0000
  9. Login SSH with Rsa Key - Mon, 30 Nov -0001 00:00:00 +0000
  10. 重新学习Linux - Mon, 30 Nov -0001 00:00:00 +0000