Skip to content

Commit

Permalink
added example code for fetching text and clearing values from the fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mfaisalkhatri committed Jul 4, 2024
1 parent 962a550 commit f53b4f6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/test/java/io/github/mfaisalkhatri/tests/TextFieldTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package io.github.mfaisalkhatri.tests;

import com.microsoft.playwright.*;
import com.microsoft.playwright.options.AriaRole;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

public class TextFieldTest {

private Playwright playwright;
Expand All @@ -30,4 +33,47 @@ public void locateFirstNameByPlaceholder() {
Locator firstNameField = page.getByPlaceholder("Your first name *");
firstNameField.fill("John");
}

@Test
public void locateFirstNameByRole() {
page.navigate("https://practicesoftwaretesting.com/contact");
Locator firstNameField = page.getByRole(AriaRole.TEXTBOX, new Page.GetByRoleOptions().setName("First name"));
firstNameField.fill("Tom");
}

@Test
public void testFocusOnField() {
page.navigate("https://practicesoftwaretesting.com/contact");
Locator emailAddressField = page.getByLabel("Email address");
emailAddressField.focus();
assertThat(emailAddressField).isFocused();
}

@Test
public void testGetValuesFromTextField() {
page.navigate("https://practicesoftwaretesting.com/contact");
Locator emailAddressField = page.getByLabel("Email address");

String emailAddress = "[email protected]";
emailAddressField.fill(emailAddress);

String emailValue = emailAddressField.inputValue();
System.out.println(emailValue);

assertThat(emailAddressField).hasValue(emailValue);
}

@Test
public void testClearFieldValues() {
page.navigate("https://practicesoftwaretesting.com/contact");
Locator messageField = page.getByLabel("Message *");
String messageOne = "This is the first message";
messageField.fill(messageOne);

messageField.clear();
String messageTwo = "This is the second message";
messageField.fill(messageTwo);

assertThat(messageField).hasValue(messageTwo);
}
}
18 changes: 18 additions & 0 deletions test-suites/testng-textfielddemo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Working with Text fields using Playwright Java" >
<test name="Text fields tests">
<classes>
<class name="io.github.mfaisalkhatri.tests.TextFieldTest">
<methods>
<include name="locateFirstNameByLabel"/>
<include name="locateFirstNameByPlaceholder"/>
<include name="locateFirstNameByRole"/>
<include name="testFocusOnField"/>
<include name="testGetValuesFromTextField"/>
<include name="testClearFieldValues"/>
</methods>
</class>
</classes>
</test>
</suite>
1 change: 1 addition & 0 deletions test-suites/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
<suite-file path="testng-doubleclicktests.xml"/>
<suite-file path="testng-screenshottests.xml"/>
<suite-file path="testng-dropdowntests.xml"/>
<suite-file path="testng-textfielddemo.xml"/>
</suite-files>
</suite>

0 comments on commit f53b4f6

Please sign in to comment.