コード例 #1
0
 def terminate_app():
     """
     强制关闭app,退出后台
     :return:
     """
     app_id = current_driver().desired_capability['appPackage']
     current_mobile().termiate_app(app_id)
コード例 #2
0
 def relaunch_app():
     """
     重启app
     :return:
     """
     app_id = current_driver().capabilities['appPackage']
     current_mobile().terminate_app(app_id)
     current_mobile().launch_app(app_id)
コード例 #3
0
 def get_current_activity_name():
     import os, sys
     global findExec
     findExec = 'findstr' if sys.platform == 'win32' else 'grep'
     device_name = current_driver().capabilities['deviceName']
     cmd = 'adb -s %s shell dumpsys window | %s mCurrentFocus' % (
         device_name, findExec)
     res = os.popen(cmd)
     time.sleep(2)
     # 截取出activity名称 == ''为第三方软件
     current_activity = res.read().split('u0 ')[-1].split('/')[0]
     res.close()
     return current_activity
コード例 #4
0
def capture_screen_shot(path):
    from library.core.utils import applicationcache
    if os.path.isfile(path):
        os.remove(path)
    dir_name, file_name = os.path.split(path)
    if not os.path.isdir(dir_name):
        os.makedirs(dir_name)
    if not isinstance(applicationcache.current_mobile(), NoConnection):
        capture = getattr(applicationcache.current_driver(),
                          'get_screenshot_as_file', lambda p: None)
        try:
            result = capture(path)
            return result
        except:
            return
    return
コード例 #5
0
def build_page_object(page_name=None,
                      page_description=None,
                      activity=None,
                      locator=None):
    temp = get_template(PAGE_OBJECT_TEMPLATE)
    file_path = ''
    source_path = ''
    try_times = 0
    while not page_name:
        sys.stdout.write('请输入页面类名:')
        sys.stdout.flush()
        page_name = sys.stdin.readline().strip()
        file_path = os.path.join(DISTINCT_PATH, page_name + '.py')
        source_path = os.path.join(DISTINCT_PATH, page_name + '.xml')
        if os.path.isfile(file_path):
            page_name = None
        try_times += 1
        if try_times > 3:
            raise Exception('尝试超过3次,请确认页面名不会重复再执行该任务!')
    if not page_description:
        sys.stdout.write('请输入页面描述:')
        sys.stdout.flush()
        page_description = sys.stdin.readline().strip()

    # output = temp % {
    #     'PageName': page_name,
    #     'PageDescription': page_description,
    #     'Activity': activity,
    #     'Locator': locator
    # }
    out = re.sub(r"(%\(PageName\)s)", page_name, temp)
    out = re.sub(r'(%\(PageDescription\)s)', page_description, out)
    out = re.sub(r'(%\(Activity\)s)', activity, out)
    out = out.replace("%(Locator)s", locator)
    # out = re.sub(r'(%\(Locator\)s)', locator, out)
    with open(file_path, 'w+', encoding='UTF-8') as f:
        f.write(out)
        sys.stdout.write('Page object created on: ' + file_path + '\n')
        sys.stdout.flush()

    with open(source_path, 'w+', encoding='UTF-8') as f:
        f.write(current_driver().page_source)
        sys.stdout.write('Page source created on: ' + source_path + '\n')
        sys.stdout.flush()
コード例 #6
0
def generate_page_object():
    driver = current_driver()
    do = True
    while do:
        sys.stdout.write('按回车键开始录制:')
        sys.stdout.flush()
        sys.stdin.readline().strip().upper()
        activity = driver.current_activity
        page_source = driver.page_source
        tree = etree.fromstring(page_source.encode())
        elements = []

        def parse(node):
            for e in node.iter():
                resource_id = e.get('resource-id')
                xpath = node.getroottree().getpath(e)
                text = e.get('text')
                if isinstance(text, str):
                    text = text.replace('\n', '')
                key = text if text else resource_id
                if key in dict(elements):
                    key = key + uuid.uuid4().__str__()
                if key:
                    if resource_id:
                        elements.append((key, (MobileBy.ID, resource_id)))
                    else:
                        elements.append((key, (MobileBy.XPATH, xpath)))

        parse(tree)
        locators = re.sub(r"('[^)]+\),?)", r'\1\n',
                          dict(OrderedDict(elements)).__repr__())
        locators = re.sub(r"(\('id')", r'(MobileBy.ID', locators)
        build_page_object(activity=activity, locator=locators)
        sys.stdout.write('\n结束录制?(Y/N):')
        sys.stdout.flush()
        feedback = sys.stdin.readline().strip().upper()
        if feedback == 'Y':
            do = False
コード例 #7
0
 def reset_and_relaunch_app():
     """首次启动APP(使用重置APP代替)"""
     app_package = 'com.chinasofti.rcs'
     current_driver().activate_app(app_package)
     current_mobile().reset_app()
コード例 #8
0
 def reset_and_relaunch_app():
     """首次启动APP(使用重置APP代替)"""
     app_package = 'com.cmic.college'
     current_driver().activate_app(app_package)
     current_mobile().reset_app()