`

selenium webdriver学习(二十二)------------XVFB实现selenium在linux上无界面运行安装篇

阅读更多

           selenium在linux上无界面运行,其实是非常简单的。具体的方法有使用HtmlUnitDriver或者PhantomJSDriver,有时间我会写写关于这两个东东的文章,其实基本和ChromeDriver 和FirefoxDriver是一样的。但是有些人或者会比较排斥他们说HtmlUnitDriver对JS支持不好,PhantomJSDriver估计也很少有人用,其实他是对Phantomjs的封装,对这些不做多过评论,我用下来感觉还好。

            还有另一种方法,就是使用XVFB, 有人说XVFB是什么,没听说过,没听说过就自己Google吧。

这里就主要是讲一下XVFB的安装使用。以chrome + ubuntu 和 firefox + centOS 为例子(chrome linux版好像是到6的时候就不支持centOS了, 都自带firefox )

一、 XVFB在Ubuntu上的安装(chrome)

1. 安装ubuntu(百度google安装步骤)

2. putty.exe 连接ubuntu

    安装openssh-server:sudo apt-get install openssh-server
    启动openssh-server:   sudo /etc/init.d/ssh start
    确认openssh-server是否启动:ps -e | grep ssh
    telnet ip 端口号

3. 安装oracle JDK6:(可跳过:自带penjdk-6-jre)
    $ remove openjdk:  sudo apt-get autoremove openjdk-6-jre
    $ sudo add-apt-repository ppa:webupd8team/java
    $ sudo apt-get update
    $ sudo apt-get install oracle-java6-installer
    $ sudo update-java-alternatives -s java-6-oracle

4. 装chrome:(自带firefox)
    http://www.ubuntuupdates.org/ppa/google_chrome
    wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
    sudo sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
    sudo apt-get update
    sudo apt-get install google-chrome-stable

5. 装xvfb 及各种:
       sudo apt-get update && sudo apt-get install -y xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic xvfb x11-apps  imagemagick firefox google-chrome-stable

 

OK, 到这一步都装好了。

 

二、测试安装

putty连接Ubuntu运行下面命令:

1. 启动Xvfb服务

 

 Xvfb -ac :7 -screen 0 1280x1024x8  (注意这个是x, 不是* 哦)

 2.  启动firefox or chrome

export  DISPLAY=:7

/usr/bin/google-chrome-stable http://www.investopedia.com         //chrome 浏览www.investopedia.com

或者

export DISPLAY=:7

firefox http://www.investopedia.com                                              //firefox 浏览www.investopedia.com

 

如果运行完后,出现:

Xlib: extension "RANDR"  missing on display ":7"

我想说,you made it. 这是个无关紧要的信息,之前我试图解决掉它,没有成功。最后我在运行selenium脚本后,完全没有出现这个信息,脚本执行很正常,所以现在我把它当做是安装成功的信息。

 

当然运行selenium 脚本前总不能老是敲一遍这些命令,太麻烦了。

弄成一个服务,先 touch /etc/init.d/xvfb

脚本如下:

#! /bin/bash
if [ -z "$1" ]; then 
echo "`basename $0` {start|stop}"
exit
fi

case "$1" in
start)
      /usr/bin/Xvfb :7 -ac -screen 0 1024x768x8 &
;;
stop)
      killall Xvfb
;;
esac

 

修改脚本权限,启动服务:

chmod +x /etc/init.d/xvfb
chkconfig xvfb on
service xvfb start

 

停止服务的话就是: service xvfb stop

完毕了。

 

 

三、Xvfb在CentsOS安装

 Install Xvfb with library:

    yum install Xvfb
    yum -y install libXfont
    yum install xorg-x11-fonts*


touch /etc/init.d/xvfb with content:

#!/bin/bash
#
# /etc/rc.d/init.d/xvfbd
#
# chkconfig: 345 95 28
# description: Starts/Stops X Virtual Framebuffer server
# processname: Xvfb
#

. /etc/init.d/functions

[ "${NETWORKING}" = "no" ] && exit 0

PROG="Xvfb"
PROG_OPTIONS=":7 -ac -screen 0 1024x768x24"
PROG_OUTPUT="/tmp/Xvfb.out"

case "$1" in
    start)
        echo -n "Starting : X Virtual Frame Buffer "
        $PROG $PROG_OPTIONS>>$PROG_OUTPUT 2>&1 &
        disown -ar
        /bin/usleep 500000
        status Xvfb & >/dev/null && echo_success || echo_failure
        RETVAL=$?
        if [ $RETVAL -eq 0 ]; then
            /bin/touch /var/lock/subsys/Xvfb
            /sbin/pidof -o  %PPID -x Xvfb > /var/run/Xvfb.pid
        fi
        echo
        ;;
    stop)
        echo -n "Shutting down : X Virtual Frame Buffer"
        killproc $PROG
        RETVAL=$?
        [ $RETVAL -eq 0 ] && /bin/rm -f /var/lock/subsys/Xvfb /var/run/Xvfb.pid
        echo
        ;;
    restart|reload)
        $0 stop
        $0 start
        RETVAL=$?
        ;;
    status)
        status Xvfb
        RETVAL=$?
        ;;
    *)
     echo $"Usage: $0 (start|stop|restart|reload|status)"
     exit 1
esac

exit $RETVAL

Registering in system and start:
chmod +x /etc/init.d/xvfb
chkconfig xvfb on
service xvfb start

now

export DISPLAY=:7 (actually you should add this to your etc/bashrc)

 

selenium 脚本测试一下环境:

随便丢个简单的selenium 写的脚本到配制好的测试机子上运行,如果没有任何报错,说明环境就是配制Ok的。

 

参考资料:

http://rrroutes.blogspot.com/2013/04/settup-environment-for-selenium-tests.html

http://www.installationpage.com/selenium/how-to-run-selenium-headless-firefox-in-ubuntu/

http://ralf.schaeftlein.de/2012/05/26/running-headless-webdriver-based-selenium-junit-tests-inside-jenkins-under-ubuntu-linux/

https://gist.github.com/textarcana/5855427

http://stackoverflow.com/questions/17944234/xlib-extension-randr-missing-on-display-21-trying-to-run-headless-googl

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics