简体   繁体   中英

Selenium webdriver Not able to launch application in IE, but opens in Firefox and chrome

I'm trying to launch the web application in browser based on user's choice. For this I used IF condition/ Switch Case. In both case, the application is not getting launched in IE11. However the same application opens fine in Firefox & Chrome.

If I don't use For loop or Switch case, hard code the browser name as IE, then the application launches in IE11 browser and continues with testing without any issue. What could be wrong with below code

    //Attempt 1. Below code NOT WORKING

    log.info("browser name received in utils is :" + browser);

    switch(browser.toLowerCase()) 
    {
    case "firefox":
        log.info("browser name before case firefox :" + browser);
        try
        {
            log.info("Launching Firefox browser");
            System.setProperty("webdriver.gecko.driver",".\\drivers\\geckodriver.exe");
            d = new FirefoxDriver();
            d.manage().timeouts().implicitlyWait(50,TimeUnit.SECONDS) ;
        }

        catch (Exception e)
        {
            log.info("Not able to launch browser");

        }
        log.info("browser name after case firefox :" + browser);
    case "iexplorer":
        log.info("browser name before case IE :" + browser);
        try
        {
            log.info("Launching IE browser");
            System.setProperty("webdriver.ie.driver", ".\\drivers\\IEDriverServer.exe");
            DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
            capabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
            WebDriver d = new InternetExplorerDriver();
        }catch (Exception e)
        {
            log.info("Not able to launch IE browser");

        }
        log.info("browser name after case IE :" + browser);

    case "chrome":
        log.info("browser name before case chrome :" + browser);
        try
        {
            log.info("Launching Chrome browser");
            System.setProperty("webdriver.chrome.driver", ".\\drivers\\chromedriver.exe");
            d = new ChromeDriver();
            d.manage().timeouts().implicitlyWait(50,TimeUnit.SECONDS) ;
        }catch (Exception e)
        {
            log.info("Not able to chrome browser");

        }
        log.info("browser name after case chrome :" + browser);
    }

----------LOG OUTPUT FOR Attempt 1 -----------

2018-05-02 19:02:27 INFO Utils:75 - browser name received in utils is :iexplorer 2018-05-02 19:02:27 INFO Utils:96 - browser name before case IE :iexplorer 2018-05-02 19:02:27 INFO Utils:99 - Launching IE browser Started InternetExplorerDriver server (32-bit) 2.53.1.0 Listening on port 45158 Only local connections are allowed May 02, 2018 7:02:29 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: OSS 2018-05-02 19:02:29 INFO Utils:109 - browser name after case IE :iexplorer 2018-05-02 19:02:29 INFO Utils:112 - browser name before case chrome :iexplorer 2018-05-02 19:02:29 INFO Utils:115 - Launching Chrome browser Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 9315 Only local connections are allowed. May 02, 2018 7:02:31 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: OSS 2018-05-02 19:02:31 INFO Utils:124 - browser name after case chrome :iexplorer

Ok, your log input says that a the browser was lauched. If it closes or if you does not have access, maybe the problem is in your driver variable while creating your IEDriver:

// Change from
WebDriver d = new InternetExplorerDriver();
// to:
d = new InternetExplorerDriver();

Assuming your var d is a global one, it will fix your problem. Since you are creating a new one with the samen name, it lost its reference when method ends.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM