How to use findPrevElement method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

dictionary-select.js

Source: dictionary-select.js Github

copy
1function loadDictionary(){
2    var url = $("#urlResource").val();
3    var currentContent = $(".dict-load").find("fieldset").not(":hidden");
4    $(currentContent).find("select.autoload").not(":hidden").each(function(){
5        if($(this).data("dictionary")){
6            if($(this).val() == null){
7                var dict = $(this).data("dictionary");
8                var data = { dictionary : dict };
9                loadJsonSelect(url, data, this);
10            }
11        }
12    });
13
14    if($(currentContent).find("select[name*='region']").not(":hidden").not(".autoload").length > 0){
15        $(currentContent).find("select[name*='region']").not(":hidden").trigger("change");
16    }
17}
18
19
20var chooserMessage = {
21    "uk_UA" : { message:"Немає збігів!" },
22    "ru_RU" : { message:"Нет совпадений!"  },
23    "en_US" : { message:"Nothing found!" }
24};
25
26function selectEvents(){
27    var $dict = $(".dict-load");
28    $dict.on("change", "select[name*='region']", function(){
29        getDistrict(this);
30    });
31    $dict.on("change", "select[data-dictionary='fidoDistrict']", function(){
32        getLocalityType(this)
33    });
34    $dict.on("change", "select[data-dictionary='fidoLocalityType']", function(){
35        getLocalityName(this)
36    });
37    $dict.on("change", "select[data-dictionary='fidoLocalityName']", function(){
38        getStreetType(this);
39    });
40    $dict.on("change", "select[data-dictionary='fidoStreetType']", function(){
41        getStreet(this)
42    });
43    $dict.on("change","select[data-dictionary='fidoStreet']", function(e){
44        getZipCode(e.target);
45    });
46}
47
48function offSelectEvents(){
49    var $dict = $(".dict-load");
50    $dict.off("change", "select[name*='region']");
51    $dict.off("change", "select[data-dictionary='fidoDistrict']");
52    $dict.off("change", "select[data-dictionary='fidoLocalityType']");
53    $dict.off("change", "select[data-dictionary='fidoLocalityName']");
54    $dict.off("change", "select[data-dictionary='fidoStreetType']");
55    $dict.off("change", "select[data-dictionary='fidoStreet']");
56    $dict.off("change", "select[data-dictionary='fidoDepartmentsCities']");
57    $dict.off("change", "select#locationName");
58    $dict.off("change", "select#locationDelivery");
59}
60
61function findNextElement(current, find){
62    var element;
63    var parents = $(current).parents(".regRow").nextAll();
64    for(var i=0; i < parents.length; i++){
65        if($(parents[i]).find(find).length > 0){
66            return $(parents[i]).find(find);
67        }
68    }
69    return element;
70}
71
72function findPrevElement(current, find){
73    var element;
74    var parents = $(current).parents(".regRow").prevAll();
75    for(var i=0; i < parents.length; i++){
76        if($(parents[i]).find(find).length > 0){
77            element = $(parents[i]).find(find);
78            break;
79        }
80    }
81    return element;
82}
83
84function getDistrict(region){
85    if($(region).val() != null){
86        var select = findNextElement(region, "select[data-dictionary='fidoDistrict']");
87        var data = {
88            region : $(region).val()
89        };
90        initSelect(select, data, $(region).val());
91    }
92}
93
94function getLocalityType(district){
95    if($(district).val() != null){
96        var select = findNextElement(district, "select[data-dictionary='fidoLocalityType']");
97        var data = {
98            district : $(district).val()
99        };
100        initSelect(select, data, $(district).val());
101    }
102}
103
104function getLocalityName(localityType){
105    if($(localityType).val() != null){
106        var select = findNextElement(localityType, "select[data-dictionary='fidoLocalityName']");
107        var fidoDistrict = findPrevElement(localityType, "select[data-dictionary='fidoDistrict']");
108        var data = {
109            localityType : $(localityType).val(),
110            district : $(fidoDistrict).val()
111        };
112        initSelect(select,data, $(localityType).val() + $(fidoDistrict).val());
113    }
114}
115
116function getStreetType(localityName){
117    if($(localityName).val() != null){
118        var select = findNextElement(localityName, "select[data-dictionary='fidoStreetType']");
119        var data = {
120            localityName : $(localityName).val()
121        };
122        initSelect(select,data, $(localityName).val());
123    }
124}
125
126function getStreet(streetType){
127    if($(streetType).val() != null){
128        var select = findNextElement(streetType, "select[data-dictionary='fidoStreet']");
129        var fidoLocalityName = findPrevElement(streetType, "select[data-dictionary='fidoLocalityName']");
130        var data = {
131            streetType : $(streetType).val(),
132            localityName : $(fidoLocalityName).val()
133        };
134        initSelect(select,data, $(streetType).val() + $(fidoLocalityName).val());
135    }
136}
137
138function getZipCode(street){
139    if($(street).val() != null){
140        var select = findNextElement(street, "select[data-dictionary='fidoZipCode']");
141        var data = {
142            street : $(street).val()
143        };
144        initSelect(select,data, $(street).val());
145    }
146}
147
148//for event or simple call
149function initSelect(select, newData, parentId){
150    var url = $("#urlResource").val();
151    if(select.length > 0 && $(select).not(":hidden")){
152        if($(select).data("dictionary")){
153            if(parentId != null){
154                var old = $(select).data("parentId");
155                if(!!old){
156                    if(old == parentId){
157                        return;
158                    }
159                }
160                $(select).data("parentId", parentId);
161            }
162            var dict = $(select).data("dictionary");
163            var data =  $.extend({ dictionary : dict }, newData);
164            loadJsonSelect(url, data, select);
165        }
166    }
167}
168
169function canLoad($select){
170    var influence, influence2;
171    if($select.data("dictionary").indexOf('fidoDistrict') > -1){
172       influence = findPrevElement($select, "select[name*='region']");
173       return !!influence.val();
174    }else if($select.data("dictionary").indexOf('fidoLocalityType') > -1){
175        influence = findPrevElement($select, "select[data-dictionary='fidoDistrict']");
176        return !!influence.val();
177    }else if($select.data("dictionary").indexOf('fidoLocalityName') > -1){
178        influence = findPrevElement($select, "select[data-dictionary='fidoLocalityType']");
179        influence2 = findPrevElement($select, "select[data-dictionary='fidoDistrict']");
180        return !!influence.val() && !!influence2.val();
181    }else if($select.data("dictionary").indexOf('fidoStreetType') > -1){
182        influence = findPrevElement($select, "select[data-dictionary='fidoLocalityName']");
183        return !!influence.val();
184    }else if($select.data("dictionary").indexOf('fidoStreet') > -1 && $select.data("dictionary").indexOf('fidoStreetType') == -1){
185        influence = findPrevElement($select, "select[data-dictionary='fidoStreetType']");
186        influence2 = findPrevElement($select, "select[data-dictionary='fidoLocalityName']");
187        return !!influence.val() && !!influence2.val();
188    }else if($select.data("dictionary").indexOf('fidoZipCode') > -1){
189        influence = findPrevElement($select, "select[data-dictionary='fidoStreet']");
190        return !!influence.val();
191    }
192    return true;
193}
194
195function loadJsonSelect(url, data, select){
196    var $select = $(select);
197    if($select.parents("fieldset")){
198        $select.parents("fieldset").addClass("loader");
199    }
200    var locale = $(".dict-load").parents("form").data("locale");
201    if(canLoad($select)){
202        $.ajaxQueue({
203            type:"POST"
204            ,url: url
205            ,data:data
206        }).done(function(dataJson){
207                $select.find("option").remove();
208                for(var i = 0; i < dataJson.length; i++){
209                    var label = dataJson[i].value;
210                    if(dataJson[i].value == ""){
211                        label = "---";
212                    }
213                    $select.append($("<option></option>").attr("value", dataJson[i].id).text(label));
214                }
215                if(!!$select.data("selected")){
216                    if($select.data("selected") != "")
217                        $select.val($select.data("selected"));
218                }
219                if($select.hasClass("sel")){
220                    $select.trigger("refresh");
221                }else if($select.hasClass("chose") != ""){
222                    var chosen = $select.data('chosen');
223                    if(chosen){
224                        chosen.destroy();
225                    }
226                    $select.chosen({
227                        width:"100%",
228                        no_results_text: chooserMessage[locale].message
229                    });
230                }
231                if($select.attr("name").indexOf('region') > -1){
232                    $select.trigger("change");
233                }else if($select.data("dictionary").indexOf('fidoDistrict') > -1){
234                    $select.trigger("change");
235                }else if($select.data("dictionary").indexOf('fidoLocalityType') > -1){
236                    $select.trigger("change");
237                }else if($select.data("dictionary").indexOf('fidoLocalityName') > -1){
238                    $select.trigger("change");
239                }else if($select.data("dictionary").indexOf('fidoStreetType') > -1){
240                    $select.trigger("change");
241                }else if($select.data("dictionary").indexOf('fidoStreet') > -1 && $select.data("dictionary").indexOf('fidoStreetType') == -1){
242                    $select.trigger("change");
243                }
244                $select.parents("fieldset").removeClass("loader");
245            }
246        );
247    }
248}
249
Full Screen

