How to use checkSorting method in Puppeteer

Best JavaScript code snippet using puppeteer

Run Puppeteer automation tests on LambdaTest cloud grid

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

test_getCookieSince.js

Source: test_getCookieSince.js Github

copy
1const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
2const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
3
4const cs = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
5const cm = cs.QueryInterface(Ci.nsICookieManager);
6
7function setCookie(name, url) {
8  let value = `${name}=${Math.random()}; Path=/; Max-Age=1000; sameSite=none`;
9  info(`Setting cookie ${value} for ${url.spec}`);
10
11  let channel = NetUtil.newChannel({
12    uri: url,
13    loadUsingSystemPrincipal: true,
14    contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
15  });
16
17  cs.setCookieStringFromHttp(url, value, channel);
18}
19
20async function sleep() {
21  await new Promise(resolve => do_timeout(1000, resolve));
22}
23
24function checkSorting(cookies) {
25  for (let i = 1; i < cookies.length; ++i) {
26    Assert.greater(
27      cookies[i].creationTime,
28      cookies[i - 1].creationTime,
29      "Cookie " + cookies[i].name
30    );
31  }
32}
33
34add_task(async function() {
35  Services.prefs.setBoolPref(
36    "network.cookieJarSettings.unblocked_for_testing",
37    true
38  );
39
40  await setCookie("A", Services.io.newURI("https://example.com/A/"));
41  await sleep();
42
43  await setCookie("B", Services.io.newURI("https://foo.bar/B/"));
44  await sleep();
45
46  await setCookie("C", Services.io.newURI("https://example.org/C/"));
47  await sleep();
48
49  await setCookie("D", Services.io.newURI("https://example.com/D/"));
50  await sleep();
51
52  Assert.equal(cm.cookies.length, 4, "Cookie check");
53
54  const cookies = cm.getCookiesSince(0);
55  Assert.equal(cookies.length, 4, "We retrieve all the 4 cookies");
56  checkSorting(cookies);
57
58  let someCookies = cm.getCookiesSince(cookies[0].creationTime + 1);
59  Assert.equal(someCookies.length, 3, "We retrieve some cookies");
60  checkSorting(someCookies);
61
62  someCookies = cm.getCookiesSince(cookies[1].creationTime + 1);
63  Assert.equal(someCookies.length, 2, "We retrieve some cookies");
64  checkSorting(someCookies);
65
66  someCookies = cm.getCookiesSince(cookies[2].creationTime + 1);
67  Assert.equal(someCookies.length, 1, "We retrieve some cookies");
68  checkSorting(someCookies);
69
70  someCookies = cm.getCookiesSince(cookies[3].creationTime + 1);
71  Assert.equal(someCookies.length, 0, "We retrieve some cookies");
72});
73
Full Screen

test_word_boundary_search.js

Source: test_word_boundary_search.js Github

