-
Notifications
You must be signed in to change notification settings - Fork 1
/
nngjs.lib.js
178 lines (160 loc) · 7.93 KB
/
nngjs.lib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* @License: MIT 2017
* @Created: 16-01-2018 [ 12:10:00Hrs IST]
* @Author: Nitya Narayan Gautam [nityanarayan44]
* @Description: JavaScript Lib for General JavaScript operations
*/
(function(window){
// Set use ristrictions
'use strict';
// Global Instance
// --- [Main Code Container] ---
var nngjs = {
//--- [Global Vars Values] ---
highlightStyle : 'background-color: yellow; outline: 1px solid rgb(136, 255, 136);',
unHighlightStyle : '',
currentOngoingAjaxCallStatus : false,
/**
* =====================================
* [Utils] AJAX Interceptor
* =====================================
*/
/* This will intercept any ajax call,
and whenever any ajax call is active
or complete it will update the ajaxCall var status.
:: Although, User needs to register/call this function once,
in order to Start intercepting AJAX.
*/
interceptXHRCalls: function() {
let xhr = XMLHttpRequest.prototype.open;
// Insert the prototype method to monitor the ongoing ajax call.
XMLHttpRequest.prototype.open = function(){
this.currentOngoingAjaxCallStatus = true;
console.log('Any Currnet OnGoing AJAX Call ' + this.currentOngoingAjaxCallStatus);
this.addEventListener('load', function() {
this.currentOngoingAjaxCallStatus = false;
console.log('Any Currnet OnGoing AJAX Call ' + this.currentOngoingAjaxCallStatus);
//will always be 4 (ajax is completed successfully)
//console.log(this.readyState);
//whatever the response was
//console.log(this.responseText);
});
xhr.apply(this, arguments);
};
},
/*
*======================================
* [Page Scroll] General Operation
*======================================
*/
// To Bottom of the page
scrollDownToBottom: function() {
window.scrollTo(0, document.body.scrollHeight);
},
// To Top of the page
scrollDownToTop: function() {
window.scrollTo(0, 0);
},
// Reload the Page
refreshPage: function(){
window.location.href = window.location.href;
},
/*
*======================================
* Finding of Target / Element by XPaths
*======================================
*/
// Get Element with direct xpath
getElementWithXPath: function(xpath) {
return eval('$x("'+xpath+'")');
},
// Get the element that has exact string inside of it.
getElementThatHasString: function(txt) {
return eval('$x("//*[text()=\''+txt+'\']")');
},
// Get the element that just contains string inside of it.
getElementThatContainsString: function(txt) {
return eval('$x("//*[contains(text(), \''+txt+'\')]")');
},
// CSS Selector [Venilla]
getElementWithCssSelector: function(selector){
return $$(selector);
},
/*
*======================================
* Accessing the Target / Element Info
*======================================
*/
getElementCountWithTag: function(tagName){
return window.document.getElementsByTagName(tagName).length;
},
// CSS Selector [Venilla]
getElementCountWithCssSelector: function(selector){
return $$(selector).length;
},
// CSS Selector [Adding . for class]
getElementCountWithClass: function(selector){
return $$('.'+selector).length;
},
// CSS Selector [Adding # for id]
getElementCountWithId: function(selector){
return $$('#'+selector).length;
},
/*
*======================================
* Operations on Targets / Elements
*======================================
*/
// Click on first element, that matches with the criteria.
clickOnElementThatHasString: function(txt){
(this.getElementThatHasString(txt))[0].click();
},
clickOnElementThatContainsString: function(txt){
(this.getElementThatContainsString(txt))[0].click();
},
clickOnElementWithCssSelector: function(selector){
(this.getElementWithCssSelector(selector))[0].click();
},
// Click on nth element, that matches with the criteria.
clickOnElementThatHasString: function(txt, index){
(this.getElementThatHasString(txt))[index].click();
},
clickOnElementThatContainsString: function(txt, index){
(this.getElementThatContainsString(txt))[index].click();
},
clickOnElementWithCssSelector: function(selector, index){
(this.getElementWithCssSelector(selector))[index].click();
},
// Highlight the Element(s).
highlightElementWithCSSSelector: function(selector){
(this.getElementWithCssSelector(selector)).map(function(element){
element.setAttribute('style', nngjs.highlightStyle);
});
},
unHighlightElementWithCSSSelector: function(selector){
(this.getElementWithCssSelector(selector)).map(function(element){
element.setAttribute('style', nngjs.unHighlightStyle);
});
},
highlightElementWithXPath: function(xpath){
(this.getElementWithXPath(xpath)).map(function(element){
element.setAttribute('style', nngjs.highlightStyle);
});
},
unHighlightElementWithXPath: function(xpath){
(this.getElementWithXPath(xpath)).map(function(element){
element.setAttribute('style', nngjs.unHighlightStyle);
});
}
};/*End_Of_nngjs code*/
/*
*=========================================================
* [At Last]
* We need that our library is globally accesible,
* then we save in the window.
*=========================================================
*/
if(typeof(window.nngjs) === 'undefined'){
window.nngjs = nngjs;
}
})(window);