Forms are like your appās "conversation starters" with users. If input fields, radio buttons, or checkboxes break, itās like your app suddenly forgets how to speak! Letās learn how to test these elements interactively using Playwrightāno prior experience needed!
š¹ļø Quick Interactive Quiz!
(Answer as you read!)
Q: Whatās the first thing you test for any form element?
A) Color
B) Visibility
C) Font size
Spoiler: The answer is at the end of this section!
š® Letās Play! Testing Input Fields
Imagine this: Youāre testing a login form. Letās make sure the email field works.
Step-by-Step Demo
Hereās a sneak peek at the some must-do tests for input fields:
Test Explanation:
Visibility: We first check if the input field is visible. A hidden input field is like an invisible formāit canāt be used!
Enabled State: Next, we ensure the email input field is enabled. A disabled field is a dead end for the userāitās not clickable or editable.
Initial Value: We then verify that the email input field is empty when the page loads. You wouldnāt want a pre-filled email like "testuser123@gmail.com" if thatās not what the user intended!
User Interaction: Now, we simulate the user typing in the email field. After typing, we confirm that the value entered matches exactly what the user typed, ensuring the input works as expected.
Clear the Field: Lastly, we test the ability to clear the field. Once the user is done, they should be able to erase what theyāve typed, and the field should become empty again.
š” Pro Tip: Try breaking the test! Change 'testeruser123@gmail.com'
to š©āš» and see what happens.
šÆ Testing Radio Buttons (The āChoose Oneā Game)
Imagine you're on a form where you need to choose your gender: Male or Female. Letās ensure both radio buttons work perfectly.
Test Explanation:
Visibility: The first test checks if the radio button is visible on the page. If the button is hidden, itās like trying to make a selection on a ghost form!
Enabled State: Here, we ensure the radio button is active and clickable. Disabled radio buttons are like locked doorsācanāt click them!
Default Unselected State: By default, the female radio button should be unselected. This ensures that no oneās choice is pre-filled (which would be a bit creepy, right?).
User Interaction (Selecting the Radio Button): Next, we simulate the userās action by selecting the female option. This mimics a real userās interaction with the form.
Verification Post-Selection: Once selected, we verify the buttonās state to ensure the UI reflects the userās choice.
Alternative Check: A quick alternative to verify if the radio button is selected is using
isChecked()
. This returnstrue
when the button is checked andfalse
when it's not.Male Button Check: Finally, we ensure that the male radio button is still unselected after selecting the female option, confirming that only one choice can be selected.
š¤ Testing Checkboxes (The āSelect Allā Puzzle)
Checkboxes are special because, unlike radio buttons (which only allow one option to be selected), you can select multiple options! This makes testing checkboxes a little differentāletās see how we can test them with Playwright.
Letās Dive In!
Test Explanation:
-
Locating Checkboxes: We identify the checkboxes using their CSS selectors and store them in an array (
checkboxLocators
). -
Checking Checkboxes: We loop through each checkbox and use
.check()
to select them. -
Unchecking Checkboxes: We loop again, checking if the checkbox is selected with
.isChecked()
, and uncheck it with.uncheck()
if true.
-
Checkboxes allow multiple selections, and each can be independently checked/unchecked.
-
Radio Buttons allow only one selection.
Pro Tip: Test visibility, enabled state, and checked stateāsame as radio buttons!
š Cheatsheet: Your Testing Toolkit
Q: Whatās the first thing you test for any form element?
ā A) Visibility! (You canāt interact with something you canāt see!)
š Do You Have to Use All Assertions?
No, you don't need all assertions for every element. Use the ones that match the behaviour you're testing.
Focus on what's important for each case, and keep it simple!
Comments
Post a Comment