describe("Browser functions", function () { "use strict"; describe("executeJavascript", function () { var doc, originaLPromise; var mockPromisesToResolveSynchronously = function () { window.Promise = testHelper.SynchronousPromise; }; beforeEach(function () { originaLPromise = window.Promise; }); afterEach(function () { window.Promise = originaLPromise; }); var mockFinishNotifyingXHRProxy = function () { var fakeXhrProxy = jasmine.createSpyObj("finishNotifyingXhrProxy", [ "send", "waitForRequestsToFinish", ]), defer = testHelper.synchronousDefer(); fakeXhrProxy.waitForRequestsToFinish.and.returnValue(defer.promise); spyOn(proxies, "finishNotifyingXhr").and.returnValue(fakeXhrProxy); return defer; }; var defaultOptionsWithViewport = function (width, height) { return { width: width || 12, height: height || 34, }; }; var defaultOptionsWithTimeout = function (timeout) { return { baseUrl: undefined, executeJsTimeout: timeout, width: 12, height: 34, }; }; var optionsWithViewport = function (width, height) { return { width: width || 12, height: height || 34, executeJsTimeout: 100, }; }; beforeEach(function () { doc = window.document.implementation.createHTMLDocument(""); }); it("should load an URL and execute the included JS", function (done) { doc.documentElement.innerHTML = "
"; browser .executeJavascript( doc.documentElement, defaultOptionsWithViewport(), ) .then(function (result) { expect(result.document.body.innerHTML).toEqual( "dynamic content", ); result.cleanUp(); done(); }); }); it("should remove the iframe element when done", function (done) { doc.documentElement.innerHTML = ""; browser .executeJavascript( doc.documentElement, defaultOptionsWithViewport(), ) .then(function (result) { result.cleanUp(); expect(document.querySelector("iframe")).toBe(null); done(); }); }); it("should wait a configured period of time before calling back", function (done) { doc.documentElement.innerHTML = ""; browser .executeJavascript( doc.documentElement, defaultOptionsWithTimeout(20), ) .then(function (result) { expect(result.document.body.innerHTML).toEqual( "dynamic content", ); result.cleanUp(); done(); }); }); it("should return only when all ajax has loaded", function (done) { var callback = jasmine.createSpy("callback"); mockPromisesToResolveSynchronously(); var xhrFinishedDefer = mockFinishNotifyingXHRProxy(); browser .executeJavascript( doc.documentElement, defaultOptionsWithTimeout(10), ) .then(function (result) { result.cleanUp(); callback(); }); // HACK fragile test. We need to wait for the iframe.onload to be triggered setTimeout(function () { expect(callback).not.toHaveBeenCalled(); xhrFinishedDefer.resolve(); expect(callback).toHaveBeenCalled(); done(); }, 100); }); it("should return only when all ajax has loaded, even if timeout is set to 0", function (done) { var callback = jasmine.createSpy("callback"); mockPromisesToResolveSynchronously(); var xhrFinishedDefer = mockFinishNotifyingXHRProxy(); browser .executeJavascript( doc.documentElement, defaultOptionsWithViewport(), ) .then(function (result) { result.cleanUp(); callback(); }); // HACK fragile test. We need to wait for the iframe.onload to be triggered setTimeout(function () { expect(callback).not.toHaveBeenCalled(); xhrFinishedDefer.resolve(); expect(callback).toHaveBeenCalled(); done(); }, 100); }); it("should be able to access CSS", function (done) { doc.documentElement.innerHTML = ''; browser .executeJavascript( doc.documentElement, defaultOptionsWithViewport(), ) .then(function (result) { expect(result.document.body.innerHTML).toEqual("20"); result.cleanUp(); done(); }); }); it("should report failing JS", function (done) { doc.documentElement.innerHTML = ""; browser .executeJavascript( doc.documentElement, defaultOptionsWithViewport(), ) .then(function (result) { expect(result.errors).toEqual([ { resourceType: "scriptExecution", msg: jasmine.any(String), }, ]); expect(result.errors[0].msg).toMatch( /(ReferenceError:\s+(.+\s+)?undefinedVar)|('undefinedVar' is undefined)/, ); result.cleanUp(); done(); }); }); it("should be able to access top 'html' tag attributes", function (done) { doc.documentElement.innerHTML = ""; doc.documentElement.setAttribute("myattr", "myvalue"); browser .executeJavascript( doc.documentElement, defaultOptionsWithViewport(), ) .then(function (result) { expect(result.document.body.innerHTML).toEqual("1"); result.cleanUp(); done(); }); }); it("should be able to load content via AJAX from the correct url", function (done) { testHelper .readHTMLDocumentFixture("ajax.html") .then(function (doc) { browser .executeJavascript(doc.documentElement, { baseUrl: testHelper.fixturesPath, executeJsTimeout: 100, width: 123, height: 456, }) .then(function (result) { expect( result.document .querySelector("div") .textContent.trim(), ).toEqual("The content"); result.cleanUp(); done(); }); }); }); it("should support window.matchMedia() with 'width' media queries", function (done) { doc.documentElement.innerHTML = ""; browser .executeJavascript( doc.documentElement, optionsWithViewport(42, 21), ) .then(function (result) { expect(result.document.body.innerHTML).toEqual("true"); result.cleanUp(); done(); }); }); it("should support window.matchMedia() with 'height' media queries", function (done) { doc.documentElement.innerHTML = ""; browser .executeJavascript( doc.documentElement, optionsWithViewport(10, 123), ) .then(function (result) { expect(result.document.body.innerHTML).toEqual("true"); result.cleanUp(); done(); }); }); it("should correctly set canvas size for media queries", function (done) { doc.documentElement.innerHTML = ""; browser .executeJavascript( doc.documentElement, defaultOptionsWithViewport(20, 123), ) .then(function (result) { expect(result.document.body.innerHTML).toEqual("true"); result.cleanUp(); done(); }); }); it("should correctly set the doctype (see issue #89)", function (done) { browser .executeJavascript( doc.documentElement, defaultOptionsWithViewport(), ) .then(function (result) { expect(result.document.doctype.name).toEqual("html"); result.cleanUp(); done(); }); }); }); describe("parseHtmlFragment", function () { it("should parse a single div", function () { var element = browser.parseHtmlFragment(''); expect(element.tagName.toLowerCase()).toEqual("div"); expect(element.id).toEqual("the_div"); }); it("should raise error when parsing an otherwise empty body element", function () { expect(function () { browser.parseHtmlFragment(""); }).toThrow("Invalid source"); }); }); describe("parseHTML", function () { var oldDOMParser = window.DOMParser; afterEach(function () { window.DOMParser = oldDOMParser; }); it("should parse HTML to a document", function () { var dom = browser.parseHTML("Text"); expect(dom.querySelector("body").textContent).toEqual("Text"); }); it("should keep 'html' tag attributes", function () { var dom = browser.parseHTML(''); expect(dom.documentElement.getAttribute("top")).toEqual( "attribute", ); }); it("should keep 'html' tag attributes even if DOMParser is not supported", function () { var dom; window.DOMParser = function () { this.parseFromString = function () { return null; }; }; dom = browser.parseHTML(''); expect(dom.documentElement.getAttribute("top")).toEqual( "attribute", ); }); it("should deal with a missing 'html' tag", function () { browser.parseHTML(""); expect(true).toBe(true); // work around warning from jasmine that no expectation is given }); it("should correctly set the doctype (see issue #89)", function () { var doc = browser.parseHTML(""); expect(doc.doctype.name).toEqual("html"); }); }); describe("validateXHTML", function () { it("should throw an exception if the document is invalid", function () { var error; try { browser.validateXHTML("