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

findByText returns [object Promise], hence erroring out with the error 'toContainText can be only used with Locator object' #513

Closed
InduKrish opened this issue Sep 19, 2022 · 4 comments
Labels

Comments

@InduKrish
Copy link

getByText - the below script with getByText() works fine
async checkActiveStatusdom(text) {

    const header = await this.screen.findByTestId('erow-GroupCode-0');
    console.log(" header" + header)
    const base = this.within(header).getByText("NEW HIRE")
    console.log("base value" + base);
    const check = this.within(header).getByText("check")
    console.log(" value " + check)
    await expect(base).toContainText(text);

}

however findByText - returns Error: toContainText can be only used with Locator object

async checkActiveStatusdom(text) {

   const header = await this.screen.findByTestId('erow-GroupCode-0');
   console.log(" header" + header)
   const base = this.within(header).findByText("LINEHOLDER")
   console.log("base value" + base);
   const check = this.within(header).findByText("check")
   console.log(" value " + check)
   await expect(base).toContainText(text);

console log:
headerLocator@query-by-test-id=["erow-GroupCode-0"]
base value[object Promise]
value [object Promise]

why getByText auto waits, but findByText returns no value, and returns Error: toContainText can be only used with Locator object.

From the previous ticket , #507

I was told that findby auto waits and getby does not auto wait, in this scenario it works differently.
Screen Shot 2022-09-19 at 4 52 10 PM

@jrolfs
Copy link
Member

jrolfs commented Sep 20, 2022

The find* queries currently return Promise<Locator> as they use Locator.waitFor() internally to wait for deferred elements, so you're missing some awaits in your example.

This will work:

const header = await this.screen.findByTestId('erow-GroupCode-0');
console.log(" header" + header)

const base = await this.within(header).findByText("LINEHOLDER")
console.log("base value" + base);

const check = await this.within(header).findByText("check")
console.log(" value " + check)

await expect(base).toContainText(text);

Depending on when different elements are rendered in your application, you probably don't need asynchronous findByText for all of your queries. For example, if data-testid="erow-GroupCode-0 is really the element you're waiting for, and its contents appear synchronously within it, then you can just do this:

// Here we wait for the element to show up in the page
const header = await this.screen.findByTestId('erow-GroupCode-0');
console.log(" header" + header)

// Since we've waited for `header`, we can use synchronous queries with `within()`
const base = this.within(header).getByText("LINEHOLDER")
console.log("base value" + base);
const check = this.within(header).getByText("check")
console.log(" value " + check)

await expect(base).toContainText(text);

I will add some documentation explaining this.

@InduKrish
Copy link
Author

Thank you for the detailed explanation!, I appreciate it

@InduKrish
Copy link
Author

InduKrish commented Sep 20, 2022

The find* queries currently return Promise<Locator> as they use Locator.waitFor() internally to wait for deferred elements, so you're missing some awaits in your example.

This will work:

const header = await this.screen.findByTestId('erow-GroupCode-0');
console.log(" header" + header)

const base = await this.within(header).findByText("LINEHOLDER")
console.log("base value" + base);

const check = await this.within(header).findByText("check")
console.log(" value " + check)

await expect(base).toContainText(text);

I tried to test this, and i see the following error

Screen Shot 2022-09-20 at 10 14 50 AM

however with by getByText, the test passed, and attached is the screenshot.

Screen Shot 2022-09-20 at 10 15 18 AM

@github-actions
Copy link

🎉 This issue has been resolved in version 4.4.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants