Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chromedriver Not Loading Page in Selenium Tests

I’m experiencing an exception scenario using chromedriver.exe with some unit tests that I am writing with the help of Selenium. When the first test that uses the chromedriver executes, the browser fires up and the test passes.

However, for all the following tests that use the chromedriver, the browser does not successfully navigate to the URL.

The browser fires up, momentarily the characters data; appear in the address bar (as it did in the first test that worked), then the correct URL is inserted into the address bar. However, the page never loads and you get the standard chrome This webpage is not available message in the body/canvas of the browser with the two buttons reload and more.

Is this a known issue?

I am using the following versions:

Selenium: 2.41.0.0
Chromedriver.exe: 2.9.0.0
Visual Studio 2013: 12.0.30110.00 Update 1

I have the following Initialize method in my ChromeTestDriver class which gets invoked in the Setup method (aka TestInitialize) for all tests:

ChromeDriverService chromeDriverService = ChromeDriverService.CreateDefaultService(DriverPath);
var chromeOptions = new ChromeOptions();

chromeDriverService.Port = DriverPort; // 9999 - this is the port for the driver, not the webpage 

webDriver = new ChromeDriver(chromeDriverService, chromeOptions);
webDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
webDriver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(10)); 

I have the following code in the Cleanup method (aka Teardown) for all tests:

TestDriver.CloseWindow();  
TestDriver.Quit();

The following exception gets chucked:

OpenQA.Selenium.NoSuchWindowException: no such window: target window already closed

Let me know if I can provide more info.

Edit I've observed that an IE test has to run first before Chrome fails. Generally, the following happens. A Chrome test runs fine. Then an IE test runs. Subsequent to that, all Chrome tests fail.

Further Edit Another unusual aspect to this is that once chrome has been sullied by Selenium for the website, I can no longer load the website in Chrome at all. That is, if I manually type in the URL in Chrome’s address bar, the same empty page is displayed.

Even weirder again, if I run up Fiddler2 (which is basically a proxy), Chrome becomes unsullied. It works again.

I’m confident proxies aren’t the issue as my system has no proxy and the same result occurs when Fiddler2 has been uninstalled from my system.

The symptoms are also reproducible by my client who is located on a different continent. We collaborate using GIT. So it is not confined to my system.

A user on the Selenium Google users group suggested reproducing the bug using the webdriver in a really simple scenario (i.e. not as part of a testing framework). Here is the code for the console app which I created to do that:

private static string Url = "http://localhost:5556";

static void Main(string[] args)
{
    var chromeWebDriver = GetChromeWebDriver();
    var nav = chromeWebDriver.Navigate();
    nav.GoToUrl(Url);
    Thread.Sleep(3000);
    chromeWebDriver.Quit();
    chromeWebDriver.Dispose();

    var iedriver = GetIeDriver();
    var nav1 = iedriver.Navigate();
    nav1.GoToUrl(Url);
    iedriver.Quit();
    iedriver.Dispose();

    var chromeWebDriver2 = GetChromeWebDriver();
    var nav2 = chromeWebDriver2.Navigate();
    nav2.GoToUrl(Url);
    chromeWebDriver2.FindElement(By.LinkText("Login")).Click();

    System.Threading.Thread.Sleep(2000);

    chromeWebDriver2.Quit();
    chromeWebDriver2.Dispose();

    Console.ReadLine();
}

private static IWebDriver GetIeDriver()
{
    InternetExplorerDriverService internetExplorerDriverService =
        InternetExplorerDriverService.CreateDefaultService(
            @"H:\BW\packages\Selenium.WebDriver.IEDriver.2.41.0.1\content");
    InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions();
    internetExplorerDriverService.Port = 9999;
    IWebDriver webdriver = new InternetExplorerDriver(internetExplorerDriverService, internetExplorerOptions);
    return webdriver;
}

private static IWebDriver GetChromeWebDriver()
{
    var chromeDriverService =
        ChromeDriverService.CreateDefaultService(
            @"H:\BW\packages\Selenium.WebDriver.ChromeDriver.2.10.0.0\content");
    var chromeOptions = new ChromeOptions();
    chromeDriverService.Port = 7777;
    IWebDriver chromeWebDriver = new ChromeDriver(chromeDriverService, chromeOptions);
    return chromeWebDriver;
}
like image 504
onefootswill Avatar asked Apr 01 '14 03:04

onefootswill


1 Answers

Try this code. It is simple and it will solve your problem.

if(browserType.equals("googleChrome")==true)
{

    System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\autoItfiles\\chromedriver.exe");
    driver = new ChromeDriver();

    Report.info("Google chrome browser is opened ");                
}

// To close browser instance. Do not use close() method mostly it does not works other then Firefox

driver.quit();

Close will shut the current active window and if it is the last window will then perform a quit(),

If your test has failed that session is probably dead, so when you call a close it doesn't know where to send the command and doesn't do anything.

Quit will shut down all clients if there are no active sessions so if you send a quit and have no active sessions it will just clean up

like image 57
Hariprasad Avatar answered Sep 19 '22 07:09

Hariprasad