Skip to content

Latest commit

 

History

History
176 lines (120 loc) · 3.27 KB

File metadata and controls

176 lines (120 loc) · 3.27 KB

Elements

Elements has a "lazy" interface (as it was in protractor). The collection is not resolved until an action is performed on it.

All examples work identically with both playwrightWD and seleniumWD.


Selector strategies

const { playwrightWD } = require('promod');
const { $$ } = playwrightWD;

const byCss = $$('.class #id div a[href*="link"]');
const byXpath = $$('xpath=.//div[@data-test="id"]/span');
const byJS = $$(() => document.querySelectorAll('div > span'));
const byCustom = $$({ query: 'button', text: 'Submit' });

get

Returns a single element from the collection by index. Negative indexes count from the end.

const buttons = $$('button');

const first = buttons.get(0);
const third = buttons.get(2);
const last = buttons.get(-1);

await first.click();

first

Shorthand for get(0).

const button = $$('button').first();
await button.click();

last

Shorthand for get(-1).

const button = $$('button').last();
await button.click();

count

Returns the number of elements matching the selector.

const count = await $$('button').count();

getFirstVisible

Returns the first visible (displayed) element in the collection.

const visibleButton = $$('button').getFirstVisible();
await visibleButton.click();

getAllVisible

Returns a new elements collection containing only visible elements.

const visibleButtons = $$('button').getAllVisible();
const visibleCount = await visibleButtons.count();

each

Iterates over each element in the collection.

const buttons = $$('button');

await buttons.each(async (button, index) => {
  await button.click();
});

map

Maps each element to a value.

const buttons = $$('button');

const texts = await buttons.map(async (button) => {
  return await button.getText();
});

some

Returns true if the callback returns true for at least one element.

const buttons = $$('button');

const isSomeVisible = await buttons.some(async (button) => {
  return await button.isDisplayed();
});

every

Returns true if the callback returns true for every element.

const buttons = $$('button');

const allVisible = await buttons.every(async (button) => {
  return await button.isDisplayed();
});

find

Finds the first element matching the callback condition.

const buttons = $$('button');

const submitBtn = await buttons.find(async (button) => {
  return (await button.getText()) === 'Submit';
});

await submitBtn.click();

filter

Returns a new elements collection with elements matching the callback condition.

const buttons = $$('button');

const enabledButtons = buttons.filter(async (button) => {
  return await button.isEnabled();
});

const count = await enabledButtons.count();

getEngineElements

Returns the underlying native engine elements (Playwright Locator[] / Selenium WebElement[]).

const nativeElements = await $$('button').getEngineElements();