Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor #504

Merged
merged 3 commits into from
Sep 18, 2024
Merged

Minor #504

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 60 additions & 46 deletions Apps/Web/aiplugin/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
});
}
Expand All @@ -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}`)
});
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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); })
}
}
);
);
34 changes: 34 additions & 0 deletions Apps/Web/aiplugin/inspect.js
Original file line number Diff line number Diff line change
@@ -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+</g, '><');

//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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
29 changes: 29 additions & 0 deletions Framework/Built_In_Automation/Web/Selenium/BuiltInFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

15 changes: 8 additions & 7 deletions Framework/MainDriverApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down