Giter VIP home page Giter VIP logo

playwright-community / expect-playwright Goto Github PK

View Code? Open in Web Editor NEW
142.0 5.0 17.0 1.35 MB

Expect utility matcher functions to simplify expect statements for the usage with Playwright Test or Jest Playwright.

Home Page: https://playwright-community.github.io/expect-playwright/

License: MIT License

JavaScript 0.63% TypeScript 97.66% HTML 1.72%
playwright jest expect testing e2e-tests testing-tools playwright-test-runner

expect-playwright's Introduction

Playwright Community

A central home for tutorials, tooling, and showcases of the Playwright ecosystem.

This is the source of playwright.tech. A Next.js based application hosted on Vercel.

Powered by Vercel

expect-playwright's People

Contributors

atomicpages avatar chaomao avatar dependabot[bot] avatar dgabriel123 avatar mmarkelov avatar mskelton avatar mxschmitt avatar pjcalvo avatar voodoofrog avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

expect-playwright's Issues

Make test site for iframe tests

Currently, we are targeting various domains for our iframe tests. Ideally we would have our own test site that we can use. Perhaps just a collection of HTML files that are served with the serve package?

[BUG] Unhandled case for toHaveText where expect contains an elementHandle, text, and options object

Context:

  • expect-playwright Version: 0.2.2
  • Playwright Version: 0.16.0
  • Operating System: Mac OS X 10.14.6
  • Node version: 13.11.0
  • Browser: Chromium
  • Extra: [email protected]

Code Snippet

      await expect(myModal).toHaveText("Message in modal", {
        waitFor: "visible",
      });

Describe the bug

I'm getting an error Error: Invalid input length: 3.

if (args.length === 2) {

I inspected the code and added console.logs in lib/matchers/utils.js:

exports.getElementText = async (...args) => {
    /**
    * Handle the following cases:
    * - expect(page).foo("bar")
    * - expect(element).foo("bar")
    */
    console.log(args)
    console.log(args.length)
    if (args.length === 2) {

With output below:

    # .... very long object/elementHandle
          [Symbol(kCapture)]: false                                                                                          
        }                                                                                                                    
      },                                                                                                                     
      'You have entered incorrect one time pin.',                                                                            
      { waitFor: 'visible' }                                                                                                 
    ]                                                                                                                        
                                                                                                                             
      at Object.exports.getElementText (node_modules/.pnpm/registry.npmjs.org/expect-playwright/0.2.2/node_modules/expect-pla
ywright/lib/matchers/utils.js:23:15)                                                                                         
                                                                                                                             
  console.log                                                                                                                
    3           

