Skip to content

Commit 6082ef0

Browse files
Playwright Java practice code
0 parents  commit 6082ef0

25 files changed

+895
-0
lines changed

.gitignore

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
allure-results/
2+
screenshots/
3+
screenshot/
4+
test-output/
5+
build/
6+
7+
application.log
8+
##############################
9+
## Java
10+
##############################
11+
.mtj.tmp/
12+
*.class
13+
*.jar
14+
*.war
15+
*.ear
16+
*.nar
17+
hs_err_pid*
18+
activityLog.log
19+
##############################
20+
## Maven
21+
##############################
22+
target/
23+
pom.xml.tag
24+
pom.xml.releaseBackup
25+
pom.xml.versionsBackup
26+
pom.xml.next
27+
pom.xml.bak
28+
release.properties
29+
dependency-reduced-pom.xml
30+
buildNumber.properties
31+
.mvn/timing.properties
32+
.mvn/wrapper/maven-wrapper.jar
33+
34+
##############################
35+
## IntelliJ
36+
##############################
37+
out/
38+
.idea/
39+
.idea_modules/
40+
*.iml
41+
*.ipr
42+
*.iws
43+
##############################
44+
## Eclipse
45+
##############################
46+
.settings/
47+
bin/
48+
tmp/
49+
.metadata
50+
.classpath
51+
.project
52+
*.tmp
53+
*.bak
54+
*.swp
55+
*~.nib
56+
local.properties
57+
.loadpath
58+
.factorypath
59+
60+
## OS X
61+
##############################
62+
.DS_Store

autostate.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"cookies": [
3+
{
4+
"sameSite": "Lax",
5+
"name": "PrestaShop-a30a9934ef476d11b6cc3c983616e364",
6+
"value": "w%2FKa4WdcU6S5h%2Fd94yXzulZH0Exo2Mc4F5derD7upFmk1tiEfQF04Dej0dHNLEuykWcLz7Rh00IMgdzS5Iu9sPfi6bF4%2B9sH%2BKea6yXIF6qUoXLQ%2B9hJJFOj1dFLkMlpD0SQgAM8Evak2gEsSRVETVT%2FMyy4buVoa0zHrbhC50pgqS%2BfELFpNtyH%2F9UXLbZvZI4nFrwuirsArdI0543IQ%2FApCFhWhhGpomlDqEMo1aC8DLbcf8q2mUazyium979cEAah5OxL1jJJ1WHM5GLOrP%2B%2BIxI3nSLBTfN4y6EWkxA2xdXxa96JNcfkbK8%2BX%2BMJ%2FHSv5gamDn%2BgvFoby3YoRdTErNQkgBRhfagyTa6qiLTAOdLI%2FFsCzeUyDwsbcRwnvj8YgpII5seYIisHFG5VHfNIvHbZ3P%2FC7zH0t%2FcML6rJUAzKXaUu2Cdowp9g1WbG000324",
7+
"domain": ".automationpractice.com",
8+
"path": "/",
9+
"expires": 1651322809.040274,
10+
"httpOnly": true,
11+
"secure": false
12+
}
13+
],
14+
"origins": []
15+
}

example.png

10.9 KB
Loading

