jba065
jba065

Reputation: 351

How to check the alignment of a text using playwright typescript

I want to validate if the text in my page is left or right aligned.

In selenium one can use driver.findElement(By.id("id1")).getLocation().x; to get the location of an element in the page based on which the alignment can be deduced.

What is the equivalent approach in Playwright?

Upvotes: 1

Views: 617

Answers (2)

Vishal Aggarwal
Vishal Aggarwal

Reputation: 4199

Instead of relying on precise co-ordinates which is an fragile approach in my opinion , you may use getAttribute property to fetch the style(or some other property as applicable in your scenario) detail which confirms the alignment property per your use case.

let locator = page.locator("someText")

let styleValue = await locator.getAttribute('style')

expect(styleValue).toContain('left');

Upvotes: 2

AutomationAndy
AutomationAndy

Reputation: 992

It looks like bounding box is similar to what you had in Selenium:

const box = await page.locator('#id1').boundingBox();
expect(box.x).toBeLessThan(100);

Upvotes: 1

Related Questions