summaryrefslogtreecommitdiffstats
path: root/remote/cdp/test/browser/dom/browser_describeNode.js
blob: 931b908dfd213cde6a206e4169c6733d9c1b5834 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const DOC = toDataURL("<div id='content'><p>foo</p><p>bar</p></div>");
const DOC_FRAME = toDataURL(`<iframe src="${DOC}"></iframe>`);

add_task(async function objectIdInvalidTypes({ client }) {
  const { DOM } = client;

  for (const objectId of [null, true, 1, [], {}]) {
    let errorThrown = "";
    try {
      await DOM.describeNode({ objectId });
    } catch (e) {
      errorThrown = e.message;
    }
    ok(
      errorThrown.match(/objectId: string value expected/),
      `Fails with invalid type: ${objectId}`
    );
  }
});

add_task(async function objectIdUnknownValue({ client }) {
  const { DOM } = client;

  let errorThrown = "";
  try {
    await DOM.describeNode({ objectId: "foo" });
  } catch (e) {
    errorThrown = e.message;
  }
  ok(
    errorThrown.match(/Could not find object with given id/),
    "Fails with unknown objectId"
  );
});

add_task(async function objectIdIsNotANode({ client }) {
  const { DOM, Runtime } = client;

  await Runtime.enable();
  const { result } = await Runtime.evaluate({
    expression: "[42]",
  });

  let errorThrown = "";
  try {
    await DOM.describeNode({ objectId: result.objectId });
  } catch (e) {
    errorThrown = e.message;
  }
  ok(
    errorThrown.match(/Object id doesn't reference a Node/),
    "Fails if objectId doesn't reference a DOM node"
  );
});

add_task(async function objectIdAllProperties({ client }) {
  const { DOM, Page, Runtime } = client;

  await Page.enable();
  const { frameId } = await Page.navigate({ url: DOC });
  await Page.loadEventFired();

  await Runtime.enable();
  const { result } = await Runtime.evaluate({
    expression: `document.getElementById('content')`,
  });
  const { node } = await DOM.describeNode({
    objectId: result.objectId,
  });

  ok(!!node.nodeId, "The node has a node id");
  ok(!!node.backendNodeId, "The node has a backend node id");
  is(node.nodeName, "DIV", "Found expected node name");
  is(node.localName, "div", "Found expected local name");
  is(node.nodeType, 1, "Found expected node type");
  is(node.nodeValue, "", "Found expected node value");
  is(node.childNodeCount, 2, "Expected number of child nodes found");
  is(node.attributes.length, 2, "Found expected attribute's name and value");
  is(node.attributes[0], "id", "Found expected attribute name");
  is(node.attributes[1], "content", "Found expected attribute value");
  is(node.frameId, frameId, "Found expected frame id");
});

add_task(async function objectIdNoAttributes({ client }) {
  const { DOM, Runtime } = client;

  await Runtime.enable();
  const { result } = await Runtime.evaluate({
    expression: "document",
  });
  const { node } = await DOM.describeNode({
    objectId: result.objectId,
  });

  is(node.attributes, undefined, "No attributes returned");
});

add_task(async function objectIdDiffersForDifferentNodes({ client }) {
  const { DOM, Runtime } = client;

  await loadURL(DOC);

  await Runtime.enable();
  const { result: doc } = await Runtime.evaluate({
    expression: "document",
  });
  const { node: node1 } = await DOM.describeNode({
    objectId: doc.objectId,
  });

  const { result: body } = await Runtime.evaluate({
    expression: `document.getElementById('content')`,
  });
  const { node: node2 } = await DOM.describeNode({
    objectId: body.objectId,
  });

  for (const prop in node1) {
    if (["nodeValue", "frameId"].includes(prop)) {
      is(node1[prop], node2[prop], `Values of ${prop} are equal`);
    } else {
      isnot(node1[prop], node2[prop], `Values of ${prop} are different`);
    }
  }
});

add_task(async function objectIdDoesNotChangeForTheSameNode({ client }) {
  const { DOM, Runtime } = client;

  await Runtime.enable();
  const { result } = await Runtime.evaluate({
    expression: "document",
  });
  const { node: node1 } = await DOM.describeNode({
    objectId: result.objectId,
  });
  const { node: node2 } = await DOM.describeNode({
    objectId: result.objectId,
  });

  for (const prop in node1) {
    is(node1[prop], node2[prop], `Values of ${prop} are equal`);
  }
});

add_task(async function frameIdForFrameElement({ client }) {
  const { DOM, Page, Runtime } = client;

  await Page.enable();

  const frameAttached = Page.frameAttached();
  await loadURL(DOC_FRAME);
  const { frameId, parentFrameId } = await frameAttached;

  await Runtime.enable();

  const { result: frameObj } = await Runtime.evaluate({
    expression: "document.getElementsByTagName('iframe')[0]",
  });
  const { node: frame } = await DOM.describeNode({
    objectId: frameObj.objectId,
  });

  is(frame.frameId, frameId, "Reported frameId is from the frame itself");
  isnot(
    frame.frameId,
    parentFrameId,
    "Reported frameId is not the parentFrameId"
  );
});