diff --git a/WADLibrary/Keywords.py b/WADLibrary/Keywords.py index da5ada2..c8d5c58 100644 --- a/WADLibrary/Keywords.py +++ b/WADLibrary/Keywords.py @@ -3,16 +3,20 @@ from .common.keys import keys from .common import execute import time +import re class Keywords: def __init__(self, path, platform, device_name, timeout): self.__sessions = [] - self.__current_session = None self.__platform = platform + self.__current_session = None self.path = path self.device_name = device_name self.timeout = timeout + self.keylist = [] + self.resultlist = [] + def set_up(self): desired_caps = dict() @@ -69,6 +73,7 @@ def get_session_by_id(self, session_id): for session in self.__sessions: if session_id == session.get_id(): return session + def get_window_handle(self, using, value, session_id=None): if session_id is None: @@ -79,6 +84,17 @@ def get_window_handle(self, using, value, session_id=None): handle = hex(int(json_obj['value'])) return handle + def get_window_title(self, session_id=None): + """ + Get the title of the window you currently handle. + """ + if session_id is None: + session_id = self.get_current_session_id() + res = execute.get(self.path + '/session/' + session_id + '/title') + json_obj = json.loads(res.text) + result = json_obj['value'] + return result + def attach_to_window(self, value, name, using='name', session_id=None): if session_id is None: session_id = self.get_current_session_id() @@ -117,6 +133,44 @@ def find_element(self, value, using='name', session_id=None): json_obj = json.loads(res.text) elem = json_obj['value']['ELEMENT'] return elem + + def find_elements(self, value, using='name', session_id=None): + """ + You can use it to get an element list you find. + """ + if session_id is None: + session_id = self.get_current_session_id() + res = execute.post(self.path + '/session/' + session_id + '/elements', + json={'using': using, 'sessionId': session_id, 'value': value}) + json_obj = json.loads(res.text) + elem = json_obj['value'] + return elem + + def get_element_length(self, value, using='name', session_id=None): + """ + Get the count of element(s) you find. + """ + if session_id is None: + session_id = self.get_current_session_id() + res = execute.post(self.path + '/session/' + session_id + '/elements', + json={'using': using, 'sessionId': session_id, 'value': value}) + json_obj = json.loads(res.text) + elem = json_obj['value'] + length = len(elem) + return length + + def find_sub_elements_under_parent(self, elem, value, using='xpath', session_id=None): + """ + You can use it to find all the sub elements under a parent element. + """ + if session_id is None: + session_id = self.get_current_session_id() + res = execute.post(self.path + '/session/' + session_id + '/element/' + elem + '/elements', + json={'using': using, 'sessionId': session_id, 'value': value}) + json_obj = json.loads(res.text) + elems = json_obj['value'] + return elems + def move_to_element(self, elem, session_id=None): if session_id is None: @@ -129,6 +183,51 @@ def mouse_click(self, button='left', session_id=None): buttons = {'left': 0, 'middle': 1, 'right': 2} execute.post(self.path + '/session/' + session_id + '/click', json={'button': buttons[button]}) + def get_attribute(self, elem, attribute, session_id=None): + """ + You can get the attribute of an element you want. + """ + if session_id is None: + session_id = self.get_current_session_id() + res = execute.get(self.path + '/session/' + session_id + '/element/' + elem + '/attribute/' + attribute) + json_obj = json.loads(res.text) + result = json_obj['value'] + return result + + def get_name(self, elem, session_id=None): + """ + You can get the name of an element you want. + """ + if session_id is None: + session_id = self.get_current_session_id() + res = execute.get(self.path + '/session/' + session_id + '/element/' + elem + '/attribute' + '/Name') + json_obj = json.loads(res.text) + result = json_obj['value'] + return result + + def is_enabled(self, elem, session_id=None): + """ + You can get the status of an element you want. + It will return boolean value, enable status is true, disable status is false. + """ + if session_id is None: + session_id = self.get_current_session_id() + res = execute.get(self.path + '/session/' + session_id + '/element/' + elem + '/enabled') + json_obj = json.loads(res.text) + result = json_obj['value'] + return result + + def get_text(self, elem, session_id=None): + """ + You can get the text of an element you want. + """ + if session_id is None: + session_id = self.get_current_session_id() + res = execute.get(self.path + '/session/' + session_id + '/element/' + elem + '/text') + json_obj = json.loads(res.text) + result = json_obj['value'] + return result + def double_click(self, session_id=None): if session_id is None: session_id = self.get_current_session_id() @@ -141,13 +240,71 @@ def double_click_element(self, value, using='name', session_id=None): self.move_to_element(elem=elem, session_id=session_id) self.double_click(session_id=session_id) - def click_element(self, value, using='name', session_id=None): + def click_element(self, value, using='name', way='left', session_id=None): if session_id is None: session_id = self.get_current_session_id() elem = self.find_element(value=value, using=using, session_id=session_id) - self.move_to_element(elem=elem, session_id=session_id) - self.mouse_click(button='left', session_id=session_id) + if self.is_visible(elem): + self.move_to_element(elem=elem, session_id=session_id) + self.mouse_click(button=way, session_id=session_id) + else: + ex = Exception(value + " is not visible") + raise ex + + def get_element_list(self, value, using='name', session_id=None): + """ + Get a list of all element you find. + """ + if session_id is None: + session_id = self.get_current_session_id() + elems = self.find_elements(value=value, using=using, session_id=session_id) + elem_list = [] + for item in elems: + elem_list.append(item.get('ELEMENT')) + return elem_list + + def get_element_selected(self, value, using='name', session_id=None): + """ + You can use it to get the status of the checkbox. + """ + if session_id is None: + session_id = self.get_current_session_id() + elem = self.find_element(value=value, using=using, session_id=session_id) + res = execute.get(self.path + '/session/' + session_id + '/element/' + elem + '/selected') + json_obj = json.loads(res.text) + result = json_obj['value'] + return result + + def get_element_attribute(self, value, attribute, using='name', session_id=None): + """ + You can get the attribute of an element you want. + """ + if session_id is None: + session_id = self.get_current_session_id() + elem = self.find_element(value=value, using=using, session_id=session_id) + result = self.get_attribute(elem=elem, attribute=attribute, session_id=session_id) + return result + def get_element_enable(self, value, using='name', session_id=None): + """ + You can get the status of an element you want. + """ + if session_id is None: + session_id = self.get_current_session_id() + elem = self.find_element(value=value, using=using, session_id=session_id) + result = self.is_enabled(elem=elem, session_id=session_id) + return result + + def get_element_text(self, value, using='name', session_id=None): + """ + You can get the text of an element you want. + """ + if session_id is None: + session_id = self.get_current_session_id() + elem = self.find_element(value=value, using=using, session_id=session_id) + result = self.get_text(elem=elem, session_id=session_id) + return result + def keyboard_keys(self, value, session_id=None): if session_id is None: session_id = self.get_current_session_id() @@ -156,9 +313,7 @@ def keyboard_keys(self, value, session_id=None): def send_key(self, value, session_id=None): if session_id is None: session_id = self.get_current_session_id() - key = keys(value) - execute.post(self.path + '/session/' + session_id + '/keys', json={'value': [key]}) def enter_value(self, value, locator, using='name', session_id=None): @@ -168,6 +323,15 @@ def enter_value(self, value, locator, using='name', session_id=None): execute.post(self.path + '/session/' + session_id + '/element/' + elem + '/value', json={'value': list(value)}) + def clear_value(self, locator, using='name', session_id=None): + """ + You can use this function to clear the value of textbox. + """ + if session_id is None: + session_id = self.get_current_session_id() + elem = self.find_element(locator, using, session_id) + execute.post(self.path + '/session/' + session_id + '/element/' + elem + '/clear') + def set_focus(self, session_id=None): if session_id is None: session_id = self.get_current_session_id() @@ -179,12 +343,13 @@ def set_focus(self, session_id=None): app_name = caps['app'] json_obj = {'name': app_name} execute.post(self.path + '/session/' + session_id + '/window', json=json_obj) + ####################################################################################################################### # Waiting functions ####################################################################################################################### - def is_visible(self, value, using='name', session_id=None): + def is_exist(self, value, using='name', session_id=None): if session_id is None: session_id = self.get_current_session_id() res = execute.post(self.path + '/session/' + session_id + '/element', @@ -197,38 +362,49 @@ def is_visible(self, value, using='name', session_id=None): return False else: execute.analyse(res, catch_error=True) + + def is_visible(self, elem, session_id=None): + """ + If element you can see on the current window, the result will return true. + """ + if session_id is None: + session_id = self.get_current_session_id() + res = execute.get(self.path + '/session/' + session_id + '/element/' + elem + '/displayed') + json_obj = json.loads(res.text) + result = json_obj['value'] + return result - def wait_until_element_is_visible(self, locator, using='name', timeout=None, error=None, session_id=None): + def wait_until_element_is_exist(self, locator, using='name', timeout=None, error=None, session_id=None): if timeout is None: timeout = self.timeout def check_visibility(): - visible = self.is_visible(locator, using, session_id) - if visible: + exist = self.is_exist(locator, using, session_id) + if exist: return - elif visible is None: + elif exist is None: return error or "Element locator '%s' did not match any elements after %s" % ( locator, self.timeout) else: - return error or "Element '%s' was not visible in %s" % (locator, self.timeout) + return error or "Element '%s' was not exist in %s" % (locator, self.timeout) - self.wait_until_no_error(timeout, check_visibility) + self.wait_until_no_error(int(timeout), check_visibility) - def wait_until_element_is_not_visible(self, locator, using='name', timeout=None, error=None, session_id=None): + def wait_until_element_is_not_exist(self, locator, using='name', timeout=None, error=None, session_id=None): if timeout is None: timeout = self.timeout def check_visibility(): - visible = self.is_visible(locator, using, session_id) - if visible: - return error or "Element '%s' is still visible in %s" % (locator, self.timeout) - elif visible is None: + exist = self.is_exist(locator, using, session_id) + if exist: + return error or "Element '%s' is still exist in %s" % (locator, self.timeout) + elif exist is None: return error or "Element locator '%s' did not match any elements after %s" % ( locator, self.timeout) else: return - self.wait_until_no_error(timeout, check_visibility) + self.wait_until_no_error(int(timeout), check_visibility) def wait_until(self, timeout, error, func, *args): error = error.replace('', self.timeout) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ca7c7b7 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +robotframework +requests +psutil \ No newline at end of file diff --git a/setup.py b/setup.py index 91f2003..b331c5d 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="robotframework-wadlibrary", - version="20.02.02", + version="20.06.04", author="Elias Hachichou, Adwisit", author_email="elias.hachichou@adwisit.se", description="WADLibrary",