pom.xml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>PlaywrightDemo</groupId>
9+
<artifactId>PlaywrightDemo</artifactId>
10+
<version>0.0.1-SNAPSHOT</version>
11+
12+
<name>PlaywrightDemo</name>
13+
<!-- FIXME change it to the project's website -->
14+
<url>http://www.example.com</url>
15+
16+
<properties>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<maven.compiler.source>1.8</maven.compiler.source>
19+
<maven.compiler.target>1.8</maven.compiler.target>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>com.microsoft.playwright</groupId>
25+
<artifactId>playwright</artifactId>
26+
<version>1.17.1</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.testng</groupId>
31+
<artifactId>testng</artifactId>
32+
<version>6.14.3</version>
33+
</dependency>
34+
</dependencies>
35+
36+
37+
</project>
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package playwrightsessions;
2+
3+
import java.nio.file.Paths;
4+
5+
import com.microsoft.playwright.Browser;
6+
import com.microsoft.playwright.BrowserContext;
7+
import com.microsoft.playwright.BrowserType;
8+
import com.microsoft.playwright.Page;
9+
import com.microsoft.playwright.Playwright;
10+
11+
public class Auth {
12+
13+
public static void main(String[] args) {
14+
Playwright playwright = Playwright.create();
15+
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
16+
BrowserContext brContext = browser.newContext();
17+
18+
//text locators
19+
Page page = brContext.newPage();
20+
page.navigate("http://automationpractice.com/");
21+
page.fill("#email", "[email protected]");
22+
page.fill("#passwd", "Selenium12345");
23+
page.click("#SubmitLogin");
24+
25+
brContext.storageState(new BrowserContext.StorageStateOptions().setPath(Paths.get("autostate.json")));
26+
27+
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package playwrightsessions;
2+
3+
import com.microsoft.playwright.Browser;
4+
import com.microsoft.playwright.BrowserContext;
5+
import com.microsoft.playwright.BrowserType;
6+
import com.microsoft.playwright.BrowserType.LaunchOptions;
7+
import com.microsoft.playwright.Page;
8+
import com.microsoft.playwright.Playwright;
9+
10+
public class BrowserContextConcept {
11+
12+
public static void main(String[] args) {
13+
14+
Playwright playwright = Playwright.create();
15+
LaunchOptions op = new LaunchOptions();
16+
op.setHeadless(false);
17+
Browser browser = playwright.chromium().launch(op);
18+
19+
BrowserContext brcx1 = browser.newContext();
20+
Page p1 = brcx1.newPage();
21+
p1.setViewportSize(1920, 1080);
22+
23+
p1.navigate("https://www.orangehrm.com/orangehrm-30-day-trial");
24+
p1.fill("#Form_submitForm_Name", "Naveen");
25+
System.out.println(p1.title());
26+
27+
String text = p1.textContent("h1:has-text('Your free trial')");
28+
System.out.println(text);
29+
30+
// BrowserContext brcx2 = browser.newContext();
31+
// Page p2 = brcx2.newPage();
32+
// p2.navigate("http://automationpractice.com/");
33+
// p2.fill("#search_query_top", "Dress");
34+
// System.out.println(p2.title());
35+
//
36+
// BrowserContext brcx3 = browser.newContext();
37+
// Page p3 = brcx3.newPage();
38+
// p3.navigate("http://google.com/");
39+
// System.out.println(p3.title());
40+
41+
// p1.close();
42+
// brcx1.close();
43+
//
44+
// p2.close();
45+
// brcx2.close();
46+
//
47+
// p3.close();
48+
// brcx3.close();
49+
//
50+
51+
52+
53+
54+
55+
}
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package playwrightsessions;
2+
3+
import com.microsoft.playwright.Browser;
4+
import com.microsoft.playwright.BrowserType;
5+
import com.microsoft.playwright.Locator;
6+
import com.microsoft.playwright.Page;
7+
import com.microsoft.playwright.Playwright;
8+
9+
public class CommaSelectors {
10+
11+
public static void main(String[] args) {
12+
Playwright playwright = Playwright.create();
13+
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
14+
15+
//text locators
16+
Page page = browser.newPage();
17+
page.navigate("https://academy.naveenautomationlabs.com");
18+
19+
// page.locator("span:has-text('SignIn'), "
20+
// + "span:has-text('LogIn'), "
21+
// + "span:has-text('Login'), "
22+
// + "span:has-text('Signin'), "
23+
// + "span:has-text('signin')").click();
24+
25+
Locator imp_ele =
26+
page.locator("span:has-text('Store'), "
27+
+ "span:has-text('signin'),"
28+
+ " a:has-text('EXPLORE COURSES')");
29+
30+
System.out.println(imp_ele.count());
31+
32+
if(imp_ele.count() == 3) {
33+
System.out.println("PASS");
34+
}
35+
else {
36+
System.out.println("FAIL");
37+
}
38+
39+
40+
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package playwrightsessions;
2+
3+
import java.nio.file.Paths;
4+
5+
import com.microsoft.playwright.*;
6+
7+
8+
public class Example {
9+
public static void main(String[] args) {
10+
try (Playwright playwright = Playwright.create()) {
11+
Browser browser = playwright.firefox().launch(new BrowserType.LaunchOptions()
12+
.setHeadless(false));
13+
BrowserContext context = browser.newContext();
14+
15+
context.tracing().start(new Tracing.StartOptions()
16+
.setScreenshots(true)
17+
.setSnapshots(true));
18+
19+
// Open new page
20+
Page page = context.newPage();
21+
22+
// Go to https://academy.naveenautomationlabs.com/
23+
page.navigate("https://academy.naveenautomationlabs.com/");
24+
25+
// Click text=Login
26+
page.click("text=Login");
27+
28+
page.pause();
29+
// Click button:has-text("Sign Up")
30+
page.click("button:has-text(\"Sign Up\")");
31+
32+
// Click [placeholder="Name"]
33+
page.click("[placeholder=\"Name\"]");
34+
35+
// Fill [placeholder="Name"]
36+
page.fill("[placeholder=\"Name\"]", "testingautomatiomn");
37+
38+
// Fill [placeholder="Email"]
39+
page.fill("[placeholder=\"Email\"]", "[email protected]");
40+
41+
42+
// Fill [placeholder="Create Password"]
43+
page.fill("[placeholder=\"Create Password\"]", "test123");
44+
45+
// Click text=+971
46+
page.click("text=+971");
47+
48+
// Click :nth-match(:text("India (भारत)"), 2)
49+
page.click(":nth-match(:text(\"India (भारत)\"), 2)");
50+
51+
// Click [placeholder="Mobile"]
52+
page.click("[placeholder=\"Mobile\"]");
53+
54+
// Fill [placeholder="Mobile"]
55+
page.fill("[placeholder=\"Mobile\"]", "9898989898");
56+
57+
// Click text=By signing up, I agree to the Terms of Use and Privacy Policy. >> i
58+
page.click("text=By signing up, I agree to the Terms of Use and Privacy Policy. >> i");
59+
60+
// Click #loginFormHtml div [aria-label="Close"]
61+
page.click("#loginFormHtml div [aria-label=\"Close\"]");
62+
63+
context.tracing().stop(new Tracing.StopOptions()
64+
.setPath(Paths.get("trace.zip")));
65+
66+
}
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package playwrightsessions;
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+
public class FillFormUsingRelativeLocators {
9+
10+
static Page page;
11+
public static void main(String[] args) {
12+
Playwright playwright = Playwright.create();
13+
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
14+
15+
page = browser.newPage();
16+
page.navigate("https://www.orangehrm.com/orangehrm-30-day-trial/");
17+
18+
page.locator("input:below(label:text('Full Name'))").first().fill("Naveen");
19+
page.locator("input:below(label:text('Email'))").first().fill("[email protected]");
20+
page.locator("input:below(label:text('Phone Number'))").first().fill("Naveen");
21+
22+
}
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package playwrightsessions;
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+
public class FrameHandle {
9+
10+
public static void main(String[] args) {
11+
Playwright playwright = Playwright.create();
12+
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
13+
14+
Page page = browser.newPage();
15+
page.navigate("https://www.formsite.com/templates/registration-form-templates/vehicle-registration-form/");
16+
17+
page.locator("img[title='vehicle-registration-forms-and-templates']").click();
18+
19+
page.frameLocator("//iframe[contains(@id,'frame-one')]").locator("#RESULT_TextField-8")
20+
.fill("Naveen Automation");
21+
22+
// String header = page.frameLocator("//frame[@name='main']").locator("h2").textContent();
23+
// System.out.println(header);
24+
25+
// String h2 = page.frame("main").locator("h2").textContent();
26+
// System.out.println(h2);
27+
28+
}
29+
30+
}

0 commit comments

Comments
 (0)