Book Image

Selenium Framework Design in Data-Driven Testing

By : Carl Cocchiaro
Book Image

Selenium Framework Design in Data-Driven Testing

By: Carl Cocchiaro

Overview of this book

The Selenium WebDriver 3.x Technology is an open source API available to test both Browser and Mobile applications. It is completely platform independent in that tests built for one browser or mobile device, will also work on all other browsers and mobile devices. Selenium supports all major development languages which allow it to be tied directly into the technology used to develop the applications. This guide will provide a step-by-step approach to designing and building a data-driven test framework using Selenium WebDriver, Java, and TestNG. The book starts off by introducing users to the Selenium Page Object Design Patterns and D.R.Y Approaches to Software Development. In doing so, it covers designing and building a Selenium WebDriver framework that supports both Browser and Mobile Devices. It will lead the user through a journey of architecting their own framework with a scalable driver class, Java utility classes, JSON Data Provider, Data-Driven Test Classes, and support for third party tools and plugins. Users will learn how to design and build a Selenium Grid from scratch to allow the framework to scale and support different browsers, mobile devices, versions, and platforms, and how they can leverage third party grids in the Cloud like SauceLabs. Other topics covered include designing abstract base and sub-classes, inheritance, dual-driver support, parallel testing, testing multi-branded applications, best practices for using locators, and data encapsulation. Finally, you will be presented with a sample fully-functional framework to get them up and running with the Selenium WebDriver for browser testing. By the end of the book, you will be able to design your own automation testing framework and perform data-driven testing with Selenium WebDriver.
Table of Contents (15 chapters)
Title Page
Dedication
Packt Upsell
Contributors
Preface

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.

Note

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;
}

Note

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; falseaccessibility.accesskeycausesactivation; trueaccessibility.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;
    }

Note

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