環(huán)境搭建
Xvfb介紹
Xvfb is an X server that can run on machines with no display hardware and no physical input devices. It emulates a dumb framebuffer using virtual memory.
https://www.x.org/archive/X11R7.6/doc/man/man1/Xvfb.1.xhtml
簡單來說,就是模擬了window的圖形化功能,所以如果想實例化FireFoxWebDriver,仍然需要安裝一個firefox。
Xvfb安裝
root@xxx:~# aptitude search xvfb
i xvfb - Virtual Framebuffer 'fake' X server
root@xxx:~# aptitude install xvfb
Xvfb啟動
root@xxx:~# Xvfb :1 -screen 1 1600x1200x16
root@xxx:~# ps -ef | grep Xvfb
root 19118 4988 0 10:48 pts/6 00:00:00 grep Xvfb
root 24808 1 0 Jun28 pts/1 00:00:33 Xvfb :1 -screen 1 1600x1200x16 -nolisten tcp
命令的意思是:作為Server Number1 監(jiān)聽。有兩個屏幕配置,默認是Screen0,width, height, and depth = 1280x1024x8,第二塊屏幕配置為width, height, and depth = 1600x1200x16。
這里如果用自動啟動腳本是更佳的:
root@xxx:/etc/init.d# vim /etc/init.d/xvfb
#!/bin/bash
#chkconfig: 345 95 50
#description: Starts xvfb on display 1
if [ -z "$1" ]; then
echo "`basename $0` {start|stop}"
exit
fi
case "$1" in
start)
Xvfb :1 -screen 1 1600x1200x16 -nolisten tcp &
export DISPLAY=:1
echo 'export DISPLAY=:1' >> ~/.bashrc
;;
stop)
killall Xvfb
;;
esac
Firefox
安裝
root@xxx:~# aptitude search firefox
i firefox-esr - Mozilla Firefox web browser - Extended Support Release (ESR)
root@xxx:~# aptitude install firefox-esr
root@xxx:~# firefox -v
Mozilla Firefox 45.2.0
啟動firefox
root@xxx:~# firefox
Xlib: extension "RANDR" missing on display ":1".
Xlib: extension "RANDR" missing on display ":1".
到這里是不是有點懵比,界面呢?? VNC server來解決這個問題
X11VNC server
介紹
x11vnc allows one to view remotely and interact with real X displays (i.e. a display corresponding to a physical monitor, keyboard, and mouse) with any VNC viewer.
http://www.karlrunge.com/x11vnc/
安裝運行
root@xxx:~# aptitude search X11VNC
i x11vnc - VNC server to allow remote access to an existing X session
root@xxx:~# aptitude install x11vnc
root@xxx:~# x11vnc -display :1 -xkb
參數(shù)解釋,Display 1,就是上面Xvfb配置的1號屏幕。
-xkb,看到官網解釋,是優(yōu)化鍵盤輸入
安裝VNC Viewer
這個就不多說了,http://www.realvnc.com/,來這里找到下載包安裝即可。
通過VNC viewer鏈接 云主機,效果如下:

測試執(zhí)行效果
測試selenium
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException, TimeoutException
browser = webdriver.Firefox()
browser.get("http://www.baidu.com")
t=browser.find_element_by_xpath("http://div[contains(@id,'ftCon')]")
print t.text
執(zhí)行效果如下:

成功
問題
- 還未大范圍執(zhí)行,不知道穩(wěn)定性如何。也不知道和window/Mac OX真實場景相比如何
- openid訪問內網一般都需要將軍令,這個需要解決。
- Chrome還未實驗,不知能否走下去。
展望
- 如果使用Docker,安裝Ubuntu的鏡像,應該可以快速解決上述環(huán)境搭建問題。