Selenium 是最流行的自动化测试工具,提供一整套的解决方案,其中webdriver remote方式比较灵活,浏览器可以指定本地或远程。部署也简单,在浏览器端运行java服务,客户端测试用例可以用 Java、Ruby、PHP、Python 编写。
测试服务器:
下载地址: http://selenium.googlecode.com/files/selenium-server-standalone-2.20.0.jar
1. java -jar selenium-server-standalone-2.20.0.jar
Ruby:
Ruby使用watir-webdriver接口。
测试代码:
1. require "watir-webdriver"
2. require "selenium/webdriver/remote/capabilities"
3.
4. browser = Watir::Browser.new :remote, :url => "http://localhost:4444/wd/hub", :desired_capabilities
5.
6. browser.goto("www.tudou.com")
7.
8. browser.text_field(:id => 'email').set 'test'
9. browser.text_field(:id => 'pwd').set '1234'
10.
11. browser.button(:class => 'lg_b').click
12.
13. puts browser.text.include? 'SHH'
14.
15. browser.li(:id, "myTudouNav").fire_event("onmouseover")
16.
17. browser.a(:xpath, ".//*[@id='myTudouNav']/div/ul/li[12]/a").click
18.
19. puts browser.text.include? 'SHH'
20.
21. js = <<JS
22. '.logo').css('border', '1px solid red');
23. JS
24.
25. browser.execute_script js
26.
27. browser.close
安装watir-webdriver:
1. 安装ruby 1.9.3。下载地址:http://rubyinstaller.org/downloads/
2. 安装watir-webdriver。
> gem update --system --no-rdoc --no-ri
> gem install watir-webdriver --no-rdoc --no-ri
4. 安装chromedriver,用于支持chrome。 下载地址:http://code.google.com/p/chromedriver/downloads/list
PHP:
PHP使用facebook的php-webdriver接口。
测试代码:
1. require_once 'php-webdriver/__init__.php';
2.
3. $wd_host = 'http://localhost:4444/wd/hub';
4. $web_driver = new WebDriver($wd_host);
5.
6. $session = $web_driver->session('chrome');
7.
8. $session->open('http://www.tudou.com');
9.
10. $session->element('css selector', '#email')->value(array('value' => str_split('test')));
11. $session->element('css selector', '#pwd')->value(array('value' => str_split('1234')));
12.
13. $session->element('css selector', '.lg_b')->click('');
14.
15. echo preg_match('/SSH/s', $session->source()) ? 'ok' : 'error';
16.
17. $element = $session->element('id', "myTudouNav");
18. $session->moveto(array('element' => $element->getID()));
19.
20. $session->element('xpath', ".//*[@id='myTudouNav']/div/ul/li[12]/a")->click('');
21.
22. $script
23. $('.logo').css('border', '1px solid red');
24. JS;
25. $session->execute(array(
26. 'script' => $script,
27. 'args' => array(),
28. ));
29.
30. $session->close();
php-webdriver 下载地址:https://github.com/facebook/php-webdriver
Python:
测试代码:
1. from selenium import
2. from selenium.webdriver.remote.command import
3. from selenium.webdriver.common.desired_capabilities import
4.
5. browser = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.CHROME);
6.
7. browser.get("http://www.tudou.com/");
8.
9. browser.find_element_by_css_selector("#email").send_keys("test")
10. browser.find_element_by_css_selector("#pwd").send_keys("1234")
11.
12. browser.find_element_by_css_selector("input.lg_b").click()
13.
14. element = browser.find_element_by_css_selector("#myTudouNav")
15. browser.execute(Command.MOVE_TO, {'element': element.id})
16.
17. browser.find_element_by_xpath(".//*[@id='myTudouNav']/div/ul/li[12]/a").click()
18.
19. js = "$('.logo').css('border', '1px solid red');";
20. browser.execute_script(js)
21.
22. browser.close()
selenium python安装方法:easy_install –U selenium
参考文档(有些网站需要翻X):
http://openqa.org/
http://seleniumhq.org/
http://watir.com
https://github.com/watir/watir-webdriver
http://code.google.com/p/selenium/
http://17test.info/
http://www.ibm.com/developerworks/cn/java/j-lo-rubytesting/
http://www.ibm.com/developerworks/cn/opensource/os-cn-watir/
http://www.facebook.com/notes/facebook-engineering/watir-to-webdriver-unit-test-frameworks/10150314152278920
http://itreallymatters.net/post/10991877834/facebook-watir-and-testing
淘宝automan介绍:
http://qa.taobao.com/wp-content/uploads/2011/06/AutomanUserMenuZH-CN.pdf
JsonWire协议:
http://code.google.com/p/selenium/wiki/JsonWireProtocol#Command_Reference