How to use defaultOnError 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.

db.js

Source: db.js Github

copy
1defaultOnsuccess = function(result) {
2    console.log(result);
3};
4
5defaultOnerror = function(error) {
6    alert(error);
7    throw error;
8};
9
10class DB {
11    constructor(onsuccess=defaultOnsuccess, onerror=defaultOnerror) {
12        const thisClass = this;
13        if (window.indexedDB == null) {
14            throw "This browser does not support IndexedDB!"
15        }
16        let request = window.indexedDB.open("leitner", 1);
17        request.onerror = function(event) {
18            onerror(event.target.error);
19        };
20        request.onupgradeneeded = function(event) {
21            thisClass.db = event.target.result;
22            switch(thisClass.db.version) {
23                case 1:
24                let objectStore = thisClass.db.createObjectStore("cards", {keyPath: "id", autoIncrement: true});
25                objectStore.createIndex("box", "box", {unique: false});
26                objectStore.createIndex("timestamp", "timestamp", {unique: false});
27                objectStore.createIndex("box_timestamp", ["box", "timestamp"], {unique: false});
28                break;
29            }
30            thisClass.db.onversionchange = function(event) {
31                thisClass.db.close();
32                alert("A new version of this page is ready. Please reload or close this tab!");
33            };
34        };
35        request.onsuccess = function(event) {
36            thisClass.db = event.target.result;
37            thisClass.db.onversionchange = function(event) {
38                thisClass.db.close();
39                alert("A new version of this page is ready. Please reload or close this tab!");
40            };
41            onsuccess("Opened.");
42        };
43    }
44
45    add(front, back, onsuccess=defaultOnsuccess, onerror=defaultOnerror) {
46        let transaction = this.db.transaction("cards", "readwrite");
47        let request = transaction.objectStore("cards").add({
48            timestamp: -getNow(),
49            box: 1,
50            front: front,
51            back: back
52        });
53        request.onerror = function(event) {
54            onerror(event.target.error);
55        };
56        request.onsuccess = function(event) {
57            let id = event.target.result;
58            onsuccess(id);
59        };
60        transaction.onabort = function(event) {
61            onerror(event.target.error);
62        };
63    }
64
65    getLastTimestamp(onsuccess=defaultOnsuccess) {
66        let objectStore = this.db.transaction("cards", "readonly").objectStore("cards");
67        objectStore.index("timestamp").openCursor().onsuccess = function (event) {
68            let cursor = event.target.result;
69            if (cursor) {
70                onsuccess(-cursor.value.timestamp);
71            } else {
72                onsuccess(0);
73            }
74        }
75    }
76
77    forEach(timestamp=0, oneach=defaultOnsuccess, oncomplete=function(){}) {
78        let objectStore = this.db.transaction("cards", "readonly").objectStore("cards");
79        objectStore.index("timestamp").openCursor(IDBKeyRange.upperBound(-timestamp, true), "prev").onsuccess = function (event) {
80            let cursor = event.target.result;
81            if (cursor) {
82                let card = cursor.value;
83                card.timestamp *= -1;
84                oneach(card);
85                cursor.continue();
86            } else {
87                oncomplete();
88            }
89        }
90    }
91
92    search(text, oneach=defaultOnsuccess, oncomplete=function(){}) {
93        text = text.toLowerCase()
94        let objectStore = this.db.transaction("cards", "readonly").objectStore("cards");
95        objectStore.index("box_timestamp").openCursor().onsuccess = function (event) {
96            let cursor = event.target.result;
97            if (cursor) {
98                let card = cursor.value;
99                card.timestamp *= -1;
100                if (text.length > 0) {
101                    if ((card.front + " " + card.back).toLowerCase().search(text) >= 0) {
102                        oneach(card);
103                    }
104                } else {
105                    oneach(card);
106                }
107                cursor.continue();
108            } else {
109                oncomplete();
110            }
111        }
112    }
113
114    findReview(onfind=defaultOnsuccess, oncomplete=null) {
115        var i = 0;
116        var isFound = false;
117        const now = getNow();
118        let objectStore = this.db.transaction("cards", "readonly").objectStore("cards");
119        objectStore.openCursor().onsuccess = function (event) {
120            let cursor = event.target.result;
121            if (cursor) {
122                let card = cursor.value;
123                card.timestamp *= -1;
124                let currentDiff = now - card.timestamp;
125                let mustDiff = 2 ** (card.box - 1) * 86400 - gconfg.softDiff;
126                if (card.box < (gconfg.maxBox + 1) && currentDiff >= mustDiff) {
127                    i++;
128                    if (!isFound) {
129                        isFound = true;
130                        onfind(card);
131                    }
132                    if (oncomplete) cursor.continue();
133                } else {
134                    cursor.continue();
135                }
136            } else {
137                oncomplete(i);
138            }
139        }
140    }
141
142    get(id, onsuccess=defaultOnsuccess, onerror=defaultOnerror) {
143        let objectStore = this.db.transaction("cards", "readonly").objectStore("cards");
144        let request = objectStore.get(id);
145        request.onerror = function(event) {
146            onerror(event.target.error);
147        };
148        request.onsuccess = function(event) {
149            let card = event.target.result;
150            if (card) {
151                card.timestamp *= -1;
152            }
153            onsuccess(card);
154        };
155    }
156
157    edit(id, front, back, onsuccess=defaultOnsuccess, onerror=defaultOnerror) {
158        let transaction = this.db.transaction("cards", "readwrite");
159        let objectStore = transaction.objectStore("cards");
160        let request = objectStore.get(id);
161        request.onerror = function(event) {
162            onerror(event.target.error);
163        };
164        request.onsuccess = function(event) {
165            let card = event.target.result;
166            if (card) {
167                card.front = front;
168                card.back = back;
169                card.timestamp = -getNow();
170                let putRequest = objectStore.put(card);
171                putRequest.onerror = function(event) {
172                    onerror(event.target.error);
173                };
174                transaction.oncomplete = function(event) {
175                    onsuccess("Edited.");
176                };
177            } else {
178                onerror("Card not found!");
179            }
180        };
181        transaction.onabort = function(event) {
182            onerror(event.target.error);
183        };
184    }
185
186    increaseBox(id, onsuccess=defaultOnsuccess, onerror=defaultOnerror) {
187        let transaction = this.db.transaction("cards", "readwrite");
188        let objectStore = transaction.objectStore("cards");
189        let request = objectStore.get(id);
190        request.onerror = function(event) {
191            onerror(event.target.error);
192        };
193        request.onsuccess = function(event) {
194            let card = event.target.result;
195            if (card) {
196                if (card.box < 6) {
197                    card.box += 1;
198                    card.timestamp = -getNow();
199                    let putRequest = objectStore.put(card);
200                    putRequest.onerror = function(event) {
201                        onerror(event.target.error);
202                    };
203                    transaction.oncomplete = function(event) {
204                        onsuccess("Increased.");
205                    };
206                }
207            } else {
208                onerror("Card not found!");
209            }
210        };
211        transaction.onabort = function(event) {
212            onerror(event.target.error);
213        };
214    }
215
216    reset(id, onsuccess=defaultOnsuccess, onerror=defaultOnerror) {
217        let transaction = this.db.transaction("cards", "readwrite");
218        let objectStore = transaction.objectStore("cards");
219        let request = objectStore.get(id);
220        request.onerror = function(event) {
221            onerror(event.target.error);
222        };
223        request.onsuccess = function(event) {
224            let card = event.target.result;
225            if (card) {
226                card.box = 1;
227                card.timestamp = -getNow();
228                let putRequest = objectStore.put(card);
229                putRequest.onerror = function(event) {
230                    onerror(event.target.error);
231                };
232                transaction.oncomplete = function(event) {
233                    onsuccess("Reset.");
234                };
235            } else {
236                onerror("Card not found!");
237            }
238        };
239        transaction.onabort = function(event) {
240            onerror(event.target.error);
241        };
242    }
243
244    remove(id, onsuccess=defaultOnsuccess, onerror=defaultOnerror) {
245        let transaction = this.db.transaction("cards", "readwrite");
246        let request = transaction.objectStore("cards").delete(id);
247        request.onerror = function(event) {
248            onerror(event.target.error);
249        };
250        transaction.onabort = function(event) {
251            onerror(event.target.error);
252        };
253        transaction.oncomplete = function(event) {
254            onsuccess("Removed.");
255        };
256    }
257
258    update(cards, onsuccess=defaultOnsuccess, onerror=defaultOnerror) {
259        let transaction = this.db.transaction("cards", "readwrite");
260        let objectStore = transaction.objectStore("cards");
261        var i = 0;
262        for (let card of cards) {
263            card.timestamp *= -1;
264            let request = objectStore.get(card.id);
265            request.onerror = function(event) {
266                onerror(event.target.error);
267            };
268            request.onsuccess = function(event) {
269                let oldCard = event.target.result;
270                let need_update = true;
271                if (oldCard) {
272                    if (oldCard.timestamp < card.timestamp) {
273                        need_update = false;
274                    }
275                }
276                if (need_update) {
277                    let putRequest = objectStore.put(card);
278                    putRequest.onerror = function(event) {
279                        onerror(event.target.error);
280                    };
281                    putRequest.onsuccess = function(event) {
282                        i++;
283                        if (i == cards.length) {
284                            onsuccess();
285                        }
286                    }
287                } else {
288                    i++;
289                    if (i == cards.length) {
290                        onsuccess();
291                    }
292                }
293            };
294        }
295        transaction.onabort = function(event) {
296            onerror(event.target.error);
297        };
298    }
299
300    import(cards, onsuccess=defaultOnsuccess, onerror=defaultOnerror) {
301        let transaction = this.db.transaction("cards", "readwrite");
302        let objectStore = transaction.objectStore("cards");
303        let request = objectStore.clear();
304        request.onerror = function(event) {
305            onerror(event.target.error);
306        };
307        request.onsuccess = function(event) {
308            var i = 0;
309            for (let card of cards) {
310                card.timestamp *= -1;
311                let request = objectStore.put(card);
312                request.onerror = function(event) {
313                    onerror(event.target.error);
314                };
315                request.onsuccess = function(event) {
316                    i++;
317                    if (i == cards.length) {
318                        onsuccess();
319                    }
320                }
321            }
322        };
323        transaction.onabort = function(event) {
324            onerror(event.target.error);
325        };
326    }
327}
328
Full Screen

