Best JavaScript code snippet using playwright-internal
test_xml_serializer.js
Source:test_xml_serializer.js
1// The xml serializer uses the default line break of the plateform.2// So we need to know the value of this default line break, in order3// to build correctly the reference strings for tests.4// This variable will contain this value.5var LB;6function run_test() {7 if (mozinfo.os == "win") {8 LB = "\r\n";9 } else {10 LB = "\n";11 }12 for (var i = 0; i < tests.length && tests[i]; ++i) {13 tests[i].call();14 }15}16var tests = [17 test1,18 test2,19 test3,20 test4,21 test5,22 test6,23 test7,24 test8,25 test9,26 test10,27 null28];29function testString(str) {30 do_check_eq(roundtrip(str), str);31}32function test1() {33 // Basic round-tripping which we expect to hand back the same text34 // as we passed in (not strictly required for correctness in some of35 // those cases, but best for readability of serializer output)36 testString('<root/>');37 testString('<root><child/></root>');38 testString('<root xmlns=""/>');39 testString('<root xml:lang="en"/>');40 testString('<root xmlns="ns1"><child xmlns="ns2"/></root>')41 testString('<root xmlns="ns1"><child xmlns=""/></root>')42 testString('<a:root xmlns:a="ns1"><child/></a:root>')43 testString('<a:root xmlns:a="ns1"><a:child/></a:root>')44 testString('<a:root xmlns:a="ns1"><b:child xmlns:b="ns1"/></a:root>')45 testString('<a:root xmlns:a="ns1"><a:child xmlns:a="ns2"/></a:root>')46 testString('<a:root xmlns:a="ns1"><b:child xmlns:b="ns1" b:attr=""/></a:root>')47}48function test2() {49 // Test setting of "xmlns" attribute in the null namespace50 // XXXbz are these tests needed? What should happen here? These51 // may be bogus.52 // Setting random "xmlns" attribute53 var doc = ParseXML('<root xmlns="ns1"/>');54 doc.documentElement.setAttribute("xmlns", "ns2");55 do_check_serialize(doc);56}57function test3() {58 // Test basic appending of kids. Again, we're making assumptions59 // about how our serializer will serialize simple DOMs.60 var doc = ParseXML('<root xmlns="ns1"/>');61 var root = doc.documentElement;62 var child = doc.createElementNS("ns2", "child");63 root.appendChild(child);64 do_check_serialize(doc);65 do_check_eq(SerializeXML(doc), 66 '<root xmlns="ns1"><child xmlns="ns2"/></root>');67 68 doc = ParseXML('<root xmlns="ns1"/>');69 root = doc.documentElement;70 child = doc.createElementNS("ns2", "prefix:child");71 root.appendChild(child);72 do_check_serialize(doc);73 do_check_eq(SerializeXML(doc), 74 '<root xmlns="ns1"><prefix:child xmlns:prefix="ns2"/></root>');75 76 doc = ParseXML('<prefix:root xmlns:prefix="ns1"/>');77 root = doc.documentElement;78 child = doc.createElementNS("ns2", "prefix:child");79 root.appendChild(child);80 do_check_serialize(doc);81 do_check_eq(SerializeXML(doc), 82 '<prefix:root xmlns:prefix="ns1"><a0:child xmlns:a0="ns2"/>'+83 '</prefix:root>');84 85}86function test4() {87 // setAttributeNS tests88 var doc = ParseXML('<root xmlns="ns1"/>');89 var root = doc.documentElement;90 root.setAttributeNS("ns1", "prefix:local", "val");91 do_check_serialize(doc);92 do_check_eq(SerializeXML(doc),93 '<root xmlns="ns1" prefix:local="val" xmlns:prefix="ns1"/>');94 doc = ParseXML('<prefix:root xmlns:prefix="ns1"/>');95 root = doc.documentElement;96 root.setAttributeNS("ns1", "local", "val");97 do_check_serialize(doc);98 do_check_eq(SerializeXML(doc),99 '<prefix:root xmlns:prefix="ns1" prefix:local="val"/>');100 doc = ParseXML('<root xmlns="ns1"/>');101 root = doc.documentElement;102 root.setAttributeNS("ns2", "local", "val");103 do_check_serialize(doc);104 do_check_eq(SerializeXML(doc),105 '<root xmlns="ns1" a0:local="val" xmlns:a0="ns2"/>');106 // Handling of prefix-generation for non-null-namespace attributes107 // which have the same namespace as the current default namespace108 // (bug 301260).109 doc = ParseXML('<root xmlns="ns1"/>');110 root = doc.documentElement;111 root.setAttributeNS("ns1", "local", "val");112 do_check_serialize(doc);113 do_check_eq(SerializeXML(doc),114 '<root xmlns="ns1" a0:local="val" xmlns:a0="ns1"/>');115 // Tree-walking test116 doc = ParseXML('<root xmlns="ns1" xmlns:a="ns2">'+117 '<child xmlns:b="ns2" xmlns:a="ns3">'+118 '<child2/></child></root>');119 root = doc.documentElement;120 // Have to QI here -- no classinfo flattening in xpcshell, apparently121 var node = root.firstChild.firstChild.QueryInterface(nsIDOMElement);122 node.setAttributeNS("ns4", "l1", "v1");123 node.setAttributeNS("ns4", "p2:l2", "v2");124 node.setAttributeNS("", "l3", "v3");125 node.setAttributeNS("ns3", "l4", "v4");126 node.setAttributeNS("ns3", "p5:l5", "v5");127 node.setAttributeNS("ns3", "a:l6", "v6");128 node.setAttributeNS("ns2", "l7", "v7");129 node.setAttributeNS("ns2", "p8:l8", "v8");130 node.setAttributeNS("ns2", "b:l9", "v9");131 node.setAttributeNS("ns2", "a:l10", "v10");132 node.setAttributeNS("ns1", "a:l11", "v11");133 node.setAttributeNS("ns1", "b:l12", "v12");134 node.setAttributeNS("ns1", "l13", "v13");135 do_check_serialize(doc);136 // Note: we end up with "a2" as the prefix on "l11" and "l12" because we use137 // "a1" earlier, and discard it in favor of something we get off the138 // namespace stack, apparently139 do_check_eq(SerializeXML(doc),140 '<root xmlns="ns1" xmlns:a="ns2">'+141 '<child xmlns:b="ns2" xmlns:a="ns3">'+142 '<child2 a0:l1="v1" xmlns:a0="ns4"' +143 ' a0:l2="v2"' +144 ' l3="v3"' +145 ' a:l4="v4"' +146 ' a:l5="v5"' +147 ' a:l6="v6"' +148 ' b:l7="v7"' +149 ' b:l8="v8"' +150 ' b:l9="v9"' +151 ' b:l10="v10"' +152 ' a2:l11="v11" xmlns:a2="ns1"' +153 ' a2:l12="v12"' +154 ' a2:l13="v13"/></child></root>');155}156function test5() {157 // Handling of kids in the null namespace when the default is a158 // different namespace (bug 301260).159 var doc = ParseXML('<root xmlns="ns1"/>')160 var child = doc.createElement('child');161 doc.documentElement.appendChild(child);162 do_check_serialize(doc);163 do_check_eq(SerializeXML(doc),164 '<root xmlns="ns1"><child xmlns=""/></root>');165}166function test6() {167 // Handling of not using a namespace prefix (or default namespace!)168 // that's not bound to our namespace in our scope (bug 301260).169 var doc = ParseXML('<prefix:root xmlns:prefix="ns1"/>');170 var root = doc.documentElement;171 var child1 = doc.createElementNS("ns2", "prefix:child1");172 var child2 = doc.createElementNS("ns1", "prefix:child2");173 child1.appendChild(child2);174 root.appendChild(child1);175 do_check_serialize(doc);176 do_check_eq(SerializeXML(doc),177 '<prefix:root xmlns:prefix="ns1"><a0:child1 xmlns:a0="ns2">'+178 '<prefix:child2/></a0:child1></prefix:root>');179 doc = ParseXML('<root xmlns="ns1"><prefix:child1 xmlns:prefix="ns2"/></root>');180 root = doc.documentElement;181 child1 = root.firstChild;182 child2 = doc.createElementNS("ns1", "prefix:child2");183 child1.appendChild(child2);184 do_check_serialize(doc);185 do_check_eq(SerializeXML(doc),186 '<root xmlns="ns1"><prefix:child1 xmlns:prefix="ns2">'+187 '<child2/></prefix:child1></root>');188 doc = ParseXML('<prefix:root xmlns:prefix="ns1">'+189 '<prefix:child1 xmlns:prefix="ns2"/></prefix:root>');190 root = doc.documentElement;191 child1 = root.firstChild;192 child2 = doc.createElementNS("ns1", "prefix:child2");193 child1.appendChild(child2);194 do_check_serialize(doc);195 do_check_eq(SerializeXML(doc),196 '<prefix:root xmlns:prefix="ns1"><prefix:child1 xmlns:prefix="ns2">'+197 '<a0:child2 xmlns:a0="ns1"/></prefix:child1></prefix:root>');198 199 doc = ParseXML('<root xmlns="ns1"/>');200 root = doc.documentElement;201 child1 = doc.createElementNS("ns2", "child1");202 child2 = doc.createElementNS("ns1", "child2");203 child1.appendChild(child2);204 root.appendChild(child1);205 do_check_serialize(doc);206 do_check_eq(SerializeXML(doc),207 '<root xmlns="ns1"><child1 xmlns="ns2"><child2 xmlns="ns1"/>'+208 '</child1></root>');209}210function test7() {211 // Handle xmlns attribute declaring a default namespace on a non-namespaced212 // element (bug 326994).213 var doc = ParseXML('<root xmlns=""/>')214 var root = doc.documentElement;215 root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns",216 "http://www.w3.org/1999/xhtml");217 do_check_serialize(doc);218 do_check_eq(SerializeXML(doc), '<root/>');219 doc = ParseXML('<root xmlns=""><child1/></root>')220 root = doc.documentElement;221 root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns",222 "http://www.w3.org/1999/xhtml");223 do_check_serialize(doc);224 do_check_eq(SerializeXML(doc), '<root><child1/></root>');225 doc = ParseXML('<root xmlns="http://www.w3.org/1999/xhtml">' +226 '<child1 xmlns=""><child2/></child1></root>')227 root = doc.documentElement;228 // No interface flattening in xpcshell229 var child1 = root.firstChild.QueryInterface(nsIDOMElement);230 child1.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns",231 "http://www.w3.org/1999/xhtml");232 do_check_serialize(doc);233 do_check_eq(SerializeXML(doc),234 '<root xmlns="http://www.w3.org/1999/xhtml"><child1 xmlns="">' +235 '<child2/></child1></root>');236 doc = ParseXML('<root xmlns="http://www.w3.org/1999/xhtml">' +237 '<child1 xmlns="">' +238 '<child2 xmlns="http://www.w3.org/1999/xhtml"></child2>' +239 '</child1></root>')240 root = doc.documentElement;241 // No interface flattening in xpcshell242 child1 = root.firstChild.QueryInterface(nsIDOMElement);243 var child2 = child1.firstChild.QueryInterface(nsIDOMElement);244 child1.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns",245 "http://www.w3.org/1999/xhtml");246 child2.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "");247 do_check_serialize(doc);248 do_check_eq(SerializeXML(doc),249 '<root xmlns="http://www.w3.org/1999/xhtml"><child1 xmlns="">' +250 '<a0:child2 xmlns:a0="http://www.w3.org/1999/xhtml" xmlns=""></a0:child2></child1></root>');251}252function test8() {253 // Test behavior of serializing with a given charset.254 var str1 = '<?xml version="1.0" encoding="ISO-8859-1"?>'+LB+'<root/>';255 var str2 = '<?xml version="1.0" encoding="UTF8"?>'+LB+'<root/>';256 var doc1 = ParseXML(str1);257 var doc2 = ParseXML(str2);258 var p = Pipe();259 DOMSerializer().serializeToStream(doc1, p.outputStream, "ISO-8859-1");260 p.outputStream.close();261 do_check_eq(ScriptableInput(p).read(-1), str1);262 p = Pipe();263 DOMSerializer().serializeToStream(doc2, p.outputStream, "ISO-8859-1");264 p.outputStream.close();265 do_check_eq(ScriptableInput(p).read(-1), str1);266 p = Pipe();267 DOMSerializer().serializeToStream(doc1, p.outputStream, "UTF8");268 p.outputStream.close();269 do_check_eq(ScriptableInput(p).read(-1), str2);270 p = Pipe();271 DOMSerializer().serializeToStream(doc2, p.outputStream, "UTF8");272 p.outputStream.close();273 do_check_eq(ScriptableInput(p).read(-1), str2);274}275function test9() {276 // Test behavior of serializing between given charsets, using277 // ISO-8859-1-representable text.278 var contents = '<root>' +279 '\u00BD + \u00BE == \u00BD\u00B2 + \u00BC + \u00BE' +280 '</root>';281 var str1 = '<?xml version="1.0" encoding="ISO-8859-1"?>'+ LB + contents;282 var str2 = '<?xml version="1.0" encoding="UTF8"?>'+ LB + contents;283 var str3 = '<?xml version="1.0" encoding="UTF-16"?>'+ LB + contents;284 var doc1 = ParseXML(str1);285 var doc2 = ParseXML(str2);286 var doc3 = ParseXML(str3);287 checkSerialization(doc1, "ISO-8859-1", str1);288 checkSerialization(doc2, "ISO-8859-1", str1);289 checkSerialization(doc3, "ISO-8859-1", str1);290 checkSerialization(doc1, "UTF8", str2);291 checkSerialization(doc2, "UTF8", str2);292 checkSerialization(doc3, "UTF8", str2);293 checkSerialization(doc1, "UTF-16", str3);294 checkSerialization(doc2, "UTF-16", str3);295 checkSerialization(doc3, "UTF-16", str3);296}297function test10() {298 // Test behavior of serializing between given charsets, using299 // Unicode characters (XXX but only BMP ones because I don't know300 // how to create one with non-BMP characters, either with JS strings301 // or using DOM APIs).302 var contents = '<root>' +303 'AZaz09 \u007F ' + // U+000000 to U+00007F304 '\u0080 \u0398 \u03BB \u0725 ' + // U+000080 to U+0007FF305 '\u0964 \u0F5F \u20AC \uFFFB' + // U+000800 to U+00FFFF306 '</root>';307 var str1 = '<?xml version="1.0" encoding="UTF8"?>'+ LB + contents;308 var str2 = '<?xml version="1.0" encoding="UTF-16"?>'+ LB + contents;309 var doc1 = ParseXML(str1);310 var doc2 = ParseXML(str2);311 checkSerialization(doc1, "UTF8", str1);312 checkSerialization(doc2, "UTF8", str1);313 checkSerialization(doc1, "UTF-16", str2);314 checkSerialization(doc2, "UTF-16", str2);315}316function checkSerialization(doc, toCharset, expectedString) {317 var p = Pipe();318 DOMSerializer().serializeToStream(doc, p.outputStream, toCharset);319 p.outputStream.close();320 var cin = C["@mozilla.org/intl/converter-input-stream;1"]321 .createInstance(I.nsIConverterInputStream);322 cin.init(p.inputStream, toCharset, 1024, 0x0);323 // compare the first expectedString.length characters for equality324 var outString = {};325 var count = cin.readString(expectedString.length, outString);326 do_check_true(count == expectedString.length);327 do_check_true(outString.value == expectedString);328 // if there's anything more in the stream, it's a bug329 do_check_eq(0, cin.readString(1, outString));330 do_check_eq(outString.value, "");...
serialize.js
Source:serialize.js
...10 res += element;11 } else {12 res += `<${element.type} ${serializeAttributes(element.attributes)}>`;13 element.children.forEach(child => {14 res += serializeXML(child);15 });16 res += `</${element.type}>`;17 }18 return res;19};...
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const page = await browser.newPage();5 const html = await page.content();6 console.log(html);7 await browser.close();8})();
Using AI Code Generation
1const { serializeXML } = require('@playwright/test/lib/utils/serializeXML');2const { test } = require('@playwright/test');3test('test', async ({ page }) => {4 await page.setContent('<html><body><div>Hi</div></body></html>');5 const html = await page.$('html');6 console.log(await html.innerHTML());7 console.log(serializeXML(await html.innerHTML()));8});
Using AI Code Generation
1const { serializeXML } = require('@playwright/test/lib/utils/serializeXML');2const { test } = require('@playwright/test');3test('test', async ({ page }) => {4 await page.setContent('<html><body><div>Hi</div></body></html>');5 const html = await page.$('html');6 console.log(await html.innerHTML());7 console.log(serializeXML(await html.innerHTML()));8});
Using AI Code Generation
1const { serializeXML } = require('playwright-core/lib/server/dom.js');2const { parse } = require('playwright-core/lib/server/common/xml.js');3const { readFileSync } = require('fs');4const path = require('path');5const html = readFileSync(path.join(__dirname, 'test.html'), 'utf8');6const doc = parse(html);7const xml = serializeXML(doc);8console.log(xml);9cont f = rqui('fs10chomiumt');11(async () => {12 cons browser = awa chromum.aunch();13 cont contet = awit bow.newContext(14 playw page =rawait conteit.newPage();15 doawait ge._delegate.seializeXML();16 f.writeFilSync'dom.ml', do);17 await browser.cose();18})(
Using AI Code Generation
1const fs = require('fs');2const { chromium } = require('playwright');3(async () => {4 const browser = await chromium.launch();5 const context = await browser.newContext();6 const page = await context.newPage();7 const dom = await page._delegate.serializeXML();8 fs.writeFileSync('dom.xml', dom);9 await browser.close();10})();
Using AI Code Generation
1const { serializeXML } = require('playwright/lib/utils/serializer');2const { parse } = require('playwright/lib/utils/xmlParser');3const xml = await page.content();4const xmlTree = parse(xml);5const serializedXML = serializeXML(xmlTree);6console.log(serializedXML);
Using AI Code Generation
1const { serializeXML } = require('playwright/lib/server/dom.js');2const { parse } = require('playwright/lib/server/common/xml.js');3const dom = parse('<div id="a">Hello</div>');4const xml = serializeXML(dom);5console.log(xml);6serializeXML(dom: Node): string
Using AI Code Generation
1const playwright = require('playwright');2const { serializeXML } = require('playwright/lib/server/serializer');3(async () => {4 const browser = await playwright.chromium.launch();5 const context = await browser.newContext();6 const page = await context.newPage();7 const xml = await serializeXML(page);8 const fs = require('fs');9 fs.writeFileSync('test.xml', xml);10 await browser.close();11})();
Using AI Code Generation
1const { serializeXML } = require('playwright/lib/server/dom.js');2console.log(serializeXML(document));3const playwright = require('playwright');4(async () => {5 const browser = await playwright['chromium'].launch({ headless: false });6 const context = await browser.newContext();7 const page = await context.newPage();8 const html = await page.$eval('section', section => section.innerHTML);9 console.log(html);10 await browser.close();11})();
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!