還有另一種方法,就是使用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