Skip to content

Commit d410bef

Browse files
Added example tests for working with text fields (#64)
* updated dependency versions in pom.xml and added a test for locating field by label * added command to install msedge in the pipeline for running tests * added exmaple code for locating field using placeholder text * added example code for fetching text and clearing values from the fields * key press example code added
1 parent 3e47e46 commit d410bef

5 files changed

Lines changed: 124 additions & 4 deletions

File tree

.github/workflows/maven.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ jobs:
3333
java-version: '17'
3434
distribution: 'temurin'
3535
cache: maven
36+
37+
- name: Install Edge browser
38+
run: mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install msedge"
3639

3740
- name: Build with Maven
3841
run: mvn clean install

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
<properties>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15-
<playwright.version>1.42.0</playwright.version>
16-
<testng.version>7.9.0</testng.version>
17-
<maven.compiler.version>3.12.1</maven.compiler.version>
18-
<surefire.version>3.2.5</surefire.version>
15+
<playwright.version>1.45.0</playwright.version>
16+
<testng.version>7.10.2</testng.version>
17+
<maven.compiler.version>3.13.0</maven.compiler.version>
18+
<surefire.version>3.3.0</surefire.version>
1919
<java.release.version>17</java.release.version>
2020
<maven.source.encoding>UTF-8</maven.source.encoding>
2121
<suite-xml>test-suites/testng.xml</suite-xml>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package io.github.mfaisalkhatri.tests;
2+
3+
import com.microsoft.playwright.*;
4+
import com.microsoft.playwright.options.AriaRole;
5+
import org.testng.annotations.BeforeClass;
6+
import org.testng.annotations.Test;
7+
8+
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
9+
10+
public class TextFieldTest {
11+
12+
private Playwright playwright;
13+
private Page page;
14+
15+
16+
@BeforeClass
17+
public void setup() {
18+
this.playwright = Playwright.create();
19+
final Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false).setChannel("chrome"));
20+
this.page = browser.newPage();
21+
}
22+
23+
@Test
24+
public void locateFirstNameByLabel() {
25+
page.navigate("https://practicesoftwaretesting.com/contact");
26+
Locator firstNameField = page.getByLabel("First name");
27+
firstNameField.fill("Faisal");
28+
}
29+
30+
@Test
31+
public void locateFirstNameByPlaceholder() {
32+
page.navigate("https://practicesoftwaretesting.com/contact");
33+
Locator firstNameField = page.getByPlaceholder("Your first name *");
34+
firstNameField.fill("John");
35+
}
36+
37+
@Test
38+
public void locateFirstNameByRole() {
39+
page.navigate("https://practicesoftwaretesting.com/contact");
40+
Locator firstNameField = page.getByRole(AriaRole.TEXTBOX, new Page.GetByRoleOptions().setName("First name"));
41+
firstNameField.fill("Tom");
42+
}
43+
44+
@Test
45+
public void testFocusOnField() {
46+
page.navigate("https://practicesoftwaretesting.com/contact");
47+
Locator emailAddressField = page.getByLabel("Email address");
48+
emailAddressField.focus();
49+
assertThat(emailAddressField).isFocused();
50+
}
51+
52+
@Test
53+
public void testGetValuesFromTextField() {
54+
page.navigate("https://practicesoftwaretesting.com/contact");
55+
Locator emailAddressField = page.getByLabel("Email address");
56+
57+
String emailAddress = "faisal.k@demo.com";
58+
emailAddressField.fill(emailAddress);
59+
60+
String emailValue = emailAddressField.inputValue();
61+
System.out.println(emailValue);
62+
63+
assertThat(emailAddressField).hasValue(emailValue);
64+
}
65+
66+
@Test
67+
public void testClearFieldValues() {
68+
page.navigate("https://practicesoftwaretesting.com/contact");
69+
Locator messageField = page.getByLabel("Message *");
70+
String messageOne = "This is the first message";
71+
messageField.fill(messageOne);
72+
73+
messageField.clear();
74+
String messageTwo = "This is the second message";
75+
messageField.fill(messageTwo);
76+
77+
assertThat(messageField).hasValue(messageTwo);
78+
}
79+
80+
@Test
81+
public void testKeyPress() {
82+
page.navigate("https://the-internet.herokuapp.com/key_presses");
83+
Locator textBox = page.locator("#target");
84+
textBox.press("Alt");
85+
Locator resultText = page.locator("p#result");
86+
87+
assertThat(resultText).containsText("ALT");
88+
89+
textBox.press("Shift");
90+
assertThat(resultText).containsText("SHIFT");
91+
92+
textBox.press("N");
93+
assertThat(resultText).containsText("N");
94+
95+
textBox.press("9");
96+
assertThat(resultText).containsText("9");
97+
}
98+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite name="Working with Text fields using Playwright Java" >
4+
<test name="Text fields tests">
5+
<classes>
6+
<class name="io.github.mfaisalkhatri.tests.TextFieldTest">
7+
<methods>
8+
<include name="locateFirstNameByLabel"/>
9+
<include name="locateFirstNameByPlaceholder"/>
10+
<include name="locateFirstNameByRole"/>
11+
<include name="testFocusOnField"/>
12+
<include name="testGetValuesFromTextField"/>
13+
<include name="testClearFieldValues"/>
14+
</methods>
15+
</class>
16+
</classes>
17+
</test>
18+
</suite>

test-suites/testng.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
<suite-file path="testng-doubleclicktests.xml"/>
1010
<suite-file path="testng-screenshottests.xml"/>
1111
<suite-file path="testng-dropdowntests.xml"/>
12+
<suite-file path="testng-textfielddemo.xml"/>
1213
</suite-files>
1314
</suite>

0 commit comments

Comments
 (0)