21.6.15

Run Selenium tests on IE and Chrome browsers

Download the driver EXEs from www.seleniumhq.org, for the OS version you have. Since firefox driver is bundled in the selenium jar, we do not need to download the firefox driver separately, however, we do have to configure it, just like the other drivers.

Create a folder called 'drivers' under the 'resources' folder, in your project structure

Extract and copy the 'IEDriverServer.exe' and 'ChromeDriver.exe' files in the 'drivers' folder.




This below code demonstrates how you can choose the browser that you want to run your tests in - all you have to do is pass the name of the browser as an input to the Driver method.


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class Drivers {


    private WebDriver driver;

    String ieDriverPath = "src\\resources\\drivers\\IEDriverServer.exe";
    String chromeDriverPath = "src\\resources\\drivers\\ChromeDriver.exe";

    public Drivers(String browser){

        setDriver(browser);
    }

    public WebDriver getDriver(){

        return driver;
    }

    public void setDriver(String browser){


        String downloadFolderPath = "C:\\z_All_Downloads\\Java";


        this.driver = null;



        if (browser.equalsIgnoreCase("IE")){

            System.setProperty("webdriver.ie.driver", ieDriverPath);

            InternetExplorerDriver ieDriver = new InternetExplorerDriver();
            this.driver = ieDriver;
        }

        if (browser.equalsIgnoreCase("CHROME")){


            System.setProperty("webdriver.chrome.driver", chromeDriverPath);

            ChromeDriver chromeDriver = new ChromeDriver();
            this.driver = chromeDriver;
        }

if (browser.equalsIgnoreCase("FIREFOX")) {


//creating an object for the profiles

            ProfilesIni allProfiles = new ProfilesIni();

//getting the firefox profile for webdriver, which was created manually

            FirefoxProfile profile = allProfiles.getProfile("webdriverprofile");

//Setting the firefox preferences

// Setting the preferences for download -
            profile.setPreference("browser.download.folderList", 2);
            profile.setPreference("browser.download.dir", downloadFolderPath);
            profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");

//Prevent Firefox from previewing PDFs -

            profile.setPreference("pdfjs.disabled", true);

//Disabling the third party PDF viewers -

            profile.setPreference("plugin.scan.plid.all", false);
            profile.setPreference("plugin.scan.Acrobat", "90.0");
            profile.setPreference("plugin.disable_full_page_plugin_for_types", "application/pdf");

//instantiating the firefox browser with this new profile and preference

            WebDriver firefoxDriver = new FirefoxDriver(profile);

            this.driver = firefoxDriver;

        }


    }


}   //end of class

Common issues and troubleshooting -

  • Error - [The driver executable does not exist: C:\BDD\BDD\src\resources\data\drivers\ChromeDriver.exe]. This generally happens when the driver exe is not located at the referred location, and can be resolved by putting the exe at the correct location

No comments:

Post a Comment