Skip to content

Commit 50a8f39

Browse files
authored
chore: add helper for a shorter syntax in xpath test file (#2729)
1 parent 58e07b2 commit 50a8f39

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

test/unit/utils-locator-generation/xpath.spec.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ function testXPath(doc, node, expectedXPath) {
2020
expect(xpath.select(expectedXPath, doc)[0]).toEqual(node);
2121
}
2222

23+
// Helper for a shorter syntax of getting elements by tag name
24+
const byTag = (doc, tagName) => doc.getElementsByTagName(tagName);
25+
2326
describe('utils/locator-generation/xpath.js', function () {
2427
describe('#getOptimalXPath', function () {
2528
describe('using only the node itself', function () {
@@ -55,31 +58,31 @@ describe('utils/locator-generation/xpath.js', function () {
5558
<node></node>
5659
<node></node>
5760
</root>`);
58-
testXPath(doc, doc.getElementsByTagName('node')[0], '/root/node[1]');
61+
testXPath(doc, byTag(doc, 'node')[0], '/root/node[1]');
5962
});
6063

6164
it('should use tagname if the node has a unique tag with no attributes', function () {
6265
const doc = xmlToDOM(`<root>
6366
<node></node>
6467
<other-node></other-node>
6568
</root>`);
66-
testXPath(doc, doc.getElementsByTagName('node')[0], '//node');
69+
testXPath(doc, byTag(doc, 'node')[0], '//node');
6770
});
6871

6972
it('should use a unique attribute and index if the node has identical siblings with the same unique attribute', function () {
7073
const doc = xmlToDOM(`<root>
7174
<node id='foo'></node>
7275
<node id='foo'></node>
7376
</root>`);
74-
testXPath(doc, doc.getElementsByTagName('node')[0], '(//node[@id="foo"])[1]');
77+
testXPath(doc, byTag(doc, 'node')[0], '(//node[@id="foo"])[1]');
7578
});
7679

7780
it('should combine unique and maybe-unique attributes if only the maybe-unique attribute differs', function () {
7881
const doc = xmlToDOM(`<root>
7982
<node id='foo' text='bar'></node>
8083
<node id='foo' text='yo'></node>
8184
</root>`);
82-
const children = doc.getElementsByTagName('node');
85+
const children = byTag(doc, 'node');
8386
testXPath(doc, children[0], '//node[@id="foo" and @text="bar"]');
8487
testXPath(doc, children[1], '//node[@id="foo" and @text="yo"]');
8588
});
@@ -92,11 +95,11 @@ describe('utils/locator-generation/xpath.js', function () {
9295
<another-node>Bar</another-node>
9396
<other-node>Baz</other-node>
9497
</root>`);
95-
testXPath(doc, doc.getElementsByTagName('node')[0], '/root/node[1]');
96-
testXPath(doc, doc.getElementsByTagName('node')[1], '/root/node[2]');
97-
testXPath(doc, doc.getElementsByTagName('other-node')[0], '/root/other-node[1]');
98-
testXPath(doc, doc.getElementsByTagName('other-node')[1], '/root/other-node[2]');
99-
testXPath(doc, doc.getElementsByTagName('another-node')[0], '//another-node');
98+
testXPath(doc, byTag(doc, 'node')[0], '/root/node[1]');
99+
testXPath(doc, byTag(doc, 'node')[1], '/root/node[2]');
100+
testXPath(doc, byTag(doc, 'other-node')[0], '/root/other-node[1]');
101+
testXPath(doc, byTag(doc, 'other-node')[1], '/root/other-node[2]');
102+
testXPath(doc, byTag(doc, 'another-node')[0], '//another-node');
100103
});
101104
});
102105

@@ -108,8 +111,8 @@ describe('utils/locator-generation/xpath.js', function () {
108111
<node>World</node>
109112
</parent>
110113
</root>`);
111-
testXPath(doc, doc.getElementsByTagName('node')[0], '//parent[@id="foo"]/node[1]');
112-
testXPath(doc, doc.getElementsByTagName('node')[1], '//parent[@id="foo"]/node[2]');
114+
testXPath(doc, byTag(doc, 'node')[0], '//parent[@id="foo"]/node[1]');
115+
testXPath(doc, byTag(doc, 'node')[1], '//parent[@id="foo"]/node[2]');
113116
});
114117

115118
it('should use indices for both parent and node if there are no unique attributes', function () {
@@ -124,10 +127,10 @@ describe('utils/locator-generation/xpath.js', function () {
124127
<node>Bar</node>
125128
</parent>
126129
</root>`);
127-
testXPath(doc, doc.getElementsByTagName('node')[0], '/root/parent[1]/node[1]');
128-
testXPath(doc, doc.getElementsByTagName('node')[1], '/root/parent[1]/node[2]');
129-
testXPath(doc, doc.getElementsByTagName('node')[2], '/root/parent[2]/node[1]');
130-
testXPath(doc, doc.getElementsByTagName('node')[3], '/root/parent[2]/node[2]');
130+
testXPath(doc, byTag(doc, 'node')[0], '/root/parent[1]/node[1]');
131+
testXPath(doc, byTag(doc, 'node')[1], '/root/parent[1]/node[2]');
132+
testXPath(doc, byTag(doc, 'node')[2], '/root/parent[2]/node[1]');
133+
testXPath(doc, byTag(doc, 'node')[3], '/root/parent[2]/node[2]');
131134
});
132135

133136
it('should use a unique attribute and index for parent if it has an identical sibling with the same unique attribute', function () {
@@ -139,7 +142,7 @@ describe('utils/locator-generation/xpath.js', function () {
139142
<node>World</node>
140143
</parent>
141144
</root>`);
142-
testXPath(doc, doc.getElementsByTagName('node')[0], '(//parent[@id="foo"])[1]/node');
145+
testXPath(doc, byTag(doc, 'node')[0], '(//parent[@id="foo"])[1]/node');
143146
});
144147
});
145148

0 commit comments

Comments
 (0)