Appium+python Automation 27-waiting for activity to appear (Android-specific wait_activity)

Source: Internet
Author: User
Tags appium

Objective

When you start the app, if you do the next click, you will often get an error, so we will add sleep when the boot is complete.
So the question is, how much is this sleep time setting? Set long, waste time, set short, you will not find the element error.
This time we can use wait_activity syntax, until you want to click on the page activity appears, and then click, you can effectively save time.

Wait_activity

1. View the source code

def wait_activity(self, activity, timeout, interval=1):    """Wait for an activity: block until target activity presents    or time out.        This is an Android-only method.    :Agrs:     - activity - target activity     - timeout - max wait time, in seconds     - interval - sleep interval between retries, in seconds    """    try:        WebDriverWait(self, timeout, interval).until(            lambda d: d.current_activity == activity)        return True    except TimeoutException:        return False

2. Explanatory notes:

wait_activity(self, activity, timeout, interval=1):    等待指定的activity出现直到超时,interval为扫描间隔1秒    即每隔几秒获取一次当前的activity        android特有的    返回的True 或 False    :Agrs:     - activity - 需等待的目标 activity     - timeout - 最大超时时间,单位是s     - interval - 循环查询时间    用法:driver.wait_activity(‘.activity.xxx’,5,2)
Get current_activity

1. After opening the app, sleep10 seconds, and so on, the app completely starts to go to the main page, then gets the activity of the current interface.

# Coding:utf-8 fromAppiumImportWebdriver fromTimeImportSleepdesired_caps={' PlatformName ':' Android ',' DeviceName ':' 127.0.0.1:62001 ',' Platformversion ':' 4.4.2 ',' Apppackage ':' com.baidu.yuedu ',' appactivity ':' com.baidu.yuedu.splash.SplashActivity '}driver=Webdriver. Remote (' Http://127.0.0.1:4723/wd/hub ', desired_caps) Sleep (Ten)# Get Current interface activityAc=Driver.current_activityPrint(AC)

2. Operation Result:

Waiting for activity

1. Use sleep too wasted time, and do not know when to start the completion, so try not to sleep

2. The previous step has been obtained when the main page of activity, then you can use wait_activity wait for it to appear, and then do the next click action

3. Reference Code

# coding:utf-8from appium import webdriverfrom 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'                }driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)# sleep(10)  # 不用sleep# 获取当前界面activityac = driver.current_activityprint(ac)# 等主页面activity出现,30秒内driver.wait_activity(".base.ui.MainActivity", 30)# 点知道了driver.find_element_by_id("com.baidu.yuedu:id/positive").click()

Appium+python Automation 27-waiting for activity to appear (Android-specific wait_activity)

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.