Selenium 2自动化测试实战18(上传文件)

一、上传文件

上传文件是比较常见的web功能之一,但WebDriver没有提供专门用于上传的方法。

一般web页面的上传功能的操作需要单击“上传”按钮后打开本地的Window窗口,从窗口选择本地文件进行上传。而WebDriver是无法操作Windows控件的,所以,对于web页面的上传功能实现一般由以下两种方式。
(1)普通上传:普通的附件上传是将本地文件的路径作为一个值放在input标签中,通过form表单将这个值提交给服务器。
(2)插件上传:一般是指基于Flash、JavaScript或Ajax等技术所实现的上传功能。

1. send_keys实现上传
对于input标签实现的上传功能,可以将其看做是一个输入框,即通过send_keys()指定本地文件路径的方式实现文件上传。

	<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> 

通过浏览器打开,效果如下图所示

#upfile.py
#coding:utf-8
from selenium import webdriver
import os,time

driver=webdriver.Chrome()
file_path='file:///'+os.path.abspath("upfile.html")
driver.get(file_path)

#定位上传按钮,添加本地文件
driver.find_element_by_name("file").send_keys("F:\\request.txt")
time.sleep(2)

driver.quit()

通过这种方法上传,就避免了操作Windows控件的步骤。如果能找到上传的input标签,那么基本上就可以通过send_keys()方法向其输入一个文件地址来实现上传。


2. AutoIt实现上传
AutoIt目前最新版本是v3,被用来进行Windows GUI的自动化测试。它利用模拟键盘按键,鼠标移动和窗口/控件的组合来实现自动化任务。
官方网站:https://www.autoitscript.com/site/
(1)AutoIt Windows Info:用于识别Windows控件信息
(2)Compile Script to.exe:用于将AutoIt生成exe执行文件
(3)Run Script:用于执行AutoIt脚本
(4)SciTE Script Editor:用于编写AutoIt脚本
下面以操作upload.html上传弹出的窗口为例,讲解AutoIt上传过程。
1. (下载的是zip)解压之后,进入到install文件夹下面,打开Au3Info_x64.exe,如下图所示


2. 用鼠标单击Finder Tool,然后拖到需要定位的输入框中,会有一个小黑框标记,如下图所示

3. 然后定位到“打开”按钮,如下图所示



4. 从而,可以通过AutoIt Windows Info获得以下信息。

(1)窗口的title为“打开”,标题的class为“#32770”;
(2)“文件名”选择框的class为“Edit”,instance为“1”,所以classnameNN为“Edit1”。
(3)“打开”按钮的class为“Button”,instance为“1”,所以classnameNN为“Button1”。


5. 因此打开SCITE scipt Editor编辑器,编写AutoIt脚本。

#upfile.au3
;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 fieled
ControlSetText("打开","","Edit1","F:\study\Jenkins.docx")
Sleep(2000)

;Click on the open button
ControlClick("打开","","Button1");

  

ControlFocus()方法用于识别Windows窗口。WinWait()方法设置10秒钟用于等待窗口的显示。ControlSetText()方法用于想“文件名”输入框内输入本地上传文件的路径。这里的Sleep()方法与Python中time模块提供的Sleep()方法用法一样,不过它是以毫秒为单位,Sleep(2000)表示固定休眠2000毫秒。ControlClick()用于单击上传窗口中的“打开”按钮。
AutoIt脚本已经写好了,可以通过菜单栏“Tools”--“Go”(或键盘F5)来运行脚本。


注意:在运行时文件上传窗口应处于打开状态。


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


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


8. 接下来通过自动化测试脚本调用upfile.exe程序,实现上传。

#upfile.py
#coding:utf-8
from selenium import webdriver
import os,time

driver=webdriver.Chrome()

#打开上传功能页面
file_path='file:///'+os.path.abspath("upfile.html")
driver.get(file_path)

#单击打开上传窗口
driver.find_element_by_name("file").click()
#调用upfile.exe上传程序
time.sleep(2)
os.system("F:\\study\webdriverAPI\Demo1\upfile.exe")
time.sleep(5)

driver.quit()


通过system()方法可以调用并执行upfile.exe程序。但是不太推荐这种解决方案,因为通过python调用的exe程序并不在python的可控范围内。换句话说,exe执行多长时间,执行是否出错,python程序都无法得知。

猜你喜欢

转载自www.cnblogs.com/Rita-LJ/p/11608953.html