如何自动化测试图形化程序

在自动化测试中,经常会遇到一些程序需要运行在X windows环境下。
通常我会在shell脚本中用以下方法来运行脚本:



setup_vnc()
{
    # start vnc number from 10 to avoid confilct with the exists.
    VNC_DISPLAY=10
    while ! vncserver :$VNC_DISPLAY
    do
       ((VNC_DISPLAY++))
       if [ $VNC_DISPLAY -gt 100 ]; then
           echo "Create vnc sessions failed."
           exit -1
       fi
    done
    export VNC_DISPLAY
    export DISPLAY=:$VNC_DISPLAY
}

teardown_vnc()
{
    vncserver -kill :$VNC_DISPLAY  || echo "for tearing down VNC"
}

# Mask this sig handler.
trap 'teardown_vnc' INT TERM EXIT
setup_vnc

# run the procedure under the X environment.

如果你使用python的话,下面推荐一个更方便的module xvfbwrapper

from xvfbwrapper import Xvfb

with Xvfb() as xvfb:
    # launch stuff inside virtual display here.
    # Xvfb will stop when this block completes

以及对应的robotframwork keyword robotframework-xvfb

*** Settings ***
Documentation     This example demonstrates how to use current library
Library      Selenium2Library
Library      XvfbRobot

*** Test Cases ***
Create Headless Browser
    Start Virtual Display    1920    1080
    Open Browser   http://google.com
    Set Window Size    1920    1080
    ${title}=    Get Title
    Should Be Equal    Google    {title}
    [Teardown]    Close Browser

在Xvfb中还可以用xwd命令来截屏,用xwud命令来查看截屏图像。

你可能感兴趣的:(Python,SHELL)