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

Feature/retryability #14

Merged
merged 4 commits into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion example/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"extends": "airbnb-base",
"plugins": ["cypress", "prettier"],
"env": {
"browser": true
"browser": true,
"cypress/globals": true
},
"rules": {
"prettier/prettier": "error",
Expand Down
6 changes: 6 additions & 0 deletions example/cypress/integration/todolist.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,10 @@ describe('Todo List', () => {
expect($el.value).to.equal(QUERY);
});
});

it('finds asynchronously added shadow DOM element', () => {
cy.shadowGet('todo-list-item', { timeout: 6000 })
.shadowFind('label')
.shadowContains('async item');
});
});
1 change: 1 addition & 0 deletions example/src/components/todo-form/todo-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default class TodoForm extends HTMLElement {
this.inputEl = this.root.querySelector('input');

this.formEl.addEventListener('submit', this.handleSubmit.bind(this));
// eslint-disable-next-line no-console
this.inputEl.addEventListener('change', e => console.log('change occurred', e));
}

Expand Down
8 changes: 8 additions & 0 deletions example/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,13 @@
<h1>Todo list</h1>
<todo-list></todo-list>
<script src="./index.js"></script>
<script>
setTimeout(() => {
const el = document.createElement('todo-list-item');
el.setAttribute('title', 'async item');
el.setAttribute('done', false);
document.body.appendChild(el);
}, 5000);
</script>
</body>
</html>
10 changes: 8 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference types="cypress" />

declare namespace Cypress {
type EventOptions = {
log?: Boolean;
Expand All @@ -8,13 +10,17 @@ declare namespace Cypress {
composed?: Boolean;
};

type CommandOptions = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears CommandOptions is already declared as an interface in Cypress library.

As such a typescript error is issued because you can't redeclare a type. Can we change this type name to ShadowOptions or something similar? If we extend CommandOptions by declaring this an interface, it results in prevSubject being required.

Copy link

@ashblox ashblox Apr 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this was awhile ago, but I'm having this exact problem. I'm using Typescript with Cypress and I am unable to specify a timeout on shadowGet() or shadowFind() because { timeout: 5000 } is not assignable to parameter of type Cypress.CommandOptions. Any chance this is something we could fix? I can open a new issue if necessary.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see these typings were not updated properly:
https:/abramenal/cypress-shadow-dom/blob/master/index.d.ts#L25

Are you able to create a MR with that fix?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it as simple as changing the shadowFind, shadowFirst, shadowGet and shadowLast methods to use ShadowCommandOptions? Also looks like you have a typo in ShadowEventOptios - I could fix that too.

timeout?: Number;
};

interface Chainable<Subject> {
shadowClick(options?: EventOptions): Chainable<Subject>;
shadowContains(content: string): Chainable<Subject>;
shadowEq(index: number): Chainable<Subject>;
shadowFind(selector: string): Chainable<Subject>;
shadowFind(selector: string, options: CommandOptions): Chainable<Subject>;
abramenal marked this conversation as resolved.
Show resolved Hide resolved
shadowFirst(): Chainable<Subject>;
shadowGet(selector: string): Chainable<Subject>;
shadowGet(selector: string, options: CommandOptions): Chainable<Subject>;
shadowLast(): Chainable<Subject>;
shadowTrigger(eventName: string, eventOptions?: EventOptions): Chainable<Subject>;
shadowType(content: string): Chainable<Subject>;
Expand Down
33 changes: 20 additions & 13 deletions src/commands/shadowFind/command.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import { validateElement, validateSelector, validateSubject } from '../../validators';
import { validateSelector, validateSubject } from '../../validators';
import resolveValue from '../../helpers/resolveValue';

export default function shadowFind(subject, selector) {
export default function shadowFind(subject, selector, options) {
validateSubject(subject);
validateSelector(selector);

const currentElement = subject[0].shadowRoot || subject[0];
const foundElements = currentElement.querySelectorAll(selector);
validateElement(foundElements[0]);
const elGetter = () => {
const currentElement = subject[0].shadowRoot || subject[0];
const found = currentElement.querySelectorAll(selector);

Cypress.log({
name: 'shadowFind',
message: `'${selector}'`,
consoleProps: () => ({
selector,
}),
});
return found;
};

return resolveValue(elGetter, options).then(foundElements => {
Cypress.log({
name: 'shadowFind',
message: `'${selector}'`,
consoleProps: () => ({
selector,
foundElements,
}),
});

return Cypress.cy.wrap(foundElements, { log: false });
return foundElements;
});
}
29 changes: 19 additions & 10 deletions src/commands/shadowGet/command.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { validateElement, validateSelector } from '../../validators';
import { validateSelector } from '../../validators';
import resolveValue from '../../helpers/resolveValue';

export default selector => {
export default (selector, options) => {
validateSelector(selector);

const element = Cypress.$(selector);
validateElement(element);

Cypress.log({
name: 'shadowGet',
message: `'${selector}'`,
consoleProps: () => ({ selector }),
Cypress._.defaults(options, {
log: true,
timeout: 10000,
abramenal marked this conversation as resolved.
Show resolved Hide resolved
});

return element;
const elGetter = () => {
return Cypress.$(selector);
};

return resolveValue(elGetter, options).then(element => {
Cypress.log({
name: 'shadowGet',
message: `'${selector}'`,
consoleProps: () => ({ selector, element }),
});

return element;
});
};
7 changes: 7 additions & 0 deletions src/helpers/resolveValue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function resolveValue(valueGetterFn, options = {}) {
abramenal marked this conversation as resolved.
Show resolved Hide resolved
return Cypress.Promise.try(valueGetterFn).then(value => {
return Cypress.cy.verifyUpcomingAssertions(value, options, {
onRetry: () => resolveValue(valueGetterFn, options),
});
});
}