• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Macbook prom1+android虚拟机+pycharm+pytest+appium 实现微信登录

武飞扬头像
Claire欢
帮助1


前言

Macbook prom1 android虚拟机 pycharm pytest appium 实现微信登录


1. 第三方库

  1. appium-python-client
  2. pyyaml
  3. pytest
  4. selenium

学新通

 app.py

  1.  
    from appium import webdriver
  2.  
    from Page.basepage import BasePage
  3.  
    from Page.main import Main
  4.  
     
  5.  
    class App(BasePage):
  6.  
     
  7.  
    # 启动app
  8.  
    def start(self):
  9.  
    _package = "com.tencent.mm"
  10.  
    _activity = "com.tencent.mm.ui.LauncherUI"
  11.  
    if self._driver is None:
  12.  
    desir_cap = {
  13.  
    "appPackage": "com.tencent.mm",
  14.  
    "appActivity": "com.tencent.mm.ui.LauncherUI",
  15.  
    "platformName": "Android",
  16.  
    "platformVersion": "12",
  17.  
    "dontStopAppOnReset": "true",
  18.  
    "noReset": "true",
  19.  
    "deviceName": "emulator-5554"
  20.  
    }
  21.  
    self._driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desir_cap)
  22.  
    self._driver.implicitly_wait(5) # 隐式等待5s
  23.  
    else:
  24.  
    self._driver.start_activity(_package, _activity)
  25.  
    return self
  26.  
     
  27.  
    def main(self):
  28.  
    return Main(self._driver)
学新通
basepage.py
  1.  
    from appium.webdriver.webdriver import WebDriver
  2.  
    from appium.webdriver.common.touch_action import TouchAction
  3.  
    import yaml
  4.  
    import pytest
  5.  
    from time import sleep
  6.  
     
  7.  
    '''创建一个基础页面类,用于封装公共模块的处理方法
  8.  
    和根据yaml配置文件进行案例的测试步骤'''
  9.  
     
  10.  
     
  11.  
    class BasePage:
  12.  
    _blackList = [] # 黑名单列表,用于处理再case运行过程中可能出现的未知弹窗
  13.  
    _errorCount = 0 # 定位元素的错误次数,元素定位中可能出现一次定位不到
  14.  
    _errorMax = 5 # 允许进行元素定位的最大错误次数
  15.  
     
  16.  
    def __init__(self, driver: WebDriver = None): # 初始化driver
  17.  
    self._driver = driver
  18.  
     
  19.  
    def find(self, by, locator): # $by>定位元素的方法,$locator>定位元素对应的所需value
  20.  
    try:
  21.  
    if isinstance(by, tuple): # 如果传入的定位是个元组形式,包括方法和locator,就进行解包的方式定位
  22.  
    element = self._driver.find_element(*by)
  23.  
    else:
  24.  
    element = self._driver.find_element(by, locator)
  25.  
    self._errorCount = 0 # 找到元素,错误次数为0
  26.  
    return element
  27.  
    except Exception as e:
  28.  
    self._errorCount = 1 # 未找到该元素,错误次数加1
  29.  
    if self._errorCount >= self._errorMax:
  30.  
    raise e # 错误次数大于设置的最大次数抛出异常
  31.  
    for black in self._blackList:
  32.  
    elements = self._driver.find_elements(*black) # 找到黑名单列表里的所有元素,[(by, locator),]的形式设置
  33.  
    if len(elements) > 0: # 出现弹框匹配黑名单列表大于0,即出现未知弹框
  34.  
    elements[0].click() # 点击过后就找不到该元素了,所以永远点击第一个找到的就可以了
  35.  
    return self.find(by, locator) # 点击后返回原方法,轮询去让定位可以继续执行
  36.  
    raise e # 没有找到,抛出异常
  37.  
     
  38.  
    def send(self, by, locator, value): # 数据输入的方法
  39.  
    try:
  40.  
    self._driver.find(by, locator).send_keys(value) # 定位到输入的位置,输入值
  41.  
    self._errorCount = 0
  42.  
    except Exception as e:
  43.  
    self._errorCount = 1
  44.  
    if self._errorCount >= self._errorMax:
  45.  
    raise e # 大于错误次数,抛出异常
  46.  
    for black in self._blackList:
  47.  
    elements = self._driver.find_element(*black)
  48.  
    if len(elements) > 0:
  49.  
    elements[0].click()
  50.  
    return self.find(by, locator)
  51.  
    raise e
  52.  
     
  53.  
    def steps(self, path): # 定义操作步骤的方法,用于通过编写配置文件,执行相关用例
  54.  
    with open(path, 'r', encoding='utf-8') as f:
  55.  
    steps: list[dict] = yaml.safe_load(f) # 操作步骤的数据类型为:[{},{}]
  56.  
    # 遍历操作步骤
  57.  
    for step in steps:
  58.  
    if 'by' in step.keys():
  59.  
    element = self.find(step['by'], step['locator']) # 定位到元素
  60.  
    # 要进行的动作操作
  61.  
    if 'action' in step.keys():
  62.  
    if 'click' == step['action']: # 点击操作
  63.  
    element.click()
  64.  
    if 'send' == step['action']:
  65.  
    element.send_keys(step['value'])
  66.  
    if 'TouchAction' in step['action']: # 滑动操作
  67.  
    action = TouchAction(self._driver)
  68.  
    action.press(x=step['value'][0]['x_start'], y=step['value'][0]['y_start']).wait(300) \
  69.  
    .move_to(x=step['value'][1]['x_end'], y=step['value'][1]['y_end']).release().perform()
  70.  
    # 断言
  71.  
    if 'assertion' in step.keys():
  72.  
    if "sleep" in step['assertion'].keys():
  73.  
    sleep(step['assertion']['sleep'])
  74.  
    element = self.find(step['assertion']['by'], step['assertion']['locator'])
  75.  
    attribute = element.get_attribute(step['assertion']['attribute'])
  76.  
    pytest.assume(attribute == step['assertion']['assert_info'])
  77.  
    if 'back' in step.keys():
  78.  
    self._driver.back()
  79.  
     
学新通

login.py
  1.  
    from Page.basepage import BasePage
  2.  
     
  3.  
     
  4.  
    # 登录page
  5.  
    class Login(BasePage):
  6.  
     
  7.  
    def daily_recommendation(self):
  8.  
    self.steps('../TestData/login.yml') #进入登录页
  9.  
     
  10.  
     

main.py

  1.  
    from Page.basepage import BasePage
  2.  
    from Page.login import Login
  3.  
     
  4.  
     
  5.  
     
  6.  
    class Main(BasePage):
  7.  
     
  8.  
    def quit(self):
  9.  
    self._driver.quit()
  10.  
     
  11.  
    def go_login(self): # 进入微信首页
  12.  
    self.steps("../TestData/main.yml")
  13.  
    return Login(self._driver) #这里应该是类的名子
  14.  
     
  15.  
    def go_password(self):
  16.  
    self.steps("../TestData/main.yml")
  17.  
    return Passoword(self._driver)
  18.  
     
  19.  
     
  20.  
     
  21.  
     
  22.  
     
  23.  
     
学新通

test_login.py

  1.  
    from Page.app import App
  2.  
    import pytest
  3.  
     
  4.  
    class TestLogin:
  5.  
     
  6.  
    def setup_class(self):
  7.  
    self.testDriver = App().start().main()
  8.  
     
  9.  
    def teardown_class(self):
  10.  
    self.testDriver.quit()
  11.  
     
  12.  
    def test_daily_recommendation(self):
  13.  
    self.testDriver.go_login().daily_recommendation()
  14.  
     
  15.  
     
  16.  
     
学新通

login.yml

  1.  
    -
  2.  
    by: id
  3.  
    locator: com.tencent.mm:id/cd6
  4.  
    action: send
  5.  
    value: 134522622
  6.  
    -
  7.  
    by: id
  8.  
    locator: com.tencent.mm:id/hfl
  9.  
    action: click

main.yml

  1.  
    -
  2.  
    by: id
  3.  
    locator: com.tencent.mm:id/j_i
  4.  
    action: click

参考:


总结

有时候不是代码的锅,是环境的问题。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcecik
系列文章
更多 icon
同类精品
更多 icon
继续加载