Skip to content

Commit 8f2f8b4

Browse files
Merge pull request #384 from AutomationSolutionz/inspector
Inspector and recorder code format fix
2 parents 44b5940 + 2fa79be commit 8f2f8b4

File tree

8 files changed

+81
-149
lines changed

8 files changed

+81
-149
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pid.txt
1919

2020
# Zeuz Node specific ignores
2121
Framework/settings.conf
22+
Apps/Web/aiplugin/data.json
2223
node-bigquery-privkey.json
2324
AutomationLog/
2425
iosSimulator/*

Apps/Web/AI_Recorder/background/back.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
const metaData = {};
1+
var metaData = {};
2+
3+
fetch("data.json")
4+
.then(Response => Response.json())
5+
.then(data => {
6+
metaData = data;
7+
});
8+
29
const browserAppData = chrome || browser;
310

411
import './back_zeuz.js';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"testNo": "TEST-0000",
3+
"testName": "Test_name",
4+
"stepNo": 0,
5+
"stepName": "Step_name",
6+
"url": "url",
7+
"apiKey": "apiKey",
8+
"jwtKey": "jwtKey"
9+
}

Apps/Web/AI_Recorder/content/recorder.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,20 @@ class Recorder {
5353
var html = document.createElement('html');
5454
html.innerHTML = document.documentElement.outerHTML;
5555

56-
for (let each of target) if (each[1] == 'xpath:position') {var xpath = each[0];break;}
56+
for (let each of target) if (each[1] == 'xpath:position') {
57+
var xpath = each[0];
58+
break;
59+
}
5760
var xPathResult = document.evaluate(xpath, html);
5861
if(xPathResult) var main_elem = xPathResult.iterateNext();
5962
else return;
6063

6164
main_elem.setAttribute('zeuz', 'aiplugin');
6265
console.log(main_elem.hasAttribute('zeuz'), main_elem);
6366

67+
// Recorder already changed the value which should not be sent to ai-engine
68+
if (command === 'text')
69+
main_elem.removeAttribute('value');
6470
// get all <head> elements from html
6571
var elements = html.getElementsByTagName('head');
6672
while (elements[0])
@@ -76,6 +82,11 @@ class Recorder {
7682
while (elements[0])
7783
elements[0].parentNode.removeChild(elements[0])
7884

85+
// get all <link> elements from html
86+
var elements = html.getElementsByTagName('link');
87+
while (elements[0])
88+
elements[0].parentNode.removeChild(elements[0])
89+
7990
var xPathResult = document.evaluate(xpath, document);
8091
if(xPathResult) console.log('doc true')
8192
else console.log('doc false');
@@ -211,12 +222,15 @@ class Recorder {
211222
return;
212223
}
213224
this.attached = false;
214-
browserAppData.storage.local.set({
215-
recorded_actions: this.recorded_actions,
216-
}).then(()=>{
217-
this.idx = 0;
218-
this.recorded_actions = [];
219-
});
225+
console.log('saving recorded_actions from content file');
226+
// When there are 2 iframes. it saves 3 times. this is a temporary fix. Should be fixed properly
227+
if (this.recorded_actions.length > 0)
228+
browserAppData.storage.local.set({
229+
recorded_actions: this.recorded_actions,
230+
}).then(()=>{
231+
this.idx = 0;
232+
this.recorded_actions = [];
233+
});
220234

221235
for (let event_key in this.eventListeners) {
222236
var event_info = this.parse_the_event_key(event_key);

Apps/Web/aiplugin/background.js

Lines changed: 25 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,31 @@ const tabs = {};
33
const inspectFile = 'inspect.js';
44
const activeIcon = 'active-64.png';
55
const defaultIcon = 'small_logo.png';
6-
let zeuz_url = '__ZeuZ__UrL_maPP';
7-
let zeuz_key = '__ZeuZ__KeY_maPP';
6+
var zeuz_url;
7+
var zeuz_key;
8+
9+
fetch("data.json")
10+
.then(Response => Response.json())
11+
.then(data => {
12+
zeuz_url = data.zeuz_url;
13+
zeuz_key = data.zeuz_key;
14+
console.log(data);
15+
console.log(zeuz_url);
16+
console.log(zeuz_key);
17+
browserAppData.storage.local.set({
18+
url: zeuz_url,
19+
key: zeuz_key,
20+
},
21+
function() {
22+
console.log("Logged in successfully!");
23+
});
24+
});
825

926
function logout() {
1027
browserAppData.storage.local.remove(['key'], function() {
1128
alert("Logged out successfully!");
1229
});
1330
}
14-
browserAppData.contextMenus.create({
15-
title: "Logout",
16-
contexts: ["all"],
17-
// onclick: logout,
18-
id: "zeuz_inspector"
19-
});
2031

2132
browserAppData.contextMenus.onClicked.addListener(logout);
2233

@@ -66,123 +77,11 @@ function toggle(tab) {
6677

6778
if (!isSupportedProtocolAndFileType(tab.url)) return;
6879

69-
if (!tabs[tab.id]) {
70-
// tabs[tab.id] = Object.create(inspect);
71-
// inspect.toggleActivate(tab.id, 'activate', activeIcon);
72-
73-
// check key exists
74-
browserAppData.storage.local.get(['key'], function(result) {
75-
// console.log('Value currently is ' + result.key);
76-
77-
if (result.key != null) {
78-
// activate
79-
tabs[tab.id] = Object.create(inspect);
80-
inspect.toggleActivate(tab.id, 'activate', activeIcon);
81-
} else {
82-
if (zeuz_url.startsWith('__ZeuZ__UrL_maP'))
83-
var server_url = prompt("Please enter your ZeuZ server address", "");
84-
else
85-
var server_url = zeuz_url;
86-
if (zeuz_key.startsWith('__ZeuZ__KeY_maP'))
87-
var api_key = prompt("Please enter your API key", "");
88-
else
89-
var api_key = zeuz_key;
90-
91-
var verify_status;
92-
var verify_token;
93-
94-
if (server_url != null && api_key != null) {
95-
96-
//process the url
97-
98-
var lastChar = server_url.substr(server_url.length - 1);
99-
if (lastChar == "/") {
100-
server_url = server_url.slice(0, -1); // remove last char '/'
101-
}
102-
103-
if (server_url.startsWith("http") == false) {
104-
105-
if ((server_url.indexOf("localhost") != -1) || (server_url.indexOf("127.0.0.1") != -1) || (server_url.indexOf("0.0.0.0") != -1)) {
106-
server_url = "http://" + server_url; // add http:// in the beginning
107-
} else {
108-
server_url = "https://" + server_url; // add http:// in the beginning
109-
}
110-
111-
}
112-
113-
if (zeuz_key.startsWith('__ZeuZ__KeY_maP')) {
114-
var xhr = new XMLHttpRequest();
115-
xhr.withCredentials = true;
116-
xhr.addEventListener("readystatechange", function() {
117-
if (this.readyState === 4) {
118-
console.log(this.responseText);
119-
120-
verify_status = this.status;
121-
verify_token = this.responseText;
122-
123-
// show message for verification
124-
if (verify_status === 200) {
125-
126-
if (verify_token === null) {
127-
alert("Sorry! Api key is wrong.");
128-
} else {
129-
// save server url and api key
130-
// browserAppData.storage.local.set({ url: server_url ,key: api_key }, function () {
131-
browserAppData.storage.local.set({
132-
url: server_url,
133-
key: JSON.parse(this.responseText).token
134-
},
135-
function() {
136-
console.log('Value is set to ', server_url, this.responseText);
137-
if (zeuz_url.startsWith('__ZeuZ__UrL_maP'))
138-
alert("Logged in successfully!");
139-
else
140-
console.log("Logged in successfully!");
141-
});
142-
143-
// activate plugin
144-
tabs[tab.id] = Object.create(inspect);
145-
inspect.toggleActivate(tab.id, 'activate', activeIcon);
146-
}
147-
148-
} else if ((verify_status === 403) || (verify_status === 0)) {
149-
alert("Sorry! Server URL is incorrect.");
150-
} else if (verify_status === 404) {
151-
alert("Sorry! Api key is incorrect.");
152-
} else {
153-
alert("Sorry! Server url/key is incorrect.");
154-
}
155-
156-
157-
}
158-
});
159-
xhr.open("GET", server_url + "/api/auth/token/verify?api_key=" + api_key);
160-
xhr.send();
161-
} else {
162-
browserAppData.storage.local.set({
163-
url: server_url,
164-
key: zeuz_key,
165-
},
166-
function() {
167-
console.log("Logged in successfully!");
168-
});
169-
170-
// activate plugin
171-
tabs[tab.id] = Object.create(inspect);
172-
inspect.toggleActivate(tab.id, 'activate', activeIcon);
173-
}
174-
175-
176-
} else {
177-
alert("Sorry! Server url/key cannot be empty.");
178-
}
179-
180-
}
181-
182-
});
183-
184-
185-
} else {
80+
if (!tabs[tab.id]){
81+
tabs[tab.id] = Object.create(inspect);
82+
inspect.toggleActivate(tab.id, 'activate', activeIcon);
83+
}
84+
else {
18685
// deactivate plugin
18786
inspect.toggleActivate(tab.id, 'deactivate', defaultIcon);
18887
for (const tabId in tabs) {
@@ -238,7 +137,7 @@ browserAppData.runtime.onMessage.addListener(
238137
function(request, sender, sendResponse) {
239138
if (request.apiName == 'ai_record_single_action') {
240139
var url = `${zeuz_url}/ai_record_single_action/`
241-
140+
console.log("zeuz_key", zeuz_key)
242141
fetch(url, {
243142
method: "POST",
244143
headers: {

Apps/Web/aiplugin/data.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"zeuz_url": "__ZeuZ__UrL_maPP",
3+
"zeuz_key": "__ZeuZ__KeY_maPP"
4+
}

Apps/Web/aiplugin/inspect.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ class Inspector {
9494
while (elements[0])
9595
elements[0].parentNode.removeChild(elements[0])
9696

97+
// get all <link> elements from div
98+
var elements = html.getElementsByTagName('link');
99+
while (elements[0])
100+
elements[0].parentNode.removeChild(elements[0])
101+
97102
// get all <script> elements from div
98103
var elements = html.getElementsByTagName('script');
99104
while (elements[0])

Framework/Built_In_Automation/Web/Selenium/BuiltInFunctions.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -460,15 +460,12 @@ def set_extension_variables():
460460
url = ConfigModule.get_config_value("Authentication", "server_address").strip()
461461
apiKey = ConfigModule.get_config_value("Authentication", "api-key").strip()
462462
jwtKey = CommonUtil.jwt_token.strip()
463-
with open(Path(aiplugin_path) / "background.js") as file:
464-
text = file.read()
465-
# if "__ZeuZ__UrL_maPP" in text or "__ZeuZ__KeY_maPP" in text:
466-
with open(Path(aiplugin_path) / "background.js", "w") as file:
467-
zeuz_url_var_idx = text.find("let zeuz_url = ")
468-
zeuz_url_var = text[zeuz_url_var_idx:zeuz_url_var_idx+text[zeuz_url_var_idx:].find("\n")]
469-
zeuz_key_var_idx = text.find("let zeuz_key = ")
470-
zeuz_key_var = text[zeuz_key_var_idx:zeuz_key_var_idx+text[zeuz_key_var_idx:].find("\n")]
471-
file.write(text.replace(zeuz_url_var, f"let zeuz_url = '{url}';", 1).replace(zeuz_key_var, f"let zeuz_key = '{apiKey}';", 1))
463+
with open(Path(aiplugin_path) / "data.json", "w") as file:
464+
json.dump({
465+
"zeuz_url": url,
466+
"zeuz_key": apiKey
467+
}, file, indent=4)
468+
472469
except:
473470
return CommonUtil.Exception_Handler(sys.exc_info(), None, "Could not load inspector extension")
474471

@@ -482,11 +479,7 @@ def set_extension_variables():
482479
with open(Path(ai_recorder_path) / "panel" / "index.html", "w") as file:
483480
# html = re.compile(r'^(\s*)', re.MULTILINE).sub(r'\1' * 4, soup.prettify())
484481
file.write(html)
485-
with open(Path(ai_recorder_path) / "background" / "back.js") as file:
486-
recorder_back_js_text = file.read()
487-
with open(Path(ai_recorder_path) / "background" / "back.js", "w") as file:
488-
metaData_idx = recorder_back_js_text.find("const metaData = ")
489-
metaData_var = recorder_back_js_text[metaData_idx:metaData_idx+recorder_back_js_text[metaData_idx:].find("\n")]
482+
with open(Path(ai_recorder_path) / "background" / "data.json", "w") as file:
490483
metaData = {
491484
"testNo": CommonUtil.current_tc_no,
492485
"testName": CommonUtil.current_tc_name,
@@ -496,7 +489,7 @@ def set_extension_variables():
496489
"apiKey": apiKey,
497490
"jwtKey": jwtKey,
498491
}
499-
file.write(recorder_back_js_text.replace(metaData_var, f"const metaData = {metaData};", 1))
492+
json.dump(metaData, file, indent=4)
500493
except:
501494
return CommonUtil.Exception_Handler(sys.exc_info(), None, "Could not load recorder extension")
502495

0 commit comments

Comments
 (0)