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
相关推荐
1.1 selenium webdriver学习(一)------------快速开始 . . . . . . . . . ....1.2 selenium webdriver...1.12 selenium webdriver学习(十二)------------如何利用selenium-webdriver截图 . . . . . . . . . . . .38
Selenium WebDriver 学习笔记 API大全 中文API 元素 操作大全
selenium RC与selenium webdriver的区别
关于Selenium WebDriver工作原理的介绍,原理相关描述
自动化测试:Selenium webdriver学习笔记C#版
Selenium WebDriver 3 Practical Guide will walk you through the various APIs of Selenium WebDriver, which are used in automation tests, followed by a discussion of the various WebDriver implementations...
• 在命令行中输入gem list,如果selenium-webdriver正确安装,则其应该出现在结果列表里。在文本写作时,selenium webdriver的最新版本应该是2.2.0; gem list selenium-webdriver 安装firefox。本文使用firefox作为...
本教程是来源与书 Selenium Testing Tools cookbook,但是其中的所有代码都是通过python来实现并且调试通过的。此文档为版本为粗略版,后期会不断的更新优化
Selenium WebDriver Practical Guide will guide you through the various APIs of WebDriver which should be used in automation tests, followed by a discussion of the various WebDriver implementations ...
selenium-webdriver-2.5.0.gem
selenium-webdriver原创selenium-webdriver原创selenium-webdriver原创selenium-webdriver原创
Selenium WebDriver 3.14.0 本来需要.net45 我改为.net2.0 希望喜欢的人需要。 dll 反编译,嵌入程序内。无需调用那个讨厌的dll。 绝对不骗人,骗人是小狗。架构复杂修改麻烦,各位体谅
在命令行中输入gem list,如果selenium-webdriver正确安装,则其应该出现在结果列表里。在文本写作时,selenium webdriver的最新版本应该是2.2.0; gem list selenium-webdriver 安装firefox。本文使用firefox...
seleniumwebdriver(python) (第三版)
selenium webdriver基于python源码案例,全是案例适合小白入门学习
从基础到精通的学习 selenium+webdriver的学习方法
selenium WebDriver 安装包及启动服务的一些命令 对于正在学习自动化测试的小伙伴们非常有用
selenium-webdriver-2.40.0.gem
对于Selenium更加详细的操作和使用,推荐一本书《selenium webdriver(python)第三版》,该书详细的讲解了webdriver的用法
Selenium Webdriver自动化测试 operadriver win64位,最新版本(20150611)