Skip to content

Commit 0394d87

Browse files
PW features code
1 parent 6082ef0 commit 0394d87

12 files changed

+272
-26
lines changed

applogin.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": "2WuazkZ%2BJcwd4rE0WzPs5Sr19wSP%2FZsDeTJ%2FEJWva0FgkGjnG913ghpTJcU%2FN%2BCUOt%2BMhKBOptxwV7IZuzQKx2jak7MHdm778Q5%2FaU4GmPZLtcLGFgJ0XCtmpO2If4mHUncaNb4685PmGg1BbQM1J3dSNeNQ0qLt49IYtImyidQjnwy0R8QsSUicxuLgUcJSttTFlj06Jhb9se%2FNLz2ujrG6YdOocG0%2FcSEFOsJll%2BYcaDbvwgeWijpka1mdWviQUouIs5TDrSBrWyDWoEYfa3LwqSN%2BPo5Zu4Ex%2FnNxhBzPSj94R%2Fr88Rnktxf86NMrWonnAwMUeZywzrcsR0gHuzZQ4aB63X8JUf7twQcjjdokyAOvbJ1eGXyBGR5BsB7WR5g9dAiC%2F%2F1S1TOo47thGU8x%2BadDBXmHmeHRGhSckqAKuHxXsEUFUFuVgNXB2661000321",
7+
"domain": ".automationpractice.com",
8+
"path": "/",
9+
"expires": 1653218984.523128,
10+
"httpOnly": true,
11+
"secure": false
12+
}
13+
],
14+
"origins": []
15+
}

autostate.json

+1-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1 @@
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-
}
1+
{"cookies":[{"sameSite":"Lax","name":"PrestaShop-a30a9934ef476d11b6cc3c983616e364","value":"2WuazkZ%2BJcwd4rE0WzPs5dO2bgZsnkh8cmUEnboeJpNZlOvjFsauu%2FFQYBFzcrDcZfgBu9yXeHU%2BbBYdKLK8X4tvAiaoKauA%2F7VYoSG2K9UKkWBYb8QB7bIsLZgrWqoB7DQNc7m%2FGop0Vajc3YHpR3AbAz4CSyVpzeZgEYT6OGcX0JfVO8AlQaYoMj7qPlkF85TAnlsCrpBvdshahuXIuUVyRiPFCk4zNLCphH2KLoBCkhrEM0zGEq76%2BT%2B7wkNlLw8rHO8qeOOvK2hOlZXBgcyuZi%2B70IyAkYUunTV6nsHRWmdkKS8qq%2F2oa9alVnNhazvqU0prMPEUSqgfrM6INBQXkkhJ9AiwjcUTuVcf7oAyx8M0E101hgpF%2BEXSSDMFpgblATYcmuiOoYIS09yEurxN5PbEH3Ci4yjhZIkzp6hCjKqynqlcEE36RTp3BiXd000321","domain":".automationpractice.com","path":"/","expires":1653217970.026861,"httpOnly":true,"secure":false}],"origins":[]}

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<dependency>
2424
<groupId>com.microsoft.playwright</groupId>
2525
<artifactId>playwright</artifactId>
26-
<version>1.17.1</version>
26+
<version>1.21.0</version>
2727
</dependency>
2828

2929
<dependency>

src/main/java/playwrightsessions/Auth.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package playwrightsessions;
22

3+
34
import java.nio.file.Paths;
45

