gristlabs/mocha-webdriver

`driver.findContent(...)` is broken in the repl

cpind opened this issue · 0 comments

cpind commented

Running node> driver.findContent('.foo', /BAR/); in the repl is broken. The culprit is in mocha-webdriver in lib/webdriver-plus.ts:

const contentRE = content instanceof RegExp ?
   content :
   new RegExp(content.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'));

This fails when making a call from the repl as node> driver.find('.foo', /Bar/);, it fails to recognize /Bar/ as being an instance of RegExp, so it treats it as a string and fails for not having a .replace function. The problem could be that the repl uses it's own RegExp constructor, making content instanceof RegExp inoperant in this context. One solution (tested on devs machine) is to change to

const contentRE = typeof content  === 'string' ?
   new RegExp(content.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')) :
   content;

.
I've thought of other solutions, such as passing the RegExp constructor to the repl context. It does solves the issues when using driver.findContent('.foo', new RegExp('BAR')); but not when using /BAR/. Maybe there could be a solution by customizing further the repl eval function maybe parsing argument, but lack of documentation makes it hard.