早一版的Appium扩展 - 18570580798/study GitHub Wiki

#coding=utf-8 from robot.libraries.BuiltIn import BuiltIn from appium import webdriver from selenium.common.exceptions import * import random

def getWebdriver(alias='AppiumLibrary'): applib=BuiltIn().get_library_instance(alias) return applib._current_application()

def execute_javascript(script): driver=getWebdriver() return driver.execute_script(script)

def select_last_window(): applib = BuiltIn().get_library_instance('AppiumLibrary') driver=applib._current_application() window_handles=driver.window_handles print window_handles current_wh=driver.current_window_handle print current_wh index=window_handles.index(current_wh) print index if index>0: driver.switch_to_window(window_handles[index-1])

def select_window(index): applib = BuiltIn().get_library_instance('AppiumLibrary') driver=applib._current_application() window_handles=driver.window_handles print window_handles current_wh=driver.current_window_handle print "CURRENT: "+current_wh if index is None: index=window_handles.index(current_wh) index-=1 else: index=int(index) print "INDEX: "+str(index) if index>=0: driver.switch_to_window(window_handles[index]) current_wh=driver.current_window_handle print "CURRENT: "+current_wh

def _get_window_infos(): window_infos = [] driver=getWebdriver() try: starting_handle = driver.current_window_handle print "F: "+starting_handle except NoSuchWindowException: starting_handle = None try: for i,handle in enumerate(driver.window_handles): driver.switch_to.window(handle) window_infos.append(_get_current_window_info(driver)) print[item for item in window_infos] finally: if starting_handle and starting_handle in driver.window_handles: driver.switch_to.window(starting_handle) return window_infos

def _get_current_window_info(driver): try: route=driver.execute_script("return window.route") html_length=driver.execute_script("return wx.getHTMLLength()") except: # The webdriver implementation doesn't support Javascript so we # can't get window id or name this way. route,html_length=None route, html_length = ( att if att is not None else 'undefined'for att in (route, html_length) ) # att if att else 'undefined' for att in (name, title, url) return driver.current_window_handle, route, html_length

def get_current_route(): driver=getWebdriver() route = driver.execute_script("return window.route") return route

def select_window_by_name(name): index=0 driver=getWebdriver() window_infos=_get_window_infos() print[item for item in window_infos] for i,item in enumerate(window_infos): # if name == item[1].split("/")[-1]: if name in item[1]: index=i break print "TARGET: "+str(index) window_handles=driver.window_handles current_wh=driver.current_window_handle cur_index=window_handles.index(current_wh) print "INDEX: "+str(cur_index)

if index >= 0 and index < len(window_handles):
   driver.switch_to_window(window_handles[index])
current_wh=driver.current_window_handle
cur_index=window_handles.index(current_wh)
print "DONE: "+str(index)

def transfer_string_to_keycode(string): keycode_list=[] for char in string: char=char.upper() num=int(ord(char)) if 48<= num <=57: keycode_list.append(num-41) elif 65<= num <=90: keycode_list.append(num-36) # The decimal for the ASCII "@" is 64 ,"-" is 45 ,"." is 46 elif num == 64: keycode_list.append(num+13) elif num == 45: keycode_list.append(num+24) elif num == 46: keycode_list.append(num+10) else: keycode_list.append(num) return keycode_list

def select_window_by_name1(name): driver=getWebdriver() index=0 window_infos=[] curr_window_handle = None curr_route = None curr_index=-1 window_handles=driver.window_handles try: curr_window_handle = driver.current_window_handle curr_route = driver.execute_script("return window.route") except NoSuchWindowException,e: print e print window_handles window_infos = [None]*len(window_handles)

if curr_window_handle is not None:
    try:
        curr_index=window_handles.index(curr_window_handle)
    except ValueError:
        curr_index=-1
print curr_index		

for i,handle in enumerate(window_handles):
    if i==curr_index:
        route=curr_route
    else:
        driver.switch_to_window(handle)
        route = driver.execute_script("return window.__route__")
        print route
    print "Switching to Window:"+str([handle,route])
    window_infos.append([handle,route])

    if name in str(route):
        print "Done"
        return
    else:
        print "Next"

def select_mp_window_by_route_name(name): driver=getWebdriver() index=0 window_infos=[] curr_window_handle = None curr_route = None curr_index=-1 js="return window.route" try: window_handles=driver.window_handles curr_window_handle = driver.current_window_handle curr_route = driver.execute_script(js) except NoSuchWindowException: pass print "handles:"+ str(window_handles) if curr_route is not None and name in curr_route: print "%s is already the %s window"%(str([curr_window_handle, curr_route]), name) return else: window_infos = [None]*len(window_handles) if curr_window_handle is not None: try: curr_index=window_handles.index(curr_window_handle) except ValueError: curr_index=-1

    for i,handle in enumerate(window_handles):
        if i==curr_index:
            route=curr_route
            window_infos[i] = [handle, route]
            print "Skipped Curr Window:" + str(window_infos[i])
            continue
        else:
            driver.switch_to_window(handle)
            route = driver.execute_script(js)
            window_infos[i]=[handle,route]
            print "Switched to Window:" + str(window_infos[i])

        if route is not None and name in route:
            print "Done: "+route
            return
        else:
            print "next"
    raise Exception("No such window")