How to use checkPreferences method in Appium Xcuitest Driver

Best JavaScript code snippet using appium-xcuitest-driver

Run Appium Xcuitest Driver automation tests on LambdaTest cloud grid

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

Sign up Free
_

test_RecommendedPreferences.js

Source: test_RecommendedPreferences.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 file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
6const { RecommendedPreferences } = ChromeUtils.import(
7  "chrome://remote/content/shared/RecommendedPreferences.jsm"
8);
9
10const COMMON_PREF = "toolkit.startup.max_resumed_crashes";
11
12const MARIONETTE_PREF = "dom.disable_beforeunload";
13const MARIONETTE_RECOMMENDED_PREFS = new Map([[MARIONETTE_PREF, true]]);
14
15const CDP_PREF = "browser.contentblocking.features.standard";
16const CDP_RECOMMENDED_PREFS = new Map([
17  [CDP_PREF, "-tp,tpPrivate,cookieBehavior0,-cm,-fp"],
18]);
19
20function cleanup() {
21  info("Restore recommended preferences and test preferences");
22  Services.prefs.clearUserPref("remote.prefs.recommended");
23  RecommendedPreferences.restoreAllPreferences();
24}
25
26// cleanup() should be called:
27// - explicitly after each test to avoid side effects
28// - via registerCleanupFunction in case a test crashes/times out
29registerCleanupFunction(cleanup);
30
31add_task(async function test_RecommendedPreferences() {
32  info("Check initial values for the test preferences");
33  checkPreferences({ cdp: false, common: false, marionette: false });
34
35  checkPreferences({ cdp: false, common: false, marionette: false });
36
37  info("Apply recommended preferences for a marionette client");
38  RecommendedPreferences.applyPreferences(MARIONETTE_RECOMMENDED_PREFS);
39  checkPreferences({ cdp: false, common: true, marionette: true });
40
41  info("Apply recommended preferences for a cdp client");
42  RecommendedPreferences.applyPreferences(CDP_RECOMMENDED_PREFS);
43  checkPreferences({ cdp: true, common: true, marionette: true });
44
45  info("Restore marionette preferences");
46  RecommendedPreferences.restorePreferences(MARIONETTE_RECOMMENDED_PREFS);
47  checkPreferences({ cdp: true, common: true, marionette: false });
48
49  info("Restore cdp preferences");
50  RecommendedPreferences.restorePreferences(CDP_RECOMMENDED_PREFS);
51  checkPreferences({ cdp: false, common: true, marionette: false });
52
53  info("Restore all the altered preferences");
54  RecommendedPreferences.restoreAllPreferences();
55  checkPreferences({ cdp: false, common: false, marionette: false });
56
57  info("Attemps to restore again");
58  RecommendedPreferences.restoreAllPreferences();
59  checkPreferences({ cdp: false, common: false, marionette: false });
60
61  cleanup();
62});
63
64add_task(async function test_RecommendedPreferences_disabled() {
65  info("Disable RecommendedPreferences");
66  Services.prefs.setBoolPref("remote.prefs.recommended", false);
67
68  info("Check initial values for the test preferences");
69  checkPreferences({ cdp: false, common: false, marionette: false });
70
71  info("Recommended preferences are not applied, applyPreferences is a no-op");
72  RecommendedPreferences.applyPreferences(MARIONETTE_RECOMMENDED_PREFS);
73  checkPreferences({ cdp: false, common: false, marionette: false });
74
75  cleanup();
76});
77
78// Check that protocols can override common preferences.
79add_task(async function test_RecommendedPreferences_override() {
80  info("Make sure the common preference has no user value");
81  Services.prefs.clearUserPref(COMMON_PREF);
82
83  const OVERRIDE_VALUE = 42;
84  const OVERRIDE_COMMON_PREF = new Map([[COMMON_PREF, OVERRIDE_VALUE]]);
85
86  info("Apply a map of preferences overriding a common preference");
87  RecommendedPreferences.applyPreferences(OVERRIDE_COMMON_PREF);
88
89  equal(
90    Services.prefs.getIntPref(COMMON_PREF),
91    OVERRIDE_VALUE,
92    "The common preference was set to the expected value"
93  );
94
95  cleanup();
96});
97
98function checkPreferences({ cdp, common, marionette }) {
99  checkPreference(COMMON_PREF, { hasValue: common });
100  checkPreference(MARIONETTE_PREF, { hasValue: marionette });
101  checkPreference(CDP_PREF, { hasValue: cdp });
102}
103
104function checkPreference(pref, { hasValue }) {
105  equal(
106    Services.prefs.prefHasUserValue(pref),
107    hasValue,
108    hasValue
109      ? `The preference ${pref} has a user value`
110      : `The preference ${pref} has no user value`
111  );
112}
113
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 Appium Xcuitest Driver 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)