如何在无显示器的Ubuntu下跑前端测试

很久以前,我也使用selenium做自动化的集成测试,使用HtmlUnit的webdriver,所以不需要显示器。但是HtmlUnit的表现总是有些不如意。而最近在项目中发现这篇文章,解决我长久以来的问题:在没有显示器的服务器上运行Firefox的集成测试。

Selenium是一个web自动化测试框架。用它可以实现web应用自动化测试。不过,我不只是用它来做测试,我还用它从电子商务网站签到页面爬取javascript生成的或AJAX的内容。

作为程序员,我不满足于使用Selenium IDE来记录和重放宏记录。那样很蹩脚,而且不适合部署到多台服务器。这时,你需要Selenium WebDriver,它又灵活,而且通过Selenium headless,运行Selenium在服务器上不需要显示设备。

为什么要运行Headless Selenium 测试?

当你希望能在服务器上运行的健壮的自动化操作,而其操作又依赖于 27X7,同时还希望它是稳定的,这时,Selenium是你唯一的选择。但是,Selenium需要运行在浏览器上。所以,你得骗Selenium,让它觉得,它正跑在一台带有显示器的机器上。这样,你就可以不间断的跑自动化测试,同时又不失稳定性和扩展性。

如何在Ubuntu上运行Selenium headless

本教程的目标是在使用Mozilla Firefox作为主浏览器的ubuntu上配置和运行selenium headless。

安装Firefox headless

确认你的ubuntu安装的是最新版本的Firefox。我遇到过Selenium的版本和Firefox的版本不兼容问题。如果你没有安装Firefox或者使用的是老版本的Firefox,可按以下步骤升级Firefox:

在/etc/apt/sources.list加入:

ppa:mozillateam/firefox-stable

运行以下命令升级或安装Firefox

sudo apt-get update

sudo apt-get install firefox

运行成功后,ubuntu上就应该安装好最新版本的Firefox了。

安装Xvfb——一个X虚拟框架

这个仿真框架使用虚拟内存能让X-Server运行在没有显示设备的机器上。这样,浏览器就可以运行了。在ubuntu和Debian上安装xvfb,只要运行:

sudo apt-get install xvfb

现在,可以运行xvfb服务上一个带有数字的显示设备上,这样是为了防止你在下阶段添加设备时引发冲突。本教程,我们分配一个显示设备 10.

sudo Xvfb :10 -ac

-ac代表关闭xvfb的访问控制。好了,服务器可以运行了。

启动浏览器

在你运行浏览器前,你首先要设置DISPLAY环境变量,以指定xvfb运行在哪个显示设备上。在加入环境变量前,我们检查一下所有的这些都如我们所料:

export DISPLAY=:10

firefox

如果终端(terminal)没有显示错误,就说明你已经成功运行Firefox在无显示设备的ubuntu上了。它会一直运行,直到你使用ctrl + C或其它类似方法来终止其运行。同时,它不会有任何输出。

如果你能成功运行以上的步骤,那么接下来的部分就是轻而易举了。现在,我们可以在ubuntu服务器上运行selenium,如同你在本地运行一样。本教程的下一部分,我展示了如何运行一个独立selenium服务器,同时使用PHP的selenium webdriver去连接。

小结

很久以前,我也使用selenium做自动化的集成测试,使用HtmlUnit的webdriver,所以不需要显示器。但是HtmlUnit的表现总是有些不如意。而最近在项目中发现这篇文章,解决我长久以来的问题:在没有显示器的服务器上运行Firefox的集成测试。

而本文,我更多尝试的是意译。有不对的地方,谢谢斧正!

更多Ubuntu相关信息见Ubuntu 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=2

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-07/104819.htm



jenkins插件安装:

You must to define installation Xvfb on Jenkins. Access Manage Jenkins >> Global Tool Configuration. In Section Xvfb installation, put name and fill the field "Directory in which to find Xvfb executable" with /usr/bin.


https://wiki.jenkins-ci.org/display/JENKINS/Xvfb+Plugin

http://stackoverflow.com/questions/32151043/xvfb-docker-cannot-open-display


Dockerfile

...
ENV DISPLAY :99

ADD run.sh /run.sh
RUN chmod a+x /run.sh

CMD /run.sh

run.sh

Xvfb :99 -screen 0 640x480x8 -nolisten tcp &
firefox

WebDriver: Getting it to play nicely with Xvfb

Mark Needham

Thoughts on Software Development

WebDriver: Getting it to play nicely with Xvfb

with 2 comments

