0

Is there any reason why a Selenium test should always fail unless the development tools in Internet Explorer (F12) are turned on?

A page that performs an Ajax call periodically is being tested and fails because no calls are being made. But the test runs successfully when I open the F12 Dev tools.

When the page is accessed manually, the everything works as expected, too. I've tried different versions of the IE WebDriver and the MS Update for WebDriver. But nothing helps. I suspect it is an issue with Selenium somehow intercepting the AJAX call.

Here is a simple HTML and Selenium test that fails with Internet Explorer 11 and Selenium IEServerDriver 2.44.0

<html>

<body>
<h1>Test</h1>

<div id="count">count placeholder</div>
<div id="thediv">div placeholder</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
    var count = 1;
    document.getElementById("thediv").innerHTML = "Test";

   $(document).ready(function () {
        doIt();
    });

    function doIt() {
        $.ajax({url: "http://localhost:8000/my_app/counter", success: function (result) {
            $("#thediv").html(result);
            $("#count").html(count);
            count++;
        }});
        setTimeout(function(){
            doIt();
        }, 1000);
    }

</script>
</body>
</html>

And the test:

@Test
public void runTest() throws Exception {
    final InternetExplorerDriver internetExplorerDriver = new InternetExplorerDriver();
    internetExplorerDriver.get("http://localhost:8000/test.html");
    Assert.assertTrue(internetExplorerDriver.findElement(By.id("thediv")).getText().contains("1"));
    Thread.sleep(5000);
    Assert.assertTrue(Integer.parseInt(internetExplorerDriver.findElement(By.id("thediv")).getText()) > 1);
    internetExplorerDriver.quit();
}

The service just returns a number and it increases by 1 every time it is called. It sets the returned value to the #thediv. This test fails because the subsequent calls are not being made (the first one is). I used a program called Fiddler to check this. When this test is run with the Development Tools ON (F12) it works and the service is being called periodically. The second div #counter is being updated just fine.

MartinTeeVarga
  • 10,478
  • 12
  • 61
  • 98
  • Are you using the Microsoft IE driver implementation, or the open-source one? Hint: If you're using the Java language bindings, you're using the open-source implementation unless you jump through some pretty significant hoops, including modifying the bindings' source code to handle the quirks of the current Microsoft implementation. If you're using the open-source implementation, I can say with some confidence that no "AJAX call interception" is happening. You'll need to provide more info, like a code sample and what exception(s) you're encountering. – JimEvans Nov 18 '14 at 11:40
  • @JimEvans I've added a simple test and html that fails. I've also removed the exceptions part, because they were not relevant. – MartinTeeVarga Nov 19 '14 at 01:38
  • BTW I read your blog entry. We are using the open source one. We've tried the driver downloaded from MS with the implementation=VENDOR. This doesn't work at all, it opens a blank page only, but we might be doing something wrong. – MartinTeeVarga Nov 19 '14 at 06:27

1 Answers1

0

The problem was that we were sending the data with content type application/json and Internet Explorer was caching the result. That's why it worked first time only. For some reason when the Development Tools are open (F12), IE stops caching the results.

The solution was to add HTTP headers to the service to prevent caching. Example how to do that is in this question on SO.

Community
  • 1
  • 1
MartinTeeVarga
  • 10,478
  • 12
  • 61
  • 98