All Downloads are FREE. Search and download functionalities are using the official Maven repository.

web.WebDriverInitializer Maven / Gradle / Ivy

package web;

import core.reports.TestReporter;
import io.github.bonigarcia.wdm.*;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.opera.OperaOptions;

/**
 * Created by Ismail on 12/25/2017.
 * This Class contains methods related to manage WebDriver
 * to run, initialize and stop WebDriver instance for web applications
 */
public class WebDriverInitializer {

    /*************** Class Variables Section***************/
    // Define static WebDriver Object for Web Platform
    public static WebDriver webDriver = null;
    // Define Browser Type (chrome / firefox / opera / safari / ie / edge)
    public static String browser = "chrome";
    // Define baseUrl value
    public static String baseUrl = "";

    // Define Parameters Names Strings
    // Define Browser Parameter name
    public static final String browserParam = "browser";
    // Define BaseUrl Parameter name
    public static final String baseUrlParam = "baseUrl";

    /*************** Class Methods Section ***************/
    // This method to run WebDriver as Web Application and open specific browser
    public static void setUpWebDriver() {
        // Check browser case to run suitable one, Running and opening browser using WebDriverManager plugin
        switch (browser.toLowerCase()) {
            case "chrome":
            case "google chrome":
            case "googlechrome": {
                // Define Browser Options
                ChromeOptions chromeOptions = new ChromeOptions();
                // Disable any info bar to appear on automated browser
                chromeOptions.addArguments("disable-infobars");
                // Run webDriver exe file
                ChromeDriverManager.getInstance().setup();
                // Initialize WebDriver with its options
                webDriver = new ChromeDriver(chromeOptions);
                break;
            }
            case "firefox":
            case "ff": {
                // Define Browser Options
                FirefoxOptions firefoxOptions = new FirefoxOptions();
                // Disable any info bar to appear on automated browser
                firefoxOptions.addArguments("disable-infobars");
                // Run webDriver exe file
                FirefoxDriverManager.getInstance().setup();
                // Initialize WebDriver with its options
                webDriver = new FirefoxDriver(firefoxOptions);
                break;
            }
            case " opera": {
                // Define Browser Options
                OperaOptions operaOptions = new OperaOptions();
                // Disable any info bar to appear on automated browser
                operaOptions.addArguments("disable-infobars");
                // Run webDriver exe file
                OperaDriverManager.getInstance().setup();
                // Initialize WebDriver with its options
                webDriver = new OperaDriver(operaOptions);
                break;
            }
            case "ie":
            case "internet":
            case "internet explorer":
            case "internetexplorer": {
                // Run webDriver exe file
                InternetExplorerDriverManager.getInstance().setup();
                // Initialize WebDriver with its options
                webDriver = new InternetExplorerDriver();
                break;
            }
            case "edge": {
                // Run webDriver exe file
                EdgeDriverManager.getInstance().setup();
                // Initialize WebDriver with its options
                webDriver = new EdgeDriver();
                break;
            }
            default: {
                TestReporter.error("Wrong Browser Type" + browser.toLowerCase(), true);
            }
        }// Here to run some specifications for browsers
        try {
            // Set focus on browser in case running on Mac platforms
            ((JavascriptExecutor) webDriver).executeScript("alert('Test')");
            webDriver.switchTo().alert().accept();

            // Provide Maximize for browser window
            webDriver.manage().window().maximize();
        } catch (Throwable throwable) {
            // If fail for any reason test case will fail and send to report
            TestReporter.error("Something went wrong, Please check message: " + throwable.getMessage(), true);
        }// If everything goes fine then will provide info step done to report
        TestReporter.info(browser.toLowerCase() + " Browser opened and Running...");
    }

    // This method to close the browser
    public static void tearDownWebDriver() {
        try {// Check if WebDriver initialized as browser and still running
            if (webDriver.toString() != null) {
                webDriver.close();// If yes then will close the browser
                // After browser closed will send info step done to report
                TestReporter.info("Browser Window Closed...");
            }
        } catch (Throwable throwable) {
            // If fail will send error to report without failing the test case
            TestReporter.error("Unable to close Browser, Please check message: " + throwable.getMessage(), false);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy