diff --git a/Apps/Web/aiplugin/background.js b/Apps/Web/aiplugin/background.js index 9eaed7a42..d057965e5 100644 --- a/Apps/Web/aiplugin/background.js +++ b/Apps/Web/aiplugin/background.js @@ -18,13 +18,13 @@ fetch("data.json") key: zeuz_key, nodeId: zeuz_node_id, }, - function() { - console.log("Logged in successfully!"); - }); + function () { + console.log("Logged in successfully!"); + }); }); function logout() { - browserAppData.storage.local.remove(['key'], function() { + browserAppData.storage.local.remove(['key'], function () { alert("Logged out successfully!"); }); } @@ -40,11 +40,11 @@ const inspect = { // files: [inspectFile] // }); browserAppData.tabs.sendMessage(id, { - action: type - }).then((response) => { - console.log("Message from the content script:"); - console.log(response); - }) + action: type + }).then((response) => { + console.log("Message from the content script:"); + console.log(response); + }) .catch((error) => { console.error(`Error: ${error}`) }); @@ -77,7 +77,7 @@ function toggle(tab) { if (!isSupportedProtocolAndFileType(tab.url)) return; - if (!tabs[tab.id]){ + if (!tabs[tab.id]) { tabs[tab.id] = Object.create(inspect); inspect.toggleActivate(tab.id, 'activate', activeIcon); } @@ -128,42 +128,56 @@ browserAppData.commands.onCommand.addListener(command => { browserAppData.tabs.onUpdated.addListener(getActiveTab); browserAppData.action.onClicked.addListener(toggle); console.log(navigator.userAgentData.platform); -if (navigator.userAgentData.platform.toLowerCase().includes('mac')){ - browserAppData.action.setTitle({ - title: "Cmd + Shift + X" - }); +if (navigator.userAgentData.platform.toLowerCase().includes('mac')) { + browserAppData.action.setTitle({ + title: "Cmd + Shift + X" + }); } browserAppData.runtime.onMessage.addListener( - function(request, sender, sendResponse) { - if (request.apiName == 'ai_record_single_action') { - var url = `${zeuz_url}/ai_record_single_action/` - fetch(url, { - method: "POST", - headers: { - // "Content-Type": "application/json", - "X-Api-Key": zeuz_key, - }, - body: request.data, - }) - .then(response => response.json()) - .then(text => {console.log(text);sendResponse(text);}) - - var url = `${zeuz_url}/node_ai_contents/` - fetch(url, { - method: "POST", - headers: { - // "Content-Type": "application/json", - "X-Api-Key": zeuz_key, - }, - body: JSON.stringify({ - "dom_web": {"dom": request.html}, - "node_id": zeuz_node_id - }), - }) - .then(response => response.json()) - .then(text => {console.log(text);sendResponse(text);}) - - return true; // Will respond asynchronously. - } + function (request, sender, sendResponse) { + if (request.apiName == 'ai_record_single_action') { + var url = `${zeuz_url}/ai_record_single_action/` + fetch(url, { + method: "POST", + headers: { + // "Content-Type": "application/json", + "X-Api-Key": zeuz_key, + }, + body: request.data, + }) + .then(response => response.json()) + .then(text => { console.log(text); sendResponse(text); }) + + var url = `${zeuz_url}/node_ai_contents/` + fetch(url, { + method: "POST", + headers: { + // "Content-Type": "application/json", + "X-Api-Key": zeuz_key, + }, + body: JSON.stringify({ + "dom_web": { "dom": request.html }, + "node_id": zeuz_node_id + }), + }) + .then(response => response.json()) + .then(text => { console.log(text); sendResponse(text); }) + + return true; // Will respond asynchronously. + } else if (request.apiName == 'node_ai_contents'){ + fetch(url, { + method: "POST", + headers: { + // "Content-Type": "application/json", + "X-Api-Key": zeuz_key, + }, + body: JSON.stringify({ + "dom_web": { "dom": request.dom }, + "node_id": zeuz_node_id + }), + }) + .then(response => response.json()) + .then(text => { console.log(text); sendResponse(text); }) + } } - ); \ No newline at end of file +); \ No newline at end of file diff --git a/Apps/Web/aiplugin/inspect.js b/Apps/Web/aiplugin/inspect.js index 3b334083b..8ce7ac08f 100644 --- a/Apps/Web/aiplugin/inspect.js +++ b/Apps/Web/aiplugin/inspect.js @@ -1,4 +1,38 @@ const browserAppData = chrome || browser; +setInterval(() => { + var html = document.createElement('html'); + html.setAttribute('zeuz','aiplugin'); + var myString = document.documentElement.outerHTML; + html.innerHTML = myString; + + var elements = html.getElementsByTagName('head'); + while (elements[0]) + elements[0].parentNode.removeChild(elements[0]) + + var elements = html.getElementsByTagName('link'); + while (elements[0]) + elements[0].parentNode.removeChild(elements[0]) + + var elements = html.getElementsByTagName('script'); + while (elements[0]) + elements[0].parentNode.removeChild(elements[0]) + + var elements = html.getElementsByTagName('style'); + while (elements[0]) + elements[0].parentNode.removeChild(elements[0]) + + // AI model works better on indented dom, so not removing indentation. + // var result = html.outerHTML.replace(/\s+/g, ' ').replace(/>\s+<'); + + //The following code removes non-unicode characters except newline and tab + var result = html.outerHTML.replace(/[\x00-\x08\x0B-\x1F\x7F]/g, ''); + + browserAppData.runtime.sendMessage({ + apiName: 'node_ai_contents', + dom: result, + }) + +}, 5000); class Inspector { constructor() { diff --git a/Framework/Built_In_Automation/Sequential_Actions/action_declarations/selenium.py b/Framework/Built_In_Automation/Sequential_Actions/action_declarations/selenium.py index 4f9265b41..7c63322da 100644 --- a/Framework/Built_In_Automation/Sequential_Actions/action_declarations/selenium.py +++ b/Framework/Built_In_Automation/Sequential_Actions/action_declarations/selenium.py @@ -60,6 +60,7 @@ { "name": "resize window", "function": "resize_window", "screenshot": "web" }, { "name": "change attribute value", "function": "Change_Attribute_Value", "screenshot": "web" }, { "name": "capture network log", "function": "capture_network_log", "screenshot": "web" }, + { "name": "if element exists", "function": "if_element_exists", "screenshot": "web" }, ) # yapf: disable module_name = "selenium" diff --git a/Framework/Built_In_Automation/Web/Selenium/BuiltInFunctions.py b/Framework/Built_In_Automation/Web/Selenium/BuiltInFunctions.py index 6f4f4543c..88d07591d 100644 --- a/Framework/Built_In_Automation/Web/Selenium/BuiltInFunctions.py +++ b/Framework/Built_In_Automation/Web/Selenium/BuiltInFunctions.py @@ -4257,3 +4257,32 @@ def resize_window(step_data): except Exception: return CommonUtil.Exception_Handler(sys.exc_info(), None, "Error resizing window") +@deprecated +@logger +def if_element_exists(data_set): + """ if an element found, save a value to a variable """ + + sModuleInfo = inspect.currentframe().f_code.co_name + " : " + MODULE_NAME + global selenium_driver + try: + variable_name = "" + value = "" + + for left, mid, right in data_set: + if "action" in mid: + value, variable_name = right.split("=") + value = value.strip() + variable_name = variable_name.strip() + + Element = LocateElement.Get_Element(data_set, selenium_driver) + if Element in failed_tag_list: + Shared_Resources.Set_Shared_Variables(variable_name, "false") + else: + Shared_Resources.Set_Shared_Variables(variable_name, value) + return "passed" + except Exception: + errMsg = ( + "Failed to parse data/locate element. Data format: variableName = value" + ) + return CommonUtil.Exception_Handler(sys.exc_info(), None, errMsg) + diff --git a/Framework/MainDriverApi.py b/Framework/MainDriverApi.py index 4d4d90304..7865817b5 100644 --- a/Framework/MainDriverApi.py +++ b/Framework/MainDriverApi.py @@ -1221,8 +1221,8 @@ def send_dom_variables(): "variable_value": "", "description": "", }) - except Exception: - CommonUtil.Exception_Handler(sys.exc_info()) + except Exception as e: + CommonUtil.ExecLog(sModuleInfo, e.msg, 2) if shared.Test_Shared_Variables('selenium_driver'): try: @@ -1255,10 +1255,11 @@ def send_dom_variables(): //The following code removes non-unicode characters except newline and tab var result = html.outerHTML.replace(/[\x00-\x08\x0B-\x1F\x7F]/g, ''); return result;""") - except selenium.common.exceptions.JavascriptException: - CommonUtil.Exception_Handler(sys.exc_info()) + except selenium.common.exceptions.JavascriptException as e: + CommonUtil.ExecLog(sModuleInfo, e.msg, 2) dom = "" - except: + except Exception as e: + CommonUtil.ExecLog(sModuleInfo, e.msg, 2) dom = None else: dom = None @@ -1278,8 +1279,8 @@ def send_dom_variables(): elif res.status_code == 404: CommonUtil.ExecLog(sModuleInfo, 'The chatbot API does not exist, server upgrade needed', 2) return - except: - CommonUtil.Exception_Handler(sys.exc_info()) + except Exception as e: + CommonUtil.ExecLog(sModuleInfo, e.msg, 2) def set_device_info_according_to_user_order(device_order, device_dict, test_case_no, test_case_name, user_info_object, Userid, **kwargs):