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

脚本如下:

Java代码  收藏代码
  1. #! /bin/bash  
  2. if [ -z "$1" ]; then   
  3. echo "`basename $0` {start|stop}"  
  4. exit  
  5. fi  
  6.   
  7. case "$1" in  
  8. start)  
  9.       /usr/bin/Xvfb :7 -ac -screen 0 1024x768x8 &  
  10. ;;  
  11. stop)  
  12.       killall Xvfb  
  13. ;;  
  14. esac  

 

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

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

 

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

完毕了。

 

 

三、Xvfb在CentsOS安装

Java代码  收藏代码
  1.  Install Xvfb with library:  
  2.   
  3.     yum install Xvfb  
  4.     yum -y install libXfont  
  5.     yum install xorg-x11-fonts*  
  6.   
  7.   
  8. touch /etc/init.d/xvfb with content:  
  9.   
  10. #!/bin/bash  
  11. #  
  12. # /etc/rc.d/init.d/xvfbd  
  13. #  
  14. # chkconfig: 345 95 28  
  15. # description: Starts/Stops X Virtual Framebuffer server  
  16. # processname: Xvfb  
  17. #  
  18.   
  19. . /etc/init.d/functions  
  20.   
  21. "${NETWORKING}" = "no" ] && exit 0  
  22.   
  23. PROG="Xvfb"  
  24. PROG_OPTIONS=":7 -ac -screen 0 1024x768x24"  
  25. PROG_OUTPUT="/tmp/Xvfb.out"  
  26.   
  27. case "$1" in  
  28.     start)  
  29.         echo -n "Starting : X Virtual Frame Buffer "  
  30.         $PROG $PROG_OPTIONS>>$PROG_OUTPUT 2>&1 &  
  31.         disown -ar  
  32.         /bin/usleep 500000  
  33.         status Xvfb & >/dev/null && echo_success || echo_failure  
  34.         RETVAL=$?  
  35.         if [ $RETVAL -eq 0 ]; then  
  36.             /bin/touch /var/lock/subsys/Xvfb  
  37.             /sbin/pidof -o  %PPID -x Xvfb > /var/run/Xvfb.pid  
  38.         fi  
  39.         echo  
  40.         ;;  
  41.     stop)  
  42.         echo -n "Shutting down : X Virtual Frame Buffer"  
  43.         killproc $PROG  
  44.         RETVAL=$?  
  45.         [ $RETVAL -eq 0 ] && /bin/rm -f /var/lock/subsys/Xvfb /var/run/Xvfb.pid  
  46.         echo  
  47.         ;;  
  48.     restart|reload)  
  49.         $0 stop  
  50.         $0 start  
  51.         RETVAL=$?  
  52.         ;;  
  53.     status)  
  54.         status Xvfb  
  55.         RETVAL=$?  
  56.         ;;  
  57.     *)  
  58.      echo $"Usage: $0 (start|stop|restart|reload|status)"  
  59.      exit 1  
  60. esac  
  61.   
  62. exit $RETVAL  
  63.   
  64. Registering in system and start:  
  65. chmod +x /etc/init.d/xvfb  
  66. chkconfig xvfb on  
  67. service xvfb start  
  68.   
  69. now  
  70.   
  71. 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

posted @ 2018-05-08 09:56  快乐就好  阅读(12963)  评论(0编辑  收藏  举报