Skip to content

Commit 306f5e5

Browse files
Added example code for browser navigations method in playwright (#58)
* added example code for back,forward and reload options * added example code for back,forward and reload options * updated the code as per sonarlint smells and resolved them * added browser navigation test to testng.xml file
1 parent b092c7f commit 306f5e5

3 files changed

Lines changed: 48 additions & 0 deletions

File tree

src/test/java/io/github/mfaisalkhatri/tests/BrowserNavigationTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.github.mfaisalkhatri.tests;
22

3+
import com.microsoft.playwright.BrowserContext;
34
import com.microsoft.playwright.Page;
5+
import com.microsoft.playwright.options.WaitUntilState;
46
import io.github.mfaisalkhatri.pages.theinternet.ChallengingDomPage;
57
import io.github.mfaisalkhatri.pages.theinternet.MainPage;
68
import org.testng.annotations.Test;
@@ -21,14 +23,21 @@ public void testBrowserNavigation() {
2123
assertEquals(challengingDomPage.getPageHeader(), "Challenging DOM");
2224

2325
page.goBack();
26+
//page.goBack(new Page.GoBackOptions().setWaitUntil(WaitUntilState.NETWORKIDLE));
27+
// page.goBack(new Page.GoBackOptions().setTimeout(50));
2428
assertEquals(mainPage.getPageHeader(), "Available Examples");
2529

2630
page.goForward();
31+
//page.goForward(new Page.GoForwardOptions().setWaitUntil(WaitUntilState.LOAD));
32+
//page.goForward(new Page.GoForwardOptions().setTimeout(40));
2733
assertEquals(challengingDomPage.getPageHeader(), "Challenging DOM");
2834

2935
page.reload();
36+
//page.reload(new Page.ReloadOptions().setTimeout(60));
37+
//page.reload(new Page.ReloadOptions().setWaitUntil(WaitUntilState.COMMIT));
3038
assertEquals(challengingDomPage.getPageHeader(), "Challenging DOM");
3139

40+
3241
}
3342

3443

src/test/java/io/github/mfaisalkhatri/tests/PlaywrightDemoTests.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,42 @@ public void testOnEdge() {
8989
assertEquals(pageTitle, "Your Store");
9090
browser.close();
9191
}
92+
93+
@Test
94+
public void testBrowserNavigation() {
95+
final Playwright playwright = Playwright.create();
96+
final Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
97+
final Page page = browser.newPage();
98+
page.navigate("https://the-internet.herokuapp.com/");
99+
100+
final String homePageHeader = page.locator("h2").textContent();
101+
assertEquals(homePageHeader, "Available Examples");
102+
103+
page.navigate("https://the-internet.herokuapp.com/challenging_dom");
104+
final String challengingDomPageHeader = page.locator("h3").textContent();
105+
assertEquals(challengingDomPageHeader, "Challenging DOM");
106+
107+
page.goBack();
108+
// page.goBack(new Page.GoBackOptions().setWaitUntil(WaitUntilState.COMMIT));
109+
// page.goBack(new Page.GoBackOptions().setTimeout(50));
110+
assertEquals(homePageHeader, "Available Examples");
111+
112+
page.goForward();
113+
// page.goForward(new Page.GoForwardOptions().setWaitUntil(WaitUntilState.LOAD));
114+
//page.goForward(new Page.GoForwardOptions().setTimeout(40));
115+
assertEquals(challengingDomPageHeader, "Challenging DOM");
116+
117+
page.reload();
118+
// page.reload(new Page.ReloadOptions().setTimeout(60));
119+
//page.reload(new Page.ReloadOptions().setWaitUntil(WaitUntilState.COMMIT));
120+
assertEquals(challengingDomPageHeader, "Challenging DOM");
121+
122+
final String currentPageUrl = page.url();
123+
final String websiteLink = "https://the-internet.herokuapp.com/challenging_dom";
124+
assertEquals(currentPageUrl, websiteLink);
125+
126+
browser.close();
127+
128+
}
92129
}
130+

test-suites/testng-browsertest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<include name="testOnChromeSlowMo"/>
1313
<include name="testOnFirefoxSlowMo"/>
1414
<include name="testOnEdge"/>
15+
<include name="testBrowserNavigation"/>
1516
</methods>
1617
</class>
1718
</classes>

0 commit comments

Comments
 (0)