linkList.js

Source: linkList.js Github

copy
1
2
3class Node {
4  constructor(element) {
5    this.element = element
6    this.next = null
7  }
8}
9class LinkList {
10  constructor() {
11    // this.sentry = new Node('header sentry') 哨兵
12    this.head = new Node('header sentry')
13  }
14
15  findByValue (element) {
16    let nextNode = this.head.next
17
18    while(nextNode && nextNode.element) {
19      if (nextNode.element === element) {
20        return nextNode
21      }
22      nextNode = nextNode.next
23    }
24  }
25
26  findPrev (element) {
27    let currentElement = this.head
28    while(currentElement.next && currentElement.next.element) {
29      if (currentElement.next.element === element) {
30        return currentElement
31      }
32      currentElement = currentElement.next
33    }
34    return currentElement ? currentElement : null
35  } 
36
37  findByIndex (num) {
38
39  }
40  append (element) {
41    // 找到链尾 next = null
42    let currentNode = this.head
43    while(currentNode.next) {
44      currentNode = currentNode.next
45    }
46    currentNode.next = new Node(element)
47  }
48  insert (newElement, currentELement) {
49    // 找到 currentELement , newElement插到currentELement之后
50    const findNode = this.findByValue(currentELement)
51    if (findNode) {
52      const newElementNode = new Node(newElement)
53      newElementNode.next = findNode.next
54      findNode.next = newElementNode
55    }
56
57  }
58  remove (element) {
59     // 找到 当前节点  prev 节点
60    const findNode = this.findByValue(element)
61    console.log(findNode)
62    if (findNode) {
63      // 找到  prevELement
64
65      const findPrevELement = this.findPrev(element)
66      if (findPrevELement) {
67        findPrevELement.next = findNode.next
68      }
69    }
70
71  }
72  showListElement (revelList) {
73    let currentNode = revelList || this.head
74
75    while(currentNode.next) {
76      console.log(currentNode.element)
77      currentNode = currentNode.next
78    }
79  }
80
81  revel() {
82    let next = null
83    let pre = null
84    while(this.head) {
85      next = this.head.next
86      // 反转后的 this.head.next = this.head 
87      this.head.next = pre
88      // 把当前的节点存起来 反转后 作为反转后的next 也就是上一句逻辑
89      pre = this.head
90      // 一步一步将this.head向后推
91      this.head = next
92    }
93    return pre
94  }
95}
96
97const linkList = new LinkList()
98linkList.append('a')
99linkList.append('c')
100linkList.insert('d', 'a') // a c d
101// linkList.insert('e', 'd') // a c d e
102// linkList.insert('f', 'e') // a c d e f
103// linkList.remove('e') // a c d f
104// linkList.remove('f') // a c d
105// linkList.insert('g', 'e') // a c d
106// linkList.insert('h', 'd') // a c d h
107const revel = linkList.revel()
108linkList.showListElement(revel)
Full Screen

