Configure Firefox browser profile to disable notifications and verify settings
Preconditions (2)
✅ Expected Result: Firefox browser launches with notifications disabled and loads https://www.example.com successfully
from selenium import webdriver from selenium.webdriver.firefox.options import Options import unittest class TestFirefoxProfile(unittest.TestCase): def setUp(self): # Create Firefox profile self.profile = webdriver.FirefoxProfile() # Disable notifications self.profile.set_preference("dom.webnotifications.enabled", False) self.profile.update_preferences() options = Options() # Launch Firefox with the custom profile self.driver = webdriver.Firefox(firefox_profile=self.profile, options=options) def test_notifications_disabled(self): self.driver.get("https://www.example.com") # Assert page title contains 'Example Domain' self.assertIn("Example Domain", self.driver.title) # Verify the preference is set (indirectly, since direct check is not possible via WebDriver) # We rely on profile setting correctness here def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
This test script uses Selenium with Python's unittest framework.
In setUp, we create a Firefox profile and set the preference dom.webnotifications.enabled to False to disable notifications.
We launch Firefox with this profile.
The test navigates to https://www.example.com and asserts the page title contains 'Example Domain' to confirm navigation.
Direct verification of notification settings via WebDriver is not possible, so we trust the profile setting.
Finally, tearDown closes the browser to clean up.
Now add data-driven testing with 3 different Firefox profile preferences to disable notifications, disable images, and set a custom homepage
set_preference with lowercase 'p' and boolean False (not string).from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
profile = FirefoxProfile()
profile.set_preference('browser.startup.homepage', 'https://example.com')
profile.set_preference('dom.webnotifications.enabled', False)
profile.update_preferences()
print(profile.default_preferences['browser.startup.homepage'])
What will be printed when this code runs?default_preferences attribute is internal and not updated by set_preference. It may not contain the key.profile.default_preferences['browser.startup.homepage'] will raise a KeyError because this key is not in default_preferences.profile = FirefoxProfile()
profile.set_preference('dom.webnotifications.enabled', 'false')
profile.update_preferences()
What is the likely cause of this problem?set_preference with correct keys and boolean False for notifications.update_preferences() and pass profile as firefox_profile=profile to WebDriver.Pass the test and this pattern moves unknown