copy
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5/**
6 * Test to make sure matches against the url, title, tags are first made on word
7 * boundaries, instead of in the middle of words, and later are extended to the
8 * whole words. For this test it is critical to check sorting of the matches.
9 *
10 * Make sure we don't try matching one after a CamelCase because the upper-case
11 * isn't really a word boundary. (bug 429498)
12 */
13
14var katakana = ["\u30a8", "\u30c9"]; // E, Do
15var ideograph = ["\u4efb", "\u5929", "\u5802"]; // Nin Ten Do
16
17add_task(async function test_escape() {
18  Services.prefs.setBoolPref("browser.urlbar.autoFill.searchEngines", false);
19  Services.prefs.setBoolPref("browser.urlbar.autoFill", false);
20
21  await PlacesTestUtils.addVisits([
22    { uri: "http://matchme/", title: "title1" },
23    { uri: "http://dontmatchme/", title: "title1" },
24    { uri: "http://title/1", title: "matchme2" },
25    { uri: "http://title/2", title: "dontmatchme3" },
26    { uri: "http://tag/1", title: "title1" },
27    { uri: "http://tag/2", title: "title1" },
28    { uri: "http://crazytitle/", title: "[email protected]#$%^&*()_+{}|:<>?word" },
29    { uri: "http://katakana/", title: katakana.join("") },
30    { uri: "http://ideograph/", title: ideograph.join("") },
31    { uri: "http://camel/pleaseMatchMe/", title: "title1" },
32  ]);
33  await addBookmark({
34    uri: "http://tag/1",
35    title: "title1",
36    tags: ["matchme2"],
37  });
38  await addBookmark({
39    uri: "http://tag/2",
40    title: "title1",
41    tags: ["dontmatchme3"],
42  });
43
44  info("Match 'match' at the beginning or after / or on a CamelCase");
45  await check_autocomplete({
46    search: "match",
47    checkSorting: true,
48    matches: [
49      {
50        uri: "http://tag/1",
51        title: "title1",
52        tags: ["matchme2"],
53        style: ["bookmark-tag"],
54      },
55      { uri: "http://camel/pleaseMatchMe/", title: "title1" },
56      { uri: "http://title/1", title: "matchme2" },
57      { uri: "http://matchme/", title: "title1" },
58      {
59        uri: "http://tag/2",
60        title: "title1",
61        tags: ["dontmatchme3"],
62        style: ["bookmark-tag"],
63      },
64      { uri: "http://title/2", title: "dontmatchme3" },
65      { uri: "http://dontmatchme/", title: "title1" },
66    ],
67  });
68
69  info("Match 'dont' at the beginning or after /");
70  await check_autocomplete({
71    search: "dont",
72    checkSorting: true,
73    matches: [
74      {
75        uri: "http://tag/2",
76        title: "title1",
77        tags: ["dontmatchme3"],
78        style: ["bookmark-tag"],
79      },
80      { uri: "http://title/2", title: "dontmatchme3" },
81      { uri: "http://dontmatchme/", title: "title1" },
82    ],
83  });
84
85  info("Match 'match' at the beginning or after / or on a CamelCase");
86  await check_autocomplete({
87    search: "2",
88    checkSorting: true,
89    matches: [
90      {
91        uri: "http://tag/2",
92        title: "title1",
93        tags: ["dontmatchme3"],
94        style: ["bookmark-tag"],
95      },
96      {
97        uri: "http://tag/1",
98        title: "title1",
99        tags: ["matchme2"],
100        style: ["bookmark-tag"],
101      },
102      { uri: "http://title/2", title: "dontmatchme3" },
103      { uri: "http://title/1", title: "matchme2" },
104    ],
105  });
106
107  info("Match 't' at the beginning or after /");
108  await check_autocomplete({
109    search: "t",
110    checkSorting: true,
111    matches: [
112      {
113        uri: "http://tag/2",
114        title: "title1",
115        tags: ["dontmatchme3"],
116        style: ["bookmark-tag"],
117      },
118      {
119        uri: "http://tag/1",
120        title: "title1",
121        tags: ["matchme2"],
122        style: ["bookmark-tag"],
123      },
124      { uri: "http://camel/pleaseMatchMe/", title: "title1" },
125      { uri: "http://title/2", title: "dontmatchme3" },
126      { uri: "http://title/1", title: "matchme2" },
127      { uri: "http://dontmatchme/", title: "title1" },
128      { uri: "http://matchme/", title: "title1" },
129      { uri: "http://katakana/", title: katakana.join("") },
130      { uri: "http://crazytitle/", title: "[email protected]#$%^&*()_+{}|:<>?word" },
131    ],
132  });
133
134  info("Match 'word' after many consecutive word boundaries");
135  await check_autocomplete({
136    search: "word",
137    checkSorting: true,
138    matches: [{ uri: "http://crazytitle/", title: "[email protected]#$%^&*()_+{}|:<>?word" }],
139  });
140
141  info("Match a word boundary '/' for everything");
142  await check_autocomplete({
143    search: "/",
144    checkSorting: true,
145    matches: [
146      {
147        uri: "http://tag/2",
148        title: "title1",
149        tags: ["dontmatchme3"],
150        style: ["bookmark-tag"],
151      },
152      {
153        uri: "http://tag/1",
154        title: "title1",
155        tags: ["matchme2"],
156        style: ["bookmark-tag"],
157      },
158      { uri: "http://camel/pleaseMatchMe/", title: "title1" },
159      { uri: "http://ideograph/", title: ideograph.join("") },
160      { uri: "http://katakana/", title: katakana.join("") },
161      { uri: "http://crazytitle/", title: "[email protected]#$%^&*()_+{}|:<>?word" },
162      { uri: "http://title/2", title: "dontmatchme3" },
163      { uri: "http://title/1", title: "matchme2" },
164      { uri: "http://dontmatchme/", title: "title1" },
165      { uri: "http://matchme/", title: "title1" },
166    ],
167  });
168
169  info("Match word boundaries '()_' that are among word boundaries");
170  await check_autocomplete({
171    search: "()_",
172    checkSorting: true,
173    matches: [{ uri: "http://crazytitle/", title: "[email protected]#$%^&*()_+{}|:<>?word" }],
174  });
175
176  info("Katakana characters form a string, so match the beginning");
177  await check_autocomplete({
178    search: katakana[0],
179    checkSorting: true,
180    matches: [{ uri: "http://katakana/", title: katakana.join("") }],
181  });
182
183  /*
184  info("Middle of a katakana word shouldn't be matched");
185  await check_autocomplete({
186    search: katakana[1],
187    matches: [ ],
188  });
189*/
190
191  info("Ideographs are treated as words so 'nin' is one word");
192  await check_autocomplete({
193    search: ideograph[0],
194    checkSorting: true,
195    matches: [{ uri: "http://ideograph/", title: ideograph.join("") }],
196  });
197
198  info("Ideographs are treated as words so 'ten' is another word");
199  await check_autocomplete({
200    search: ideograph[1],
201    checkSorting: true,
202    matches: [{ uri: "http://ideograph/", title: ideograph.join("") }],
203  });
204
205  info("Ideographs are treated as words so 'do' is yet another word");
206  await check_autocomplete({
207    search: ideograph[2],
208    checkSorting: true,
209    matches: [{ uri: "http://ideograph/", title: ideograph.join("") }],
210  });
211
212  info("Match in the middle. Should just be sorted by frecency.");
213  await check_autocomplete({
214    search: "ch",
215    checkSorting: true,
216    matches: [
217      {
218        uri: "http://tag/2",
219        title: "title1",
220        tags: ["dontmatchme3"],
221        style: ["bookmark-tag"],
222      },
223      {
224        uri: "http://tag/1",
225        title: "title1",
226        tags: ["matchme2"],
227        style: ["bookmark-tag"],
228      },
229      { uri: "http://camel/pleaseMatchMe/", title: "title1" },
230      { uri: "http://title/2", title: "dontmatchme3" },
231      { uri: "http://title/1", title: "matchme2" },
232      { uri: "http://dontmatchme/", title: "title1" },
233      { uri: "http://matchme/", title: "title1" },
234    ],
235  });
236
237  // Also this test should just be sorted by frecency.
238  info(
239    "Don't match one character after a camel-case word boundary (bug 429498). Should just be sorted by frecency."
240  );
241  await check_autocomplete({
242    search: "atch",
243    checkSorting: true,
244    matches: [
245      {
246        uri: "http://tag/2",
247        title: "title1",
248        tags: ["dontmatchme3"],
249        style: ["bookmark-tag"],
250      },
251      {
252        uri: "http://tag/1",
253        title: "title1",
254        tags: ["matchme2"],
255        style: ["bookmark-tag"],
256      },
257      { uri: "http://camel/pleaseMatchMe/", title: "title1" },
258      { uri: "http://title/2", title: "dontmatchme3" },
259      { uri: "http://title/1", title: "matchme2" },
260      { uri: "http://dontmatchme/", title: "title1" },
261      { uri: "http://matchme/", title: "title1" },
262    ],
263  });
264
265  await cleanup();
266});
267
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 Puppeteer 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)