manager.js

Source: manager.js Github

copy
1/* global angular, module, utils, moduleName */
2module.service('focusManager', function (focusQuery, focusDispatcher) {
3
4    var scope = this,
5        dispatcher = focusDispatcher();
6
7    /**
8     * Set the focus to a particular element
9     * @param el
10     * @returns {*|activeElement}
11     */
12    function focus(el) {
13        if (typeof el === 'undefined') {
14            return scope.activeElement;
15        }
16
17        if (scope.activeElement !== el) {
18            var eventObj = {
19                'oldTarget': scope.activeElement,
20                'newTarget': el
21            };
22
23            //if(scope.activeElement) {
24            //    scope.activeElement.setAttribute('tabindex', '-1');
25            //}
26
27            dispatcher.trigger('focusout', eventObj);
28
29            scope.activeElement = el;
30
31            if (el) {
32                el.focus();
33            }
34
35            dispatcher.trigger('focusin', eventObj);
36        }
37    }
38
39    function getActiveElement() {
40        return focusQuery.getElement(focusQuery.getElementId(scope.activeElement));
41    }
42
43    function canReceiveFocus(el) {
44        return focusQuery.canReceiveFocus(el);
45    }
46
47    /**
48     * Public interface for invoking the next focus element
49     */
50    function next() {
51        var groupId, elementId;
52        var activeElement = getActiveElement();
53
54        if (activeElement) {
55            groupId = focusQuery.getParentId(activeElement);
56            elementId = focusQuery.getElementId(activeElement);
57            findNextElement(groupId, elementId);
58        } else {
59            findNextElement();
60        }
61    }
62
63    /**
64     * Public interface for invoking the previous focus element
65     */
66    function prev() {
67        var groupId, elementId;
68        var activeElement = getActiveElement();
69
70        if (activeElement) {
71            groupId = focusQuery.getParentId(activeElement);
72            elementId = focusQuery.getElementId(activeElement);
73            findPrevElement(groupId, elementId);
74        } else {
75            findPrevElement();
76        }
77    }
78
79    /**
80     * Internal implementation of indexOf for arrays
81     * @param list
82     * @param item
83     * @returns {number}
84     */
85    function getElementIndex(list, item) {
86        var i = 0, len = list.length;
87
88        while (i < len) {
89            if (list[i] === item) {
90                return i;
91            }
92            i += 1;
93        }
94        return -1;
95    }
96
97    /**
98     * Searches list of elements and finds the next best element
99     * @param elements
100     * @param elementId
101     * @returns {*}
102     */
103    function getPrevElement(elements, elementId) {
104        var element, index;
105
106        if (elements && elements.length) {
107            if (elementId) {
108                element = focusQuery.getElement(elementId);
109                index = getElementIndex(elements, element);
110                if (index > 0) {
111                    return elements[index - 1];
112                }
113            } else {
114                return elements[elements.length - 1];
115            }
116        }
117    }
118
119    /**
120     * Searches list of groups and finds the next best groups
121     * @param groups
122     * @param groupId
123     * @returns {*}
124     */
125    function getPrevGroup(groups, groupId) {
126        var group, index;
127
128        if (groups && groups.length) {
129            if (groupId) {
130                group = focusQuery.getGroup(groupId);
131                index = getElementIndex(groups, group);
132                if (index > 0) {
133                    return groups[index - 1];
134                }
135            } else {
136                return groups[0];
137            }
138        }
139    }
140
141    /**
142     * Searches list of elements and finds the next best element
143     * @param elements
144     * @param elementId
145     * @returns {*}
146     */
147    function getNextElement(elements, elementId) {
148        var element, index;
149
150        if (elements && elements.length) {
151            if (elementId) {
152                element = focusQuery.getElement(elementId);
153                index = getElementIndex(elements, element);
154                if (index !== -1 && index + 1 < elements.length) {
155                    return elements[index + 1];
156                }
157            } else {
158                return elements[0];
159            }
160        }
161    }
162
163    /**
164     * Searches list of elements and finds the next best group
165     * @param groups
166     * @param groupId
167     * @returns {*}
168     */
169    function getNextGroup(groups, groupId) {
170        var group, index;
171
172        if (groups) {
173            group = focusQuery.getGroup(groupId);
174            index = getElementIndex(groups, group);
175            if (index !== -1 && index + 1 < groups.length) {
176                return groups[index + 1];
177            }
178        }
179    }
180
181    /**
182     * Finds the next available group. Once a group is found it search for an element to set focus to.
183     * If no group is found, it will go up the parent groups to find another group. If no groupId is
184     * passed in it will find the first isolate group encountered.
185     *
186     * @param parentGroupId
187     * @param groupId
188     */
189    function findNextGroup(parentGroupId, groupId) {
190        var group, groups, nextGroup, nextGroupId, parentGroup, grandparentGroupId, hasTail;
191        group = focusQuery.getGroup(groupId);
192        hasTail = focusQuery.hasGroupTail(group);
193
194        if (hasTail || !parentGroupId) {
195            findNextStep(groupId);
196        } else {
197            parentGroupId = focusQuery.getParentGroupId(group);
198            groups = focusQuery.getChildGroups(parentGroupId);
199            nextGroup = getNextGroup(groups, groupId);
200            if (nextGroup) {
201                nextGroupId = focusQuery.getGroupId(nextGroup);
202                return findNextElement(nextGroupId);
203            } else {
204                // no next group go up, if there is not a parentGroup, we are at the top of the isolate group
205                parentGroup = focusQuery.getGroup(parentGroupId);
206                grandparentGroupId = focusQuery.getParentGroupId(parentGroup);
207                return findNextGroup(grandparentGroupId, parentGroupId);
208            }
209        }
210    }
211
212    /**
213     * Check if the element should be stopped, looped or released
214     * @param groupId
215     */
216    function findNextStep(groupId) {
217        var group, tail;
218        group = focusQuery.getGroup(groupId);
219        tail = focusQuery.getGroupTail(group);
220        if (groupId) {
221            if (tail === 'stop') {
222                return; // do nothing
223            }
224            if (!tail) {
225                disable();
226                return;
227            }
228        } else {
229            groupId = focusQuery.getFirstGroupId();
230        }
231        // loop
232        findNextElement(groupId);
233    }
234
235    /**
236     * Searches for a child group within another group. Once a group has been
237     * found, search for an element in group. If no group found, go up parent
238     * groups to find another group.
239     *
240     * @param groupId
241     */
242    function findNextChildGroup(groupId) {
243        var groups, group, nextGroupId, parentGroupId;
244
245        groups = focusQuery.getChildGroups(groupId);
246        if (groups.length) {
247            nextGroupId = focusQuery.getGroupId(groups[0]);
248            findNextElement(nextGroupId);
249        } else {
250            // there are no child groups, go back up parent chain
251            group = focusQuery.getGroup(groupId);
252            parentGroupId = focusQuery.getParentGroupId(group);
253            findNextGroup(parentGroupId, groupId);
254        }
255    }
256
257    /**
258     * Searches for an element within a group. If an element is found, place focus on element. If no element found,
259     * start traversing through child groups. If a groupId is not passed in, the first available isolate group will
260     * be used.
261     * @param groupId
262     * @param elementId
263     */
264    function findNextElement(groupId, elementId) {
265        var els, nextElement;
266
267        if (groupId) {
268            els = focusQuery.getGroupElements(groupId);
269            nextElement = getNextElement(els, elementId);
270            if (nextElement) {
271                // focus on next element
272                if (scope.callback) {
273                    scope.callback(nextElement);
274                } else {
275                    focus(nextElement);
276                }
277            } else {
278                // there are no focus elements, go to next child focus group
279                findNextChildGroup(groupId);
280            }
281        } else {
282            // no group defined, find a group
283            findNextGroup();
284        }
285    }
286
287    /**
288     * Finds the next available group. Once a group is found it will search for the child group. If no child group
289     * is found, it will then search for elements within the group. If no elements are found, it will go back up
290     * parent groups to find next available group.
291     *
292     * @param ParentGroupId
293     * @param groupId
294     */
295    function findPrevGroup(ParentGroupId, groupId) {
296        var groups, prevGroup, prevGroupId, parentParentGroup, parentParentGroupId;
297
298        if (ParentGroupId) {
299            groups = focusQuery.getChildGroups(ParentGroupId);
300            prevGroup = getPrevGroup(groups, groupId);
301            if (prevGroup) {
302                // found group, now find child group
303                prevGroupId = focusQuery.getGroupId(prevGroup);
304                findPrevChildGroup(prevGroupId);
305            } else {
306                // otherwise find an element in this ParentGroup
307                findPrevElement(ParentGroupId);
308            }
309        } else {
310            // find the top most isolated group and start traversing through its child groups
311            groupId = focusQuery.getLastGroupId();
312            findPrevChildGroup(groupId);
313        }
314    }
315
316    /**
317     * Finds the child group within another group. Once group is found, it will search for elements within a group.
318     * @param groupId
319     */
320    function findPrevChildGroup(groupId) {
321        var groups, childGroupId;
322
323        if (groupId) {
324            groups = focusQuery.getChildGroups(groupId);
325            if (groups.length) {
326                childGroupId = focusQuery.getGroupId(groups[groups.length - 1]);
327                findPrevChildGroup(childGroupId);
328            } else {
329                findPrevElement(groupId);
330            }
331        } else {
332            findPrevGroup();
333        }
334    }
335
336    /**
337     * Finds an next available element within a group. If an element is found, focus will be set.
338     * @param groupId
339     * @param elementId
340     */
341    function findPrevElement(groupId, elementId) {
342        var els, prevEl, group, hasHead;
343        if (groupId) {
344            els = focusQuery.getGroupElements(groupId);
345            prevEl = getPrevElement(els, elementId);
346            if (prevEl) {
347                // set focus to next element
348                if (scope.callback) {
349                    scope.callback(prevEl);
350                } else {
351                    focus(prevEl);
352                }
353            } else {
354                findPrevStep(groupId);
355            }
356        } else {
357            findPrevChildGroup();
358        }
359    }
360
361    /**
362     * Finds the previous step within the group using a head.
363     * @param groupId
364     */
365    function findPrevStep(groupId) {
366        var ParentGroupId, group, hasHead;
367        group = focusQuery.getGroup(groupId);
368        hasHead = focusQuery.hasGroupHead(group);
369        ParentGroupId = focusQuery.getParentGroupId(group);
370        // if there is a parent group then go to it, otherwise check for a loop
371        if (hasHead || !ParentGroupId) {
372            // if there was no parent group, we are at the top of the isolate group
373            var head = focusQuery.getGroupHead(group);
374            if (head === 'loop') {
375                findPrevChildGroup(groupId);
376            } else if (!head) {
377                disable();
378            }
379        } else {
380            findPrevGroup(ParentGroupId, groupId);
381        }
382    }
383
384    function on() {
385        scope.active = true;
386    }
387
388    function off() {
389        scope.active = false;
390    }
391
392    function enable() {
393        if (!scope.enabled && scope.active) {
394            scope.enabled = true;
395            scope.activeElement = document.activeElement;
396            dispatcher.trigger('enabled');
397        }
398    }
399
400    function disable() {
401        if (scope.enabled) {
402            scope.enabled = false;
403            dispatcher.trigger('disabled');
404        }
405    }
406
407    // :: Public API :: //
408    scope.active = true;
409    scope.enabled = true;
410    scope.activeElement = null;
411
412    scope.focus = focus;
413    scope.prev = prev;
414    scope.next = next;
415    scope.on = on;
416    scope.off = off;
417
418    // :: Protected API :: //
419    scope.getActiveElement = getActiveElement;
420    scope.findPrevChildGroup = findPrevChildGroup;
421    scope.findNextElement = findNextElement;
422    scope.canReceiveFocus = canReceiveFocus;
423    scope.enable = enable;
424    scope.disable = disable;
425
426    exports.manager = scope;
427});
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)