Skip to content

Commit

Permalink
Added example code for mouse hover (#65)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mfaisalkhatri authored Aug 7, 2024
1 parent d410bef commit 608f781
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ I have tried to answer the below questions by providing working code example in
1. How to set different window sizes?
1. How to double on a button?
1. How to use Page Object Model using Playwright Java?
1. How to perform browser navigations?
1. How to perform browser navigation?
1. How to perform Mouse hover?

## How to run the Tests?

Expand All @@ -45,6 +46,7 @@ I have tried to answer the below questions by providing working code example in
- [Playwright Java Tutorial: Web Automation Testing | Installation and Setup](https://medium.com/@iamfaisalkhatri/playwright-java-tutorial-web-automation-testing-installation-and-setup-545c9c7661c8)
- [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)
- [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)
- [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)

## :question: Need Assistance?

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<playwright.version>1.45.0</playwright.version>
<playwright.version>1.45.1</playwright.version>
<testng.version>7.10.2</testng.version>
<maven.compiler.version>3.13.0</maven.compiler.version>
<surefire.version>3.3.0</surefire.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public Page getPage() {

public void closeBrowser() {
this.browser.close();
this.playwright.close();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ public void testDoubleClick () {
final ButtonsPage buttonsPage = new ButtonsPage(page);
buttonsPage.doubleClickAndCheckAlertText("You double clicked me.. Thank You..");

browser.close();
playwright.close();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public void testMultiSelectOptions() {

@AfterClass
public void tearDown() {
this.page.close();
this.playwright.close();
}
}
38 changes: 38 additions & 0 deletions src/test/java/io/github/mfaisalkhatri/tests/MouseHoverTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.github.mfaisalkhatri.tests;

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

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

public class MouseHoverTest {

private Playwright playwright;
private Page page;


@BeforeClass
public void setup() {
this.playwright = Playwright.create();
final Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false).setChannel("chrome"));
this.page = browser.newPage();
}

@Test
public void testMouseHover() {
page.navigate("https://the-internet.herokuapp.com/hovers");
Locator firstImage = page.locator("#content div.figure:nth-child(3)");
firstImage.hover();
Locator userNameText = page.getByRole(AriaRole.HEADING, new Page.GetByRoleOptions().setName("name: user1"));
assertThat(userNameText).hasText("name: user1");
}

@AfterClass
public void tearDown() {
this.page.close();
this.playwright.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void testOnChromeHeadless() {
final String pageTitle = page.title();
assertEquals(pageTitle, "Your Store");
browser.close();
playwright.close();
}


Expand All @@ -32,6 +33,7 @@ public void testOnChrome() {
final String pageTitle = page.title();
assertEquals(pageTitle, "Your Store");
browser.close();
playwright.close();
}

@Test
Expand All @@ -43,6 +45,7 @@ public void testOnFirefoxHeadless() {
final String pageTitle = page.title();
assertEquals(pageTitle, "Your Store");
browser.close();
playwright.close();
}

@Test
Expand All @@ -54,6 +57,7 @@ public void testOnFirefox() {
final String pageTitle = page.title();
assertEquals(pageTitle, "Your Store");
browser.close();
playwright.close();

}

Expand All @@ -66,6 +70,7 @@ public void testOnFirefoxSlowMo() {
final String pageTitle = page.title();
assertEquals(pageTitle, "Your Store");
browser.close();
playwright.close();
}

@Test
Expand All @@ -77,6 +82,7 @@ public void testOnChromeSlowMo() {
final String pageTitle = page.title();
assertEquals(pageTitle, "Your Store");
browser.close();
playwright.close();
}

@Test
Expand All @@ -88,6 +94,7 @@ public void testOnEdge() {
final String pageTitle = page.title();
assertEquals(pageTitle, "Your Store");
browser.close();
playwright.close();
}

@Test
Expand Down Expand Up @@ -124,6 +131,7 @@ public void testBrowserNavigation() {
assertEquals(currentPageUrl, websiteLink);

browser.close();
playwright.close();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

Expand Down Expand Up @@ -95,4 +96,11 @@ public void testKeyPress() {
textBox.press("9");
assertThat(resultText).containsText("9");
}

@AfterClass
public void tearDown() {
this.page.close();
this.playwright.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void testDifferentWindowSize() {
final String pageHeaderText = pageHeader.textContent();
assertEquals(pageHeaderText, "Selenium Playground");
browser.close();
playwright.close();

}

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

}

Expand Down
2 changes: 1 addition & 1 deletion test-suites/testng-formauthenticationtests.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Browser Navigation test suite">
<test name="Browser navigation tests using Playwright on Chrome">
<parameter name="browser" value="firefox"/>
<parameter name="browser" value="chrome"/>
<classes>
<class name="io.github.mfaisalkhatri.tests.FormAuthenticationTests">
<methods>
Expand Down
13 changes: 13 additions & 0 deletions test-suites/testng-mousehovertest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Playwright demo test suite " >
<test name="Mouse hover demo on Chrome">
<classes>
<class name="io.github.mfaisalkhatri.tests.MouseHoverTest">
<methods>
<include name="testMouseHover"/>
</methods>
</class>
</classes>
</test>
</suite>
36 changes: 36 additions & 0 deletions test-suites/testng-playwrightdemotests.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Playwright Demo Tests" parallel="tests">
<test name="Web automation tests using Playwright on Chrome">
<classes>
<class name="io.github.mfaisalkhatri.tests.PlaywrightDemoTests">
<methods>
<include name="testOnChromeHeadless"/>
<include name="testOnChrome"/>
<include name="testOnChromeSlowMo"/>
<include name="testBrowserNavigation"/>
</methods>
</class>
</classes>
</test>
<test name="Web automation tests using Playwright on Firefox">
<classes>
<class name="io.github.mfaisalkhatri.tests.PlaywrightDemoTests">
<methods>
<include name="testOnFirefoxHeadless"/>
<include name="testOnFirefox"/>
<include name="testOnFirefoxSlowMo"/>
</methods>
</class>
</classes>
</test>
<test name="Web automation tests using Playwright on Edge">
<classes>
<class name="io.github.mfaisalkhatri.tests.PlaywrightDemoTests">
<methods>
<include name="testOnEdge"/>
</methods>
</class>
</classes>
</test>
</suite>
4 changes: 3 additions & 1 deletion test-suites/testng.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Playwright framework example tests " parallel="tests">
<suite name="Playwright framework example tests ">
<suite-files>
<suite-file path="testng-browsertest.xml"/>
<suite-file path="testng-browsernavigationtests.xml"/>
Expand All @@ -10,5 +10,7 @@
<suite-file path="testng-screenshottests.xml"/>
<suite-file path="testng-dropdowntests.xml"/>
<suite-file path="testng-textfielddemo.xml"/>
<suite-file path="testng-mousehovertest.xml"/>
<suite-file path="testng-playwrightdemotests.xml"/>
</suite-files>
</suite>

0 comments on commit 608f781

Please sign in to comment.