Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Selenium Framework Design in Data-Driven Testing

You're reading from   Selenium Framework Design in Data-Driven Testing Build data-driven test frameworks using Selenium WebDriver, AppiumDriver, Java, and TestNG

Arrow left icon
Product type Paperback
Published in Jan 2018
Publisher Packt
ISBN-13 9781788473576
Length 354 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Author (1):
Arrow left icon
Carl Cocchiaro Carl Cocchiaro
Author Profile Icon Carl Cocchiaro
Carl Cocchiaro
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Building a Scalable Selenium Test Driver Class for Web and Mobile Applications FREE CHAPTER 2. Selenium Framework Utility Classes 3. Best Practices for Building Selenium Page Object Classes 4. Defining WebDriver and AppiumDriver Page Object Elements 5. Building a JSON Data Provider 6. Developing Data-Driven Test Classes 7. Encapsulating Data in Data-Driven Testing 8. Designing a Selenium Grid 9. Third-Party Tools and Plugins 10. Working Selenium WebDriver Framework Samples

Using preferences to support browsers and platforms

The browser preferences and behavior can be set to specific defaults when the driver is created, set on the fly using optional parameters, or set as system properties. Preferences can be set for different languages, geolocations, focus, download folders, and so on. This section will cover the basics of how to set default preferences and capabilities in the driver method.

The Selenium HQ documentation on Desired Capabilities is located at https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities.

Browser preferences

  • Firefox: Preferences for this browser are set using the FirefoxProfile class, the FirefoxOptions class, and Desired Capabilities. The list of preferences and options set in the profile are then passed to the driver as DesiredCapabilites. The following example shows various profile preferences passed into the driver as default settings using both profile preferences and Desired Capabilities:
switch (browser) {
case "firefox":
caps = DesiredCapabilities.firefox();

FirefoxOptions ffOpts = new FirefoxOptions();
FirefoxProfile ffProfile = new FirefoxProfile();
ffProfile.setPreference("browser.autofocus",
true
);

caps.setCapability(FirefoxDriver.PROFILE,
ffProfile);

caps.setCapability("marionette",
true);

webDriver.set(new FirefoxDriver(caps));

// Selenium 3.7.x
// webDriver.set(new FirefoxDriver(ffOpts.merge(caps)));
}

break;
}

Firefox preferences can be found by typing the following into the Firefox location bar: about:config or at https://github.com/mozilla/geckodriver/.

accessibility.AOM.enabled; false
accessibility.accesskeycausesactivation; true
accessibility.blockautorefresh; false
...
  • Chrome: Preferences for this browser are set using the ChromeOptions class and Desired Capabilities. The list of preferences and/or arguments are then passed to the driver as DesiredCapabilites. The following example shows various preferences and arguments passed into the driver as default settings using both preferences and Desired Capabilities:

switch (browser) {
case "chrome":
caps = DesiredCapabilities.chrome();

ChromeOptions chOptions = new ChromeOptions();
Map<String, Object> chromePrefs =
new HashMap<String, Object>();

chromePrefs.put("credentials_enable_service",
false);
chOptions.setExperimentalOption("prefs",
chromePrefs);
chOptions.addArguments("--disable-plugins",
"--disable-extensions",
"--disable-popup-blocking");

caps.setCapability(ChromeOptions.CAPABILITY,
chOptions);

caps.setCapability("applicationCacheEnabled",
false);

webDriver.set(new ChromeDriver(caps));

// Selenium 3.7.x
// webDriver.set(new ChromeDriver(chOptions.merge(caps)));

break;
}

Chrome preferences can be found by typing the following into the Chrome location bar: chrome://flags or https://sites.google.com/a/chromium.org/chromedriver/capabilities.
  • Internet Explorer, Safari, and Microsoft Edge: Preferences for these browsers are also set using the InternetExplorerOptions, SafariOptions, EdgeOptions classes, and Desired Capabilities. Users can query for the available options and capabilities for each of these browsers. The following code sample shows an abbreviated case for each.

For Internet Explorer:

switch (browser) {
case "internet explorer":
caps = DesiredCapabilities.internetExplorer();

InternetExplorerOptions ieOpts =
new InternetExplorerOptions();

ieOpts.requireWindowFocus();

ieOpts.merge(caps);
caps.setCapability("requireWindowFocus",
true);

webDriver.set(new InternetExplorerDriver(caps));

// Selenium 3.7.x
// webDriver.set(new InternetExplorerDriver(
ieOpts.merge(caps))
);

break;
}

For Safari:


switch
(browser) {
case "safari":
caps = DesiredCapabilities.safari();

SafariOptions safariOpts = new SafariOptions();
safariOpts.setUseCleanSession(true);

caps.setCapability(SafariOptions.CAPABILITY,
safariOpts);
caps.setCapability("autoAcceptAlerts",
true);

webDriver.set(new SafariDriver(caps));

// Selenium 3.7.x
// webDriver.set(new SafariDriver(safariOpts.merge(caps)));

break;
}

For Microsoft Edge:

switch(browser) {
case "microsoftedge":
caps = DesiredCapabilities.edge();

EdgeOptions edgeOpts = new EdgeOptions();
edgeOpts.setPageLoadStrategy("normal");

caps.setCapability(EdgeOptions.CAPABILITY,
edgeOpts);
caps.setCapability("requireWindowFocus",
true);


webDriver.set(new EdgeDriver(caps));

// Selenium 3.7.x
// webDriver.set(new EdgeDriver(edgeOpts.merge(caps)));

break
;
}

Platforms

There are some specific system properties that need to be set for each driver; specifically, the path to the local driver in the GIT repository of the project. By storing the driver in the project, users will not have to download or install the drivers for each browser when testing locally from their IDE. The path also depends on the OS of the development platform. The following examples are for Windows platforms:

  • Firefox:
    System.setProperty("webdriver.gecko.driver","gecko_driver_windows_path/geckodriver.exe");
  • Chrome:
    System.setProperty("webdriver.chrome.driver","chrome_driver_windows_path/chromedriver.exe");
  • IE:
    System.setProperty("webdriver.ie.driver","ie_driver_windows_path/IEDriverServer.exe");
  • Edge:
    System.setProperty("webdriver.edge.driver","edge_driver_windows_path/MicrosoftWebDriver.exe");
  • Safari: The Safari driver is now built into the browser by Apple
You have been reading a chapter from
Selenium Framework Design in Data-Driven Testing
Published in: Jan 2018
Publisher: Packt
ISBN-13: 9781788473576
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime