WebDriver高阶API(3)

WebDriver高阶API(3)


2)模拟键盘操作,实现上传文件

#encoding=utf-8
import unittest,time
from selenium import webdriver
import traceback
import win32clipboard as w
import win32con
import win32api
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException,NoSuchElementException

#设置剪切板内容
def setText(aString):
    w.OpenClipboard()
    w.EmptyClipboard()
    w.SetClipboardData(win32con.CF_UNICODETEXT,aString)
    w.CloseClipboard()

VK_CODE = {
    "enter":0x0d,
    "ctrl":0x11,
    "a":0x41,
    "v":0x56,
    "x":0x58
}

#键盘皱键按下
def keyDown(keyName):
    win32api.keybd_event(VK_CODE[keyName],0,0,0)
#键盘键抬起
def keyUp(keyName):
    win32api.keybd_event(VK_CODE[keyName],0,win32con.KEYEVENTF_KEYUP,0)

class TestDemo(unittest.TestCase):

    def setUp(self):
        #启动浏览器
        self.driver = webdriver.Chrome(executable_path = "D:\\chromedriver")

    def test_uploadFileByKeybd(self):
        url = "http://127.0.0.1:8080/test_upload_file.html"
        #访问自定义网页
        self.driver.get(url)
        try:
            #创建一个显式等待对象
            wait = WebDriverWait(self.driver,10,0.2)
            #显式等待判断被测页面上的上传文件按钮是否处于可被单击状态
            wait.until(EC.element_to_be_clickable((By.ID,"file")))
        except TimeoutException,e:
            #捕获TimeoutException异常
            print traceback.print_exc()
        except NoSuchElementException,e:
            #捕获NoSuchElementException异常
            print traceback.print_exc()
        except Exception,e:
            #捕获其他异常
            print traceback.print_exc()
        else:
            #将即将要上传的文件名及路径设置到剪切板中
            setText(u"D:\\test\\test.txt")
            #查找页面上ID属性值为“file”的文件上传框,并单击调出选择文件上传框
            self.driver.find_element_by_id("file").click()
            time.sleep(2)
            #模拟键盘按下Ctrl+V组合键
            print VK_CODE["ctrl"]
            keyDown("ctrl")
            keyDown("v")
            # 释放Ctrl+V组合键
            keyUp("v")
            keyUp("ctrl")
            time.sleep(1)
            #模拟键盘按下回车键
            keyDown("enter")
            #模拟键盘释放回车键
            keyDown("enter")
            #暂停查看上传的文件
            time.sleep(3)
            #找到页面上ID属性值为filesubmit的文件提交按钮对象
            fileSubmitButton = self.driver.find_element_by_id("filesubmit")
            #单击提交按钮,完成文件上传操作
            fileSubmitButton.click()
            #因为文件上传需要时间,所以这里可以添加 显式等待场景
            #判断文件上传成功后,页面是否跳转到文件瞧去成功页面
            #通过EC.title_is()方法判断跳转后的页面title值是否符合
            #期望,如果匹配将继续执行后续代码
            # wait.until(EC.title_is(u"文件上传成功"))

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main()


3)使用第三方工具AutoIt上传文件
需要安装AutoIt工具 autoit-v3-setup.exe和 SciTE4AutoIt3.exe
在编辑器中输入文件上传的脚本,如下:
#include<Constants.au3>

Send("D:\\test\\test.txt")
Send("{ENTER}")
Send("{ENTER}")

并将.au3文件转换为.exe可执行文件
注:本例只针对Chrome浏览器

#encoding=utf-8
import unittest,time,traceback,os
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException,NoSuchElementException

class TestDemo(unittest.TestCase):

    def setUp(self):
        #启动浏览器
        self.driver = webdriver.Chrome(executable_path = "D:\\chromedriver")

    def test_uploadFileByAutoIt(self):
        url = "http://127.0.0.1:8080/test_upload_file.html"
        #访问自定义网页
        self.driver.get(url)
        try:
            #创建一个显式等待对象
            wait = WebDriverWait(self.driver,10,0.2)
            #显式等待判断被测试页面上的上传文件按钮是否处于可被单击状态
            wait.until(EC.element_to_be_clickable((By.ID,"file")))
        except TimeoutException,e:
            #捕获TimeoutException异常
            print traceback.print_exc()
        except NoSuchElementException,e:
            #捕获NoSuchElementException异常
            print traceback.print_exc()
        except Exception,e:
            #捕获其他异常
            print traceback.print_exc()
        else:
            #查找页面上id属性值为"file"的文件上传框,
            #并单击调出选择文件上传框
            self.driver.find_element_by_id("file").click()
            #通过Python提供的os模块的system方法执行生成的test.exe文件
            os.system("D:\\test\\test.exe")
            #由于AutoIt脚本转换后的可执行文件test.exe可能执行速度比较慢,
            #这里等待5秒,以确保test.exe脚本执行成功
            time.sleep(5)
            #找到页面上ID属性值为“filesubmit”的文件提交按钮
            fileSubmitButton = self.driver.find_element_by_id("filesubmit")
            #单击提交按钮,完成文件上传操作
            fileSubmitButton.click()
            # 因为文件上传需要时间,所以这里可以添加 显式等待场景
            # 判断文件上传成功后,页面是否跳转到文件瞧去成功页面
            # 通过EC.title_is()方法判断跳转后的页面title值是否符合
            # 期望,如果匹配将继续执行后续代码
            # wait.until(EC.title_is(u"文件上传成功"))

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main()

猜你喜欢

转载自www.cnblogs.com/test-chen/p/10602110.html