Search code examples
playwrightplaywright-testplaywright-typescript

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?


Solution

  • 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');