上星期,无意在网上看到selenium,觉得挺好用的,他是一个web测试工具,并具不受操作系统,浏览器限制(IE,firefox,safari)。
我们可用来测试手工任务的功能的,但是手工测试这些任务可能很花时间,并且容易出现人为的错误。使用这工具定制一些测试,或者在项目开始时写好测试角本,通过所有角本后,功能算是OK。
但我将其进行了稍微改装也可以用其抓取ajax的网站,或者很多用post提交方法的网站(一般会将其下一页的LINK给隐式提交,无法得知下一页的链接,于是spider over)。
下面将其稍微介绍一下,大家有兴趣的可以去研究,并不一定会用到实处,但做点准备,将来万一用得着了,这里给大家一点点经验,抛砖引玉。
Selenium Remote Control (RC) is a test tool that allows you to write automated web application UI tests in any programming language against any
HTTP website using any mainstream JavaScript-enabled browser.
Selenium RC comes in two parts.
1、 A server which automatically launches and kills browsers, and acts as a HTTP proxy for web requests from them.
2、 Client libraries for your favorite computer language.
The Selenium Server is great for testing complex AJAX based web user interfaces under a Continuous Integration system. It is also an ideal solution for users of Selenium Core or Selenium IDE
who want to write tests in a more expressive programming language than the Selenese HTML table format customarily used with Selenium Core.
这是从其官网(http://selenium-rc.openqa.org/)摘下来的描述,如想了解更多就去官网并下载相应的架包。
我上周末曾试用过,感觉不错,操作起来也挺简单的,现将具体的只得步骤简单描述一下:
1、首先我们要获得selenium的服务,启动服务:
Java –jar selenium-server.jar –interactive
这个将启动Selenium服务器,并允许你在命令窗口输入命令。在一些登录信息后边,你应该看到如下的信息:
Entering interactive mode... type Selenium commands here(e.g:cmd=open&1=http://www.yahoo.com)
(当然得安装好jre,我在第一次使用时,曾因jre导致未启动,具体好像是selenium对jre太"挑剔",是因我的jre是非正式版的,于是修改过后,启动服务成功,稍兴奋了一场,接下来的,照着文档操作,很快熟练了)
2、启动服务后,我们可以通过命令来打开IE或者firefox等;
如使用IE:cmd=getNewBrowserSession&1=*iexplore&2=http://www.google.com
Firefox: cmd=getNewBrowserSession&1=*firefox&2=http://www.google.com
如果一切正常,你将会发现在你选择的浏览器中打开了一个新的窗口,回到Selenium服务器窗口中,你会发现如下的信息:
Got result: OK,405dc
3f
7914848eebb72dab11cbe
0a
3e on session 405dc
3f
7914848eebb72dab11cbe
0a
3e
3、然后我们可以通过得到的Session进行操作窗口:
(打开google主页)cmd=open&1=http://www.google.com/webhp&sessionId=405dc
3f
7914848eebb72dab11cbe
0a
3e
(在输入框中输入“hello world”)cmd=type&1=q&2=hello world&sessionId=405dc
3f
7914848eebb72dab11cbe
0a
3e
(点击搜索按钮)cmd=click&1=btnG&sessionId=405dc
3f
7914848eebb72dab11cbe
0a
3e
… …
Selenium提供了很多命令,具体可以看帮助文档。
以上是通过命令行,来使用Selenium来操作。下面我们通过java来实现同样的操作(需在项目中添加selenium相应的架包 - selenium-java-client-driver.jar):
首先仍需启动服务;
下面是一个java应用的测试程序:
- import com.thoughtworks.selenium.DefaultSelenium;
- import com.thoughtworks.selenium.Selenium;
- public class SeleniumTest {
- public static void main(String[] args) {
- Selenium browser = new DefaultSelenium("localhost", 4444, "*iexplore","http://www.google.com/");
- browser.start();//启动服务
- browser.open("http://www.google.com/");
- browser.type("q", "hello world");
- browser.click("btnG");
- browser.waitForPageToLoad("5000");//等待页面装载完成
- String[] Link = browser.getAllLinks();//提出所有的LINK
- System.out.println(Link.length);
- System.out.println(browser.getTitle());
- }
- }//~
out:
81
hello world - Google 搜索
测试版:
- import com.thoughtworks.selenium.*;
- import junit.framework.*;
- public class GoogleTest extends TestCase {
- private Selenium sel;
- public void setUp() {
- sel = new DefaultSelenium("localhost",
- 4444, "*firefox", "http://www.google.com");
- sel.start();
- }
- public void testGoogle() {
- sel.open("http://www.google.com/webhp");
- sel.type("q", "hello world");
- sel.click("btnG");
- sel.waitForPageToLoad("5000");
- assertEquals("hello world - Google Search", sel.getTitle());
- }
- public void tearDown() {
- sel.stop();
- }
- }
C# 代码如下:
- using Selenium;
- using NUnit.Framework;
- namespace MyTests {
- [TestFixture]
- public class GoogleTest {
- private ISelenium sel;
- [SetUp]
- public void SetUp() {
- sel = new DefaultSelenium("localhost",
- 4444, "*firefox", "http://www.google.com");
- sel.Start();
- }
- [Test]
- public void testGoogle() {
- sel.Open("http://www.google.com/webhp");
- sel.Type("q", "hello world");
- sel.Click("btnG");
- sel.WaitForPageToLoad("5000");
- Assert.AreEqual("hello world - Google Search", sel.GetTitle());
- }
- [TearDown]
我的工作主要是抓取数据,我现正在发掘,Selenium能给我工作带来什么方便之处,如有新的"研究"出来,将再发布,大家共同交流。
内嵌浏览器的spider关键是在性能问题,如果抓取大量的网页,单个的去访问,这未免效率太过低了。因些首先是解析效率问题,先前我也曾使用过chickenfoot、JDIC等,但稳定性都不见好,程序跑不了很久。而Selenium有google的资金注入及广大的社区。我想其性能及稳定性要强于前二都(JDIC还未正式发布,现只是Beta版)。
我想浏览器也可以支持并发,比如,我们在搜索结果页面点击一个LINK后,往往会打开新的窗体,如果这时在该窗体网页未完全打开时回到搜索结果页面再点击下一个LINK。这样浏览器里可以同时并行下载多个网页的目的,实现并发下载,但此功能还没去"挑战"。