除非打开Internet (F12)中的开发工具,否则硒测试总是失败的原因吗?
定期执行Ajax调用的页面正在测试,并且由于没有调用而失败。但是,当我打开F12开发工具时,测试成功地运行了。
当手动访问页面时,所有内容也会按预期工作。我尝试过不同版本的IE WebDriver和。但什么都帮不上忙。我怀疑Selenium以某种方式拦截AJAX调用是个问题。
下面是一个简单的HTML和Selenium测试,它在Internet 11和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>而测试:
@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();
}该服务只返回一个数字,每次调用时它都会增加1。它将返回的值设置为#thediv。此测试失败,因为没有进行后续调用(第一个调用是)。我用了一个叫做Fiddler的程序来检查这个。当使用开发工具(F12)运行此测试时,它可以工作,并且定期调用该服务。第二个div #计数器正在进行更新。
发布于 2014-11-19 22:08:52
问题是,我们发送内容类型为application/json的数据时,Internet正在缓存结果。这就是为什么它第一次起作用的原因。由于某些原因,当开发工具(F12)打开时,IE停止缓存结果。
解决方案是将HTTP头添加到服务中,以防止缓存。例如,如何做到这一点是in this question on SO。
https://stackoverflow.com/questions/26987288
复制相似问题