Selenium 2 test automation combat 18 (upload)

First, upload files

Upload files is one of the more common web features, but does not provide a method WebDriver specifically for upload.

After uploading the general operation of the web page you need to click the "Upload" button to open a local Window window, select local files to upload from the window. The WebDriver Windows controls is inoperable, so, for uploading web pages are generally realized by the following two ways.
(1) Ordinary Upload: Upload a common accessory is the path to a local file as a value on the input tab, this value will be submitted to the server via form form.
(2) plug-in to upload: generally refers to uploading and other features based on Flash, JavaScript or Ajax technologies implemented.

1. send_keys to upload
to the input tag uploading implemented, it can be seen as an input box, a local file path to specify the file upload manner i.e. by 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> 

Opening a browser, results as shown below

 

#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()

 

In this way uploading, avoiding steps Windows controls. If you can find input labels to upload, then basically you can send_keys () method to upload a file to which the input address.


2. AutoIt to upload
AutoIt latest version is v3, be used for Windows GUI test automation. It is implemented using a combination of automated tasks by simulating keystrokes, mouse movements and window / control.
Official Website: HTTPS: //www.autoitscript.com/site/
(1) AutoIt Windows Info: Windows controls for identifying information
(2) Compile Script to.exe: AutoIt used to generate exe executable file
(3) Run Script : AutoIt script for execution
(4) SciTE script Editor: AutoIt used to write scripts
to operate upload.html upload pop-up window, for example, to explain the process of uploading AutoIt below.
1. After (downloaded zip) decompression proceeds to install folder, opened Au3Info_x64.exe, as shown in FIG.

 

 


2. click Finder Tool, and drag input box needs to be positioned, there will be a small black box flag, as shown in FIG.

 

 

3. Then navigate to the "Open" button, as shown in FIG.

 

 



4. Thus, by AutoIt Windows Info obtain the following information.

Title (1) window is "open", the title of the class is "# 32770";
(2) class "File Name" selection box for "Edit", instance "1", so classnameNN as "Edit1".
(3) "open" class button is "Button", instance "1", so as classnameNN "Button1."


5. So open SCITE scipt Editor Editor, write AutoIt script.

#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 () method is used to identify Windows window. WinWait () method set to wait 10 seconds for displaying the window. ControlSetText () method is used to upload files you want to enter the local path "File name" input box. Sleep () method of use herein Sleep () method in Python module provides the same time, but it is in milliseconds, Sleep (2000) Sleep 2000 indicates a fixed milliseconds. ControlClick () is used to upload window, click the "Open" button.
AutoIt script already written, by the menu bar "Tools" - "Go" (or keyboard F5) to run the script.


Note: File Upload at runtime window should open.


6. The script is working correctly, save it as upfile.au3 file, save the script where it can be run through the Run Script tool open, but I hope this script is called python program, then it needs to be generated as exe program. Open Compile Script to.exe tool, which is generated as exe executable file , as shown below.


7. Click the "Browse" button, select upfile.au3 file, click "Convert" button to be generated as upfile.exe program.


8. The next call via automated test scripts upfile.exe program to upload.

upfile.py # 
#coding: UTF-8 
from the Selenium webdriver Import 
Import os, Time 

Driver = webdriver.Chrome () 

# open the upload page 
file_path = 'file: ///'+os.path.abspath ( " upfile.html ") 
driver.get (file_path) 

#-click to open the upload window 
driver.find_element_by_name (" File ") the click (). 
# upload the program calls upfile.exe 
the time.sleep (2) 
F os.system (": Study \\ \ webdriverAPI \ Demo1 \ upfile.exe ") 
the time.sleep (5) 

driver.quit ()


Through the system () can be called and executed method upfile.exe program. However, this solution is not recommended, because the controllable range by calling python exe program is not in the python. In other words, exe execution for how long, whether an error occurred, python programs are not known.

Guess you like

Origin www.cnblogs.com/Rita-LJ/p/11608953.html