Playwright Python Interview Questions
How do you pass the CSS locator and XPATH to find elements in playwright Python?
We do not have to mention XPATH or CSS, the playwright can understand what we passed and find the element.
#css
page.locator("css_value").click()
page.locator("button#login").click()
#xpath
page.locator("xpath_value").click()
page.locator("//button[@id='login']").click()
How do you check whether the dropdown has all the expected options?
You can verify whether all values are present in the dropdown using a few methods,
- By getting all the texts
- By asserting using expect(), and verifying values
- forming right XPath details.
page.goto("https://demo.testercoder.com/dropdowns.html")
dropdown_texts_raw = page.locator("#first").all_text_contents()
dropdown_texts_no_space = []
for val in dropdown_texts_raw:
dropdown_texts_no_space.append(val.strip())
expected_texts = ['Bing', 'Google', 'Yahoo', 'iPhone']
assert expected_texts == dropdown_texts_no_space
expected_values =['Microsoft', 'Google', 'Yahoo', 'Apple']
expect(page.locator("#first")).to_have_values(expected_values)
How do you select a dropdown value?
Select dropdown using the value attribute
select_option = page.locator("//select[@id='first']").select_option(value="Yahoo")
Select dropdown using the text present
page.locator("//select[@id='first']").select_option(label="Apple")
How do you select a dropdown value when the dropdown is not created using the ‘select’ tag?
You can retrieve the text normally, just like you get text from p, span, and label elements.
page.locator("locator value").text_content()
How do you get value from the input field?
page.locator("#input").input_value()
How do you move the mouse on an element?
Use the hover()
function from the playwright to move your cursor on the element.
page.locator("locator-value").hover()
Could you write code and select a check box?
You can select the checkbox using the click method but if it is already checked, clicking makes it unchecked.
So it is better to use the check()
function that will ensure the checkbox is checked irrespective of the previous status.
page.locator("#input[type=checkbox]").check() # better
#or
page.locator("#input[type=checkbox]").click()
Write a Python program to open a browser and go to a URL?
from playwright.sync_api import sync_playwright, expect
playwright = sync_playwright().start()
browser = playwright.chromium.launch(headless=False)
page = browser.new_page()
page.goto("https://demo.testercoder.com/dropdowns.html")
Could you write a Python code to wait for a particular element?
You can use wait_for()
to wait for a particular element.
# 60 seconds
page.locator("#input").wait_for(timeout=60000)
What is a playwright docker image?
How do you run Playwright tests in parallel in Python?
Can you run a single test multiple times?
You can use the count parameter in the command line, to use this you have to install pytest-repeat
.
pip install pytest-repeat
You can run specific or all tests in a file multiple times
pytest --count=10 test_file.py
What are playwright pytest hooks?
How do you use fixtures with the playwright?
Write a Python program to parameterize a playwright test.
How do you upload files with playwright Python?
What is wait_for()?
wait_for()
waits for the element to be present till the specified time. If the element is present before the time then the control moves to the next line in the program. If the element is not present within the given time then it will fail the test with the timeout error.
page.locator("#input").wait_for(timeout=60000)
How to take a screenshot?
You can capture a screenshot in Playwright Python using the screenshot()
function, also you can take screenshots of the viewport (what you are seeing on screen), a full page, a specific element
# viewport
page.screenshot(path="./screencapture.png")
# full page
page.screenshot(path="./full_screencapture.png", full_page=True)
# specific element
page.locator("[name='q']").screenshot(path="./searchbar.png")
Why should someone choose a playwright?
How do you maximize the browser window?
How do you switch tabs in playwright Python?
Can the playwright be used for API testing?
Can I use Playwright to automate Firefox?
Command to install playwright?
Could you make the playwright run over multiple machines?
How do you interact with network requests in Playwright Python?
How do you handle popups?
How do you handle dynamic elements?
Can we automate desktop applications using Playwright?
How do you wait for an element to be visible when the element is hidden initially?
Could you explain why the pytest is required?
Explain your framework.
Explain login in your framework.
Do you use any other tools as part of your playwright Python framework?
Could you tell me how finding an element and performing an operation on it works for the playwright?
Posts You Might Like
- Why playwright is better than selenium webdriver, is it?
- Handle dropdowns in Playwright Python
- Open the browser and Close in Playwright Python
- Handle checkbox in Playwright Python
- Handle IFrames in Playwright Python
- Element Operations in Playwright Python
- Page level commands in Playwright Python
- Element State with Playwright Python