Best JavaScript code snippet using wpt
basic-popup-and-iframe-tests.https.js
Source:basic-popup-and-iframe-tests.https.js  
1/**2 * This test checks the Secure Context state of documents for various3 * permutations of document URI types and loading methods.4 *5 * The hierarchy that is tested is:6 *7 *   creator-doc > createe-doc8 *9 * The creator-doc is one of:10 *11 *   http:12 *   https:13 *14 * The createe-doc is loaded as either a:15 *16 *   popup17 *   iframe18 *   sandboxed-iframe19 *20 * into which we load and test:21 *22 *   http:23 *   https:24 *   blob:25 *   javascript:26 *   about:blank27 *   initial about:blank28 *   srcdoc29 *30 * TODO once web-platform-tests supports it:31 *   - test http://localhost32 *   - test file:33 *34 * TODO once https://github.com/w3c/webappsec-secure-contexts/issues/26 is resolved35 *   - test data:36 */37setup({explicit_done:true});38const host_and_dirname = location.host +39                         location.pathname.substr(0, location.pathname.lastIndexOf("/") + 1);40// Flags to indicate where document types should be loaded for testing:41const eLoadInPopup             = (1 << 0);42const eLoadInUnsandboxedIframe = (1 << 1);43const eLoadInSandboxedIframe   = (1 << 2);44const eLoadInEverything        = eLoadInPopup | eLoadInUnsandboxedIframe | eLoadInSandboxedIframe;45// Flags indicating if a document type is expected to be a Secure Context:46const eSecureNo              = 1;47const eSecureIfCreatorSecure = 2;48// Flags indicating how the result of a test is obtained:49const eResultFromPostMessage       = 1;50const eResultFromExaminationOnLoad = 2;51const eResultFromExaminationSync   = 3;52const loadTypes = [53  new LoadType("an http: URI",54               eLoadInEverything,55               http_dir + "postMessage-helper.html",56               eSecureNo,57               eResultFromPostMessage),58  new LoadType("an https: URI",59               eLoadInEverything,60               https_dir + "postMessage-helper.https.html",61               eSecureIfCreatorSecure,62               eResultFromPostMessage),63  new LoadType("a blob: URI",64               eLoadInEverything,65               URL.createObjectURL(new Blob(["<script>(opener||parent).postMessage(isSecureContext, '*')</script>"], {type: "text/html"})),66               eSecureIfCreatorSecure,67               eResultFromPostMessage),68  new LoadType("a srcdoc",69               // popup not relevant:70               eLoadInUnsandboxedIframe | eLoadInSandboxedIframe,71               "<script>(opener||parent).postMessage(isSecureContext, '*')</script>",72               eSecureIfCreatorSecure,73               eResultFromPostMessage),74  new LoadType("a javascript: URI",75               // can't load in sandbox:76               eLoadInUnsandboxedIframe | eLoadInPopup,77               "javascript:(opener||parent).postMessage(isSecureContext, '*')",78               eSecureIfCreatorSecure,79               eResultFromPostMessage),80  new LoadType("about:blank",81               // can't obtain state if sandboxed:82               eLoadInUnsandboxedIframe | eLoadInPopup,83               "about:blank",84               eSecureIfCreatorSecure,85               eResultFromExaminationOnLoad),86  new LoadType("initial about:blank",87               // can't obtain state if sandboxed:88               eLoadInUnsandboxedIframe | eLoadInPopup,89               "about:blank", // we don't wait for this to load, so whatever90               eSecureIfCreatorSecure,91               eResultFromExaminationSync),92];93const loadTargets = [94  new LoadTarget("an iframe",          eLoadInUnsandboxedIframe),95  new LoadTarget("a sandboxed iframe", eLoadInSandboxedIframe),96  new LoadTarget("a popup",            eLoadInPopup),97];98function LoadType(description, loadInFlags, uri, expectedSecureFlag, resultFrom) {99  this.desc = description;100  this.loadInFlags = loadInFlags;101  this.uri = uri;102  this.expectedSecureFlag = expectedSecureFlag;103  this.resultFrom = resultFrom;104}105function LoadTarget(description, loadInFlag) {106  this.desc = description;107  this.loadInFlag = loadInFlag;108}109LoadTarget.prototype.open = function(loadType) {110  let loadTarget = this;111  this.currentTest.step(function() {112    assert_true((loadTarget.loadInFlag & loadType.loadInFlags) != 0,113                loadType.desc + " cannot be tested in " + loadTarget.desc);114  });115  if (this.loadInFlag == eLoadInUnsandboxedIframe) {116    let iframe = document.createElement("iframe");117    document.body.appendChild(iframe);118    iframe[loadType.desc == "a srcdoc" ? "srcdoc" : "src"] = loadType.uri;119    return iframe;120  }121  if (this.loadInFlag == eLoadInSandboxedIframe) {122    let iframe = document.body.appendChild(document.createElement("iframe"));123    iframe.setAttribute("sandbox", "allow-scripts");124    iframe[loadType.desc == "a srcdoc" ? "srcdoc" : "src"] = loadType.uri;125    return iframe;126  }127  if (this.loadInFlag == eLoadInPopup) {128    return window.open(loadType.uri);129  }130  this.currentTest.step(function() {131    assert_unreached("Unknown load type flag: " + loadInFlags);132  });133  return null;134}135LoadTarget.prototype.close = function(domTarget) {136  if (this.loadInFlag == eLoadInUnsandboxedIframe ||137      this.loadInFlag == eLoadInSandboxedIframe) {138    domTarget.remove();139    return;140  }141  if (this.loadInFlag == eLoadInPopup) {142    domTarget.close();143    return;144  }145  this.currentTest.step(function() {146    assert_unreached("Unknown load type flag: " + loadInFlags);147  });148}149LoadTarget.prototype.load_and_get_result_for = function(loadType) {150  if (!(loadType.loadInFlags & this.loadInFlag)) {151    return Promise.reject("not applicable");152  }153  if (!(this.loadInFlag & eLoadInPopup) &&154      location.protocol == "https:" &&155      loadType.uri.substr(0,5) == "http:") {156    // Mixed content blocker will prevent this load157    return Promise.reject("not applicable");158  }159  this.currentTest = async_test("Test Window.isSecureContext in " + this.desc +160                                " loading " + loadType.desc)161  if (loadType.resultFrom == eResultFromExaminationSync) {162    let domTarget = this.open(loadType);163    let result = domTarget instanceof Window ?164          domTarget.isSecureContext : domTarget.contentWindow.isSecureContext;165    this.close(domTarget);166    return Promise.resolve(result);167  }168  let target = this;169  if (loadType.resultFrom == eResultFromExaminationOnLoad) {170    return new Promise(function(resolve, reject) {171      function handleLoad(event) {172        let result = domTarget instanceof Window ?173              domTarget.isSecureContext : domTarget.contentWindow.isSecureContext;174        domTarget.removeEventListener("load", handleLoad);175        target.close(domTarget);176        resolve(result);177      }178      let domTarget = target.open(loadType);179      domTarget.addEventListener("load", handleLoad, false);180    });181  }182  if (loadType.resultFrom == eResultFromPostMessage) {183    return new Promise(function(resolve, reject) {184      function handleMessage(event) {185        window.removeEventListener("message", handleMessage);186        target.close(domTarget);187        resolve(event.data);188      }189      window.addEventListener("message", handleMessage, false);190      let domTarget = target.open(loadType);191   });192  }193  return Promise.reject("unexpected 'result from' type");194}195let current_type_index = -1;196let current_target_index = 0;197function run_next_test() {198  current_type_index++;199  if (current_type_index >= loadTypes.length) {200    current_type_index = 0;201    current_target_index++;202    if (current_target_index >= loadTargets.length) {203      done();204      return; // all test permutations complete205    }206  }207  let loadTarget = loadTargets[current_target_index];208  let loadType = loadTypes[current_type_index];209  loadTarget.load_and_get_result_for(loadType).then(210    function(value) {211      run_next_test_soon();212      loadTarget.currentTest.step(function() {213        if (loadType.expectedSecureFlag == eSecureNo) {214          assert_false(value, loadType.desc + " in " + loadTarget.desc + " should not create a Secure Context");215        } else if (loadType.expectedSecureFlag == eSecureIfCreatorSecure) {216          if (!window.isSecureContext) {217            assert_false(value, loadType.desc + " in " + loadTarget.desc + " should not create a Secure Context when its creator is not a Secure Context.");218          } else {219            assert_true(value, loadType.desc + " in " + loadTarget.desc + " should create a Secure Context when its creator is a Secure Context");220          }221        } else {222          assert_unreached(loadType.desc + " - unknown expected secure flag: " + expectedSecureFlag);223        }224        loadTarget.currentTest.done();225      });226    },227    function(failReason) {228      run_next_test_soon();229      if (failReason == "not applicable") {230        return;231      }232      loadTarget.currentTest.step(function() {233        assert_unreached(loadType.desc + " - got unexpected rejected promise");234      });235    }236  );237}238function run_next_test_soon() {239  setTimeout(run_next_test, 0);240}241function begin() {242  test(function() {243    if (location.protocol == "http:") {244      assert_false(isSecureContext,245                   "http: creator should not be a Secure Context");246    } else if (location.protocol == "https:") {247      assert_true(isSecureContext,248                  "https: creator should be a Secure Context");249    } else {250      assert_unreached("Unknown location.protocol");251    }252  });253  run_next_test();...Using AI Code Generation
1var wpt = new WebPagetest('www.webpagetest.org');2    if (err) {3        console.log(err);4    } else {5        console.log(data);6    }7});8var wpt = new WebPagetest('www.webpagetest.org');9    if (err) {10        console.log(err);11    } else {12        console.log(data);13    }14});15var wpt = new WebPagetest('www.webpagetest.org');16    if (err) {17        console.log(err);18    } else {19        console.log(data);20    }21});22var wpt = new WebPagetest('www.webpagetest.org');23    if (err) {24        console.log(err);25    } else {26        console.log(data);27    }28});29var wpt = new WebPagetest('www.webpagetest.org');30    if (err) {31        console.log(err);32    } else {33        console.log(data);34    }35});36var wpt = new WebPagetest('www.webpagetest.org');37    if (err) {38        console.log(err);39    } else {Using AI Code Generation
1driver.eExecuteScriptInSandboxedIframe("return document.title");2driver.eGetElementInSandboxedIframe("id", "lst-ib");3driver.eGetElementInSandboxedIframe("name", "q");4driver.eGetElementInSandboxedIframe("id", "lst-ib");5driver.eTypeInSandboxedIframe("Selenium WebDriver");6driver.eGetElementInSandboxedIframe("id", "lst-ib");Using AI Code Generation
1var iframe = document.createElement("iframe");2document.body.appendChild(iframe);3iframe.onload = function() {4    eCallInSandboxedIframe(iframe, "wpt.test", ["hello world"], function(result) {5      alert(result);6    });7  });8};Using AI Code Generation
1var wpt = new WebPageTest('www.webpagetest.org');2wpt.runTest(url, {3}, function(err, data) {4  console.log(data);5});6var wpt = new WebPageTest('www.webpagetest.org');7wpt.runTest(url, {8}, function(err, data) {9  console.log(data);10});11var wpt = new WebPageTest('www.webpagetest.org');12wpt.runTest(url, {13}, function(err, data) {14  console.log(data);15});16var wpt = new WebPageTest('www.webpagetest.org');17wpt.runTest(url, {18}, function(err, data) {19  console.log(data);20});21var wpt = new WebPageTest('www.webpagetest.org');22wpt.runTest(url, {23}, function(err, data) {24  console.log(data);25});Using AI Code Generation
1var wptextpattern = require('wptextpattern');2var eLoadInSandboxedIframe = wptextpattern.eLoadInSandboxedIframe;3var eGetIframeContent = wptextpattern.eGetIframeContent;4eLoadInSandboxedIframe(url, function (err, content) {5  if (err) {6    console.log(err);7  } else {8    console.log("content is " + content);9  }10});11eGetIframeContent(url, function (err, content) {12  if (err) {13    console.log(err);14  } else {15    console.log("content is " + content);16  }17});18var wptextpattern = require('wptextpattern');19var eGetIframeContent = wptextpattern.eGetIframeContent;20eGetIframeContent(url, function (err, content) {21  if (err) {22    console.log(err);23  } else {24    console.log("content is " + content);25  }26});27eGetIframeContent(url, function (err, content) {28  if (err) {29    console.log(err);30  } else {31    console.log("content is " + content);32  }33});34var wptextpattern = require('wptextpattern');Using AI Code Generation
1function onload(wpt) {2  var iframe = document.getElementById("iframe");3  wpt.sendMessage("loaded");4}5function onload(wpt) {6  var iframe = document.getElementById("iframe");7  wpt.sendMessage("loaded");8}9function onload(wpt) {10  var iframe = document.getElementById("iframe");11  wpt.sendMessage("loaded");12}13function onload(wpt) {14  var iframe = document.getElementById("iframe");15  wpt.eLoadInSandboxedIframe(iframe, "Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
