在没有图形界面的Ubuntu server上运行Selenium测试

简单记录一下:
首先下载Selenium server,一个jar包:
http://selenium.googlecode.com/files/selenium-server-standalone-2.37.0.jar

然后安装运行Selenium需要的软件(本人使用PHPUnit跑测试,浏览器用的是firefox):

1
2
3
sudo apt-get install phpunit-selenium
sudo apt-get install xvfb
sudo apt-get install firefox

然后是启动selenium server和xvfb,xvfb的作用是骗过firefox,将图形界面输出到无底洞:

1
2
3
Xvfb :99 -ac &
export DISPLAY=:99
java -jar ./selenium-server-standalone-2.37.0.jar &

现在可以创建和运行测试了,PHP代码”helloTest.php”:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php

require_once 'PHPUnit/Extensions/SeleniumTestCase.php';

class helloTest extends PHPUnit_Extensions_SeleniumTestCase
{
  function setUp()
  {
    $this->setBrowser("*firefox");
    $this->setBrowserUrl("http://www.uiu.cc/");
  }


  function testMe(){
    $this->open("/hello");
    $this->assertTrue("hello" == "hello");
  }
}
?>

然后运行:

1
phpunit ./helloTest.php

输出:

1
2
3
4
5
6
7
PHPUnit 3.6.10 by Sebastian Bergmann.

.

Time: 6 seconds, Memory: 3.50Mb

OK (1 test, 1 assertion)

成功!

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *