• Appium模拟器如何获取tost元素


    1-19 如何获取tost元素

    1、导包

    from appium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions

    2、Desired Capabilities

    #抓取toast才配置
    "automationName": "UiAutomator2"

    3、def login_by_password_toast():故意输入错误密码,再点击登录,触发条件

    4、xpath定位元素

    def get_toast():
        time.sleep(2)
        toast_element = ("xpath","//*[contains(@text,'登录密码错误')]")
        result = WebDriverWait(driver,10,0.1).until(expected_conditions.presence_of_element_located(toast_element))
        print(result)

    本小节完整的测试代码如下

    # -*- coding:utf-8 -*-
    # Author:Marlon Kang
    # 什么是toast参考博客https://blog.csdn.net/fitaotao/article/details/82251750
    import time
    from appium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions
    
    
    def get_driver():
        capabilities = {
            "platformName": "Android",
            "platformVersion": "6.0.1",
            "deviceName": "127.0.0.1:7555",
            "appPackage":'cn.com.open.mooc',
            "appActivity": "com.imooc.component.imoocmain.index.MCMainActivity",
            "noReset": 'true',
            #抓取toast才配置
            "automationName": "UiAutomator2"
        }
        driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", capabilities)
        return driver
    
    def login_by_node1():
        element1 = driver.find_element_by_android_uiautomator('new UiSelector().resourceId("cn.com.open.mooc:id/tab_layout")')
        #通过层级关系和index定位页面元素
        element2 = element1.find_element_by_android_uiautomator('new UiSelector().index(0)')
        element3 = element2.find_element_by_android_uiautomator('new UiSelector().index(4)')
        element3.click()
        #点击登录
        # login_by_password()
        return print("login_by_node1()点击账号")
    
    
    def login_by_password_toast():
        time.sleep(1)
        driver.find_element_by_android_uiautomator('new UiSelector().resourceId("cn.com.open.mooc:id/header_line")').click()
        time.sleep(1)
        driver.find_element_by_android_uiautomator(
            'new UiSelector().resourceId("cn.com.open.mooc:id/tvPassLogin")').click()
        #填入信息
        driver.find_element_by_android_uiautomator('new UiSelector().resourceId("cn.com.open.mooc:id/accountEditChannel2")').send_keys('15652236641')
        time.sleep(1)
        driver.find_element_by_android_uiautomator('new UiSelector().resourceId("cn.com.open.mooc:id/passwordEditChannel2")').send_keys('showthetoast')
        #点击登录
        time.sleep(1)
        driver.find_element_by_android_uiautomator('new UiSelector().resourceId("cn.com.open.mooc:id/login")').click()
    
    
    def get_toast():
        time.sleep(2)
        toast_element = ("xpath","//*[contains(@text,'登录密码错误')]")
        result = WebDriverWait(driver,10,0.1).until(expected_conditions.presence_of_element_located(toast_element))
        print(result)
    
    
    if __name__ == '__main__':
        driver = get_driver()
        time.sleep(3)
        #切换到“账号”底边选项
        login_by_node1()
        #错误密码调用触发toast
        login_by_password_toast()
        get_toast()
  • 相关阅读:
    Jquery EasyUI tabs处理
    C# ToString格式控制符
    SQL删除重复数据,保留一条
    stm32f4xx 的IWDG使用的一般步骤
    stm32f4xx 的EXTI使用的一般步骤
    STM32F4xx---EXTI 外部中断
    数组和指针 到 存储类(1)
    uCosII 从 OSStart开始到思维定势··········
    《C和指针》一书介绍操作符优先级
    OSTimeTick()函数解析
  • 原文地址:https://www.cnblogs.com/MarlonKang/p/14226946.html
Copyright © 2020-2023  润新知