axios.service.js

Source: axios.service.js Github

copy
1import axios from 'axios';
2import { HttpCodes } from '../shared';
3
4export default class AxiosService {
5	static instance;
6
7	constructor() {
8		if (AxiosService.instance) {
9			return AxiosService.instance;
10		}
11		AxiosService.instance = this;
12	}
13
14	defaultOnError = (err) => {
15		if (!err) {
16			return;
17		}
18		let ex = '';
19		if (err.status === HttpCodes.BAD_REQUEST) {
20			ex = 'got status 400 - bad request';
21			console.error(ex);
22			throw ex;
23		} else if (err.status === HttpCodes.UNAUTHORIZE) {
24			ex = 'got status 401 - unauthorized';
25			console.error(ex);
26			throw ex;
27		} else if (err.status === HttpCodes.NOT_FOUND) {
28			ex = 'got status 404 - not found';
29			console.error(ex);
30			throw ex;
31		}
32		console.error('Error: ', err);
33		throw ex;
34	};
35
36	defaultOnSuccess = (response) => {
37		// console.log('response ?', response);
38		return response;
39	};
40
41	get = async (route, params, onSuccess, onError) => {
42		if (!onSuccess) {
43			onSuccess = this.defaultOnSuccess;
44		}
45		if (!onError) {
46			onError = this.defaultOnError;
47		}
48		try {
49			const response = await axios.get(route, params);
50			return onSuccess(response.data);
51		} catch (error) {
52			onError(error);
53		}
54	};
55
56	post = async (route, params, onSuccess, onError) => {
57		if (!onSuccess) {
58			onSuccess = this.defaultOnSuccess;
59		}
60		if (!onError) {
61			onError = this.defaultOnError;
62		}
63		const options = {
64			headers: { 'Content-Type': 'application/json' },
65		};
66		try {
67			const response = await axios.post(route, params, options || {});
68			return onSuccess(response.data);
69		} catch (error) {
70			onError(error);
71		}
72	};
73
74	put = async (route, params, onSuccess, onError) => {
75		if (!onSuccess) {
76			onSuccess = this.defaultOnSuccess;
77		}
78		if (!onError) {
79			onError = this.defaultOnError;
80		}
81		const options = {
82			headers: { 'Content-Type': 'application/json' },
83		};
84		try {
85			const response = await axios.put(route, params, options || {});
86			return onSuccess(response.data);
87		} catch (error) {
88			onError(error);
89		}
90	};
91
92	delete = async (route, onSuccess, onError) => {
93		if (!onSuccess) {
94			onSuccess = this.defaultOnSuccess;
95		}
96		if (!onError) {
97			onError = this.defaultOnError;
98		}
99		try {
100			const response = await axios.delete(route);
101			return onSuccess(response.data);
102		} catch (error) {
103			onError(error);
104		}
105	};
106}
107
Full Screen

xbLibrary-pack.js

Source: xbLibrary-pack.js Github

copy
1
2if(!document.getElementById||navigator.userAgent.indexOf('Opera')!=-1)
3{window.onerror=defaultOnError;function defaultOnError(msg,url,line)
4{if(typeof(msg)!='string')
5{msg='unknown error';}
6if(typeof(url)!='string')
7{url=document.location;}
8alert('An error has occurred at '+url+', line '+line+': '+msg);}}
9function xbLibrary(path)
10{this.path=path;}
11xbLibrary.prototype.loadScript=function(scriptName)
12{document.write('<script language="javascript" src="'+scriptName+'"><\/script>');};xblibrary=new xbLibrary('./');
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)