A console app in C# that requests four images in a tight loop sometimes returns a previous request. The code is as below and works against any web site, I typically see 3 or 4 errors per run. I developed this code after reports from people browsing a web site I manage where occasionally a jpeg or script would be loaded when the user requested a HTML page.
I don’t know if it is a Chrome or ChromeDriver issue. If the previous request was an HTML page then you can end up with getting that instead of the image. Seems to be a race condition.
Has anyone else seen this behaviour and can they repeat it with the code below?
class ContentVerify
{
OpenQA.Selenium.IWebDriver driver;
readonly System.Collections.Generic.List<string> testUrls = new System.Collections.Generic.List<string>()
{
"http://i.imgur.com/zNJvS.jpg",
"http://i.imgur.com/lzVec.jpg",
"http://i.imgur.com/rDuhT.jpg",
"http://i.imgur.com/sZ26q.jpg"
};
public void Check()
{
driver = new OpenQA.Selenium.Chrome.ChromeDriver(); // Both InternetExplorerDriver and FirefoxDriver work OK.
for (int i = 0; i < 10; i++)
{
TestUrls();
}
driver.Quit(); // The driver also crashes on exit, but this seems to be a known bug in Selenium.
}
private void TestUrls()
{
foreach (var item in testUrls)
{
System.Console.WriteLine(item);
//System.Threading.Thread.Sleep(1); // Uncommenting this makes Chrome & ChromeDriver work as expected.
driver.Url = item;
// Requests for images come back as an HTML image tag wrapped in a brief HTML page, like below;
//<html><body style="margin: 0px;"><img style="-webkit-user-select: none" src="http://i.imgur.com/zNJvS.jpg"></body></html>
// So the image should always be in the page, but sometimes (not always) we get the previous image requested.
if (!driver.PageSource.Contains(item))
{
System.Console.ForegroundColor = System.ConsoleColor.Red;
System.Console.WriteLine("Expected: {0}, got: {1}", item, driver.PageSource);
System.Console.ResetColor();
}
}
}
}
It could be that you’re not giving the driver enough time to complete the call and have the page load, so it’ll “return” whatever previous page it had returned. Have you looked into setting up a timeout/wait on the driver?
EDIT
With regards to the question of why there is this issue in Chrome but not the other browsers, I’d had to venture a guess and say that it probably has to do with how the different browser engines handle displaying an image directly instead of HTML. I make this assumption due to the fact that this discrepancy as described is not seen when running similar code against an HTML page like the Google home page.
Each browser wraps the image in some HTML. For example, IE9 wraps as such:
Whereas Firefox wraps it like:
And finally, Chrome:
Now, I don’t know why the Chrome version causes the webdriver to be unable to detect the pageload. It certainly is the most minimal of the three HTML wrappers, and the w3 validator has a mild panic attack when asked to validate its HTML while the other two validate relatively well.
Also, as mentioned by mootinator, there have been numerous complaints about the Chrome driver in general so it could be just an issue with the Chrome webdriver itself. I just found the above interesting and thought it might be worthwhile to share.