AppiumExpand - 18570580798/study GitHub Wiki
-- coding: utf-8 --
"""AppiumEnhanceLibrary is the enhancement of robotframework-appiumlibrary. It supports some testing requirements of Weixin Mini Program """ import os from robot.libraries.BuiltIn import BuiltIn from selenium.webdriver.common.action_chains import ActionChains from selenium.common.exceptions import * from selenium.webdriver.remote.webelement import WebElement
from appium import webdriver from decrypt import Decrypt
class AppiumHybridLibrary(object): applib = None
def __init__(self, alias='AppiumLibrary'):
self.alias = alias
def _current_application(self):
if self.applib is None:
self.applib = BuiltIn().get_library_instance(self.alias)
return self.applib._current_application()
def execute_javascript(self, *code):
js = ''.join(code)
return self._current_application().execute_script(js)
def input_secured_password(self, key_name):
pw = Decrypt().get_password_by_key(key_name)
key_codes = self.transfer_string_to_keycode(pw)
for kc in key_codes:
self._current_application().press_keycode(kc)
def select_last_window(self):
driver = self._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(self, index):
driver = self._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_current_route(self):
route = self._current_application().execute_script("return window.__route__")
return route
def select_mp_window_by_route_name(self, route_name):
driver = self._current_application()
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 route_name in curr_route:
print "%s is already the %s window" % (str([curr_window_handle, curr_route]), 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 route_name in route:
print "Done: " + route
return
else:
print "next"
raise Exception("No such window")
def is_element_present(self, locator):
driver = self._current_application()
element = driver.find_element(locator, "True")
print element
if element is not None:
return element.is_displayed()
return None
def transfer_string_to_keycode(self, 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