From 0bd1da38bfd3c0263aa39cea0676fc2d1d1815ff Mon Sep 17 00:00:00 2001 From: Elias Hachichou Date: Thu, 4 Jun 2020 12:47:35 +0200 Subject: [PATCH 1/4] Added requirements --- requirements.txt | 3 +++ setup.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 requirements.txt 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", From 3dbc3c1dadac918d6154dbf12e40278c5c5d5084 Mon Sep 17 00:00:00 2001 From: gregwg_cheng Date: Wed, 22 Jul 2020 14:13:20 +0800 Subject: [PATCH 2/4] I add the following functions to WADLibrary to make it better : get_window_title, set_value_table, get_value_table_list, find_elements, get_element_length, find_sub_elements_under_parent, get_attribute, get_name, is_enabled, get_text, click_element_text, get_element_list, get_element_selected, get_element_attribute, get_element_enable, get_element_text, is_visible_v2 --- WADLibrary/Keywords.py | 223 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 215 insertions(+), 8 deletions(-) diff --git a/WADLibrary/Keywords.py b/WADLibrary/Keywords.py index da5ada2..a786ea7 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() @@ -89,6 +105,29 @@ def attach_to_window(self, value, name, using='name', session_id=None): desired_caps["deviceName"] = self.device_name self.__current_session = self.create_session(desired_caps, name) self.get_sessions() + + def set_value_table(self, value, using='name', path = '//*', session_id=None): + """ + You should use it before using keyword 'Click Element Text'. + Then you can click something you want by text. + If current window's name is calculator, and you set value = calculator, + the all text element in caculator will write in value table. + It will take additional times to build table. + """ + if session_id is None: + session_id = self.get_current_session_id() + self.keylist.clear() + self.resultlist.clear() + elem = self.find_element(value=value, using=using, session_id=session_id) + elems = self.find_elements_by_element(elem=elem, value= path, using='xpath', session_id=session_id) + self.keylist = [item.get('ELEMENT') for item in elems] + self.resultlist = [self.get_text(item, session_id=session_id) for item in self.keylist] + + def get_value_table_list(self): + """ + Get the list of the value table. + """ + return self.resultlist def create_session(self, desired_caps, name): res = execute.post(self.path + '/session/', json={'desiredCapabilities': desired_caps}) @@ -117,6 +156,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 +206,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 +263,88 @@ 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_v2(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 click_element_text(self, value, location=0, way='left', session_id=None): + """ + You should use Keyword 'Set Value Table' before use it. + Then you can click the text exist on current window. + If the area have the same text not only one, + you can set location to click the number of location of the same text elements.(location default value is 0) + """ + if session_id is None: + session_id = self.get_current_session_id() + indexlist = [i for i,v in enumerate(self.resultlist) if v==value] + if self.is_visible_v2(self.keylist[indexlist[location]]): + self.move_to_element(elem=self.keylist[indexlist[location]], session_id=session_id) + self.mouse_click(button=way, session_id=session_id) + else: + ex = Exception("The Location " + str(location) + " of " + 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 +353,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): @@ -179,6 +374,7 @@ 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 @@ -197,6 +393,17 @@ def is_visible(self, value, using='name', session_id=None): return False else: execute.analyse(res, catch_error=True) + + def is_visible_v2(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): if timeout is None: @@ -212,7 +419,7 @@ def check_visibility(): else: return error or "Element '%s' was not visible 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): if timeout is None: @@ -228,7 +435,7 @@ def check_visibility(): 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) From 69d66b6527aefda9ca40f57c1ebe8c49a8ce3854 Mon Sep 17 00:00:00 2001 From: gregwg_cheng Date: Mon, 3 Aug 2020 16:05:12 +0800 Subject: [PATCH 3/4] Add clear_value function to WADLibrary. --- WADLibrary/Keywords.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/WADLibrary/Keywords.py b/WADLibrary/Keywords.py index a786ea7..0ca1989 100644 --- a/WADLibrary/Keywords.py +++ b/WADLibrary/Keywords.py @@ -363,6 +363,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() From a39a6c902b6227798c33dc8fa683f37eaa267b9c Mon Sep 17 00:00:00 2001 From: gregwg_cheng Date: Thu, 6 Aug 2020 16:04:23 +0800 Subject: [PATCH 4/4] 1.remove the function set_value_table and its related fuctions. 2.change the naming of is_visible to is_exist 3.change the naming of wait_until_element_is_visible to wait_until_element_is_exist 4.change the naming of wait_until_element_is_not_visible to wait_until_element_is_not_exist 5.change the naming of variable in wait_until_element_is_exist 6.change the naming of variable in wait_until_element_is_not_exist --- WADLibrary/Keywords.py | 66 +++++++++--------------------------------- 1 file changed, 13 insertions(+), 53 deletions(-) diff --git a/WADLibrary/Keywords.py b/WADLibrary/Keywords.py index 0ca1989..c8d5c58 100644 --- a/WADLibrary/Keywords.py +++ b/WADLibrary/Keywords.py @@ -105,29 +105,6 @@ def attach_to_window(self, value, name, using='name', session_id=None): desired_caps["deviceName"] = self.device_name self.__current_session = self.create_session(desired_caps, name) self.get_sessions() - - def set_value_table(self, value, using='name', path = '//*', session_id=None): - """ - You should use it before using keyword 'Click Element Text'. - Then you can click something you want by text. - If current window's name is calculator, and you set value = calculator, - the all text element in caculator will write in value table. - It will take additional times to build table. - """ - if session_id is None: - session_id = self.get_current_session_id() - self.keylist.clear() - self.resultlist.clear() - elem = self.find_element(value=value, using=using, session_id=session_id) - elems = self.find_elements_by_element(elem=elem, value= path, using='xpath', session_id=session_id) - self.keylist = [item.get('ELEMENT') for item in elems] - self.resultlist = [self.get_text(item, session_id=session_id) for item in self.keylist] - - def get_value_table_list(self): - """ - Get the list of the value table. - """ - return self.resultlist def create_session(self, desired_caps, name): res = execute.post(self.path + '/session/', json={'desiredCapabilities': desired_caps}) @@ -267,29 +244,12 @@ 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) - if self.is_visible_v2(elem): + 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 click_element_text(self, value, location=0, way='left', session_id=None): - """ - You should use Keyword 'Set Value Table' before use it. - Then you can click the text exist on current window. - If the area have the same text not only one, - you can set location to click the number of location of the same text elements.(location default value is 0) - """ - if session_id is None: - session_id = self.get_current_session_id() - indexlist = [i for i,v in enumerate(self.resultlist) if v==value] - if self.is_visible_v2(self.keylist[indexlist[location]]): - self.move_to_element(elem=self.keylist[indexlist[location]], session_id=session_id) - self.mouse_click(button=way, session_id=session_id) - else: - ex = Exception("The Location " + str(location) + " of " + value + + " is not visible") - raise ex def get_element_list(self, value, using='name', session_id=None): """ @@ -389,7 +349,7 @@ def set_focus(self, session_id=None): # 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', @@ -403,7 +363,7 @@ def is_visible(self, value, using='name', session_id=None): else: execute.analyse(res, catch_error=True) - def is_visible_v2(self, elem, session_id=None): + def is_visible(self, elem, session_id=None): """ If element you can see on the current window, the result will return true. """ @@ -414,31 +374,31 @@ def is_visible_v2(self, elem, session_id=None): 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(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: