分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

【Web自动化测试——代码篇八】常用方法——上传/下载文件

发布时间:2023-09-06 02:12责任编辑:熊小新关键词:Web

上传文件

对于Web页面的上传功能实现一般有一下俩种方式:

  • 普通上传:将本地文件的路径作为一个值放在input标签中,通过form表单将这个值提交给服务器(不做介绍send_keys方法)。
  • AutoIt上传:利用模拟键盘按键,鼠标移动和窗口/控件的组合来实现自动化任务。

下面我们实际操作一下来讲解AutoIt上传文件的过程:
1、安装AutoIt(下载网址:https://www.autoitscript.com/site/autoit/downloads/)
2、打开AutoIt Windows Info工具(我的电脑是64位的)

3、用鼠标左键点击弹出窗口的Finder Tool,此时鼠标将变成一个小风扇形状的图标

4、按住Finder Tool不松,将其拖动到需要识别的控件上
窗口的title为“文件上传”,标题的class为“#32770”

文件名输入框的class为“Edit”,Instance为“1”,所以classnameNN为“ComboBox1”

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

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

;ControlFocus("title","text",controlID) ComboBox1=Combobox instance 1ControlFocus("文件上传", "" , "Edit1");Wait 10 Seconds for the Upload window to appearWinWait("[CLASS:#32770]", "", 10);Set the file name text on the Edit fieldControlSetText("文件上传", "", "Edit1", "alert.html")Sleep(2000);Click on the open buttonControlClick("文件上传", "", "Button1")

6、保存文件后,打开Compile Script to.exe工具,将其生成为.exe可执行文件

注意点

  • 不同浏览器的窗口title有可能不一样


  • 文件名输入框不要定位到下拉框上去

  • 注意上传路径的分隔符是单斜杠还是双斜杠
  • exe执行多长时间,执行是否出错,自动化脚本都无法得知(不在可控范围内)

**代码时间 **

Java

 1 package JavaTest; 2 ?3 import java.io.IOException; 4 import java.util.NoSuchElementException; 5 import java.util.concurrent.TimeUnit; 6 import org.openqa.selenium.By; 7 import org.openqa.selenium.WebDriver; 8 import org.openqa.selenium.firefox.FirefoxDriver; 9 10 public class Test {11 ????public static void main(String[] arg) throws InterruptedException, IOException12 ????{13 ????????WebDriver driver = new FirefoxDriver();14 15 ?????????// 设置隐示等待时长:10秒;16 ????????driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);17 ????????driver.get("http://www.jq22.com/yanshi5310"); ???18 ????????19 ????????try {20 ????????????driver.switchTo().frame("iframe"); 21 ????????????driver.findElement(By.xpath("//*[@id=‘upload_form‘]/div[1]/div[2]")).click(); //打开上传窗口22 ????????????Runtime.getRuntime().exec("C:\\Users\\xxx\\Desktop\\upfile.exe"); ?// 调用upfile.exe上传程序23 ????????}24 ????????catch(NoSuchElementException e)25 ????????{26 ????????????System.out.println(e.getMessage());27 ????????}28 ????????finally29 ????????{30 ????????????driver.close();31 ????????} ???32 ????}33 }

Python

 1 from selenium import webdriver 2 from selenium.webdriver.common.by import By 3 import os 4 ?5 # 启动Firefox浏览器 6 driver = webdriver.Firefox() 7 ?8 # 隐式等待10S,打开网址(可直接通过frame的id和name定位) 9 driver.implicitly_wait(10)10 driver.get("http://www.jq22.com/yanshi5310")11 12 try:13 ????driver.switch_to.frame("iframe")14 ????driver.find_element(By.XPATH, "//*[@id=‘upload_form‘]/div[1]/div[2]").click() ?# 打开上传窗口15 ????os.system("C:\\Users\\xxx\\Desktop\\upfile.exe") # 调用upfile.exe上传程序16 except Exception as e:17 ????print(e.args[0])18 finally:19 ????driver.close()

Ruby

 1 class Baidu 2 ??require ‘rubygems‘ 3 ??require ‘selenium-webdriver‘ 4 ?5 ??# 打开firefox并输入网址 6 ??driver = Selenium::WebDriver.for :firefox 7 ?8 ??# 设置隐式等待时间10S 9 ??driver.manage.timeouts.implicit_wait = 1010 ??driver.navigate.to "http://www.jq22.com/yanshi5310"11 12 ??begin13 ????driver.switch_to.frame(‘iframe‘)14 ????driver.find_element(:xpath => "//*[@id=‘upload_form‘]/div[1]/div[2]").click ??# 打开上传窗口15 ????exec("C:\\Users\\xxx\\Desktop\\upfile.exe")16 ??rescue => e17 ????puts e.message # 显示报错信息18 ??ensure19 ????driver.close20 ??end21 end

下载文件

话不多说,<( ̄︶ ̄)↗[GO!]
FireFox about:config详细介绍:https://www.cnblogs.com/abcd19880817/p/7210711.html

**代码时间 **

Java

FirefoxProfile fp = new FirefoxProfile();// 为0是下载到浏览器默认下载路径,1是“我的下载”;2是自定义fp.setPreference("browser.download.folderList", 2);// 是否显示开始fp.setPreference("browser.download.manager.showWhenStarting", false);// 指定所下载文件的目录fp.setPreference("browser.download.dir", "d:\\");// 下载文件类型fp.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");

Python

fp = webdriver.FirefoxProfile()fp.set_preference("browser.download.folderList", 2)fp.set_preference("browser.download.manager.showWhenStarting", False)# 指定所下载文件的目录。os.getcwd()函数不需要传递参数,用于返回当前的目录fp.set_preference("browser.download.dir",os.getcwd())fp.set_preference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream")

Ruby

profile = Selenium::WebDriver::Firefox::Profile.newprofile[‘browser.download.folderList‘] = 2profile[‘browser.download.manager.showWhenStarting‘] = falsedriver = Selenium::WebDriver.for :firefox, :profile => profile

【Web自动化测试——代码篇八】常用方法——上传/下载文件

原文地址:https://www.cnblogs.com/CSgarcia/p/9413951.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved