├── .gitignore
├── .settings
├── org.eclipse.m2e.core.prefs
└── org.eclipse.jdt.core.prefs
├── target
└── classes
│ ├── META-INF
│ ├── MANIFEST.MF
│ └── maven
│ │ └── playwrightfeatures
│ │ └── playwrightfeatures
│ │ ├── pom.properties
│ │ └── pom.xml
│ ├── launchBrowsers
│ ├── LaunchChrome.class
│ ├── LaunchMsEdge.class
│ ├── LaunchWebKit.class
│ ├── LaunchChromeUI.class
│ ├── LaunchChromium.class
│ ├── LaunchFirefox.class
│ ├── LaunchFirefoxUI.class
│ ├── LaunchMsEdgeUI.class
│ ├── LaunchWebKitUI.class
│ └── LaunchChromiumUI.class
│ ├── elementLocators
│ ├── singleElement.class
│ ├── singleElement2.class
│ ├── multipleElementsUsingListFor.class
│ ├── multipleElementsUsingNthIndex.class
│ └── multipleElementsUsingLambaForeach.class
│ └── browserContext
│ ├── browserContextsList.class
│ ├── browserContextsUsage.class
│ ├── browserContextOptions.class
│ └── browserContextSampleCode.class
├── videos
├── 26e403f329953c7304eedb83658bfc59.webm
├── 4559b3a8521e8c7507f2069ddf301a8b.webm
├── 479ee80d5ba5074b6bd2041b2c67249e.webm
└── ea3be17361e47e9bbe588ab7383e3ea1.webm
├── state.json
├── src
└── main
│ └── java
│ ├── handleFrames
│ ├── frameUsingId.java
│ ├── frameUsingName.java
│ ├── frameLocatorMethod.java
│ └── frameUsingXpath.java
│ ├── launchBrowsers
│ ├── LaunchWebKit.java
│ ├── LaunchChromium.java
│ ├── LaunchFirefox.java
│ ├── LaunchChrome.java
│ ├── LaunchMsEdge.java
│ ├── LaunchFirefoxUI.java
│ ├── LaunchWebKitUI.java
│ ├── LaunchChromiumUI.java
│ ├── LaunchChromeUI.java
│ └── LaunchMsEdgeUI.java
│ ├── elementLocators
│ ├── singleElement2.java
│ ├── multipleElementsUsingListFor.java
│ ├── multipleElementsUsingLambaForeach.java
│ ├── multipleElementsUsingNthIndex.java
│ └── singleElement.java
│ ├── authentication
│ ├── useSavedStorageState.java
│ └── saveStorageState.java
│ ├── browserContext
│ ├── browserContextOptions.java
│ ├── browserContextSampleCode.java
│ ├── browserContextsUsage.java
│ └── browserContextsList.java
│ ├── selectors
│ ├── hasElements2.java
│ ├── hasElements.java
│ ├── textSelectors.java
│ └── cssSelectors.java
│ ├── handleWebTables
│ ├── scopeLocators1.java
│ └── scopeLocators2.java
│ ├── handleDialogs
│ ├── handleJSAlert.java
│ ├── handleJSConfirm.java
│ ├── handleJSPrompt.java
│ ├── handleJSDialogsUsingOnDialogHandlerDismiss.java
│ └── handleJSDialogsUsingOnDialogHandlerAccept.java
│ ├── apiTesting
│ ├── deleteMethod
│ │ └── deleteCall.java
│ ├── getMethod
│ │ ├── apiGetCall.java
│ │ └── apiGetCall2.java
│ ├── patchMethod
│ │ └── patchCall.java
│ ├── postMethod
│ │ ├── postCall.java
│ │ └── postCall2.java
│ └── putMethod
│ │ └── putCall.java
│ ├── handleShadowDOM
│ └── shadowRootElements.java
│ ├── reactSelectors
│ └── reactElement.java
│ ├── recordVideos
│ ├── recordVideoOfTests.java
│ ├── recordVideoOfTests2.java
│ └── recordVideoViewPortSize.java
│ ├── getByLocatorAPIs
│ ├── afterGetByAPIs.java
│ └── beforeGetByAPIs.java
│ └── visibleElements
│ └── selectingVisibleElements.java
├── .project
├── pom.xml
├── .classpath
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.m2e.core.prefs:
--------------------------------------------------------------------------------
1 | activeProfiles=
2 | eclipse.preferences.version=1
3 | resolveWorkspaceProjects=true
4 | version=1
5 |
--------------------------------------------------------------------------------
/target/classes/META-INF/MANIFEST.MF:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | Built-By: kumar
3 | Build-Jdk: 17.0.3
4 | Created-By: Maven Integration for Eclipse
5 |
6 |
--------------------------------------------------------------------------------
/videos/26e403f329953c7304eedb83658bfc59.webm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/videos/26e403f329953c7304eedb83658bfc59.webm
--------------------------------------------------------------------------------
/videos/4559b3a8521e8c7507f2069ddf301a8b.webm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/videos/4559b3a8521e8c7507f2069ddf301a8b.webm
--------------------------------------------------------------------------------
/videos/479ee80d5ba5074b6bd2041b2c67249e.webm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/videos/479ee80d5ba5074b6bd2041b2c67249e.webm
--------------------------------------------------------------------------------
/videos/ea3be17361e47e9bbe588ab7383e3ea1.webm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/videos/ea3be17361e47e9bbe588ab7383e3ea1.webm
--------------------------------------------------------------------------------
/target/classes/launchBrowsers/LaunchChrome.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/target/classes/launchBrowsers/LaunchChrome.class
--------------------------------------------------------------------------------
/target/classes/launchBrowsers/LaunchMsEdge.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/target/classes/launchBrowsers/LaunchMsEdge.class
--------------------------------------------------------------------------------
/target/classes/launchBrowsers/LaunchWebKit.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/target/classes/launchBrowsers/LaunchWebKit.class
--------------------------------------------------------------------------------
/target/classes/elementLocators/singleElement.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/target/classes/elementLocators/singleElement.class
--------------------------------------------------------------------------------
/target/classes/elementLocators/singleElement2.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/target/classes/elementLocators/singleElement2.class
--------------------------------------------------------------------------------
/target/classes/launchBrowsers/LaunchChromeUI.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/target/classes/launchBrowsers/LaunchChromeUI.class
--------------------------------------------------------------------------------
/target/classes/launchBrowsers/LaunchChromium.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/target/classes/launchBrowsers/LaunchChromium.class
--------------------------------------------------------------------------------
/target/classes/launchBrowsers/LaunchFirefox.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/target/classes/launchBrowsers/LaunchFirefox.class
--------------------------------------------------------------------------------
/target/classes/launchBrowsers/LaunchFirefoxUI.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/target/classes/launchBrowsers/LaunchFirefoxUI.class
--------------------------------------------------------------------------------
/target/classes/launchBrowsers/LaunchMsEdgeUI.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/target/classes/launchBrowsers/LaunchMsEdgeUI.class
--------------------------------------------------------------------------------
/target/classes/launchBrowsers/LaunchWebKitUI.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/target/classes/launchBrowsers/LaunchWebKitUI.class
--------------------------------------------------------------------------------
/target/classes/launchBrowsers/LaunchChromiumUI.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/target/classes/launchBrowsers/LaunchChromiumUI.class
--------------------------------------------------------------------------------
/target/classes/browserContext/browserContextsList.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/target/classes/browserContext/browserContextsList.class
--------------------------------------------------------------------------------
/target/classes/browserContext/browserContextsUsage.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/target/classes/browserContext/browserContextsUsage.class
--------------------------------------------------------------------------------
/target/classes/browserContext/browserContextOptions.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/target/classes/browserContext/browserContextOptions.class
--------------------------------------------------------------------------------
/target/classes/browserContext/browserContextSampleCode.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/target/classes/browserContext/browserContextSampleCode.class
--------------------------------------------------------------------------------
/target/classes/elementLocators/multipleElementsUsingListFor.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/target/classes/elementLocators/multipleElementsUsingListFor.class
--------------------------------------------------------------------------------
/target/classes/elementLocators/multipleElementsUsingNthIndex.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/target/classes/elementLocators/multipleElementsUsingNthIndex.class
--------------------------------------------------------------------------------
/target/classes/elementLocators/multipleElementsUsingLambaForeach.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/swaroopnadella/playwright-java/HEAD/target/classes/elementLocators/multipleElementsUsingLambaForeach.class
--------------------------------------------------------------------------------
/target/classes/META-INF/maven/playwrightfeatures/playwrightfeatures/pom.properties:
--------------------------------------------------------------------------------
1 | #Generated by Maven Integration for Eclipse
2 | #Tue Nov 08 23:33:04 IST 2022
3 | m2e.projectLocation=C\:\\Users\\kumar\\Projects\\PlaywrightJavaBasics
4 | m2e.projectName=playwrightfeatures
5 | groupId=playwrightfeatures
6 | artifactId=playwrightfeatures
7 | version=0.0.1-SNAPSHOT
8 |
--------------------------------------------------------------------------------
/state.json:
--------------------------------------------------------------------------------
1 | {"cookies":[{"name":"OCSESSID","value":"708a2ba4191b8f443593b72d2e","domain":"naveenautomationlabs.com","path":"/","expires":-1,"httpOnly":false,"secure":true,"sameSite":"Lax"},{"name":"language","value":"en-gb","domain":".naveenautomationlabs.com","path":"/","expires":1669051995.768896,"httpOnly":false,"secure":true,"sameSite":"Lax"},{"name":"currency","value":"USD","domain":".naveenautomationlabs.com","path":"/","expires":1669051995.769073,"httpOnly":false,"secure":true,"sameSite":"Lax"}],"origins":[]}
--------------------------------------------------------------------------------
/src/main/java/handleFrames/frameUsingId.java:
--------------------------------------------------------------------------------
1 | package handleFrames;
2 |
3 | import com.microsoft.playwright.*;
4 |
5 | public class frameUsingId {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | Playwright pw = Playwright.create();
10 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
11 |
12 | Page page = browser.newPage();
13 | page.navigate("https://demoqa.com/frames");
14 |
15 | String str = page.frameLocator("#frame2").locator("h1").textContent();
16 | System.out.println(str);
17 |
18 | pw.close();
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | playwright-features
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.m2e.core.maven2Builder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.jdt.core.javanature
21 | org.eclipse.m2e.core.maven2Nature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
4 | org.eclipse.jdt.core.compiler.compliance=11
5 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
6 | org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
7 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
8 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
9 | org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
10 | org.eclipse.jdt.core.compiler.release=enabled
11 | org.eclipse.jdt.core.compiler.source=11
12 |
--------------------------------------------------------------------------------
/src/main/java/handleFrames/frameUsingName.java:
--------------------------------------------------------------------------------
1 | package handleFrames;
2 |
3 | import com.microsoft.playwright.*;
4 |
5 | public class frameUsingName {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | Playwright pw = Playwright.create();
10 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
11 |
12 | Page page = browser.newPage();
13 | page.navigate("http://www.londonfreelance.org/courses/frames/index.html");
14 |
15 | Frame frame = page.frame("main");
16 | String st = frame.locator("h2").textContent();
17 | System.out.println(st);
18 |
19 | pw.close();
20 |
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/handleFrames/frameLocatorMethod.java:
--------------------------------------------------------------------------------
1 | package handleFrames;
2 |
3 | import com.microsoft.playwright.*;
4 |
5 | public class frameLocatorMethod {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | Playwright pw = Playwright.create();
10 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
11 |
12 | Page page = browser.newPage();
13 | page.navigate("http://www.londonfreelance.org/courses/frames/index.html");
14 |
15 | String str = page.frameLocator("frame[name='main']").locator("h2").textContent();
16 | System.out.println(str);
17 |
18 | pw.close();
19 |
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/launchBrowsers/LaunchWebKit.java:
--------------------------------------------------------------------------------
1 | package launchBrowsers;
2 |
3 | import com.microsoft.playwright.Browser;
4 | import com.microsoft.playwright.Page;
5 | import com.microsoft.playwright.Playwright;
6 |
7 | public class LaunchWebKit {
8 |
9 | public static void main(String[] args) {
10 |
11 | Playwright pw = Playwright.create();
12 | //By default, Playwright runs the browsers in headless mode.
13 | Browser browser = pw.webkit().launch();
14 | Page page = browser.newPage();
15 | page.navigate("http://playwright.dev");
16 | System.out.println("Page Title: "+page.title());
17 | System.out.println("Page URL: "+page.url());
18 |
19 | browser.close();
20 | pw.close();
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/launchBrowsers/LaunchChromium.java:
--------------------------------------------------------------------------------
1 | package launchBrowsers;
2 |
3 | import com.microsoft.playwright.Browser;
4 | import com.microsoft.playwright.Page;
5 | import com.microsoft.playwright.Playwright;
6 |
7 | public class LaunchChromium {
8 |
9 | public static void main(String[] args) {
10 |
11 | Playwright pw = Playwright.create();
12 | //By default, Playwright runs the browsers in headless mode.
13 | Browser browser = pw.chromium().launch();
14 | Page page = browser.newPage();
15 | page.navigate("http://playwright.dev");
16 | System.out.println("Page Title: "+page.title());
17 | System.out.println("Page URL: "+page.url());
18 |
19 | browser.close();
20 | pw.close();
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/launchBrowsers/LaunchFirefox.java:
--------------------------------------------------------------------------------
1 | package launchBrowsers;
2 |
3 | import com.microsoft.playwright.Browser;
4 | import com.microsoft.playwright.Page;
5 | import com.microsoft.playwright.Playwright;
6 |
7 | public class LaunchFirefox {
8 |
9 | public static void main(String[] args) {
10 |
11 | Playwright pw = Playwright.create();
12 | //By default, Playwright runs the browsers in headless mode.
13 | Browser browser = pw.firefox().launch();
14 | Page page = browser.newPage();
15 | page.navigate("http://playwright.dev");
16 | System.out.println("Page Title: "+page.title());
17 | System.out.println("Page URL: "+page.url());
18 |
19 | browser.close();
20 | pw.close();
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/elementLocators/singleElement2.java:
--------------------------------------------------------------------------------
1 | package elementLocators;
2 |
3 | import com.microsoft.playwright.*;
4 |
5 | public class singleElement2 {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | Playwright pw = Playwright.create();
10 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
11 |
12 | BrowserContext browserContext = browser.newContext();
13 | Page page = browserContext.newPage();
14 | page.navigate("https://academy.naveenautomationlabs.com/");
15 |
16 | Locator login = page.locator("text=Login");
17 | int loginsCount = login.count();
18 | System.out.println(loginsCount);
19 | login.first().click();
20 |
21 | pw.close();
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/launchBrowsers/LaunchChrome.java:
--------------------------------------------------------------------------------
1 | package launchBrowsers;
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 LaunchChrome {
9 |
10 | public static void main(String[] args) {
11 |
12 | Playwright pw = Playwright.create();
13 | //By default, Playwright runs the browsers in headless mode.
14 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setChannel("chrome"));
15 | Page page = browser.newPage();
16 | page.navigate("http://playwright.dev");
17 | System.out.println("Page Title: "+page.title());
18 | System.out.println("Page URL: "+page.url());
19 |
20 | browser.close();
21 | pw.close();
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/launchBrowsers/LaunchMsEdge.java:
--------------------------------------------------------------------------------
1 | package launchBrowsers;
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 LaunchMsEdge {
9 |
10 | public static void main(String[] args) {
11 |
12 | Playwright pw = Playwright.create();
13 | //By default, Playwright runs the browsers in headless mode.
14 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setChannel("msedge"));
15 | Page page = browser.newPage();
16 | page.navigate("http://playwright.dev");
17 | System.out.println("Page Title: "+page.title());
18 | System.out.println("Page URL: "+page.url());
19 |
20 | browser.close();
21 | pw.close();
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/handleFrames/frameUsingXpath.java:
--------------------------------------------------------------------------------
1 | package handleFrames;
2 |
3 | import com.microsoft.playwright.*;
4 |
5 | public class frameUsingXpath {
6 |
7 | public static void main(String[] args) {
8 | // TODO Auto-generated method stub
9 | Playwright pw = Playwright.create();
10 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
11 |
12 | Page page = browser.newPage();
13 | page.navigate("https://www.formsite.com/templates/registration-form-templates/club-registration-signup-form/");
14 |
15 | page.locator("img[title='Club Signup Forms and Templates']").click();
16 |
17 | page.frameLocator("//iframe[contains(@id,'frame-one')]")
18 | .locator("#RESULT_TextField-2").fill("PlaywrightAutomation");
19 |
20 | pw.close();
21 |
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/launchBrowsers/LaunchFirefoxUI.java:
--------------------------------------------------------------------------------
1 | package launchBrowsers;
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 LaunchFirefoxUI {
9 |
10 | public static void main(String[] args) {
11 |
12 | Playwright pw = Playwright.create();
13 | //To see the browser UI, pass the headless=false flag while launching the browser.
14 | Browser browser = pw.firefox().launch(new BrowserType.LaunchOptions().setHeadless(false));
15 | Page page = browser.newPage();
16 | page.navigate("http://playwright.dev");
17 | System.out.println("Page Title: "+page.title());
18 | System.out.println("Page URL: "+page.url());
19 |
20 | browser.close();
21 | pw.close();
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/launchBrowsers/LaunchWebKitUI.java:
--------------------------------------------------------------------------------
1 | package launchBrowsers;
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 LaunchWebKitUI {
9 |
10 | public static void main(String[] args) {
11 |
12 | Playwright pw = Playwright.create();
13 | //To see the browser UI, pass the headless=false flag while launching the browser.
14 | Browser browser = pw.webkit().launch(new BrowserType.LaunchOptions().setHeadless(false));
15 | Page page = browser.newPage();
16 | page.navigate("http://playwright.dev");
17 | System.out.println("Page Title: "+page.title());
18 | System.out.println("Page URL: "+page.url());
19 |
20 | browser.close();
21 | pw.close();
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/launchBrowsers/LaunchChromiumUI.java:
--------------------------------------------------------------------------------
1 | package launchBrowsers;
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 LaunchChromiumUI {
9 |
10 | public static void main(String[] args) {
11 |
12 | Playwright pw = Playwright.create();
13 | //To see the browser UI, pass the headless=false flag while launching the browser.
14 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
15 | Page page = browser.newPage();
16 | page.navigate("http://playwright.dev");
17 | System.out.println("Page Title: "+page.title());
18 | System.out.println("Page URL: "+page.url());
19 |
20 | browser.close();
21 | pw.close();
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/authentication/useSavedStorageState.java:
--------------------------------------------------------------------------------
1 | package authentication;
2 |
3 | import java.nio.file.Paths;
4 |
5 | import com.microsoft.playwright.*;
6 |
7 | public class useSavedStorageState {
8 |
9 | public static void main(String[] args) {
10 | // TODO Auto-generated method stub
11 |
12 | Playwright pw = Playwright.create();
13 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
14 |
15 | BrowserContext brContext = browser.newContext(new Browser.NewContextOptions().setStorageStatePath(Paths.get("state.json")));
16 |
17 | Page page = brContext.newPage();
18 | page.navigate("https://naveenautomationlabs.com/opencart/index.php?route=account/login");
19 |
20 | Locator loc=page.locator("div");
21 | loc.allInnerTexts().forEach(e->System.out.println(e));
22 |
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/launchBrowsers/LaunchChromeUI.java:
--------------------------------------------------------------------------------
1 | package launchBrowsers;
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 LaunchChromeUI {
9 |
10 | public static void main(String[] args) {
11 |
12 | Playwright pw = Playwright.create();
13 | //To see the browser UI, pass the headless=false flag while launching the browser.
14 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setChannel("chrome").setHeadless(false));
15 | Page page = browser.newPage();
16 | page.navigate("http://playwright.dev");
17 | System.out.println("Page Title: "+page.title());
18 | System.out.println("Page URL: "+page.url());
19 |
20 | browser.close();
21 | pw.close();
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/launchBrowsers/LaunchMsEdgeUI.java:
--------------------------------------------------------------------------------
1 | package launchBrowsers;
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 LaunchMsEdgeUI {
9 |
10 | public static void main(String[] args) {
11 |
12 | Playwright pw = Playwright.create();
13 | //To see the browser UI, pass the headless=false flag while launching the browser.
14 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setChannel("msedge").setHeadless(false));
15 | Page page = browser.newPage();
16 | page.navigate("http://playwright.dev");
17 | System.out.println("Page Title: "+page.title());
18 | System.out.println("Page URL: "+page.url());
19 |
20 | browser.close();
21 | pw.close();
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 | playwrightfeatures
4 | playwrightfeatures
5 | 0.0.1-SNAPSHOT
6 |
7 |
8 |
9 |
10 | com.microsoft.playwright
11 | playwright
12 | 1.27.1
13 |
14 |
15 |
16 | org.testng
17 | testng
18 | 7.6.1
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/src/main/java/authentication/saveStorageState.java:
--------------------------------------------------------------------------------
1 | package authentication;
2 |
3 | import java.nio.file.Paths;
4 |
5 | import com.microsoft.playwright.*;
6 |
7 | public class saveStorageState {
8 |
9 | public static void main(String[] args) {
10 | // TODO Auto-generated method stub
11 |
12 | Playwright pw = Playwright.create();
13 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
14 | BrowserContext brContext = browser.newContext();
15 |
16 | Page page = brContext.newPage();
17 | page.navigate("https://naveenautomationlabs.com/opencart/index.php?route=account/login");
18 |
19 | page.locator("#input-email").fill("testuseroct2022@gmail.com");
20 | page.locator("#input-password").fill("nCxhyCKA4fDWDqj");
21 | page.locator("role=button[name='Login']").click();
22 |
23 | brContext.storageState(new BrowserContext.StorageStateOptions().setPath(Paths.get("state.json")));
24 |
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/target/classes/META-INF/maven/playwrightfeatures/playwrightfeatures/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 | playwrightfeatures
4 | playwrightfeatures
5 | 0.0.1-SNAPSHOT
6 |
7 |
8 |
9 |
10 | com.microsoft.playwright
11 | playwright
12 | 1.27.1
13 |
14 |
15 |
16 | org.testng
17 | testng
18 | 7.6.1
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/src/main/java/browserContext/browserContextOptions.java:
--------------------------------------------------------------------------------
1 | package browserContext;
2 |
3 | import com.microsoft.playwright.Browser;
4 | import com.microsoft.playwright.BrowserContext;
5 | import com.microsoft.playwright.BrowserType;
6 | import com.microsoft.playwright.Page;
7 | import com.microsoft.playwright.Playwright;
8 | import com.microsoft.playwright.options.ColorScheme;
9 |
10 | public class browserContextOptions {
11 |
12 | public static void main(String[] args) {
13 | // TODO Auto-generated method stub
14 | Playwright pw = Playwright.create();
15 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
16 | BrowserContext context = browser.newContext(new Browser.NewContextOptions().setViewportSize(1920, 1080)
17 | .setColorScheme(ColorScheme.DARK).setLocale("de-DE"));
18 | Page page = context.newPage();
19 | page.navigate("https://www.google.co.in/");
20 | System.out.println(page.title());
21 | pw.close();
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/selectors/hasElements2.java:
--------------------------------------------------------------------------------
1 | package selectors;
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 hasElements2 {
10 |
11 | public static void main(String[] args) {
12 | // TODO Auto-generated method stub
13 |
14 | Playwright pw = Playwright.create();
15 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
16 |
17 | Page page = browser.newPage();
18 | page.navigate("https://www.orangehrm.com/hris-hr-software-demo/");
19 |
20 | //Locator for country dropdown which has value of Australia
21 | Locator country = page.locator("select#Form_getForm_Country:has(option[value='Australia'])");
22 |
23 | country.allInnerTexts().forEach(e->System.out.println(e));
24 | country.allTextContents().forEach(e->System.out.println(e));
25 |
26 | pw.close();
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/handleWebTables/scopeLocators1.java:
--------------------------------------------------------------------------------
1 | package handleWebTables;
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 scopeLocators1 {
10 |
11 | public static void main(String[] args) {
12 |
13 | Playwright pw = Playwright.create();
14 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
15 |
16 | Page page = browser.newPage();
17 | page.navigate("https://www.primefaces.org/primeng/");
18 |
19 | Locator row = page.locator("p-table.p-element tr");
20 |
21 | row.locator(":scope", new Locator.LocatorOptions()
22 | .setHasText("Lenna Paprocki"))
23 | .locator("div.p-checkbox-box.p-component")
24 | .click();
25 |
26 | System.out.println("*************************************");
27 |
28 | row.locator(":scope").allInnerTexts().forEach(e->System.out.println(e));
29 | pw.close();
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/handleWebTables/scopeLocators2.java:
--------------------------------------------------------------------------------
1 | package handleWebTables;
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 scopeLocators2 {
10 |
11 | public static void main(String[] args) {
12 |
13 | Playwright pw = Playwright.create();
14 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
15 |
16 | Page page = browser.newPage();
17 | page.navigate("https://datatables.net/");
18 |
19 | Locator row = page.locator("table#example tr");
20 |
21 | Locator rowElements = row.locator(":scope", new Locator.LocatorOptions().setHasText("Bradley Greer")).locator(".dt-body-right");
22 | rowElements.allInnerTexts().forEach(e->System.out.println(e));
23 |
24 | System.out.println("*************************************");
25 |
26 | row.locator(":scope").allInnerTexts().forEach(e->System.out.println(e));
27 |
28 | pw.close();
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/main/java/handleDialogs/handleJSAlert.java:
--------------------------------------------------------------------------------
1 | package handleDialogs;
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 handleJSAlert {
9 |
10 | public static void main(String[] args) {
11 | // TODO Auto-generated method stub
12 |
13 | Playwright pw = Playwright.create();
14 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
15 | Page page = browser.newPage();
16 | page.navigate("https://the-internet.herokuapp.com/javascript_alerts");
17 |
18 | //Java script Alert (Alert Popup has OK Button)
19 | //Clicking OK on the JS Alert is automatically Handled by Playwright when Alert popup appears.
20 | //No additional code is required.
21 | page.click("text=Click for JS Alert");
22 | String result=page.textContent("#result");
23 | System.out.println(result);
24 | //text printed on Console: You successfully clicked an alert
25 |
26 | page.close();
27 | pw.close();
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/handleDialogs/handleJSConfirm.java:
--------------------------------------------------------------------------------
1 | package handleDialogs;
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 handleJSConfirm {
9 |
10 | public static void main(String[] args) {
11 | // TODO Auto-generated method stub
12 |
13 | Playwright pw = Playwright.create();
14 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
15 | Page page = browser.newPage();
16 | page.navigate("https://the-internet.herokuapp.com/javascript_alerts");
17 |
18 | //Java script Confirm (Confirm Popup has Cancel button and OK Button)
19 | //Clicking Cancel on the JS Confirm is automatically Handled by Playwright when Confirm popup appears.
20 | //No additional code is required.
21 | page.click("text=Click for JS Confirm");
22 | String result=page.textContent("#result");
23 | System.out.println(result);
24 | //text printed on Console: You clicked: Cancel
25 |
26 | page.close();
27 | pw.close();
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/handleDialogs/handleJSPrompt.java:
--------------------------------------------------------------------------------
1 | package handleDialogs;
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 handleJSPrompt {
9 |
10 | public static void main(String[] args) {
11 | // TODO Auto-generated method stub
12 |
13 | Playwright pw = Playwright.create();
14 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
15 | Page page = browser.newPage();
16 | page.navigate("https://the-internet.herokuapp.com/javascript_alerts");
17 |
18 | //Javascript Prompt (Prompt Popup has Textbox, Cancel button and OK Button)
19 | //Clicking Cancel on the JS Prompt is automatically Handled by Playwright when Prompt popup appears.
20 | //No additional code is required.
21 | page.click("text=Click for JS Prompt");
22 | String result=page.textContent("#result");
23 | System.out.println(result);
24 | //text printed on Console: You entered: null
25 |
26 | page.close();
27 | pw.close();
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/selectors/hasElements.java:
--------------------------------------------------------------------------------
1 | package selectors;
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 hasElements {
10 |
11 | public static void main(String[] args) {
12 | // TODO Auto-generated method stub
13 |
14 | Playwright pw = Playwright.create();
15 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
16 |
17 | Page page = browser.newPage();
18 | page.navigate("https://dzone.com/");
19 |
20 | //Locator for footer which has About link
21 | Locator footer = page.locator("ul.link-group:has(a[href='/pages/about'])");
22 | footer.allInnerTexts().forEach(e->System.out.println(e));
23 |
24 | System.out.println("***************************");
25 | //Locator for footer which has Writers Zone link
26 | Locator footer2 = page.locator("ul.link-group:has(a[href='/writers-zone'])");
27 | footer2.allInnerTexts().forEach(e->System.out.println(e));
28 |
29 | pw.close();
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/browserContext/browserContextSampleCode.java:
--------------------------------------------------------------------------------
1 | package browserContext;
2 |
3 | import com.microsoft.playwright.Browser;
4 | import com.microsoft.playwright.BrowserContext;
5 | import com.microsoft.playwright.BrowserType;
6 | import com.microsoft.playwright.Page;
7 | import com.microsoft.playwright.Playwright;
8 |
9 | public class browserContextSampleCode {
10 |
11 | public static void main(String[] args) {
12 |
13 | Playwright pw = Playwright.create();
14 |
15 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
16 |
17 | //BrowserContext opens the session in Incognito mode. It won't share cookies/cache with other browser contexts.
18 | BrowserContext bcx1 = browser.newContext();
19 | Page page1 = bcx1.newPage();
20 | page1.navigate("http://google.com");
21 | System.out.println(page1.title());
22 | page1.close();
23 | bcx1.close();
24 |
25 | BrowserContext bcx2 = browser.newContext();
26 | Page page2 = bcx2.newPage();
27 | page2.navigate("http://facebook.com");
28 | System.out.println(page2.title());
29 | page2.close();
30 | bcx2.close();
31 |
32 |
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/elementLocators/multipleElementsUsingListFor.java:
--------------------------------------------------------------------------------
1 | package elementLocators;
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 multipleElementsUsingListFor {
13 |
14 | public static void main(String[] args) {
15 | // TODO Auto-generated method stub
16 | Playwright pw = Playwright.create();
17 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
18 |
19 | BrowserContext browserContext = browser.newContext();
20 | Page page = browserContext.newPage();
21 | page.navigate("https://www.orangehrm.com/orangehrm-30-day-trial/?");
22 |
23 | Locator countryOptions = page.locator("select#Form_getForm_Country option");
24 | System.out.println(countryOptions.count());
25 |
26 | List optionsTextList = countryOptions.allTextContents();
27 |
28 | for(String e : optionsTextList)
29 | System.out.println(e);
30 |
31 | pw.close();
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/elementLocators/multipleElementsUsingLambaForeach.java:
--------------------------------------------------------------------------------
1 | package elementLocators;
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 multipleElementsUsingLambaForeach {
13 |
14 | public static void main(String[] args) {
15 | // TODO Auto-generated method stub
16 | Playwright pw = Playwright.create();
17 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
18 |
19 | BrowserContext browserContext = browser.newContext();
20 | Page page = browserContext.newPage();
21 | page.navigate("https://www.orangehrm.com/orangehrm-30-day-trial/?");
22 |
23 | Locator countryOptions = page.locator("select#Form_getForm_Country option");
24 | System.out.println(countryOptions.count());
25 |
26 | List optionsTextList = countryOptions.allTextContents();
27 |
28 | optionsTextList.forEach(ele -> System.out.println(ele));
29 |
30 | pw.close();
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/elementLocators/multipleElementsUsingNthIndex.java:
--------------------------------------------------------------------------------
1 | package elementLocators;
2 |
3 | import com.microsoft.playwright.Browser;
4 | import com.microsoft.playwright.BrowserContext;
5 | import com.microsoft.playwright.BrowserType;
6 | import com.microsoft.playwright.Locator;
7 | import com.microsoft.playwright.Page;
8 | import com.microsoft.playwright.Playwright;
9 |
10 | public class multipleElementsUsingNthIndex {
11 |
12 | public static void main(String[] args) {
13 | // TODO Auto-generated method stub
14 | Playwright pw = Playwright.create();
15 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
16 |
17 | BrowserContext browserContext = browser.newContext();
18 | Page page = browserContext.newPage();
19 | page.navigate("https://www.orangehrm.com/orangehrm-30-day-trial/?");
20 |
21 | Locator countryOptions = page.locator("select#Form_getForm_Country option");
22 | System.out.println(countryOptions.count());
23 |
24 | for(int i=0;i listText = page.locator("h2.title").allTextContents();
30 | listText.stream().forEach(e->System.out.println(e));
31 |
32 | Locator loc = page.locator("div#placeholder img");
33 | int count = loc.count();
34 |
35 | for(int i=0;i> input").first();
20 | email.click();
21 | email.fill("reactselectors@testing.com");
22 |
23 | Locator getStarted = page.locator("_react=[text='Get Started']").first();
24 | getStarted.click();
25 |
26 | Thread.sleep(5000);
27 |
28 | Locator text = page.locator("_react=UIMarkup");
29 | text.allInnerTexts().forEach(e->System.out.println(e));
30 |
31 | page.goBack();
32 |
33 | page.locator("_react=UISelect[data-uia='language-picker']").click();
34 | System.out.println("************************");
35 | Locator footer = page.locator("_react=[data-uia='data-uia-footer-label']");
36 | footer.allInnerTexts().forEach(e->System.out.println(e));
37 |
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/java/apiTesting/getMethod/apiGetCall.java:
--------------------------------------------------------------------------------
1 | package apiTesting.getMethod;
2 |
3 | import com.microsoft.playwright.APIRequest;
4 | import com.microsoft.playwright.APIRequestContext;
5 | import com.microsoft.playwright.APIResponse;
6 | import com.microsoft.playwright.Playwright;
7 |
8 | public class apiGetCall {
9 |
10 | public static void main(String[] args) {
11 | // TODO Auto-generated method stub
12 |
13 | Playwright pw = Playwright.create();
14 | APIRequestContext apiRequestContext = pw.request()
15 | .newContext(new APIRequest.NewContextOptions()
16 | .setBaseURL("https://reqres.in"));
17 |
18 | APIResponse response = apiRequestContext.get("/api/users?page=2");
19 |
20 | //Contains the URL of the response.
21 | System.out.println(response.url());
22 |
23 | //Contains a boolean stating whether the response was successful.
24 | System.out.println(response.ok()); //true
25 |
26 | // Contains the status code of the response (e.g., 200 for a success).
27 | System.out.println(response.status()); //200
28 |
29 | // Contains the status text of the response (e.g. usually an "OK" for a success).
30 | System.out.println(response.statusText()); //OK
31 |
32 | // Returns the text representation of response body.
33 | System.out.println(response.text());
34 |
35 | // An object with all the response HTTP headers associated with this response.
36 | System.out.println(response.headers());
37 |
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/java/recordVideos/recordVideoOfTests.java:
--------------------------------------------------------------------------------
1 | package recordVideos;
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 recordVideoOfTests {
12 |
13 | public static void main(String[] args) {
14 | // TODO Auto-generated method stub
15 |
16 | Playwright pw = Playwright.create();
17 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
18 |
19 | BrowserContext brContext = browser.newContext(new Browser
20 | .NewContextOptions()
21 | .setRecordVideoDir(Paths.get("videos/")));
22 |
23 | Page page = brContext.newPage();
24 | page.navigate("https://naveenautomationlabs.com/opencart/");
25 |
26 | page.click("text=MacBook");
27 |
28 | page.click("'Add to Cart'");
29 |
30 | page.click("'Shopping Cart'");
31 |
32 | page.locator("#content").allInnerTexts().forEach(e->System.out.println(e));
33 |
34 | // Saved video files will appear in the specified folder. They all have generated unique names.
35 | // For the multi-page scenarios, you can access the video file associated with the page via the Page.video()
36 | System.out.println(page.video().path().toString());
37 |
38 | page.close();
39 | brContext.close();
40 | browser.close();
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/browserContext/browserContextsList.java:
--------------------------------------------------------------------------------
1 | package browserContext;
2 |
3 | import com.microsoft.playwright.Browser;
4 | import com.microsoft.playwright.BrowserContext;
5 | import com.microsoft.playwright.BrowserType;
6 | import com.microsoft.playwright.Page;
7 | import com.microsoft.playwright.Playwright;
8 |
9 | public class browserContextsList {
10 |
11 | public static void main(String[] args) {
12 |
13 | Playwright pw = Playwright.create();
14 |
15 | // BrowserContext opens the session in Incognito mode.
16 | // It won't share cookies/cache with other browser contexts.
17 |
18 | BrowserType chromium = pw.chromium();
19 | // Create a Chromium browser instance
20 | Browser browser = chromium.launch(new BrowserType.LaunchOptions().setHeadless(false));
21 |
22 | // Create two isolated browser contexts
23 | BrowserContext userContext = browser.newContext();
24 | BrowserContext adminContext = browser.newContext();
25 |
26 | // Create pages and interact with contexts independently
27 | Page userPage = userContext.newPage();
28 | userPage.navigate("https://yahoomail.com/");
29 | System.out.println(userPage.title());
30 |
31 | Page adminPage = adminContext.newPage();
32 | adminPage.navigate("https://gmail.com/");
33 | System.out.println(adminPage.title());
34 |
35 | //Returns an array of all open browser contexts. In a newly created browser, this will return zero browser contexts.
36 | System.out.println(browser.contexts());
37 | pw.close();
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/java/apiTesting/getMethod/apiGetCall2.java:
--------------------------------------------------------------------------------
1 | package apiTesting.getMethod;
2 |
3 | import com.microsoft.playwright.APIRequest;
4 | import com.microsoft.playwright.APIRequestContext;
5 | import com.microsoft.playwright.APIResponse;
6 | import com.microsoft.playwright.Playwright;
7 |
8 | public class apiGetCall2 {
9 |
10 | public static void main(String[] args) {
11 | // TODO Auto-generated method stub
12 |
13 | Playwright pw = Playwright.create();
14 | APIRequestContext apiRequestContext = pw.request()
15 | .newContext(new APIRequest.NewContextOptions().setBaseURL("https://reqres.in"));
16 |
17 | APIResponse response = apiRequestContext.get("/api/users/23");
18 |
19 | //Contains the URL of the response.
20 | System.out.println(response.url());
21 |
22 | //Contains a boolean stating whether the response was successful (status in the range 200-299) or not.
23 | System.out.println(response.ok()); //false
24 |
25 | // Contains the status code of the response (e.g., 200 for a success).
26 | System.out.println(response.status()); //404 - Not Found
27 |
28 | // Contains the status text of the response (e.g. usually an "OK" for a success).
29 | System.out.println(response.statusText());
30 |
31 | // Returns the text representation of response body.
32 | System.out.println(response.text());
33 |
34 | // An object with all the response HTTP headers associated with this response.
35 | System.out.println(response.headers());
36 |
37 | apiRequestContext.dispose();
38 | pw.close();
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/src/main/java/handleDialogs/handleJSDialogsUsingOnDialogHandlerDismiss.java:
--------------------------------------------------------------------------------
1 | package handleDialogs;
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 handleJSDialogsUsingOnDialogHandlerDismiss {
9 |
10 | public static void main(String[] args) {
11 | // TODO Auto-generated method stub
12 |
13 | Playwright pw = Playwright.create();
14 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
15 | Page page = browser.newPage();
16 | page.onDialog(dialog -> {
17 | String text = dialog.message();
18 | System.out.println(text);
19 | //dialog.accept("Message from Javascript alerts - SN");
20 | dialog.dismiss();
21 | });
22 | page.navigate("https://the-internet.herokuapp.com/javascript_alerts");
23 |
24 | // JS Alerts, Confirm, Prompt pop ups
25 |
26 | page.click("text=Click for JS Alert");
27 | String result=page.textContent("#result");
28 | System.out.println(result);
29 |
30 | page.click("text=Click for JS Confirm");
31 | result=page.textContent("#result");
32 | System.out.println(result);
33 |
34 | page.click("text=Click for JS Prompt");
35 | result=page.textContent("#result");
36 | System.out.println(result);
37 |
38 | page.close();
39 | pw.close();
40 |
41 | //Console messages
42 | //I am a JS Alert
43 | //You successfully clicked an alert
44 | //I am a JS Confirm
45 | //You clicked: Cancel
46 | //I am a JS prompt
47 | //You entered: null
48 |
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/handleDialogs/handleJSDialogsUsingOnDialogHandlerAccept.java:
--------------------------------------------------------------------------------
1 | package handleDialogs;
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 handleJSDialogsUsingOnDialogHandlerAccept {
9 |
10 | public static void main(String[] args) {
11 | // TODO Auto-generated method stub
12 |
13 | Playwright pw = Playwright.create();
14 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
15 | Page page = browser.newPage();
16 | page.onDialog(dialog -> {
17 | String text = dialog.message();
18 | System.out.println(text);
19 | dialog.accept("Message from Javascript alerts - SN");
20 | //dialog.dismiss();
21 | });
22 | page.navigate("https://the-internet.herokuapp.com/javascript_alerts");
23 |
24 | // JS Alerts, Confirm, Prompt pop ups
25 |
26 | page.click("text=Click for JS Alert");
27 | String result=page.textContent("#result");
28 | System.out.println(result);
29 |
30 | page.click("text=Click for JS Confirm");
31 | result=page.textContent("#result");
32 | System.out.println(result);
33 |
34 | page.click("text=Click for JS Prompt");
35 | result=page.textContent("#result");
36 | System.out.println(result);
37 |
38 | page.close();
39 | pw.close();
40 |
41 | //Console messages
42 | //I am a JS Alert
43 | //You successfully clicked an alert
44 | //I am a JS Confirm
45 | //You clicked: Ok
46 | //I am a JS prompt
47 | //You entered: Message from Javascript alerts - SN
48 |
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/elementLocators/singleElement.java:
--------------------------------------------------------------------------------
1 | package elementLocators;
2 |
3 | import com.microsoft.playwright.Browser;
4 | import com.microsoft.playwright.BrowserContext;
5 | import com.microsoft.playwright.BrowserType;
6 | import com.microsoft.playwright.Locator;
7 | import com.microsoft.playwright.Page;
8 | import com.microsoft.playwright.Playwright;
9 |
10 | public class singleElement {
11 |
12 | public static void main(String[] args) {
13 | // TODO Auto-generated method stub
14 | Playwright pw = Playwright.create();
15 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
16 |
17 | BrowserContext browserContext = browser.newContext();
18 | Page page = browserContext.newPage();
19 | page.navigate("https://www.orangehrm.com/");
20 |
21 | Locator contactSales = page.locator("text = Contact Sales");
22 |
23 | //contactSales.click(); -- when there are multiple elements for an Locator, then Playwright Exception is thrown
24 |
25 | //com.microsoft.playwright.PlaywrightException: Error {
26 | //Error: strict mode violation: "text= Contact Sales" resolved to 2 elements:
27 | // 1) aka playwright.$("text=Contact Sales >> nth=0")
28 | // 2) aka playwright.$("text=Contact Sales >> nth=1")
29 |
30 | //contactSales.first().click();
31 | contactSales.last().click();
32 | //using first() or last() method we can access the respective Locator elements.
33 |
34 | String pageUrl = page.url();
35 | System.out.println(pageUrl);
36 |
37 | pw.close();
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/java/getByLocatorAPIs/afterGetByAPIs.java:
--------------------------------------------------------------------------------
1 | package getByLocatorAPIs;
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 afterGetByAPIs {
10 |
11 | public static void main(String[] args) throws InterruptedException {
12 | // TODO Auto-generated method stub
13 |
14 | Playwright pw=Playwright.create();
15 | Browser browser=pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
16 |
17 | Page page = browser.newPage();
18 | page.navigate("https://bookcart.azurewebsites.net/");
19 |
20 | Locator firstBookCoverImg = page.getByAltText("Book cover image");
21 | for(int i=0;i linksText = page.locator("a:visible").allInnerTexts();
19 | System.out.println(linksText.size());
20 | linksText.forEach(ele -> System.out.println(ele));
21 | System.out.println("*********************************************************");
22 |
23 | String inputBox = page.locator("input:visible").nth(0).getAttribute("aria-label");
24 | System.out.println(inputBox);
25 | String inputBox1 = page.locator("input:visible").nth(1).getAttribute("type");
26 | System.out.println(inputBox1);
27 | System.out.println("*********************************************************");
28 |
29 | page.locator("input:visible").nth(0).fill("macbook");
30 | page.keyboard().press("Enter");
31 |
32 | Thread.sleep(5000);
33 | List macbookResults = page.locator("a:visible").allInnerTexts();
34 | System.out.println(macbookResults.size());
35 | macbookResults.forEach(ele -> System.out.println(ele));
36 |
37 | System.out.println("*********************************************************");
38 | int imgCount = page.locator("//img >> visible=true").count();
39 | System.out.println(imgCount);
40 | pw.close();
41 |
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/getByLocatorAPIs/beforeGetByAPIs.java:
--------------------------------------------------------------------------------
1 | package getByLocatorAPIs;
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 beforeGetByAPIs {
10 |
11 | public static void main(String[] args) throws InterruptedException {
12 | // TODO Auto-generated method stub
13 |
14 | Playwright pw=Playwright.create();
15 | Browser browser=pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
16 |
17 | Page page = browser.newPage();
18 | page.navigate("https://bookcart.azurewebsites.net/");
19 |
20 | Locator firstBookCoverImg = page.locator("[alt='Book cover image']");
21 | for(int i=0;i headers = new HashMap<>();
18 | headers.put("content-type", "application/json");
19 |
20 | Playwright pw = Playwright.create();
21 | APIRequestContext apiRequestContext = pw.request().newContext(
22 | new APIRequest.NewContextOptions().setBaseURL("https://reqres.in/").setExtraHTTPHeaders(headers));
23 |
24 | Map payload = new HashMap<>();
25 | payload.put("job", "zion resident test");
26 |
27 | APIResponse response = apiRequestContext.patch("/api/users/2", RequestOptions.create().setData(payload));
28 |
29 | // Contains the URL of the response.
30 | System.out.println(response.url());
31 |
32 | // Contains a boolean stating whether the response was successful (status in the
33 | // range 200-299) or not.
34 | System.out.println(response.ok()); // true
35 |
36 | // Contains the status code of the response.
37 | System.out.println(response.status()); // 200
38 |
39 | // Contains the status text of the response.
40 | System.out.println(response.statusText()); //OK
41 |
42 | // Returns the text representation of response body.
43 | System.out.println(response.text());
44 |
45 | // An object with all the response HTTP headers associated with this response.
46 | System.out.println(response.headers());
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/apiTesting/postMethod/postCall.java:
--------------------------------------------------------------------------------
1 | package apiTesting.postMethod;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | import com.microsoft.playwright.APIRequest;
7 | import com.microsoft.playwright.APIRequestContext;
8 | import com.microsoft.playwright.APIResponse;
9 | import com.microsoft.playwright.Playwright;
10 | import com.microsoft.playwright.options.RequestOptions;
11 |
12 | public class postCall {
13 |
14 | public static void main(String[] args) {
15 | // TODO Auto-generated method stub
16 |
17 | Map headers = new HashMap<>();
18 | headers.put("content-type", "application/json");
19 |
20 | Playwright pw = Playwright.create();
21 | APIRequestContext apiRequestContext = pw.request().newContext(
22 | new APIRequest.NewContextOptions().setBaseURL("https://reqres.in/").setExtraHTTPHeaders(headers));
23 |
24 | Map payload = new HashMap<>();
25 | payload.put("name", "morpheus");
26 | payload.put("job", "leader");
27 |
28 | APIResponse response = apiRequestContext.post("/api/users", RequestOptions.create().setData(payload));
29 |
30 | // Contains the URL of the response.
31 | System.out.println(response.url());
32 |
33 | // Contains a boolean stating whether the response was successful (status in the
34 | // range 200-299) or not.
35 | System.out.println(response.ok()); // true
36 |
37 | // Contains the status code of the response.
38 | System.out.println(response.status()); // 201
39 |
40 | // Contains the status text of the response.
41 | System.out.println(response.statusText()); //Created
42 |
43 | // Returns the text representation of response body.
44 | System.out.println(response.text());
45 |
46 | // An object with all the response HTTP headers associated with this response.
47 | System.out.println(response.headers());
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/apiTesting/putMethod/putCall.java:
--------------------------------------------------------------------------------
1 | package apiTesting.putMethod;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | import com.microsoft.playwright.APIRequest;
7 | import com.microsoft.playwright.APIRequestContext;
8 | import com.microsoft.playwright.APIResponse;
9 | import com.microsoft.playwright.Playwright;
10 | import com.microsoft.playwright.options.RequestOptions;
11 |
12 | public class putCall {
13 |
14 | public static void main(String[] args) {
15 | // TODO Auto-generated method stub
16 |
17 | Map headers = new HashMap<>();
18 | headers.put("content-type", "application/json");
19 |
20 | Playwright pw = Playwright.create();
21 | APIRequestContext apiRequestContext = pw.request().newContext(
22 | new APIRequest.NewContextOptions().setBaseURL("https://reqres.in/").setExtraHTTPHeaders(headers));
23 |
24 | Map payload = new HashMap<>();
25 | payload.put("name", "morpheus test");
26 | payload.put("job", "zion resident test");
27 |
28 | APIResponse response = apiRequestContext.put("/api/users/2", RequestOptions.create().setData(payload));
29 |
30 | // Contains the URL of the response.
31 | System.out.println(response.url());
32 |
33 | // Contains a boolean stating whether the response was successful (status in the
34 | // range 200-299) or not.
35 | System.out.println(response.ok()); // true
36 |
37 | // Contains the status code of the response.
38 | System.out.println(response.status()); // 200
39 |
40 | // Contains the status text of the response.
41 | System.out.println(response.statusText()); //OK
42 |
43 | // Returns the text representation of response body.
44 | System.out.println(response.text());
45 |
46 | // An object with all the response HTTP headers associated with this response.
47 | System.out.println(response.headers());
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/apiTesting/postMethod/postCall2.java:
--------------------------------------------------------------------------------
1 | package apiTesting.postMethod;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | import com.microsoft.playwright.APIRequest;
7 | import com.microsoft.playwright.APIRequestContext;
8 | import com.microsoft.playwright.APIResponse;
9 | import com.microsoft.playwright.Playwright;
10 | import com.microsoft.playwright.options.RequestOptions;
11 |
12 | public class postCall2 {
13 |
14 | public static void main(String[] args) {
15 | // TODO Auto-generated method stub
16 |
17 | Map headers = new HashMap<>();
18 | headers.put("content-type", "application/json");
19 |
20 | Playwright pw = Playwright.create();
21 | APIRequestContext apiRequestContext = pw.request().newContext(
22 | new APIRequest.NewContextOptions().setBaseURL("https://reqres.in/").setExtraHTTPHeaders(headers));
23 |
24 | Map payload = new HashMap<>();
25 | payload.put("email", "eve.holt@reqres.in");
26 | payload.put("password", "pistol2");
27 |
28 | APIResponse response = apiRequestContext.post("/api/register", RequestOptions.create().setData(payload));
29 |
30 | // Contains the URL of the response.
31 | System.out.println(response.url());
32 |
33 | // Contains a boolean stating whether the response was successful (status in the
34 | // range 200-299) or not.
35 | System.out.println(response.ok()); // true
36 |
37 | // Contains the status code of the response.
38 | System.out.println(response.status()); // 200
39 |
40 | // Contains the status text of the response.
41 | System.out.println(response.statusText()); //Created
42 |
43 | // Returns the text representation of response body.
44 | System.out.println(response.text());
45 |
46 | // An object with all the response HTTP headers associated with this response.
47 | System.out.println(response.headers());
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/recordVideos/recordVideoOfTests2.java:
--------------------------------------------------------------------------------
1 | package recordVideos;
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 recordVideoOfTests2 {
12 |
13 | public static void main(String[] args) {
14 | // TODO Auto-generated method stub
15 |
16 | Playwright pw = Playwright.create();
17 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
18 |
19 | BrowserContext brContext = browser.newContext(new Browser
20 | .NewContextOptions()
21 | .setRecordVideoDir(Paths.get("videos/")));
22 |
23 | Page page = brContext.newPage();
24 | page.navigate("https://www.facebook.com/");
25 |
26 | page.locator("[name='email']").fill("testuser007@mail.com");
27 | page.locator("#pass").fill("ASDFlkj1234");
28 |
29 | page.click("text=Create New Account");
30 |
31 | page.locator("[aria-label='First name']").fill("Test1");
32 | page.locator("[aria-label='Surname']").fill("Test2");
33 | page.locator("[aria-label='Mobile number or email address']").fill("testemailaddr@gmail.com"+"");
34 | page.locator("[aria-label='New password']").fill("QWERTY98765");
35 | page.locator("[aria-label='Re-enter email address']").fill("testemailaddr@gmail.com");
36 | page.click("'Male'");
37 |
38 | page.click("[name='websubmit']");
39 |
40 | // Saved video files will appear in the specified folder. They all have generated unique names.
41 | // For the multi-page scenarios, you can access the video file associated with the page via the Page.video()
42 | System.out.println(page.video().path().toString());
43 |
44 | page.close();
45 | brContext.close();
46 | browser.close();
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/recordVideos/recordVideoViewPortSize.java:
--------------------------------------------------------------------------------
1 | package recordVideos;
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 recordVideoViewPortSize {
12 |
13 | public static void main(String[] args) {
14 | // TODO Auto-generated method stub
15 |
16 | Playwright pw = Playwright.create();
17 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
18 |
19 | BrowserContext brContext = browser.newContext(new Browser
20 | .NewContextOptions()
21 | .setRecordVideoDir(Paths.get("videos/"))
22 | .setRecordVideoSize(640, 480)); //desired video size.
23 | //video clarity will be reduced when we give lower resolution size
24 |
25 | Page page = brContext.newPage();
26 | page.navigate("https://www.facebook.com/");
27 |
28 | page.locator("[name='email']").fill("testuser007@mail.com");
29 | page.locator("#pass").fill("ASDFlkj1234");
30 |
31 | page.click("text=Create New Account");
32 |
33 | page.locator("[aria-label='First name']").fill("Test1");
34 | page.locator("[aria-label='Surname']").fill("Test2");
35 | page.locator("[aria-label='Mobile number or email address']").fill("testemailaddr@gmail.com"+"");
36 | page.locator("[aria-label='New password']").fill("QWERTY98765");
37 | page.locator("[aria-label='Re-enter email address']").fill("testemailaddr@gmail.com");
38 | page.click("'Male'");
39 |
40 | page.click("[name='websubmit']");
41 |
42 | // Saved video files will appear in the specified folder. They all have generated unique names.
43 | // For the multi-page scenarios, you can access the video file associated with the page via the Page.video()
44 | System.out.println(page.video().path().toString());
45 |
46 | page.close();
47 | brContext.close();
48 | browser.close();
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/selectors/cssSelectors.java:
--------------------------------------------------------------------------------
1 | package selectors;
2 |
3 | import java.util.List;
4 |
5 | import com.microsoft.playwright.*;
6 |
7 | public class cssSelectors {
8 |
9 | public static void main(String[] args) {
10 | // TODO Auto-generated method stub
11 | Playwright pw = Playwright.create();
12 | Browser browser = pw.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
13 |
14 | Page page = browser.newPage();
15 | page.navigate("https://www.opencart.com/"); //Launches Open cart website
16 |
17 | page.locator("p.hidden-xs a:has-text('Free Download')").click(); //Finds and clicks on Free Download button
18 | System.out.println(page.url()); //Prints the page url of the Download
19 |
20 | String text = page.locator("div#steps h2").textContent(); //Finds text - How to install OpenCart
21 | System.out.println(text);
22 |
23 | page.goBack(); //Navigate back to previous url - Open cart home page
24 | page.locator("p.hidden-xs a:has-text('View Demo')").click(); //Finds and clicks on View Demo button
25 | System.out.println(page.url()); //Prints the page url of the Demo
26 |
27 | String header = page.locator("div#cms-demo h1").textContent(); //Finds text - OpenCart Demonstration
28 | System.out.println(header);
29 |
30 | List boxesText = page.locator("div.demonstration-box h2").allTextContents();
31 | System.out.println(boxesText); // Prints text values as List - [Store Front, Administration]
32 |
33 | Locator imgElement = page.locator("div.demonstration-box img"); //Locator for Demonstration Box Image tagname
34 |
35 | for(int i=0;i https://playwright.dev/java/docs/intro
5 |
6 | 1. Cross Browser Testing (launchBrowsers) - Chromium, Firefox, WebKit browser binaries are available
7 | - Default browser mode is headless
8 | - Set headless to false to launch Browser UI
9 | - Chromium can be used with channel as Chrome or MS Edge browsers
10 | - WebKit is the rendering engine for Safari browser
11 | - Testing the upcoming versions by setting the channel like "chrome-beta", "msedge-beta" etc
12 |
13 | 2. Browser Context
14 | - Launches the browser sessions in the Incognito mode
15 | - Each Browser Context won't share cookies/cache with other browser contexts.
16 | - Browser contexts can also be used to emulate multi-page scenarios involving mobile devices, permissions, locale and color scheme.
17 | - Browser contexts are isolated environments on a single browser instance
18 | - This is useful when you want to test for multi-user functionality, like chat
19 |
20 | 3. Locators
21 | - Locators represent a way to find DOM element(s) on the page.
22 | - Locators are strict, so if more than one element matches with given selector and we try to perform operations then Exception is thrown
23 | - Using first(), last() methods we can disable strictness and perform actions of corresponding elements.
24 | - To access multiple elements with same selector, we can use the count() and nth index methods
25 |
26 | 4. Selectors
27 |
28 | i. Text selector locates elements that contain passed text.
29 | - text=value IS SAME AS 'value' when we specify inside method page.locator("");
30 | - example: page.locator("text=value"); page.locator("'value'");
31 | - tagname:has-text('value')
32 | - example: page.locator("a:has-text('View Demo')");
33 | - Input elements of the type button and submit are matched by their value instead of text content.
34 | example, text=Log in matches < input type=button value="Log in" >.
35 |
36 | ii. CSS Selectors
37 | - Selector strategy is same as like Selenium usage.
38 | - tagname and classvalue is written as -> tagname.classvalue
39 | - tagname and idvalue is written as -> tagname#idvalue
40 | - we can use parent tag (div.demonstration-box) followed by space then child tag name(img) to form an CSS Selector
41 | - Example: "div.demonstration-box img"
42 |
43 | iii. React Selectors
44 | - React selectors allow selecting elements by its component name and property values.
45 | - React selectors can be more stable than the DOM element selectors, as the property values are specific to the React components and not the HTML DOM structure.
46 | - React Developer Tools browser extension helps to identify the react selectors on the React web application. We can test them using the Playwright CLI by launching the codegen for the website.
47 | - Use the Components and Profiler in the DevTools (F12) of the web browser to identify the React selectors.
48 | - Syntax for React selectors: "_react=componentName[attributeName='attributeValue']"
49 | - Example code for React selectors with Netflix website: page.locator("_react=UISelect[data-uia='language-picker']")
50 |
51 | 5. Frames
52 | - frameLocator method : using selector
53 | - frame method : using name
54 |
55 | 6. Shadow DOM Elements (shadow-root)
56 | - shadow-root elements can be accessed the same way like other elements (non shadow-root)
57 | - No need to use the Javascriptexecutor code in Playwright, which is needed in Selenium.
58 |
59 | 7. Web Tables
60 | - Using the Scope selector we can work on the Web Table elements.
61 | - Syntax - locator(":scope") method
62 | - We can pass a unique value to select an row and perform operations on the elements in that row.
63 |
64 | 8. Authentication - Reuse signed in state
65 | - By Reusing the signed in session state, we can skip the log in step for all of the tests.
66 | - Step1 is save the logged in session state as json file using the Browser context
67 | - Step2 is use the state json in the tests Browser context and we can auto login.
68 |
69 | 9. Handling Javascript Dialogs using Handler Listener
70 | - Javascript Dialogs are three types
71 | - JS Alert : has OK button
72 | - JS Confirm : has Cancel and OK buttons
73 | - JS Prompt : has Textbox, Cancel and OK buttons
74 | - When an JS Dialog appears on the Web page, Playwright automatically handles it (Clicking on Cancel or OK), when there is no listener for Page.onDialog(handler).
75 | - By Using the onDialog Handler on the JS Dialog we can do the following actions.
76 | - Retrieve the message
77 | - Click Ok (accept)
78 | - Click Ok with prompt text (accept with specified text)
79 | - Click Cancel (dismiss)
80 | - Page.onDialog(handler) listener if used must handle the dialog with accept or dismiss, otherwise the page will get hanged and block further page execution.
81 |
82 | 10. Record Videos of script execution
83 | - Set the Video Directory using the Browser Context options
84 | - Videos are saved upon browser context closure at the end of a test.
85 | - We can set the Video size to the desired viewport size.
86 | - Videos are generated with unique names in .webm format.
87 | - For the multi-page scenarios, we can access the video file associated with the page via the Page.video()
88 |
--------------------------------------------------------------------------------