New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(ct): correctly support custom element commands #915
Conversation
@@ -101,12 +102,18 @@ module.exports = class Browser { | |||
} | |||
|
|||
_startCollectingCustomCommands() { | |||
this._session.overwriteCommand("addCommand", (origCommand, name, ...rest) => { | |||
if (!this._session[name]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a mistake here. It is not possible to check the presence of a command on an element in this way. also, if the user has a custom command with the same name on the browser and the element (our case with assertView
for example), then it was added only for the browser instance.
So I rewrite it to use command list for browser and element from history.
src/browser/browser.js
Outdated
const browserCommands = getBrowserCommands(); | ||
const elementCommands = getElementCommands(); | ||
|
||
this._session.overwriteCommand("addCommand", (origCommand, name, wrapper, elementScope) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need elementScope
field in order to understand how exactly I should add command in browser environment.
@@ -23,6 +24,7 @@ const wdioBrowserCommands = [ | |||
"mockClearAll", | |||
"mockRestoreAll", | |||
"newWindow", | |||
"overwriteCommand", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these commands were skipped for some reason
@@ -88,7 +88,7 @@ const overwriteBrowserCommands = (session, callstack) => | |||
overwriteCommands({ | |||
session, | |||
callstack, | |||
commands: cmds.getBrowserCommands(), | |||
commands: cmds.getBrowserCommands().filter(cmd => !shouldNotWrapCommand(cmd)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I should skip addCommand
and overwriteCommand
return monad(window.__testplane__.sessionId, commandWrapper); | ||
const browser = monad(window.__testplane__.sessionId, commandWrapper); | ||
|
||
window.__testplane__.customCommands.forEach(({ name, elementScope }) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All custom commands are now added using the addCommand
call
const modulesToMock = DEFAULT_MODULES_TO_MOCK.reduce((acc, val) => _.set(acc, val, mockDefaultModulePath), { | ||
"@wdio/logger": mockWdioLoggerPath, | ||
"import-meta-resolve": mockImportMetaResolvePath, | ||
}) as Record<string, string>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here, instead of using virtual modules, I started using mock files from the file system
const wdioInstance = element ? await session.$(element) : session; | ||
|
||
if (isWdioElement(wdioInstance) && !wdioInstance.selector) { | ||
wdioInstance.selector = element?.selector as Selector; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need recover selector because it is undefined when element found by elementId
session: WebdriverIO.Browser, | ||
element?: WebdriverIO.Element, | ||
): Promise<WebdriverIO.Browser | WebdriverIO.Element> { | ||
const wdioInstance = element ? await session.$(element) : session; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here element is found by elementId
(BrowserAgent.prototype.getBrowser as SinonStub).resolves(browser); | ||
const socket = mkSocket_() as BrowserViteSocket; | ||
socketClientStub.returns(socket); | ||
describe("call command on element instance", () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to combine the tests for the element and for the browser, but the code turns out to be too complicated
const browser = mkBrowser_(); | ||
browser.publicAPI.execute = sandbox.stub().resolves(result); | ||
(BrowserAgent.prototype.getBrowser as SinonStub).resolves(browser); | ||
describe("call command on browser instance", () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here just add one additional describe
without changes in tests
1ffd457
to
db5292d
Compare
db5292d
to
8756e19
Compare
No description provided.