多测师是一家拥有先进的教学理念,强大的师资团队,业内好评甚多的接口自动化测试培训机构!

17727591462

联系电话

您现在所在位置:接口自动化测试培训 > 新闻资讯

Selenium借助AutoIt识别上传(下载)详解-自动化测试

更新时间:2022-05-06 09:06:28 作者:多测师 浏览:226

  AutoIt是一个使用类似BASIC脚本语言的免费软件,它设计用于Windows GUI(图形用户界面)中进行自动化操作。它利用模拟键盘按键,鼠标移动和窗口/控件的组合来实现自动化任务。

  从网站上下载AutoIt并安装,安装完成在菜单中会看到目录:

  AutoIt Windows Info   用于帮助我们识Windows控件信息。

  Compile Script to.exe 用于将AutoIt生成 exe 执行文件。

  Run Script            用于执行AutoIt脚本。

  SciTE Script Editor   用于编写AutoIt脚本。

Selenium借助AutoIt识别上传(下载)详解-自动化测试

<html>

<head>

<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<title>upload_file</title>

<link href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" />

</head>

<body>

<div class="row-fluid">

<div class="span6 well">

<h3>upload_file</h3>

<input type="file" name="file" />

</div>

</div>

</body>

<script src="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"></script>

</html>

  将上面的html代码保存为uplad.html文件,通过浏览器打开,效果如下:

  下面以操作upload.html上传弹出的窗口为例讲解AutoIt实现上传过程。

  1、首先打开AutoIt Windows Info 工具,鼠标点击Finder Tool,鼠标将变成一个小风扇形状的图标,按住鼠标左键拖动到需要识别的控件上。

通过AutoIt Windows Info 获得以下信息:

  窗口的title为“选择要加载的文件”,标题的Class为“#32770”。

  文件名输入框的class 为“Edit”,Instance为“1” ,所以ClassnameNN为“Edit1”。

  打开按钮的class 为“Button”,Instance为“1” ,所以ClassnameNN为“Button1”。

2、根据AutoIt Windows Info 所识别到的控件信息打开SciTE Script Editor编辑器,编写脚本。

  ;ControlFocus("title","text",controlID) Edit1=Edit instance 1

  ControlFocus("选择要加载的文件", "","Edit1")

  ; Wait 10 seconds for the Upload window to appear

  WinWait("[CLASS:#32770]","",10)

  ; Set the File name text on the Edit field

  ControlSetText("选择要加载的文件", "", "Edit1", "D:\\upload_file.txt")

  Sleep(2000)

  ; Click on the Open button

  ControlClick("选择要加载的文件", "","Button1");

  ControlFocus()方法用于识别Window窗口。WinWait()设置10秒钟用于等待窗口的显示,其用法与WebDriver 所提供的implicitly_wait()类似。ControlSetText()用于向“文件名”输入框内输入本地文件的路径。这里的Sleep()方法与Python中time模块提供的Sleep()方法用法一样,不过它是以毫秒为单位,Sleep(2000)表示固定休眠2000毫秒。ControlClick()用于点击上传窗口中的“打开”按钮。

  AutoIt的脚本已经写好了,可以通过菜单栏“Tools”-->“Go” (或按键盘F5)来运行一个脚本吧!注意在运行时上传窗口当前处于打开状态。

3、脚本运行正常,将其保存为upfile.au3,这里保存的脚本可以通过Run Script 工具将其打开运行,但我们的目的是希望这个脚本被Python程序调用,那么就需要将其生成exe程序。打开Compile Script to.exe工具,将其生成为exe可执行文件。

  点击“Browse”选择upfile.au3文件,点击“Convert”按钮将其生成为upfile.exe程序。

  4、下面就是通过自动化测试脚本调用upfile.exe程序实现上传了。

#coding=utf-8

from selenium import webdriver

import os

driver = webdriver.Firefox()

#打开上传功能页面

file_path =  'file:///' + os.path.abspath('upfile.html')

driver.get(file_path)

#点击打开上传窗口

driver.find_element_by_name("file").click()

#调用upfile.exe上传程序

os.system("D:\\upfile.exe")

driver.quit()

  通过Python 的os模块的system()方法可以调用exe程序并执行。了解了上传的实现过程,那么下载也是一样的。

  以上内容为大家介绍了自动化测试中的Selenium借助AutoIt识别上传(下载)详解,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/

联系电话

17727591462

返回顶部