if (args.length === 3 && lastElementHasType(args, "string") || args.length === 4 && lastElementHasType(args, "object")) {

Based on the line here, I believe the case * - expect(elementHandle).foo("bar", {obj}) has not yet been handled.

[Question] How do we setup default timeout?

Hey team,

May I ask how could we setup default timeout? as document said it's 30 seconds, but I don't find where we set it. Is it possible to set it from jest config also?

Thanks

PageWaitForSelectorOptions incorrectly contains waitFor

The option waitFor has changed to state as part of the Playwright 1.0 release, so this needs to change as well.

interface PageWaitForSelectorOptions {
  /**
   * Wait for element to become visible (`visible`), hidden (`hidden`), present in dom (`attached`) or not present in dom (`detached`). Defaults to `attached`.
   */
  waitFor?: "attached" | "detached" | "visible" | "hidden";
```

.not not exposed

playwright-core: ^1.7.1
playwright-expect: 0.3.0

I am trying following code per documentation

await expect(page).not.toHaveSelector("#foobar", {
  timeout: 1 * 1000
})

However I get following error:

    TypeError: Cannot read property 'toHaveSelector' of undefined

      11 | 
      12 | export const isWidgetMessageHidden = (page) => {
    > 13 |   return expect(page)
         |          ^
      14 |     .not
      15 |     .toHaveSelector(selectId(REFERRAL.WIDGET_MESSAGE))
      16 | }

Logging shows below:

console.log(expect(page));

/* returns below, missing not method
    {
      toHaveText: [AsyncFunction: toHaveText],
      toEqualText: [AsyncFunction: toEqualText],
      toHaveSelector: [AsyncFunction: toHaveSelector],
      toEqualValue: [AsyncFunction: toEqualValue]
    }
*/

Am I missing something? Many thanks for these great helpers!

[Documentation] toHaveSelectorCount matcher

I just wanted to point out the Readme has not been updated with the toHaveSelectorCount documentation. I'd do it myself but I don't want to presume I 100% know how it works (although I assume it's roughly the same as toHaveSelector but with an additional param for the expected count).

Integration with text selector

Nice to see expect-playwright 😃 Considering starting using it, but I could not find info about this and was curious about a few things.

Playwright supports text selector engines like so:

    const text = await page.waitFor('text=Unauthorized');
    expect(await text.evaluate((el) => el.innerText)).toBe('Unauthorized');

That I would have wished to replace with:

await expect(page).toHaveText('Unauthorized'); // using the underlying text selector
  1. The first thing I noticed is that the example at https://github.com/mxschmitt/expect-playwright#why-do-i-need-it does not show the above (nicer) way to get a text element that Playwright already supports.

  2. I also wrote another issue that text selectors should be case insensitive and "partial match" by default, which landed in another MR, see new description here https://github.com/microsoft/playwright/blob/master/docs/selectors.md#text

  3. The second thing I know is that an issue I wrote microsoft/playwright#1375 just landed its first merge request microsoft/playwright#1619. The text selector now pierces open shadow roots.

NOTE Text engine searches for elements inside open shadow roots, but not inside closed shadow roots or iframes.

Should/are all these features taking into consideration with these new expect functions?

toHaveSelector with .not.

The first case below does not work, whilst the second does. What is the implication of using expect(page).not.toHaveSelector? I would have thought it would be similar to the second example, but it is not.

// Does not work
await page.click('text=Delete');
await expect(page).not.toHaveSelector(selector); 
// Works
await page.click('text=Delete');
await expect(page).toHaveSelector(selector, { state: 'detached' }); 

(feature request): Adding .toHaveFocus() to the library

Hello folks and thank you for the great work you're doing with the tool! That's amazing 😊

I'm opening this issue to request for a .toHaveFocus() function that will allow to verify what is the actually focused element. This may become very helpful when testing for accessibility.

Actually, to determine if an element has focused on it, I have two options that are quite verbose:

  const axTreeSnapshot = await page.accessibility.snapshot({ root });
  // does not look very consistent on firefox from my experimentation, but I may be wrong
  axTreeSnapshot.focused

or as described in microsoft/playwright#2159 (comment) with the following:

const foo1Isfocused = await page.$eval("#foo1", (el) => el === document.activeElement)

What do you think?

toEqualValue does not retry, fails instantly

Hi there,

I am trying to use toEqualValue, and noticed that it does not wait for the condition to be true as I would have expected.

Below is an example where it checks the value of an input. Since the value is not Playwright, the test fails instantly. I would have expected it to keep checking the input value before timing out, as sometimes inputs can be filled in with a delay. The error message is 'Playwright' does not equal ''.

const qawolf = require("qawolf");
const selectors = require("./selectors/myTest.json");

let browser;
let page;

beforeAll(async () => {
  browser = await qawolf.launch();
  const context = await browser.newContext();
  await qawolf.register(context);
  page = await context.newPage();
});

afterAll(async () => {
  await qawolf.stopVideos();
  await browser.close();
});

test("myTest", async () => {
  await page.goto("http://todomvc.com/examples/react");
  await page.click(selectors["0_what_needs_to_b_input"]);
  await page.type(selectors["1_what_needs_to_b_input"], "enter todo");
  await page.press(selectors["2_what_needs_to_b_input"], "Enter");

  const element = await page.$(selectors["2_what_needs_to_b_input"]);
  await expect(element).toEqualValue("Playwright");
});

Another note - the README still says 1 second. Just wanted to see if that was still the actual timeout, or if it is just out of date.

Thanks in advance :)

expect() should accept Frame argument.

Given frame = await handle.contentFrame().
When I need both expect(handle).toEqualText() and expect(frame.isEnabled()).toBeTruthy()
Then it is not clear that the handle and frame are refering to the "same" iframe:

await expect(handle).toEqualText('#completeButton', 'Submit')
expect(await frame.isEnabled('#completeButton')).toBeTruthy()

With support Frame argument, it should be much clear:

await expect(frame).toEqualText('#completeButton', 'Submit')
expect(await frame.isEnabled('#completeButton')).toBeTruthy()

Support for @playwright/test

Does this work with the @playwright/test package?

Can't seem to follow the instructions for the Playwright test runner.

Version 0.4.0 is installed.

image

playwright-core vs playwright as peerDependency

Currently I have to install both playwright and playwright-core in my package.json in order to run expect-playwright.

If I compare the subdependencies of playwright and playwright-core, they are identical. How come playwright could not be listed as the peer dependency instead?

Release new version

Latest version (0.2.5) depends on playwright-core ^1.2.1:

"playwright-core": "^1.2.1"

When using latest playwright and jest-playwright there are mismatched versions of playwright-core.
I noticed that master is already updated with playwright-core ^1.5.1 as a peer dependency.

Error: `elementHandle.waitForSelector` is not a function

Hi,
I tried to follow the jest-playwritght basic example, so the following test:

test("TEST", async () => {
  await page.goto("https://playwright.dev/");
  const title = page.locator(".navbar__inner .navbar__title");
  await expect(title).toHaveText("Playwright");
})

However, I got Error: Timeout exceed for element 'body'. I followed the error message to here, commented out the try-catch (leaving the body of course) and I got TypeError: elementHandle.waitForSelector is not a function - which I don't know how to solve.

At the line in question, I listed the following variables for context:
args: [ [email protected]__inner .navbar__title ]
elementHandle: [email protected]__inner .navbar__title
elementHandle.waitForSelector: undefined
console.trace():

Trace:
        at Object.getElementHandle (/home/user/project/node_modules/expect-playwright/lib/matchers/utils.js:32:21)
        at runNextTicks (internal/process/task_queues.js:60:5)
        at processImmediate (internal/timers.js:437:9)
        at Object.toHaveText (/home/user/project/node_modules/expect-playwright/lib/matchers/toHaveText/index.js:10:50)

Timeout option not working

Hi there,

Thanks for this library! I am working to integrate it with qawolf, and I noticed that it times out very quickly when looking for text that is not on the page. This is true even if I pass a longer timeout in options.

As a point of feedback, I was also surprised that the default timeout was only 1 second. I would have expected something longer like 30 seconds, consistent with the Playwright API.

Here is a reproduction with qawolf. Even though the text is not on the page and I wanted to wait for 30 seconds before failing, the test seems to fail instantly (Error message: Error: Timeout exceed for element 'not there').

Please let me know if I'm doing something wrong or missing something. Also happy to help fix this issue :)

const qawolf = require("qawolf");

let browser;
let page;

beforeAll(async () => {
  browser = await qawolf.launch({ headless: false });
  const context = await browser.newContext();
  await qawolf.register(context);
  page = await context.newPage();
});

afterAll(async () => {
  await browser.close();
});

test("test", async () => {
  await page.goto("http://todomvc.com/examples/react");
  await page.type("input.new-todo", "create test!");
  await page.press("input.new-todo", "Enter");
  await page.click("input.toggle");
  await page.click("button.clear-completed");
  await expect(page).toHaveText("not there", { timeout: 30000 });
});

[Feature request] Add `toHaveCount`, assertion for count of elements

I need new assertion checking the count of elements which the selector matches.
It's very useful to verify the count of elements.

      // before
      await page.waitForSelector(selector);
      const count = await page.$$eval(selector, (el) => el.length);
      expect(count).toBe(1);

      // after
      await expect(page).toHaveCount(selector, 1)

It might be better to consider if the new assertion can verify the greater or less count.

await expect(page).toHaveCount(selector, '> 1')
await expect(page).toHaveGreaterThanCount(selector, 1)

Close to maintenance mode, add notice?

I recently saw this notice on the jest-playwright GitHub page:

⚠️ We recommend the official Playwright test-runner ⚠️

It's more flexible, lightweight, optimized for Playwright, and has TypeScript support out of the box. This doesn't mean, that we stop with maintaining this package.

I decided to take the plunge, and in the process realized almost everything I had been using expect-playwright (with jest-playwright) for was handled by @playwright/test.

I also ran across this comment on an issue:

With @playwright/test taking over, this repo will not likely live long, so this can be closed.

-- @mskelton, #119 (comment)

I wonder if it's time to give a heads up at the top of the README à la jest-playwright? Could be a little softer if this repo isn't quite as close to maintenance mode, but might still be useful for new folks, or those (like me) making the move from jest-playwright to @playwright/test.

Q: using toHaveSelector

This fails

await expect(page).toHaveSelector('div.ember-cli-notifications-notification__container');

where this passes

await expect(page).toHaveSelector('div#content-wrapper');

are selector's something other then CSS selectors? Or perhaps I expect different behavior?

I am certain a div.ember-cli-notifications-notification__container exists on my page

<div class="ember-cli-notifications-notification__container ember-cli-notifications-notification__container--bottom-right" style="z-index: 1060;" data-test-notification-container="bottom-right">
<!----></div>
<div id="content-wrapper" class="min-100h d-flex flex-column flex-nowrap"></div>

Assertions should work against a list of ElementHandles

Hi here,

Currently, all assertions are designed to check a single element. It would be helpful to check multiple elements (list items, table cells) and assert that they contain certain text.

Just spitballing here, but this is what I'm envisioning 🤔 :

  • A new matcher, expect(page).toAllHaveText(selector, text, options) (this would use Playwright's page.$$ method to select all elements matching the provided selector).

Would love thoughts from the community. Happy to collaborate and contribute! 😄

expect-playwright not working with jest-playwright

While trying out jest-playwright i've experienced issue with expect-playwright. My project GitHub repo. I cannot use any assertions from expect-playwright, when running tests from terminal the following error occurs:

TypeError: expect(...).toHaveTitle is not a function

  1 | export async function testGoogle(){
  2 |     await page.screenshot({path: './target/sampleTest/sampleTest.png'})
> 3 |     await expect(page).toHaveTitle('Google');
    |                        ^
  4 | }

  at Object.testGoogle (src/util/sampleTest.js:3:24)
  at Object.<anonymous> (src/spec/sample.spec.js:8:5)

Introduce automated code formatting

While working on the PR for the improved matcher messages, I've noticed that there isn't any code formatting tool setup in this repository. Would you be opposed to introducing a tool such as Prettier to help with code consistency, readability, and ease of development?

toHaveText timeout

What is the reason that await expect(page).toHaveText("Playwright") has a timeout of only a 1 sec? it's a real cumbersome to pass the timeout as a second parameter every time

Negative (`.not`) assertion timeout defaults to 30 seconds

According to the docs helper methods liketoHaveSelector, toHaveText, toEqualText, and toEqualValue have a default timeout of 1 second.

It has a default timeout of 1 second, which you can overwrite via the options.

This is working as expected for positive assertions:

// Immediate (working)
await expect(page).toHaveSelector('#foo');

But negative assertions are defaulting to a 30 second timeout:

// 30 second timeout (not working)
await expect(page).not.toHaveSelector('#foo');

I can fix this by manually setting the timeout in options, but it would be nice to not have to do this:

// Immediate (manual fix)
await expect(page).not.toHaveSelector('#foo', { timeout: 1 });

Configuration:

  • jest: 26.1.0
  • jest-playwright-preset: 1.2.1
  • playwright: 1.2.0

Thanks!

Option to disable dumping whole page into console

I test multi-megabyte apps and it's huge pain having it dumped into console, sometimes multiple times. I need to scroll tons just to find which test failed.

We should have option to disable that.

Simplifying match/equal matchers

I had an idea about matchers like toMatchText/toEqualText or future matchers like toMatchTitle/toEqualTitle. What if instead of having multiple matchers, we have a single matcher that if given a string, matches exactly and given a regex matches partial.

expect(page).toMatchText('.my-element', 'Playwright') // exact text match
expect(page).toMatchText('.my-element', /^Playwright$/) // full regex match
expect(page).toMatchText('.my-element', /Play/) // partial regex match

This would make it so we only have a single matcher for each purpose which could make things less confusing. That said, this might just make things more confusing.

toHaveSelector type

It appears there's no type defined on PlaywrightMatchers for toHaveSelectorCount

type definitions from an editor

2020-05-04 01 59 48

Although this seems to be a common problem when extending expect in jest, the editor can't find the type definition. (the test itself works since setupFilesAfterEnv is specified)
Explicitly import "expect-playwright" in the file works well, but do you know the other way?

To reproduce:

git clone -b repro-expect-playwright https://github.com/banyan/issue-repro.git
cd issue-repro
yarn
code .qawolf/myTest.test.ts # using vscode

0.7.0 has broken expecting on ElementHandle

v0.6.0 works fine.

Updating to 0.7.1 fails tests.

Trying to

const firstWidgetOfUser = await page.waitForSelector(DashboardSelectors.widget);
// works
expect(await firstWidgetOfUser.getAttribute('data-id')).toStrictEqual('File/my_open_cases');
// fails in 0.7.0/0.7.1, works fine in 0.6.0
await expect(firstWidgetOfUser).toHaveSelector(DashboardSelectors.chartWidget);

fails on second expect with

TypeError: Cannot read property 'waitForSelector' of null;

toEqualText() should support empty string

Given the following HTML:

<p></p>

The following toEqualText() call will timeout:

await expect(page).toEqualText('p', '');

The workaround is easy enough:

expect(await page.innerText('p')).toBe('');

But it feels out of place when testing multiple elements using toEqualText():

await expect(page).toEqualText('.foo', 'foo');
await expect(page).toEqualText('.bar', 'bar');
await expect(page).toEqualText('.baz', 'baz');
expect(await page.innerText('.empty')).toBe('');

Ideally, toEqualText() would support an empty string as a valid text match.

Feature request: expect(frame: Frame)

We have a lot of iframes inside our application and would like to test inside the iframe pages. It would be great if expect could also work with a Frame object.

toMatchText failed due to whitespaces

Hi guys,

I replaced the deprecated method toHaveText with the new one toMatchText and many of the tests failed. The reason for the failure is the presence of whitespace.

For example, this code is going to fail:

<button>
<i class="click-icon"></i>
<span>Click here</span>
</button>
------------------------
await expect(page).toMatchText('button', 'Click here');

Is there an option to modify toMatchText to run trim on textContent?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.