Skip to content

Commit 608f781

Browse files
Added example code for mouse hover (#65)
* added example code to perform mouse hover and updated readme * updated dependency version in pom.xml * formatted code * added code to close browser and playwright sessions gracefully using close() method * updated testng xml files
1 parent d410bef commit 608f781

13 files changed

+118
-4
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ I have tried to answer the below questions by providing working code example in
2727
1. How to set different window sizes?
2828
1. How to double on a button?
2929
1. How to use Page Object Model using Playwright Java?
30-
1. How to perform browser navigations?
30+
1. How to perform browser navigation?
31+
1. How to perform Mouse hover?
3132

3233
## How to run the Tests?
3334

@@ -45,6 +46,7 @@ I have tried to answer the below questions by providing working code example in
4546
- [Playwright Java Tutorial: Web Automation Testing | Installation and Setup](https://medium.com/@iamfaisalkhatri/playwright-java-tutorial-web-automation-testing-installation-and-setup-545c9c7661c8)
4647
- [Playwright Java Tutorial: Web Automation Testing | Writing and running tests on Chrome, Firefox and Edge browsers](https://medium.com/@iamfaisalkhatri/playwright-java-tutorial-web-automation-testing-writing-and-running-tests-on-chrome-firefox-and-d2446b9a69ce)
4748
- [Playwright Java Tutorial: Web Automation Testing | How to perform browser navigation?](https://medium.com/@iamfaisalkhatri/playwright-java-tutorial-web-automation-testing-how-to-perform-browser-navigation-043f14af5c97)
49+
- [Playwright Java Tutorial: Web Automation Testing | How to work with text fields?](https://medium.com/@iamfaisalkhatri/playwright-java-tutorial-web-automation-testing-how-to-work-with-text-fields-6cc9982ed7b2)
4850

4951
## :question: Need Assistance?
5052

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<properties>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15-
<playwright.version>1.45.0</playwright.version>
15+
<playwright.version>1.45.1</playwright.version>
1616
<testng.version>7.10.2</testng.version>
1717
<maven.compiler.version>3.13.0</maven.compiler.version>
1818
<surefire.version>3.3.0</surefire.version>

src/main/java/io/github/mfaisalkhatri/browsers/BrowserManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public Page getPage() {
4646

4747
public void closeBrowser() {
4848
this.browser.close();
49+
this.playwright.close();
4950
}
5051

5152

src/test/java/io/github/mfaisalkhatri/tests/DoubleClickTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@ public void testDoubleClick () {
2121
final ButtonsPage buttonsPage = new ButtonsPage(page);
2222
buttonsPage.doubleClickAndCheckAlertText("You double clicked me.. Thank You..");
2323

24+
browser.close();
25+
playwright.close();
26+
2427
}
2528
}

src/test/java/io/github/mfaisalkhatri/tests/DropdownTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public void testMultiSelectOptions() {
8686

8787
@AfterClass
8888
public void tearDown() {
89+
this.page.close();
8990
this.playwright.close();
9091
}
9192
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.github.mfaisalkhatri.tests;
2+
3+
import com.microsoft.playwright.*;
4+
import com.microsoft.playwright.options.AriaRole;
5+
import org.testng.annotations.AfterClass;
6+
import org.testng.annotations.BeforeClass;
7+
import org.testng.annotations.Test;
8+
9+
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
10+
11+
public class MouseHoverTest {
12+
13+
private Playwright playwright;
14+
private Page page;
15+
16+
17+
@BeforeClass
18+
public void setup() {
19+
this.playwright = Playwright.create();
20+
final Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false).setChannel("chrome"));
21+
this.page = browser.newPage();
22+
}
23+
24+
@Test
25+
public void testMouseHover() {
26+
page.navigate("https://the-internet.herokuapp.com/hovers");
27+
Locator firstImage = page.locator("#content div.figure:nth-child(3)");
28+
firstImage.hover();
29+
Locator userNameText = page.getByRole(AriaRole.HEADING, new Page.GetByRoleOptions().setName("name: user1"));
30+
assertThat(userNameText).hasText("name: user1");
31+
}
32+
33+
@AfterClass
34+
public void tearDown() {
35+
this.page.close();
36+
this.playwright.close();
37+
}
38+
}

src/test/java/io/github/mfaisalkhatri/tests/PlaywrightDemoTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public void testOnChromeHeadless() {
1919
final String pageTitle = page.title();
2020
assertEquals(pageTitle, "Your Store");
2121
browser.close();
22+
playwright.close();
2223
}
2324

2425

@@ -32,6 +33,7 @@ public void testOnChrome() {
3233
final String pageTitle = page.title();
3334
assertEquals(pageTitle, "Your Store");
3435
browser.close();
36+
playwright.close();
3537
}
3638

3739
@Test
@@ -43,6 +45,7 @@ public void testOnFirefoxHeadless() {
4345
final String pageTitle = page.title();
4446
assertEquals(pageTitle, "Your Store");
4547
browser.close();
48+
playwright.close();
4649
}
4750

4851
@Test
@@ -54,6 +57,7 @@ public void testOnFirefox() {
5457
final String pageTitle = page.title();
5558
assertEquals(pageTitle, "Your Store");
5659
browser.close();
60+
playwright.close();
5761

5862
}
5963

@@ -66,6 +70,7 @@ public void testOnFirefoxSlowMo() {
6670
final String pageTitle = page.title();
6771
assertEquals(pageTitle, "Your Store");
6872
browser.close();
73+
playwright.close();
6974
}
7075

7176
@Test
@@ -77,6 +82,7 @@ public void testOnChromeSlowMo() {
7782
final String pageTitle = page.title();
7883
assertEquals(pageTitle, "Your Store");
7984
browser.close();
85+
playwright.close();
8086
}
8187

8288
@Test
@@ -88,6 +94,7 @@ public void testOnEdge() {
8894
final String pageTitle = page.title();
8995
assertEquals(pageTitle, "Your Store");
9096
browser.close();
97+
playwright.close();
9198
}
9299

93100
@Test
@@ -124,6 +131,7 @@ public void testBrowserNavigation() {
124131
assertEquals(currentPageUrl, websiteLink);
125132

126133
browser.close();
134+
playwright.close();
127135

128136
}
129137
}

src/test/java/io/github/mfaisalkhatri/tests/TextFieldTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.microsoft.playwright.*;
44
import com.microsoft.playwright.options.AriaRole;
5+
import org.testng.annotations.AfterClass;
56
import org.testng.annotations.BeforeClass;
67
import org.testng.annotations.Test;
78

@@ -95,4 +96,11 @@ public void testKeyPress() {
9596
textBox.press("9");
9697
assertThat(resultText).containsText("9");
9798
}
99+
100+
@AfterClass
101+
public void tearDown() {
102+
this.page.close();
103+
this.playwright.close();
104+
}
105+
98106
}

src/test/java/io/github/mfaisalkhatri/tests/WindowSizeTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public void testDifferentWindowSize() {
2121
final String pageHeaderText = pageHeader.textContent();
2222
assertEquals(pageHeaderText, "Selenium Playground");
2323
browser.close();
24+
playwright.close();
2425

2526
}
2627

@@ -40,6 +41,7 @@ public void testDifferentWindowSizeMethodTwo() {
4041
final String pageHeaderText = pageHeader.textContent();
4142
assertEquals(pageHeaderText, "Selenium Playground");
4243
browser.close();
44+
playwright.close();
4345

4446
}
4547

test-suites/testng-formauthenticationtests.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
33
<suite name="Browser Navigation test suite">
44
<test name="Browser navigation tests using Playwright on Chrome">
5-
<parameter name="browser" value="firefox"/>
5+
<parameter name="browser" value="chrome"/>
66
<classes>
77
<class name="io.github.mfaisalkhatri.tests.FormAuthenticationTests">
88
<methods>

test-suites/testng-mousehovertest.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite name="Playwright demo test suite " >
4+
<test name="Mouse hover demo on Chrome">
5+
<classes>
6+
<class name="io.github.mfaisalkhatri.tests.MouseHoverTest">
7+
<methods>
8+
<include name="testMouseHover"/>
9+
</methods>
10+
</class>
11+
</classes>
12+
</test>
13+
</suite>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite name="Playwright Demo Tests" parallel="tests">
4+
<test name="Web automation tests using Playwright on Chrome">
5+
<classes>
6+
<class name="io.github.mfaisalkhatri.tests.PlaywrightDemoTests">
7+
<methods>
8+
<include name="testOnChromeHeadless"/>
9+
<include name="testOnChrome"/>
10+
<include name="testOnChromeSlowMo"/>
11+
<include name="testBrowserNavigation"/>
12+
</methods>
13+
</class>
14+
</classes>
15+
</test>
16+
<test name="Web automation tests using Playwright on Firefox">
17+
<classes>
18+
<class name="io.github.mfaisalkhatri.tests.PlaywrightDemoTests">
19+
<methods>
20+
<include name="testOnFirefoxHeadless"/>
21+
<include name="testOnFirefox"/>
22+
<include name="testOnFirefoxSlowMo"/>
23+
</methods>
24+
</class>
25+
</classes>
26+
</test>
27+
<test name="Web automation tests using Playwright on Edge">
28+
<classes>
29+
<class name="io.github.mfaisalkhatri.tests.PlaywrightDemoTests">
30+
<methods>
31+
<include name="testOnEdge"/>
32+
</methods>
33+
</class>
34+
</classes>
35+
</test>
36+
</suite>

test-suites/testng.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3-
<suite name="Playwright framework example tests " parallel="tests">
3+
<suite name="Playwright framework example tests ">
44
<suite-files>
55
<suite-file path="testng-browsertest.xml"/>
66
<suite-file path="testng-browsernavigationtests.xml"/>
@@ -10,5 +10,7 @@
1010
<suite-file path="testng-screenshottests.xml"/>
1111
<suite-file path="testng-dropdowntests.xml"/>
1212
<suite-file path="testng-textfielddemo.xml"/>
13+
<suite-file path="testng-mousehovertest.xml"/>
14+
<suite-file path="testng-playwrightdemotests.xml"/>
1315
</suite-files>
1416
</suite>

0 commit comments

Comments
 (0)