Skip to content

Commit b5aecbc

Browse files
committed
section 3
1 parent 01619f9 commit b5aecbc

8 files changed

Lines changed: 312 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>java-playwright-junit</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>25</maven.compiler.source>
13+
<maven.compiler.target>25</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<!-- Source: https://mvnrepository.com/artifact/com.microsoft.playwright/playwright -->
19+
<dependency>
20+
<groupId>com.microsoft.playwright</groupId>
21+
<artifactId>playwright</artifactId>
22+
<version>1.58.0</version>
23+
<scope>compile</scope>
24+
</dependency>
25+
26+
<!-- Source: https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
27+
<dependency>
28+
<groupId>org.junit.jupiter</groupId>
29+
<artifactId>junit-jupiter-api</artifactId>
30+
<version>6.1.0-M1</version>
31+
<scope>test</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.junit.jupiter</groupId>
35+
<artifactId>junit-jupiter-api</artifactId>
36+
<version>6.1.0-M1</version>
37+
<scope>compile</scope>
38+
</dependency>
39+
40+
</dependencies>
41+
42+
</project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.luv2code.framework.base;
2+
3+
import com.microsoft.playwright.*;
4+
import org.junit.jupiter.api.AfterEach;
5+
import org.junit.jupiter.api.BeforeEach;
6+
7+
import java.util.List;
8+
9+
public class BaseTest {
10+
11+
public Playwright playwright;
12+
public Page page;
13+
public Browser browser;
14+
15+
@BeforeEach
16+
public void setUp() {
17+
18+
// Launch Chrome browser
19+
playwright = Playwright.create();
20+
browser = playwright.chromium().launch(
21+
new BrowserType.LaunchOptions()
22+
.setHeadless(false)
23+
.setArgs(List.of("--start-maximized"))
24+
);
25+
26+
// Create context without viewport (true maximize effect)
27+
page = browser.newContext(new Browser.NewContextOptions().setViewportSize(null)).newPage();
28+
29+
// Navigate to luv2test site
30+
page.navigate("https://www.luv2test.com/");
31+
System.out.println("Home page Title: " + page.title());
32+
33+
}
34+
35+
// method to highlight element
36+
public static void highlightElement(Locator locator) {
37+
38+
ElementHandle handle = locator.elementHandle();
39+
if(handle != null) {
40+
locator.page().evaluate("e => {" +
41+
"e.style.border='3px solid green'; " +
42+
"e.style.backgroundColor='yellow'; }", handle);
43+
}
44+
45+
}
46+
47+
@AfterEach
48+
public void tearDown() {
49+
50+
// Close the browser
51+
browser.close();
52+
playwright.close();
53+
54+
}
55+
56+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.luv2code.o3_quick_start;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.luv2code.framework.base.BaseTest;
5+
6+
public class FinalTest extends BaseTest {
7+
8+
@Test
9+
public void runFinalTest() {
10+
11+
// Click on Authentication link
12+
page.click("text=Authentication");
13+
14+
// Verify Login heading is present
15+
boolean isHeadingPresent = page.locator("//h2[text()='Login']").isVisible();
16+
System.out.println("Is Login form heading present: " + isHeadingPresent);
17+
18+
}
19+
20+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.luv2code.o3_quick_start;
2+
3+
import com.microsoft.playwright.Browser;
4+
import com.microsoft.playwright.BrowserType;
5+
import com.microsoft.playwright.Page;
6+
import com.microsoft.playwright.Playwright;
7+
import org.junit.jupiter.api.AfterEach;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
import java.util.List;
12+
13+
public class FirstPlaywrightJUnitTest {
14+
15+
Playwright playwright;
16+
Page page;
17+
Browser browser;
18+
19+
@BeforeEach
20+
public void setUp() {
21+
22+
// Launch Chrome browser
23+
playwright = Playwright.create();
24+
browser = playwright.chromium().launch(
25+
new BrowserType.LaunchOptions()
26+
.setHeadless(false)
27+
.setArgs(List.of("--start-maximized"))
28+
);
29+
30+
// Create context without viewport (true maximize effect)
31+
page = browser.newContext(new Browser.NewContextOptions().setViewportSize(null)).newPage();
32+
33+
// Navigate to luv2test site
34+
page.navigate("https://www.luv2test.com/");
35+
System.out.println("Home page Title: " + page.title());
36+
37+
}
38+
39+
@Test
40+
public void firstPlaywrightJUnitTest() {
41+
42+
// Click on Authentication link
43+
page.click("text=Authentication");
44+
45+
// Verify Login heading is present
46+
boolean isHeadingPresent = page.locator("//h2[text()='Login']").isVisible();
47+
System.out.println("Is Login form heading present: " + isHeadingPresent);
48+
49+
}
50+
51+
@AfterEach
52+
public void tearDown() {
53+
54+
// Close the browser
55+
browser.close();
56+
playwright.close();
57+
58+
}
59+
60+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.luv2code.o3_quick_start;
2+
3+
import com.microsoft.playwright.Browser;
4+
import com.microsoft.playwright.BrowserType;
5+
import com.microsoft.playwright.Page;
6+
import com.microsoft.playwright.Playwright;
7+
8+
import java.util.List;
9+
10+
public class FirstPlaywrightTest {
11+
12+
public static void main (String [] args) {
13+
14+
// Launch Chrome browser
15+
Playwright playwright = Playwright.create();
16+
Browser browser = playwright.chromium().launch(
17+
new BrowserType.LaunchOptions()
18+
.setHeadless(false)
19+
.setArgs(List.of("--start-maximized"))
20+
);
21+
22+
// Create context without viewport (true maximize effect)
23+
Page page = browser.newContext(new Browser.NewContextOptions().setViewportSize(null)).newPage();
24+
25+
// Navigate to luv2test site
26+
page.navigate("https://www.luv2test.com/");
27+
System.out.println("Home page Title: " + page.title());
28+
29+
// Click on Authentication link
30+
page.click("text=Authentication");
31+
32+
// Verify Login heading is present
33+
boolean isHeadingPresent = page.locator("//h2[text()='Login']").isVisible();
34+
System.out.println("Is Login form heading present: " + isHeadingPresent);
35+
36+
// Close the browser
37+
browser.close();
38+
playwright.close();
39+
40+
41+
}
42+
43+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.luv2code.o3_quick_start;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.luv2code.framework.base.BaseTest;
5+
6+
public class HighlightElementTest extends BaseTest {
7+
8+
@Test
9+
public void highlightElementTest() {
10+
11+
highlightElement(page.locator("xpath=//h1[text()='Luv2Code Automation Practice (Web and Mobile Web)']"));
12+
highlightElement(page.locator("xpath=//h3[text()='Authentication']"));
13+
System.out.println("Success");
14+
15+
}
16+
17+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.luv2code.o3_quick_start;
2+
3+
import com.microsoft.playwright.Page;
4+
import com.microsoft.playwright.options.AriaRole;
5+
import org.junit.jupiter.api.Test;
6+
import org.luv2code.framework.base.BaseTest;
7+
8+
public class LocatorStrategiesTest extends BaseTest {
9+
10+
@Test
11+
public void locatorStrategiesTest() {
12+
13+
page.navigate("https://www.luv2test.com/#/registration-form");
14+
15+
// locate by Role-based
16+
page.getByRole(AriaRole.TEXTBOX, new Page.GetByRoleOptions().setName("Full Name"));
17+
18+
// locate by app test IDs
19+
page.getByTestId("password");
20+
21+
// locate by Label & Placeholder
22+
page.getByLabel("Address");
23+
// page.getByPlaceholder("placeholder");
24+
25+
// locate by Text locator
26+
page.getByText("Email:");
27+
// page.getByAltText("King image");
28+
29+
// locate by Title locator
30+
page.getByTitle("Luv2Code Automation");
31+
32+
// locate by CSS Selector
33+
page.locator("input[type='Number']");
34+
35+
// locate by XPath
36+
page.locator("//input[@id='gender-female']");
37+
38+
39+
// locate by Nth locator (index based)
40+
page.locator("input[type='radio']").nth(1);
41+
42+
}
43+
44+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.luv2code.o3_quick_start;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.luv2code.framework.base.BaseTest;
5+
6+
public class NavigationCommandsTest extends BaseTest {
7+
8+
@Test
9+
public void navigationCommandsTest() {
10+
11+
// Navigate to a specific URL
12+
page.navigate("https://www.luv2test.com/#/tables");
13+
14+
// Wait until a specific URL is loaded
15+
page.waitForURL("**/tables");
16+
17+
18+
// Go back to the previous page in browser history
19+
page.goBack();
20+
21+
// Go forward to the next page in browser history
22+
page.goForward();
23+
24+
// Refresh the current page
25+
page.reload();
26+
27+
28+
}
29+
30+
}

0 commit comments

Comments
 (0)