56
import com.microsoft.playwright.Browser;
@@ -15,15 +16,15 @@ public static void main(String[] args) {
1516
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
1617
BrowserContext brContext = browser.newContext();
1718

18-
//text locators
1919
Page page = brContext.newPage();
2020
page.navigate("http://automationpractice.com/");
21-
page.fill("#email", "[email protected]");
22-
page.fill("#passwd", "Selenium12345");
21+
page.click("a:text('Sign in')");
22+
page.fill("#email", "[email protected]");
23+
page.fill("#passwd", "testrigor123");
2324
page.click("#SubmitLogin");
2425

25-
brContext.storageState(new BrowserContext.StorageStateOptions().setPath(Paths.get("autostate.json")));
26-
26+
brContext
27+
.storageState(new BrowserContext.StorageStateOptions().setPath(Paths.get("applogin.json")));
2728

2829
}
2930

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 AutoWaitConcept {
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://www.bigbasket.com/");
18+
Locator login = page.locator("a:text('Login/Sign') >> nth=1");
19+
login.click();
20+
System.out.println(page.locator("button.login-btn:text('Continue')").isEnabled());
21+
22+
page.locator("#otpEmail").fill("9898989898");
23+
System.out.println(page.locator("button.login-btn:text('Continue')").isEnabled());
24+
25+
page.locator(".login-icon.login-icon-close").click();
26+
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package playwrightsessions;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.nio.file.Path;
5+
import java.nio.file.Paths;
6+
7+
import com.microsoft.playwright.Browser;
8+
import com.microsoft.playwright.BrowserType;
9+
import com.microsoft.playwright.Page;
10+
import com.microsoft.playwright.Playwright;
11+
import com.microsoft.playwright.options.FilePayload;
12+
13+
public class FileUploadHandle {
14+
15+
public static void main(String[] args) throws InterruptedException {
16+
Playwright playwright = Playwright.create();
17+
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
18+
Page page = browser.newPage();
19+
page.navigate("https://cgi-lib.berkeley.edu/ex/fup.html");
20+
//https://davidwalsh.name/demo/multiple-file-upload.php
21+
22+
//input type = file
23+
24+
//select one file:
25+
// page.setInputFiles("input#filesToUpload", Paths.get("applogin.json"));
26+
// Thread.sleep(4000);
27+
// page.setInputFiles("input#filesToUpload", new Path[0]);
28+
29+
//multiple files:
30+
// page.setInputFiles("input#filesToUpload",
31+
// new Path[] {
32+
// Paths.get("applogin.json"),
33+
// Paths.get("autostate.json"),
34+
// Paths.get("example.png")});
35+
// Thread.sleep(3000);
36+
// page.setInputFiles("input#filesToUpload", new Path[0]);
37+
38+
//run time file - upload:
39+
page.setInputFiles("input[name='upfile']", new FilePayload("naveen.text",
40+
"text/plain",
41+
"this is naveen here".getBytes(StandardCharsets.UTF_8)));
42+
43+
page.click("input[value='Press']");
44+
}
45+
46+
}

src/main/java/playwrightsessions/HasElements.java

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public static void main(String[] args) {
2020

2121
Locator footerList = page.locator("div.navFooterLinkCol:has(a[href='https://www.amazon.jobs'])");
2222
footerList.allInnerTexts().forEach(e -> System.out.println(e));
23+
2324

2425
}
2526

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.Page;
6+
import com.microsoft.playwright.Playwright;
7+
8+
public class JsPopUpHandle {
9+
10+
public static void main(String[] args) throws InterruptedException {
11+
12+
Playwright playwright = Playwright.create();
13+
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
14+
Page page = browser.newPage();
15+
page.onDialog(dialog -> {
16+
String text = dialog.message();
17+
System.out.println(text);
18+
dialog.accept("Alert from Naveen Autmation Labs");
19+
// dialog.dismiss();
20+
});
21+
22+
// JS Alerts, Prompt, Confirmation pop ups
23+
page.navigate("https://the-internet.herokuapp.com/javascript_alerts");
24+
page.click("//button[text()='Click for JS Alert']");
25+
String result = page.textContent("#result");
26+
System.out.println(result);
27+
28+
page.click("//button[text()='Click for JS Confirm']");
29+
result = page.textContent("#result");
30+
System.out.println(result);
31+
32+
page.click("//button[text()='Click for JS Prompt']");
33+
34+
result = page.textContent("#result");
35+
System.out.println(result);
36+
37+
page.close();
38+
browser.close();
39+
playwright.close();
40+
41+
}
42+
43+
}

src/main/java/playwrightsessions/NewAuth.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ public class NewAuth {
1313
public static void main(String[] args) {
1414
Playwright playwright = Playwright.create();
1515
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
16-
BrowserContext context = browser.newContext(
17-
new Browser.NewContextOptions().setStorageStatePath(Paths.get("autostate.json")));
18-
19-
//text locators
20-
Page page = context.newPage();
16+
17+
BrowserContext brContext =
18+
browser.newContext(new Browser.NewContextOptions().setStorageStatePath(Paths.get("applogin.json")));
19+
20+
Page page = brContext.newPage();
2121
page.navigate("http://automationpractice.com/");
22+
23+
2224
}
2325

2426
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package playwrightsessions;
2+
3+
import java.util.List;
4+
5+
import com.microsoft.playwright.Browser;
6+
import com.microsoft.playwright.BrowserContext;
7+
import com.microsoft.playwright.BrowserType;
8+
import com.microsoft.playwright.Locator;
9+
import com.microsoft.playwright.Page;
10+
import com.microsoft.playwright.Playwright;
11+
12+
public class ReactElement {
13+
14+
public static void main(String[] args) throws InterruptedException {
15+
Playwright playwright = Playwright.create();
16+
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
17+
18+
//React locators
19+
BrowserContext browserContext = browser.newContext();
20+
Page page = browserContext.newPage();
21+
page.navigate("https://www.netflix.com/ae-en/");
22+
23+
// Locator email = page.locator("_react=p[name='email'] >> input").first();
24+
// email.click();
25+
// email.fill("[email protected]");
26+
27+
page.locator("_react=UISelect[data-uia='language-picker']").click();
28+
29+
Locator footer = page.locator("_react=UIMarkup[data-uia='data-uia-footer-label']");
30+
//List<String> footerList = footer.allInnerTexts();
31+
32+
// for(String e : footerList) {
33+
// System.out.println(e);
34+
// }
35+
footer.allInnerTexts().forEach(e -> System.out.println(e));
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 ScopeLocators {
10+
11+
static Page page;
12+
public static void main(String[] args) {
13+
Playwright playwright = Playwright.create();
14+
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
15+
16+
page = browser.newPage();
17+
//page.navigate("https://datatables.net/extensions/select/examples/initialisation/checkbox.html");
18+
19+
// Locator row = page.locator("table#example tr");
20+
// row.locator(":scope", new Locator.LocatorOptions().setHasText("Ashton Cox")).locator(".select-checkbox").click();
21+
// row.locator(":scope").allInnerTexts().forEach(e -> System.out.println(e));
22+
23+
24+
page.navigate("https://www.primefaces.org/primeng/table");
25+
26+
Locator row = page.locator("table#pr_id_2-table tr");
27+
row.locator(":scope", new Locator.LocatorOptions().setHasText("James Butt")).locator(".p-checkbox-box")
28+
.click();
29+
30+
row.locator(":scope").allInnerTexts().forEach(e -> System.out.println(e));
31+
32+
33+
34+
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package playwrightsessions;
2+
3+
import java.util.List;
4+
5+
import com.microsoft.playwright.Browser;
6+
import com.microsoft.playwright.BrowserType;
7+
import com.microsoft.playwright.Locator;
8+
import com.microsoft.playwright.Page;
9+
import com.microsoft.playwright.Playwright;
10+
11+
public class XpathLocator {
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+
17+
Page page = browser.newPage();
18+
19+
page.navigate("https://selectorshub.com/xpath-practice-page/");
20+
21+
// page.locator("//a[text()='Joe.Root']/parent::td/preceding-sibling::td/input[@type='checkbox']")
22+
// .click();
23+
24+
25+
// Locator checkboxes = page.locator("//table[@id='resultTable']//input[@type='checkbox']");
26+
// for(int i=0; i<checkboxes.count(); i++) {
27+
// checkboxes.nth(i).click();
28+
// }
29+
30+
page.locator("xpath=(//table[@id='resultTable']//input[@type='checkbox'])[1]").click();
31+
32+
page.locator("(//table[@id='resultTable']//input[@type='checkbox'])[last()]").click();
33+
34+
//
35+
// //page.locator("//input[@id='twotabsearchtextbox']").fill("Macbook pro");
36+
// Locator totalAmazonLinks = page.locator("//a[contains(text(),'Amazon')]");
37+
// System.out.println(totalAmazonLinks.count());
38+
//
39+
// List<String> textList = totalAmazonLinks.allInnerTexts();
40+
// for(String e : textList) {
41+
// System.out.println(e);
42+
// }
43+
//
44+
45+
46+
}
47+
48+
}

0 commit comments

Comments
 (0)