Assert Multi-select dropdown values in Playwright Python

Tutorial 11​​ ​​ Assert​​ Multi-select dropdown​​ values​​ in Playwright​​ Python​​ 

What you will Learn​​ in this​​ blog:

  • Assert​​ multiple-select​​ dropdown​​ values

  • Asserting using regular expressions

  • Code snippet

Assert multiple-select dropdown values

In the​​ previous blog​​ https://www.way2automation.com/multi-select-dropdown-list-in-playwright-python/​​ , we have seen how to select multiple values from the dropdown list

In this blog,​​ we will see how do we assert the selected values.

Go to official page

https://playwright.dev/python/docs/test-assertions​​ 


Click​​ expect(locator).to_have_values()​​ method and notice that this assertion method was added in playwright version 1.23

So, before we proceed, let us make sure that we have playwright version >= 1.23

To do that, let us check the playwright version by execution ‘pip list​​ command:

As seen above, we do have playwright ver >1.23

If you are having an older version, than execute the below 2 commands​​ in sequence:​​ 

pip install --upgrade playwright

playwright install​​ (to upgrade browsers)

Below is the code snippet from previous blog

Comment line#3 and let us separate out the locator (line#14):

Next we can write

Next, we can use the​​ to_have_values()​​ method

We can now mention the values that we expect during runtime

Save and execute

The assertion passes

Let us introduce an error in script

Save and execute.

This time we can see that the assertion fails

Asserting using regular expressions

Go to​​ https://playwright.dev/python/docs/api/class-locatorassertions#locator-assertions-to-have-values​​ 

We can see the usage of regular expressions (‘re’ stands for regular expression)

Let us import ‘re’ library

Comment line #18 and add line#19 using ‘Re’.​​ 

So basically, instead of writign ‘May’, we are writing ‘M’ and so on

Save and execute, the assertion passes

Code snippet

​​ import​​ time

from​​ playwright.sync_api​​ import​​ Playwright,​​ sync_playwright,​​ expect


def​​ run(playwright: Playwright) ->​​ None:
 ​​ ​​ ​​​​ browser = playwright.chromium.launch(
headless=False,​​ slow_mo=3000)
 ​​ ​​ ​​​​ context = browser.new_context()


 ​​ ​​ ​​​​ page = context.new_page()

 ​​ ​​ ​​​​ page.goto(
"https://selenium08.blogspot.com/2019/11/dropdown.html")

 ​​ ​​ ​​​​ 
#page.locator("select[name='Month']").select_option(['May', 'July', 'Sept'])
 ​​ ​​ ​​​​ 
loc = page.locator("select[name='Month']")
 ​​ ​​ ​​​​ loc.select_option([
'May',​​ 'July',​​ 'Sept'])

 ​​ ​​ ​​​​ #Assertion
 ​​ ​​ ​​​​ 
expect(loc).to_have_values(['My',​​ 'July',​​ 'Sept'])


 ​​ ​​ ​​​​ 
# ---------------------
 ​​ ​​ ​​​​ 
context.close()
 ​​ ​​ ​​​​ browser.close()



with​​ sync_playwright()​​ as​​ playwright:
 ​​ ​​ ​​​​ run(playwright)

 

Code snippet (regular expr)

import​​ time
import​​ re
from​​ playwright.sync_api​​ import​​ Playwright,​​ sync_playwright,​​ expect


def​​ run(playwright: Playwright) ->​​ None:
 ​​ ​​ ​​​​ browser = playwright.chromium.launch(
headless=False,​​ slow_mo=3000)
 ​​ ​​ ​​​​ context = browser.new_context()


 ​​ ​​ ​​​​ page = context.new_page()

 ​​ ​​ ​​​​ page.goto(
"https://selenium08.blogspot.com/2019/11/dropdown.html")

 ​​ ​​ ​​​​ 
#page.locator("select[name='Month']").select_option(['May', 'July', 'Sept'])
 ​​ ​​ ​​​​ 
loc = page.locator("select[name='Month']")
 ​​ ​​ ​​​​ loc.select_option([
'May',​​ 'July',​​ 'Sept'])

 ​​ ​​ ​​​​ 
#Assertion
 ​​ ​​ ​​​​ #expect(loc).to_have_values(['May', 'July', 'Sept'])

 ​​ ​​ ​​​​ 
expect(loc).to_have_values([re.compile(r"M"),​​ re.compile(r"J"),​​ re.compile(r"S")])


 ​​ ​​ ​​​​ 
# ---------------------
 ​​ ​​ ​​​​ 
context.close()
 ​​ ​​ ​​​​ browser.close()



with​​ sync_playwright()​​ as​​ playwright:
 ​​ ​​ ​​​​ run(playwright)

 

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 31st DEC 2024