Skip to content

Commit 193b4b4

Browse files
committed
test: avoid false positives in xpath @Class tests
1 parent 8f088d2 commit 193b4b4

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

packages/scratch-gui/test/helpers/selenium-helper.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,23 @@ class SeleniumHelper {
145145
/**
146146
* List of useful xpath scopes for finding elements.
147147
* @returns {object} An object mapping names to xpath strings.
148+
* @note Do not check for an exact class name with `contains(@class, "foo")`: that will match `class="foo2"`.
149+
* Instead, use `contains(concat(" ", @class, " "), " foo ")`, which in this example will correctly report that
150+
* " foo2 " does not contain " foo ". Similarly, to check if an element has any class starting with "foo", use
151+
* `contains(concat(" ", @class), " foo")`. Checking with `starts-with(@class, "foo")`, for example, will only
152+
* work if the whole "class" attribute starts with "foo" -- it will fail if another class is listed first.
148153
*/
149154
get scope () {
150155
return {
151156
blocksTab: "*[@id='react-tabs-1']",
152157
costumesTab: "*[@id='react-tabs-3']",
153-
modal: '*[@class="ReactModalPortal"]',
154-
reportedValue: '*[@class="blocklyDropDownContent"]',
158+
modal: '*[contains(concat(" ", @class, " "), " ReactModalPortal ")]',
159+
reportedValue: '*[contains(concat(" ", @class, " "), " blocklyDropDownContent ")]',
155160
soundsTab: "*[@id='react-tabs-5']",
156-
spriteTile: '*[starts-with(@class,"react-contextmenu-wrapper")]',
157-
menuBar: '*[contains(@class,"menu-bar_menu-bar_")]',
158-
monitors: '*[starts-with(@class,"stage_monitor-wrapper")]',
159-
contextMenu: '*[starts-with(@class,"react-contextmenu")]'
161+
spriteTile: '*[contains(concat(" ", @class, " "), " react-contextmenu-wrapper ")]',
162+
menuBar: '*[contains(concat(" ", @class), " menu-bar_menu-bar_")]',
163+
monitors: '*[contains(concat(" ", @class), " stage_monitor-wrapper_")]',
164+
contextMenu: '*[contains(concat(" ", @class, " "), " react-contextmenu ")]'
160165
};
161166
}
162167

@@ -343,9 +348,9 @@ class SeleniumHelper {
343348
// then finally click the toolbox text.
344349
try {
345350
await this.setTitle(`clickBlocksCategory ${categoryText}`);
346-
await this.findByXpath('//div[contains(@class, "blocks_blocks")]');
351+
await this.findByXpath('//div[contains(concat(" ", @class), " blocks_blocks_")]');
347352
await this.driver.sleep(100);
348-
await this.clickText(categoryText, 'div[contains(@class, "blocks_blocks")]');
353+
await this.clickText(categoryText, 'div[contains(concat(" ", @class), " blocks_blocks_")]');
349354
await this.driver.sleep(500); // Wait for scroll to finish
350355
} catch (cause) {
351356
throw await enhanceError(outerError, cause);

0 commit comments

Comments
 (0)