Another thing we’ve been doing with WebDriver is having it run with the FirefoxDriver while redirecting the display output into the Xvfb framebuffer so that we can run it on our continuous integration agents which don’t have a display attached.

The first thing we needed to do was set the environment property ‘webdriver.firefox.bin’ to our own script which would point the display to Xvfb before starting Firefox:

import java.lang.System._
lazy val firefoxDriver: FirefoxDriver = {
  setProperty("webdriver.firefox.bin", "/our/awesome/starting-firefox.sh")
  new FirefoxDriver()
}

Our first version of the script looked like this:

/our/awesome/starting-firefox.sh

#!/bin/bash
 
rm -f ~/.mozilla/firefox/*/.parentlock
rm -rf /var/go/.mozilla
 
 
XVFB=`which xVfb`
if [ "$?" -eq 1 ];
then
    echo "Xvfb not found."
    exit 1
fi
 
$XVFB :99 -ac &
 
 
BROWSER=`which firefox`
if [ "$?" -eq 1 ];
then
    echo "Firefox not found."
    exit 1
fi
 
export DISPLAY=:99
$BROWSER &

The mistake we made here was that we started Xvfb in the background which meant that sometimes it hadn’t actually started by the time Firefox tried to connect to the display and we ended up with this error message:

No Protocol specified
Error cannot open display :99

We really wanted to keep Xvfb running regardless of whether the Firefox instances being used by WebDriver were alive or not so we moved the starting of Xvfb out into a separate script which we run as one of the earlier steps in the build.

We also struggled to get the FirefoxDriver to kill itself after each test as calling ‘close’ or ‘quit’ on the driver didn’t seem to kill off the process.

We eventually resorted to putting a ‘pkill firefox’ statement at the start of our firefox starting script:

/our/awesome/starting-firefox.sh

#!/bin/bash
 
rm -f ~/.mozilla/firefox/*/.parentlock
rm -rf /var/go/.mozilla
 
pkill firefox
 
BROWSER=`which firefox`
if [ "$?" -eq 1 ];
then
    echo "Firefox not found."
    exit 1
fi
 
export DISPLAY=:99
$BROWSER &

It’s a bit hacky but it does the job more deterministically than anything else we’ve tried previously.

Be Sociable, Share!

Mark Needham

Thoughts on Software Development

WebDriver: Getting it to play nicely with Xvfb

with 2 comments

Another thing we’ve been doing with WebDriver is having it run with the FirefoxDriver while redirecting the display output into the Xvfb framebuffer so that we can run it on our continuous integration agents which don’t have a display attached.

The first thing we needed to do was set the environment property ‘webdriver.firefox.bin’ to our own script which would point the display to Xvfb before starting Firefox:

import java.lang.System._
lazy val firefoxDriver: FirefoxDriver = {
  setProperty("webdriver.firefox.bin", "/our/awesome/starting-firefox.sh")
  new FirefoxDriver()
}

Our first version of the script looked like this:

/our/awesome/starting-firefox.sh

#!/bin/bash
 
rm -f ~/.mozilla/firefox/*/.parentlock
rm -rf /var/go/.mozilla
 
 
XVFB=`which xVfb`
if [ "$?" -eq 1 ];
then
    echo "Xvfb not found."
    exit 1
fi
 
$XVFB :99 -ac &
 
 
BROWSER=`which firefox`
if [ "$?" -eq 1 ];
then
    echo "Firefox not found."
    exit 1
fi
 
export DISPLAY=:99
$BROWSER &

The mistake we made here was that we started Xvfb in the background which meant that sometimes it hadn’t actually started by the time Firefox tried to connect to the display and we ended up with this error message:

No Protocol specified
Error cannot open display :99

We really wanted to keep Xvfb running regardless of whether the Firefox instances being used by WebDriver were alive or not so we moved the starting of Xvfb out into a separate script which we run as one of the earlier steps in the build.

We also struggled to get the FirefoxDriver to kill itself after each test as calling ‘close’ or ‘quit’ on the driver didn’t seem to kill off the process.

We eventually resorted to putting a ‘pkill firefox’ statement at the start of our firefox starting script:

/our/awesome/starting-firefox.sh

#!/bin/bash
 
rm -f ~/.mozilla/firefox/*/.parentlock
rm -rf /var/go/.mozilla
 
pkill firefox
 
BROWSER=`which firefox`
if [ "$?" -eq 1 ];
then
    echo "Firefox not found."
    exit 1
fi
 
export DISPLAY=:99
$BROWSER &

It’s a bit hacky but it does the job more deterministically than anything else we’ve tried previously.

Be Sociable, Share!

你可能感兴趣的:(Docker,Maven,&,Jenkins)