-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert TestBrowser to use fixtures (#1506)
- Loading branch information
Showing
5 changed files
with
209 additions
and
142 deletions.
There are no files selected for viewing
141 changes: 0 additions & 141 deletions
141
playwright/src/test/java/com/microsoft/playwright/TestBrowser.java
This file was deleted.
Oops, something went wrong.
99 changes: 99 additions & 0 deletions
99
playwright/src/test/java/com/microsoft/playwright/TestBrowser1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.microsoft.playwright; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.microsoft.playwright.junit.UsePlaywright; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.condition.EnabledIf; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.regex.Pattern; | ||
|
||
import static com.microsoft.playwright.Utils.getBrowserTypeFromEnv; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@UsePlaywright(TestOptionsFactories.BasicOptionsFactory.class) | ||
public class TestBrowser1 { | ||
|
||
@Test | ||
void shouldCreateNewPage(Browser browser) { | ||
Page page1 = browser.newPage(); | ||
assertEquals(1, browser.contexts().size()); | ||
|
||
Page page2 = browser.newPage(); | ||
assertEquals(2, browser.contexts().size()); | ||
|
||
page1.close(); | ||
assertEquals(1, browser.contexts().size()); | ||
|
||
page2.close(); | ||
assertEquals(0, browser.contexts().size()); | ||
} | ||
|
||
@Test | ||
void shouldThrowUponSecondCreateNewPage(Browser browser) { | ||
Page page = browser.newPage(); | ||
PlaywrightException e = assertThrows(PlaywrightException.class, () -> page.context().newPage()); | ||
assertTrue(e.getMessage().contains("Please use browser.newContext()")); | ||
page.close(); | ||
} | ||
|
||
@Test | ||
void versionShouldWork(Browser browser) { | ||
switch (browser.browserType().name()) { | ||
case "chromium": | ||
assertTrue(Pattern.matches("^\\d+\\.\\d+\\.\\d+\\.\\d+$", browser.version())); | ||
break; | ||
case "webkit": | ||
assertTrue(Pattern.matches("^\\d+\\.\\d+", browser.version())); | ||
break; | ||
case "firefox": | ||
// It can be 85.0b1 in Firefox. | ||
assertTrue(Pattern.matches("^\\d+\\.\\d+.*", browser.version())); | ||
break; | ||
default: | ||
fail("Unknown browser"); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldReturnBrowserType(Playwright playwright, Browser browser) { | ||
assertEquals(getBrowserTypeFromEnv(playwright), browser.browserType()); | ||
} | ||
|
||
@Test | ||
@EnabledIf(value = "com.microsoft.playwright.TestOptionsFactories#isChromium", | ||
disabledReason = "Chrome Devtools Protocol supported by chromium only") | ||
void shouldWorkWithNewBrowserCDPSession(Browser browser) { | ||
CDPSession session = browser.newBrowserCDPSession(); | ||
|
||
JsonElement response = session.send("Browser.getVersion"); | ||
assertNotNull(response.getAsJsonObject().get("userAgent").toString()); | ||
|
||
AtomicReference<Boolean> gotEvent = new AtomicReference<>(false); | ||
|
||
session.on("Target.targetCreated", jsonElement -> { | ||
gotEvent.set(true); | ||
}); | ||
|
||
JsonObject params = new JsonObject(); | ||
params.addProperty("discover", true); | ||
session.send("Target.setDiscoverTargets", params); | ||
|
||
Page page = browser.newPage(); | ||
assertTrue(gotEvent.get()); | ||
page.close(); | ||
|
||
session.detach(); | ||
} | ||
|
||
@Test | ||
void shouldPropagateCloseReasonToPendingActions(Browser browser) { | ||
BrowserContext context = browser.newContext(); | ||
PlaywrightException e = assertThrows(PlaywrightException.class, () -> context.waitForPage(() -> { | ||
browser.close(new Browser.CloseOptions().setReason("The reason.")); | ||
})); | ||
assertTrue(e.getMessage().contains("The reason."), e.getMessage()); | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
playwright/src/test/java/com/microsoft/playwright/TestBrowser2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.microsoft.playwright; | ||
|
||
import com.microsoft.playwright.junit.Options; | ||
import com.microsoft.playwright.junit.OptionsFactory; | ||
import com.microsoft.playwright.junit.UsePlaywright; | ||
import com.microsoft.playwright.options.BrowserChannel; | ||
import org.junit.jupiter.api.Assumptions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static com.microsoft.playwright.TestBrowser2.ChannelOptionsFactory.getBrowserChannelEnumFromEnv; | ||
import static com.microsoft.playwright.TestOptionsFactories.createLaunchOptions; | ||
import static com.microsoft.playwright.TestOptionsFactories.getBrowserChannelFromEnv; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
@UsePlaywright(TestBrowser2.ChannelOptionsFactory.class) | ||
public class TestBrowser2 { | ||
|
||
public static class ChannelOptionsFactory implements OptionsFactory { | ||
@Override | ||
public Options getOptions() { | ||
BrowserChannel channel = getBrowserChannelEnumFromEnv(); | ||
|
||
BrowserType.LaunchOptions launchOptions = createLaunchOptions(); | ||
launchOptions.channel = channel; | ||
return new Options().setLaunchOptions(launchOptions); | ||
} | ||
|
||
public static BrowserChannel getBrowserChannelEnumFromEnv() { | ||
String channel = getBrowserChannelFromEnv(); | ||
if (channel == null) { | ||
return null; | ||
} | ||
switch (channel) { | ||
case "chrome": | ||
return BrowserChannel.CHROME; | ||
case "chrome-beta": | ||
return BrowserChannel.CHROME_BETA; | ||
case "chrome-dev": | ||
return BrowserChannel.CHROME_DEV; | ||
case "chrome-canary": | ||
return BrowserChannel.CHROME_CANARY; | ||
case "msedge": | ||
return BrowserChannel.MSEDGE; | ||
case "msedge-beta": | ||
return BrowserChannel.MSEDGE_BETA; | ||
case "msedge-dev": | ||
return BrowserChannel.MSEDGE_DEV; | ||
case "msedge-canary": | ||
return BrowserChannel.MSEDGE_CANARY; | ||
default: | ||
throw new IllegalArgumentException("Unknown BROWSER_CHANNEL " + channel); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
void shouldSupportDeprecatedChannelEnum(Playwright playwright) { | ||
BrowserChannel channel = getBrowserChannelEnumFromEnv(); | ||
Assumptions.assumeTrue(channel != null); | ||
BrowserType.LaunchOptions options = createLaunchOptions(); | ||
options.setChannel(channel); | ||
BrowserType browserType = Utils.getBrowserTypeFromEnv(playwright); | ||
Browser browser = browserType.launch(options); | ||
assertNotNull(browser); | ||
browser.close(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
playwright/src/test/java/com/microsoft/playwright/TestOptionsFactories.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.microsoft.playwright; | ||
|
||
import com.microsoft.playwright.junit.Options; | ||
import com.microsoft.playwright.junit.OptionsFactory; | ||
|
||
public class TestOptionsFactories { | ||
|
||
public static class BasicOptionsFactory implements OptionsFactory { | ||
@Override | ||
public Options getOptions() { | ||
return new Options().setBrowserName(getBrowserName()); | ||
} | ||
} | ||
|
||
public static String getBrowserChannelFromEnv() { | ||
return System.getenv("BROWSER_CHANNEL"); | ||
} | ||
|
||
public static BrowserType.LaunchOptions createLaunchOptions() { | ||
BrowserType.LaunchOptions options; | ||
options = new BrowserType.LaunchOptions(); | ||
options.headless = !getHeadful(); | ||
return options; | ||
} | ||
|
||
private static boolean getHeadful() { | ||
String headfulEnv = System.getenv("HEADFUL"); | ||
return headfulEnv != null && !"0".equals(headfulEnv) && !"false".equals(headfulEnv); | ||
} | ||
|
||
private static String getBrowserName() { | ||
String browserName = System.getenv("BROWSER"); | ||
if (browserName == null) { | ||
browserName = "chromium"; | ||
} | ||
return browserName; | ||
} | ||
|
||
public static boolean isChromium() { | ||
return getBrowserName().equals("chromium"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters