team470 发表于 2018-8-9 06:44:44

python之自动化测试

  功能:


[*]在ubuntu上面,通过shell脚本打开 VirtualBox(windows xp),并调用XP里面的python脚本,来打开一些windows上面的软件,并做模拟操作,完成之后关闭VirtualBox(windows xp)。
  环境:


[*]ubuntu 10.0.4
[*]Oracle VM VirtualBox(windows xp)
[*]windows xp 上安装Python 2.6.5+pywinauto+freesshd+GnuWin32
  pywinauto 下载地址:http://sourceforge.net/projects/pywinauto/
  GnuWin32-coreutils 下载地址:http://gnuwin32.sourceforge.net/packages/coreutils.htm
  准备条件:


[*]windows xp 上安装freesshd 确保ubuntu能够ssh到windows。
  以下提供两种方法设置来实现ubuntu ssh到windows。


[*]如果VirtualBox虚拟机使用nat模式则可以使用端口转发功能
  


[*]VBoxManage modifyvm "VM name" --natpf1 "guestssh,tcp,,2222,,22"
  

  2. 使用桥接网络。


[*]使用key认证登录模式,不需要使用密码。
  通过key认证ssh windows实现后 则准备工作完成。
  贴脚本。
  


[*]#--调用入口脚本
[*]function test(){
[*]    #use windows
[*]    echo 'Start VM ...'
[*]    #VBoxManage startvm "XP"
[*]    sleep 60
[*]    VBoxManage list runningvms | grep XP && echo "VM is running" ||exit 1
[*]
[*]    echo 'use windows tools ...'
[*]    ssh -p 2201 Administrator@localhost "c:\\\\runTools.bat" "${name}" "${pass}" #经测试直接调用python脚本不太稳定,故先调用bat脚本,在有bat调用python脚本。
[*]    echo "wait....use tool for windows VM"
[*]    sleep 600
[*]
[*]    #echo 'Shut Down VM...'
[*]    VBoxManage controlvm "shreck" acpipowerbutton
[*]    sleep 60
[*]}
  

  windows 上的脚本可以提前拷贝过去,或者使用脚本自动拷贝。
  


[*]#--runTools.bat
[*]python c:\TestAuto.py %1 %2
  

  主要python实现脚本:
  


[*]#!/usr/bin/env python
[*]# -*- coding: utf-8 -*-
[*]
[*]from pywinauto import application
[*]import time
[*]import os
[*]import sys
[*]import glob
[*]
[*]
[*]def main():
[*]    if len(sys.argv) != 3:
[*]      print '%s name pass' % sys.argv
[*]      sys.exit(1)
[*]    name = sys.argv
[*]    pass = sys.argv
[*]    os.chdir('c:\\')
[*]    os.system('rm -rvf test')
[*]    os.system('md test')
[*]    os.chdir('test')
[*]    os.system('wget -r -nd -np --no-proxy -l1 http://xxxxx')
[*]
[*]    os.system('ls -l')
[*]    print 'Start Tools...'
[*]    app = application.Application()
[*]    app.start_("C:\Program Files\TestTools\Test.exe") #打开软件
[*]    app.Test.Edit1.SetText('xxxx') #用户名
[*]    app.Test.Edit2.SetText('xxxx') #密码
[*]    app.Test.Button1.Click() #点击登录
[*]    time.sleep(3)
[*]    app.connect_(title_re='Test.*')
[*]    dlg = app.window_(title_re='Test.*')
[*]    if dlg.CheckBox1.GetCheckState() == 1: #验证是否打开成功
[*]      dlg.CheckBox1.Click()
[*]    dlg.ComboBox2.Select(name) #打开后进行选择下拉框
[*]    time.sleep(3)
[*]    dlg['Choose'].Click() #Choose按钮
[*]    time.sleep(3)
[*]    opendlg = app.top_window_()
[*]    opendlg.Edit.SetText('c:\\test\file.xxxx') #输入地址路径
[*]    opendlg.Button1.Click()
[*]    dlg['Start'].Wait('enabled', timeout=600)
[*]    dlg['Start'].Click()
[*]    time.sleep(450)
[*]    os.system('Shutdown -s -t 2') #关机
[*]
[*]
[*]if __name__ == '__main__':
[*]    main()
  

  至此整个windows操作过程结束,可进行后面的shell脚本。
页: [1]
查看完整版本: python之自动化测试