• 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()
  • 相关阅读:
    cocos2d-x笔记5: 通过jni实现C++调用Java
    cocos2d-x笔记4: TextField不能删除内容,以及我的解决办法。。。
    C++ 11 笔记 (六) : 随机数
    C++ 11 笔记 (五) : std::thread
    C++ 11 笔记 (四) : std::bind
    C++ 11 笔记 (三) : auto
    C++ 11 笔记 (二) : for循环
    要做的题目
    【C补充】结构体的内存分配,匈牙利命名法
    【C补充】文件操作
  • 原文地址:https://www.cnblogs.com/MarlonKang/p/14226946.html
Copyright © 2020-2023  润新知