0

I would like to store the text from an object locator and use it for assertion. For instance, I have a trade number - 1234. This trade number only appears after a transaction, so it is not static on other screens. This number is located on several other screens and I need to validate that it appears. I am able to locate the element through inspect and Playwright accepts it, but having issues:

  1. Grabbing the text (1234)
  2. Then setting up an assertion statement to compare it

Below are my humble and naïve attempts:

async getConfirmNumber() { //Store the contents in the page locator which has the trade number const tradeNumber = page.locator('div:nth-of-type(2) > .col-md-9.display-value.ng-binding').textContent;

  //Navigate to a different screen which now will display the trade number
  await this.page.click('a[caption="History"]')
  await this.page.click('a[href="#/trade-summary"]')
  
  //Line of code that I am not sure how to correctly write.  ".bidconfirmation" is the locator on the new screen which displays the trade number. 
  //If the contents or value of ".bidconfirmation" is NOT 1234 then an error needs to display.
  await expect(tradeNumber).toHaveCSS('.bidconfirmation', tradeNumber);

}

1 Answers1

0

Just to let you know I would change the tag on this post to playwright-JavaScript to better reach the intended audience. However, if I understand your question correctly you are trying to get the text content of an element but the textContent() method is not working, I would try to use the innerText() method and see if that works. Apologies if this is a little off as I work with the java version of Playwright but you could do:

    const tradeNumber = page.locator('div:nth-of-type(2) > .col-md-9.display-value.ng-binding').innerText(); //BTW I would change this locator to something unique or a little more stable -- this should give you the tradeNumber
//then I'm not 100% sure what your trying to do here but if I understand correctly this might help
await expect(page.locator('.bidconfirmation').toHaveValue(tradeNumber));

I hope this helped a little, Im sorry I couldn't really get an understanding fully of the question you were asking but feel free to take a look at playwright.dev to find documentation surrounding Playwright.

Lilpercy11
  • 86
  • 3