test_generate_xpath.js (3130B)
1 function run_test() { 2 test_generate_xpath(); 3 } 4 5 // TEST CODE 6 7 function test_generate_xpath() { 8 let docString = ` 9 <html> 10 <body> 11 <label><input type="checkbox" id="input1" />Input 1</label> 12 <label><input type="checkbox" id="input2'" />Input 2</label> 13 <label><input type="checkbox" id='"input3"' />Input 3</label> 14 <label><input type="checkbox"/>Input 4</label> 15 <label><input type="checkbox" />Input 5</label> 16 </body> 17 </html> 18 `; 19 let doc = getParser().parseFromString(docString, "text/html"); 20 21 // Test generate xpath for body. 22 info("Test generate xpath for body node"); 23 let body = doc.getElementsByTagName("body")[0]; 24 let bodyXPath = body.generateXPath(); 25 let bodyExpXPath = "/xhtml:html/xhtml:body"; 26 equal(bodyExpXPath, bodyXPath, " xpath generated for body"); 27 28 // Test generate xpath for input with id. 29 info("Test generate xpath for input with id"); 30 let inputWithId = doc.getElementById("input1"); 31 let inputWithIdXPath = inputWithId.generateXPath(); 32 let inputWithIdExpXPath = "//xhtml:input[@id='input1']"; 33 equal( 34 inputWithIdExpXPath, 35 inputWithIdXPath, 36 " xpath generated for input with id" 37 ); 38 39 // Test generate xpath for input with id has single quote. 40 info("Test generate xpath for input with id has single quote"); 41 let inputWithIdSingleQuote = doc.getElementsByTagName("input")[1]; 42 let inputWithIdXPathSingleQuote = inputWithIdSingleQuote.generateXPath(); 43 let inputWithIdExpXPathSingleQuote = '//xhtml:input[@id="input2\'"]'; 44 equal( 45 inputWithIdExpXPathSingleQuote, 46 inputWithIdXPathSingleQuote, 47 " xpath generated for input with id" 48 ); 49 50 // Test generate xpath for input with id has double quote. 51 info("Test generate xpath for input with id has double quote"); 52 let inputWithIdDoubleQuote = doc.getElementsByTagName("input")[2]; 53 let inputWithIdXPathDoubleQuote = inputWithIdDoubleQuote.generateXPath(); 54 let inputWithIdExpXPathDoubleQuote = "//xhtml:input[@id='\"input3\"']"; 55 equal( 56 inputWithIdExpXPathDoubleQuote, 57 inputWithIdXPathDoubleQuote, 58 " xpath generated for input with id" 59 ); 60 61 // Test generate xpath for input with id has both single and double quote. 62 info("Test generate xpath for input with id has single and double quote"); 63 let inputWithIdSingleDoubleQuote = doc.getElementsByTagName("input")[3]; 64 inputWithIdSingleDoubleQuote.setAttribute("id", "\"input'4"); 65 let inputWithIdXPathSingleDoubleQuote = 66 inputWithIdSingleDoubleQuote.generateXPath(); 67 let inputWithIdExpXPathSingleDoubleQuote = 68 "//xhtml:input[@id=concat('\"input',\"'\",'4')]"; 69 equal( 70 inputWithIdExpXPathSingleDoubleQuote, 71 inputWithIdXPathSingleDoubleQuote, 72 " xpath generated for input with id" 73 ); 74 75 // Test generate xpath for input without id. 76 info("Test generate xpath for input without id"); 77 let inputNoId = doc.getElementsByTagName("input")[4]; 78 let inputNoIdXPath = inputNoId.generateXPath(); 79 let inputNoIdExpXPath = "/xhtml:html/xhtml:body/xhtml:label[5]/xhtml:input"; 80 equal( 81 inputNoIdExpXPath, 82 inputNoIdXPath, 83 " xpath generated for input without id" 84 ); 85 }