Best Selenium code snippet using org.openqa.selenium.firefox.FirefoxProfile.onlyOverrideThisIfYouKnowWhatYouAreDoing
Source:FirefoxProfile.java
...44 @Beta45 @VisibleForTesting46 protected FirefoxProfile(Reader defaultsReader, File profileDir) {47 if (defaultsReader == null) {48 defaultsReader = onlyOverrideThisIfYouKnowWhatYouAreDoing();49 }50 51 additionalPrefs = new Preferences(defaultsReader);52 53 model = profileDir;54 verifyModel(model);55 56 File prefsInModel = new File(model, "user.js");57 if (prefsInModel.exists()) {58 StringReader reader = new StringReader("{\"frozen\": {}, \"mutable\": {}}");59 Preferences existingPrefs = new Preferences(reader, prefsInModel);60 acceptUntrustedCerts = getBooleanPreference(existingPrefs, "webdriver_accept_untrusted_certs", true);61 untrustedCertIssuer = getBooleanPreference(existingPrefs, "webdriver_assume_untrusted_issuer", true);62 existingPrefs.addTo(additionalPrefs);63 } else {64 acceptUntrustedCerts = true;65 untrustedCertIssuer = true;66 }67 68 loadNoFocusLib = false;69 try70 {71 defaultsReader.close();72 } catch (IOException e) {73 throw new WebDriverException(e);74 }75 }76 77 @Beta78 protected Reader onlyOverrideThisIfYouKnowWhatYouAreDoing()79 {80 URL resource = Resources.getResource(FirefoxProfile.class, "/org/openqa/selenium/firefox/webdriver_prefs.json");81 try {82 return new InputStreamReader(resource.openStream());83 } catch (IOException e) {84 throw new WebDriverException(e);85 }86 }87 88 private boolean getBooleanPreference(Preferences prefs, String key, boolean defaultValue) {89 Object value = prefs.getPreference(key);90 if (value == null) {91 return defaultValue;92 }93 94 if ((value instanceof Boolean)) {95 return ((Boolean)value).booleanValue();96 }97 98 throw new WebDriverException("Expected boolean value is not a boolean. It is: " + value);99 }100 101 public String getStringPreference(String key, String defaultValue) {102 Object preference = additionalPrefs.getPreference(key);103 if ((preference != null) && ((preference instanceof String))) {104 return (String)preference;105 }106 return defaultValue;107 }108 109 public int getIntegerPreference(String key, int defaultValue) {110 Object preference = additionalPrefs.getPreference(key);111 if ((preference != null) && ((preference instanceof Integer))) {112 return ((Integer)preference).intValue();113 }114 return defaultValue;115 }116 117 public boolean getBooleanPreference(String key, boolean defaultValue) {118 Object preference = additionalPrefs.getPreference(key);119 if ((preference != null) && ((preference instanceof Boolean))) {120 return ((Boolean)preference).booleanValue();121 }122 return defaultValue;123 }124 125 private void verifyModel(File model) {126 if (model == null) {127 return;128 }129 130 if (!model.exists())131 {132 throw new UnableToCreateProfileException("Given model profile directory does not exist: " + model.getPath());133 }134 135 if (!model.isDirectory())136 {137 throw new UnableToCreateProfileException("Given model profile directory is not a directory: " + model.getAbsolutePath());138 }139 }140 141 public boolean containsWebDriverExtension() {142 return extensions.containsKey("webdriver");143 }144 145 public void addExtension(Class<?> loadResourcesUsing, String loadFrom)146 {147 File file = new File(loadFrom);148 if (file.exists()) {149 addExtension(file);150 return;151 }152 153 addExtension(loadFrom, new ClasspathExtension(loadResourcesUsing, loadFrom));154 }155 156 public void addExtension(File extensionToInstall)157 {158 addExtension(extensionToInstall.getName(), new FileExtension(extensionToInstall));159 }160 161 public void addExtension(String key, Extension extension) {162 String name = deriveExtensionName(key);163 extensions.put(name, extension);164 }165 166 private String deriveExtensionName(String originalName) {167 String[] pieces = originalName.replace('\\', '/').split("/");168 169 String name = pieces[(pieces.length - 1)];170 name = name.replaceAll("\\..*?$", "");171 return name;172 }173 174 public void setPreference(String key, String value)175 {176 additionalPrefs.setPreference(key, value);177 }178 179 public void setPreference(String key, boolean value)180 {181 additionalPrefs.setPreference(key, value);182 }183 184 public void setPreference(String key, int value)185 {186 additionalPrefs.setPreference(key, value);187 }188 189 protected Preferences getAdditionalPreferences() {190 return additionalPrefs;191 }192 193 public void updateUserPrefs(File userPrefs) {194 Preferences prefs = new Preferences(onlyOverrideThisIfYouKnowWhatYouAreDoing());195 196 prefs.setPreference("browser.startup.homepage", "about:blank");197 198 prefs.setPreference("browser.startup.page", 0);199 200 if (userPrefs.exists()) {201 prefs = new Preferences(onlyOverrideThisIfYouKnowWhatYouAreDoing(), userPrefs);202 if (!userPrefs.delete()) {203 throw new WebDriverException("Cannot delete existing user preferences");204 }205 }206 207 additionalPrefs.addTo(prefs);208 209 prefs.setPreference("webdriver_accept_untrusted_certs", acceptUntrustedCerts);210 211 prefs.setPreference("webdriver_assume_untrusted_issuer", untrustedCertIssuer);212 213 Object homePage = prefs.getPreference("browser.startup.homepage");214 if ((homePage != null) && ((homePage instanceof String))) {215 prefs.setPreference("startup.homepage_welcome_url", "");...
Source:SynthesizedFirefoxDriver.java
...107 }108 private static class CustomProfile extends FirefoxProfile {109 private static Path prefs;110 @Override111 protected Reader onlyOverrideThisIfYouKnowWhatYouAreDoing() {112 try {113 return super.onlyOverrideThisIfYouKnowWhatYouAreDoing();114 } catch (RuntimeException e) {115 if (!DevMode.isInDevMode()) {116 throw e;117 }118 }119 prefs = actuallyGetPrefsPath();120 try {121 return Files.newBufferedReader(prefs);122 } catch (IOException e) {123 fail(Throwables.getStackTraceAsString(e));124 throw new RuntimeException(e);125 }126 }127 private Path actuallyGetPrefsPath() {...
LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.
Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.
What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.
Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.
Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.
How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.
Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.
Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!