summaryrefslogtreecommitdiffstats
path: root/dom/base/test/unit/test_generate_xpath.js
blob: e8c704369096185fbedd0495be0ed7b2631c4465 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function run_test() {
  test_generate_xpath();
}

// TEST CODE

function test_generate_xpath() {
  let docString = `
    <html>
    <body>
      <label><input type="checkbox" id="input1" />Input 1</label>
      <label><input type="checkbox" id="input2'" />Input 2</label>
      <label><input type="checkbox" id='"input3"' />Input 3</label>
      <label><input type="checkbox"/>Input 4</label>
      <label><input type="checkbox" />Input 5</label>
    </body>
    </html>
  `;
  let doc = getParser().parseFromString(docString, "text/html");

  // Test generate xpath for body.
  info("Test generate xpath for body node");
  let body = doc.getElementsByTagName("body")[0];
  let bodyXPath = body.generateXPath();
  let bodyExpXPath = "/xhtml:html/xhtml:body";
  equal(bodyExpXPath, bodyXPath, " xpath generated for body");

  // Test generate xpath for input with id.
  info("Test generate xpath for input with id");
  let inputWithId = doc.getElementById("input1");
  let inputWithIdXPath = inputWithId.generateXPath();
  let inputWithIdExpXPath = "//xhtml:input[@id='input1']";
  equal(
    inputWithIdExpXPath,
    inputWithIdXPath,
    " xpath generated for input with id"
  );

  // Test generate xpath for input with id has single quote.
  info("Test generate xpath for input with id has single quote");
  let inputWithIdSingleQuote = doc.getElementsByTagName("input")[1];
  let inputWithIdXPathSingleQuote = inputWithIdSingleQuote.generateXPath();
  let inputWithIdExpXPathSingleQuote = '//xhtml:input[@id="input2\'"]';
  equal(
    inputWithIdExpXPathSingleQuote,
    inputWithIdXPathSingleQuote,
    " xpath generated for input with id"
  );

  // Test generate xpath for input with id has double quote.
  info("Test generate xpath for input with id has double quote");
  let inputWithIdDoubleQuote = doc.getElementsByTagName("input")[2];
  let inputWithIdXPathDoubleQuote = inputWithIdDoubleQuote.generateXPath();
  let inputWithIdExpXPathDoubleQuote = "//xhtml:input[@id='\"input3\"']";
  equal(
    inputWithIdExpXPathDoubleQuote,
    inputWithIdXPathDoubleQuote,
    " xpath generated for input with id"
  );

  // Test generate xpath for input with id has both single and double quote.
  info("Test generate xpath for input with id has single and double quote");
  let inputWithIdSingleDoubleQuote = doc.getElementsByTagName("input")[3];
  inputWithIdSingleDoubleQuote.setAttribute("id", "\"input'4");
  let inputWithIdXPathSingleDoubleQuote =
    inputWithIdSingleDoubleQuote.generateXPath();
  let inputWithIdExpXPathSingleDoubleQuote =
    "//xhtml:input[@id=concat('\"input',\"'\",'4')]";
  equal(
    inputWithIdExpXPathSingleDoubleQuote,
    inputWithIdXPathSingleDoubleQuote,
    " xpath generated for input with id"
  );

  // Test generate xpath for input without id.
  info("Test generate xpath for input without id");
  let inputNoId = doc.getElementsByTagName("input")[4];
  let inputNoIdXPath = inputNoId.generateXPath();
  let inputNoIdExpXPath = "/xhtml:html/xhtml:body/xhtml:label[5]/xhtml:input";
  equal(
    inputNoIdExpXPath,
    inputNoIdXPath,
    " xpath generated for input without id"
  );
}