From d181c00378fcf786fc40a819e5dfe42e5725475f Mon Sep 17 00:00:00 2001 From: Ariya Hidayat Date: Sun, 2 Jun 2013 13:33:52 -0700 Subject: [PATCH] Unit tests: reduce the flakiness of loading progress tests. For whatever reason, swapping the order with the render() tests solve the intermittent random failures. Also, split the tests between loading start and finish checks (to better recognize which one is failing, if there is a failure), also make the tests more asynchoronous. https://github.com/ariya/phantomjs/issues/11091 --- test/webpage-spec.js | 97 ++++++++++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 34 deletions(-) diff --git a/test/webpage-spec.js b/test/webpage-spec.js index c302296f..e4b561f8 100644 --- a/test/webpage-spec.js +++ b/test/webpage-spec.js @@ -1905,6 +1905,69 @@ describe('WebPage navigation events', function() { }); }); +describe("WebPage loading/loadingProgress properties", function() { + var p = require("webpage").create(); + + it("should not be loading when page has just been created", function() { + expect(p.loading).toBeFalsy(); + expect(p.loadingProgress).toEqual(0); + }); + + it("should be loading when 'page.open' is invoked", function() { + var s = require("webserver").create(); + + s.listen(12345, function(request, response) { + setTimeout(function() { + response.statusCode = 200; + response.write('Loaded!'); + response.close(); + }, 200); + }); + + runs(function() { + p.open("http://localhost:12345"); + expect(p.loading).toBeTruthy(); + expect(p.loadingProgress).toBeGreaterThan(0); + }); + + waits(500); + + runs(function() { + s.close(); + }); + }); + + it("should be completed when page is fully loaded", function() { + var s = require("webserver").create(); + + s.listen(12345, function(request, response) { + setTimeout(function() { + response.statusCode = 200; + response.write('Loaded!'); + response.close(); + }, 500); + }); + + var loaded = false; + + runs(function() { + p.open("http://localhost:12345", function () { + loaded = true; + }); + }); + + waitsFor(function () { + return loaded; + }, 'Can not test loading progress' , 3000); + + runs(function() { + expect(p.loading).toBeFalsy(); + expect(p.loadingProgress).toEqual(100); + s.close(); + }); + }); +}); + describe("WebPage render image", function(){ var TEST_FILE_DIR = "webpage-spec-renders/"; @@ -2008,37 +2071,3 @@ describe("WebPage render image", function(){ }); -describe("WebPage loading/loadingProgress properties", function() { - var p = require("webpage").create(); - - it("should not be loading when page has just been created", function() { - expect(p.loading).toBeFalsy(); - expect(p.loadingProgress).toEqual(0); - }); - - it("should be loading when 'page.open' is invoked", function() { - var s = require("webserver").create(); - - s.listen(12345, function(request, response) { - setTimeout(function() { - response.statusCode = 200; - response.write('Loaded!'); - response.close(); - }, 500); - }); - - p.onLoadFinished = function(status) { - expect(p.loading).toBeFalsy(); - expect(p.loadingProgress).toEqual(0); - }; - p.open("http://localhost:12345"); - expect(p.loading).toBeTruthy(); - expect(p.loadingProgress).toBeGreaterThan(0); - - waits(500); - - runs(function() { - s.close(); - }); - }); -});