Appium+python Automation 29-toast

Source: Internet
Author: User
Tags deprecated xpath appium

Supported platforms

1. View Appium v1.7 version official documentation

Supported platforms

Appium supports app automation across a variety of platforms, like IOS, Android, and Windows. Each platform was supported by one or more "drivers", which know how to automate that particular platform. Choose a driver below for specific information on how this driver works and how to set it up:

    • Ios
    • The Xcuitest Driver
    • (DEPRECATED) The UIAutomation Driver
    • Android
    • (BETA) The Espresso Driver
    • The UiAutomator2 Driver
    • (DEPRECATED) The Uiautomator Driver
    • (DEPRECATED) The Selendroid Driver
    • The Windows Driver (for Windows Desktop Apps)
    • The Mac Driver (for Mac Desktop apps)

2. From the above information can be seen in the current 1.7 of the Android version can support: Espresso, UiAutomator2, Uiautomator, selendroid Four drive mode, the following two are not recommended, too old, Espresso This is the latest support in the beta phase, UiAutomator2 is currently the most stable.

3.appium The latest version can also support Windows and Mac Desktop app, this is stable, wait and see!

Toast Positioning

1. First look at what the toast looks like, such as the pop-up message "Press again to exit", this is toast.

2. To locate the toast element, it is important to note that the Automationname parameter must be Uiautomator2 to navigate to.

' Automationname ': ' Uiautomator2 '

# coding:utf-8from appium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom time import sleepdesired_caps = {                'platformName': 'Android',                'deviceName': '127.0.0.1:62001',                'platformVersion': '4.4.2',                'appPackage': 'com.baidu.yuedu',                'appActivity': 'com.baidu.yuedu.splash.SplashActivity',                'noReset': 'true',                'automationName': 'Uiautomator2'                }driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)# 等主页面activity出现driver.wait_activity(".base.ui.MainActivity", 10)driver.back()   # 点返回# 定位toast元素toast_loc = ("xpath", ".//*[contains(@text,'再按一次退出')]")t = WebDriverWait(driver, 10, 0.1).until(EC.presence_of_element_located(toast_loc))print t

3. Printed results, the following information appears, indicating that the toast is located

<appium.webdriver.webelement.webelement (session= "02813cce-9aaf-4754-a532-07ef7aebeb88", element= " 339f72c4-d2e0-4d98-8db0-69be741a3d1b ") >

Encapsulating toast Judgment

1. Write a function individually to encapsulate whether a toast message exists, there is a return of true, there is no return false

def is_toast_exist(driver,text,timeout=30,poll_frequency=0.5):    '''is toast exist, return True or False    :Agrs:     - driver - 传driver     - text   - 页面上看到的文本内容     - timeout - 最大超时时间,默认30s     - poll_frequency  - 间隔查询时间,默认0.5s查询一次    :Usage:     is_toast_exist(driver, "看到的内容")    '''    try:        toast_loc = ("xpath", ".//*[contains(@text,'%s')]"%text)        WebDriverWait(driver, timeout, poll_frequency).until(EC.presence_of_element_located(toast_loc))        return True    except:        return False
Reference Code
# coding:utf-8from Appium Import webdriverfrom selenium.webdriver.support.ui import Webdriverwaitfrom                Selenium.webdriver.support Import expected_conditions as Ecdesired_caps = {' PlatformName ': ' Android ', ' DeviceName ': ' 127.0.0.1:62001 ', ' platformversion ': ' 4.4.2 ', ' apppackage ': ' Com.b                Aidu.yuedu ', ' appactivity ': ' com.baidu.yuedu.splash.SplashActivity ', ' noreset ': ' true ',     ' Automationname ': ' Uiautomator2 '}def is_toast_exist (driver,text,timeout=30,poll_frequency=0.5): "Is toast exist, return True or False:agrs:-Driver-driver-text-text content seen on page-timeout-Max Super Time, default 30s-poll_frequency-interval query time, default 0.5s query once: usage:is_toast_exist (Driver, "What to See") "Try:t Oast_loc = ("XPath", ".//*[contains (@text, '%s ')]"%text) webdriverwait (driver, timeout, poll_frequency). Until (Ec.pre        Sence_of_element_located (Toast_loc))return True Except:return falseif __name__ = = "__main__": Driver = webdriver. Remote (' Http://127.0.0.1:4723/wd/hub ', desired_caps) # etc Main Page activity appears driver.wait_activity (". Base.ui.MainActivity" , Driver.back () # Point return # to determine if there is a toast-' and press exit ' Print is_toast_exist (Driver, "Press and Exit Again")

Appium+python Automation 29-toast

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.