Skip to content
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

Merged
merged 1 commit into from May 23, 2024

Conversation

DudaGod
Copy link
Member

@DudaGod DudaGod commented Apr 23, 2024

No description provided.

@@ -101,12 +102,18 @@ module.exports = class Browser {
}

_startCollectingCustomCommands() {
this._session.overwriteCommand("addCommand", (origCommand, name, ...rest) => {
if (!this._session[name]) {
Copy link
Member Author

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.

const browserCommands = getBrowserCommands();
const elementCommands = getElementCommands();

this._session.overwriteCommand("addCommand", (origCommand, name, wrapper, elementScope) => {
Copy link
Member Author

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",
Copy link
Member Author

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)),
Copy link
Member Author

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 }) => {
Copy link
Member Author

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>;
Copy link
Member Author

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;
Copy link
Member Author

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;
Copy link
Member Author

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", () => {
Copy link
Member Author

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", () => {
Copy link
Member Author

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

@DudaGod DudaGod force-pushed the HERMIONE-1511.fix_custom_commands branch 4 times, most recently from 1ffd457 to db5292d Compare April 25, 2024 08:41
src/browser/browser.js Outdated Show resolved Hide resolved
src/browser/browser.js Outdated Show resolved Hide resolved
@DudaGod DudaGod force-pushed the HERMIONE-1511.fix_custom_commands branch from db5292d to 8756e19 Compare May 23, 2024 11:47
@DudaGod DudaGod merged commit 5817e92 into master May 23, 2024
2 checks passed
@DudaGod DudaGod deleted the HERMIONE-1511.fix_custom_commands branch May 23, 2024 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants