zoukankan      html  css  js  c++  java
  • 【Web自动化测试——代码篇八】常用方法——上传/下载文件

    上传文件

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

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

    下面我们实际操作一下来讲解AutoIt上传文件的过程:
    1、安装AutoIt(下载网址:https://www.autoitscript.com/site/autoit/downloads/)
    2、打开AutoIt Windows Info工具(我的电脑是64位的)
    图片.png
    3、用鼠标左键点击弹出窗口的Finder Tool,此时鼠标将变成一个小风扇形状的图标
    图片.png
    4、按住Finder Tool不松,将其拖动到需要识别的控件上
    窗口的title为“文件上传”,标题的class为“#32770”
    图片.png
    文件名输入框的class为“Edit”,Instance为“1”,所以classnameNN为“ComboBox1”
    图片.png
    打开按钮的class为“Button”,Instance为“1”,所以classnameNN为“Button1”
    图片.png
    5、根据AutoIt Windows Info所识别到的控件信息打开SciTE Script Editor编辑器,编写AutoIt脚本。

    ;ControlFocus("title","text",controlID) ComboBox1=Combobox 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", "alert.html")
    Sleep(2000)
    
    ;Click on the open button
    ControlClick("文件上传", "", "Button1")
    

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

    注意点

    • 不同浏览器的窗口title有可能不一样
      图片.png
      图片.png
      图片.png
    • 文件名输入框不要定位到下拉框上去
      图片.png
      图片.png
    • 注意上传路径的分隔符是单斜杠还是双斜杠
      图片.png
    • 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, IOException
    12     {
    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         finally
    29         {
    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 = 10
    10   driver.navigate.to "http://www.jq22.com/yanshi5310"
    11 
    12   begin
    13     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 => e
    17     puts e.message # 显示报错信息
    18   ensure
    19     driver.close
    20   end
    21 end

    下载文件

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

    **代码时间 **

    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.new
    profile['browser.download.folderList'] = 2
    profile['browser.download.manager.showWhenStarting'] = false
    
    driver = Selenium::WebDriver.for :firefox, :profile => profile
  • 相关阅读:
    771. Jewels and Stones
    706. Design HashMap
    811. Subdomain Visit Count
    733. Flood Fill
    117. Populating Next Right Pointers in Each Node II
    250. Count Univalue Subtrees
    94. Binary Tree Inorder Traversal
    116. Populating Next Right Pointers in Each Node
    285. Inorder Successor in BST
    292. Nim Game Java Solutin
  • 原文地址:https://www.cnblogs.com/CSgarcia/p/9413951.html
Copyright © 2011-2022 走看看