zoukankan      html  css  js  c++  java
  • 基于airtest-selenium的UI自动化测试

    一. airtest-selenium环境搭建

    1.1 安装与介绍

    # airtest-selenium库是基于selenium库的进一步封装: https://airtest.doc.io.netease.com/tutorial/13_Selenium/
    pip install airtest-selenium
    pip install pynput

    airtest-selenium库的几个特点: 1) 对切换标签的界面进行了友好封装, 2)支持图像识别功能, 3)自动进行log记录(参考selenium-java的监听模式), 4)兼容selenium的原生api

    1.2 下载浏览器和浏览器驱动

    本文使用Chrome浏览器,将下载与之对应的浏览器驱动chromedirver.exe

    1) 查看chrome版本

    chrome://version/

    2) 关闭Chrome浏览器的自动更新功能

    # 如果浏览器自动更新后,浏览器的版本会发生变化,这样可能会导致已下载的chromedirver.exe失效
    右击我的电脑——管理——服务和应用程序——服务——停止:google更新服务(gupdate)、google更新服务(gupdatem);启动类型:手动

    3) chromedriver与chrome版本的映射关系

    ChromeDriver版本    Chrome版本
        v2.44           v69-71
        v2.42           v68-70
        v2.41           v67-69
        v2.40           v66-68
        v2.38           v65-67
        v2.37           v64-66
        v2.35           v62-64
        v2.34           v61-63
        v2.33           v60-62
        v2.32           v59-61
        v2.31           v58-60

    4) 下载Chrome驱动,并将其与Chrome.exe存放到同一目录下

    Chrome.exe的默认存放路径为:C:Program Files (x86)GoogleChromeApplication
    chromedriver下载地址: http://npm.taobao.org/mirrors/chromedriver
    不同版本的chrome下载: https://www.slimjet.com/chrome/google-chrome-old-version.php

    3. 验证环境

    # -*- coding:utf-8 -*-
    # Author:chinablue
    
    import time
    
    from airtest_selenium.proxy import WebChrome
    
    driver = WebChrome(executable_path=r"C:Program Files (x86)GoogleChromeApplicationchromedriver.exe")
    driver.maximize_window()
    driver.get("http://www.baidu.com/")
    time.sleep(5)
    driver.quit()

    4. 安装问题记录

    4.1 如果出现如下报错,可能需要降低numpy的版本

    ImportError: numpy.core.multiarray failed to import
    # 通过查看发现当前numpy版本为1.19.4,将其降为1.15.4
    pip install numpy==1.15.4

    4.2 如果出现如下报错,尝试以管理员权限重新启动Pycharm软件

    selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to create Chrome process.

     二. 常用使用场景说明

    2.1 Chrome浏览器常用参数配置

    # -*- coding: utf-8 -*-
    # @Time    : 2020/11/18 20:52
    # @Author  : chinablue
    
    import time
    
    from airtest_selenium.proxy import WebChrome
    from selenium import webdriver
    
    options = webdriver.ChromeOptions()
    options.add_argument('--disable-gpu')
    
    # 指定浏览器的分辨率
    options.add_argument('--window-size=1920,1080')
    
    # 关闭提示: Chrome正受到自动测试软件的控制
    # options.add_argument('--disable-infobars')  # 老版本Chrome浏览器的写法
    options.add_experimental_option("excludeSwitches", ['enable-automation'])
    
    # 无界面运行
    # options.add_argument('--headless')
    
    # 配置代理
    # options.add_argument("--proxy-server=http://127.0.0.1:9631")
    
    # 其他设置: 不加载图片, 设置语言, 设置User-Agent等
    
    driver = WebChrome(executable_path=r"C:Program Files (x86)GoogleChromeApplicationchromedriver.exe",
                       chrome_options=options)
    
    driver.get("http://www.baidu.com/")
    
    time.sleep(5)
    driver.quit()

    注意事项:

      1)  若无界面运行时,抛出MoveTargetOutOfBoundsException异常, 则给浏览器设置--window-size参数后再试

    2.2 Alert框的处理

    driver.switch_to.alert.accept()

    注意事项:

      1)  若当前页面存在alert框,此时关闭浏览器时会抛出异常

    2.3 登录操作中简单滑动滑块操作

    from selenium.webdriver import ActionChains
    
    action_chains = ActionChains(driver)
    d1 = driver.find_element_by_class_name("el-icon-d-arrow-right")  # 定位滑块
    action_chains.click_and_hold(d1).perform() # 鼠标左键按住滑块不动
    action_chains.reset_actions()  # 清楚之前的action
    action_chains.move_by_offset(300, 0).perform() # 平行移动滑块, 其中300表示x轴, 0表示y轴

    2.4 自动处理Chrome浏览器的通知弹框

    # 创建一个名字为run.reg的文件, 内容如下. 双击执行此文件来修改注册表信息
    Windows Registry Editor Version 5.00
    
    [HKEY_LOCAL_MACHINESOFTWAREPoliciesGoogle]
    
    [HKEY_LOCAL_MACHINESOFTWAREPoliciesGoogleChrome]
    "DefaultPluginsSetting"=dword:00000001
    
    [HKEY_LOCAL_MACHINESOFTWAREPoliciesGoogleChromePluginsAllowedForUrls]
    "1"="http://ztc.njtopsun.com/topsun/#/"

    注意事项:

      1) 文件中蓝色部分内容需要替换成你自己要测试的网站

      2) Chrome在不同版本下设置自动处理通知弹框的方法不一样,本示例中使用的Chrome版本为Chrome 86

      3) Chrome中DefaultPluginsSetting参数说明, 请点击这里

  • 相关阅读:
    洛谷P1527 矩阵乘法——二维树状数组+整体二分
    bzoj1503 [NOI2004]郁闷的出纳员——splay
    bzoj4811 [Ynoi2017]由乃的OJ 树链剖分+位运算
    D Dandan's lunch
    C Sleepy Kaguya
    B bearBaby loves sleeping
    A AFei Loves Magic
    II play with GG
    angular的路由和监听路由的变化和用户超时的监听
    ng-disabled 指令
  • 原文地址:https://www.cnblogs.com/reconova-56/p/13967170.html
Copyright © 2011-2022 走看看