New Automation Tool : Playwright by Microsoft
#To Initialize Playwright server :
Playwright playwright = Playwright.create()
#To Create Browser Instance
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
.setHeadless(false));
Browser browser = playwright.firefox().launch(new BrowserType.LaunchOptions()
.setHeadless(false));
Browser browser = playwright.webkit().launch(new BrowserType.LaunchOptions()
.setHeadless(false));
where setHeadLess(true or false)
# Create BrowserContext
BrowserContext browserContext = browser.newContext()
# Create Page instance
Page page = browserContext.newPage();
A Simple script:
package playwrightdemo;
import com.microsoft.playwright.*;
import com.microsoft.playwright.options.*;
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
import java.nio.file.Paths;
import java.util.*;
public class test {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
.setHeadless(false));
BrowserContext context = browser.newContext();
// Open new page
Page page = context.newPage();
// Go to https://playwright.dev/
page.navigate("https://playwright.dev/");
// Click text=Get started
page.locator("text=Get started").click();
assertThat(page).hasURL("https://playwright.dev/docs/intro");
// Click text=Release notes
page.locator("text=Release notes").click();
assertThat(page).hasURL("https://playwright.dev/docs/release-notes");
// Click a:has-text("Version 1.12")
page.locator("a:has-text(\"Version 1.12\")").click();
assertThat(page).hasURL("https://playwright.dev/docs/release-notes#version-112");
// Click a:has-text("Version 1.9")
page.locator("a:has-text(\"Version 1.9\")").click();
assertThat(page).hasURL("https://playwright.dev/docs/release-notes#version-19");
// Click text=Authentication >> nth=0
page.locator("text=Authentication").first().click();
assertThat(page).hasURL("https://playwright.dev/docs/test-auth");
// Click aside >> text=Videos
page.locator("aside >> text=Videos").click();
assertThat(page).hasURL("https://playwright.dev/docs/videos");
// Click text=Integrations
page.locator("text=Integrations").click();
// Click text=BrowserContext >> nth=1
page.locator("text=BrowserContext").nth(1).click();
assertThat(page).hasURL("https://playwright.dev/docs/api/class-browsercontext");
// Click span:has-text("API reference")
page.locator("span:has-text(\"API reference\")").click();
}
}
}
To Run Script Need a Maven Framework:
And to initiate Record feature minimum required maven FW and command prompt Terminal to trigger following querry:
mvn exec:java -e -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="codegen playwright.dev
Comments
Post a Comment