Skip to content

Commit

Permalink
Convert TestBrowser to use fixtures (#1506)
Browse files Browse the repository at this point in the history
  • Loading branch information
uchagani authored Mar 11, 2024
1 parent f98ad20 commit 3417717
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 142 deletions.
141 changes: 0 additions & 141 deletions playwright/src/test/java/com/microsoft/playwright/TestBrowser.java

This file was deleted.

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());
}

}
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();
}
}
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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ static String getBrowserNameFromEnv() {
return browserName;
}

static BrowserType getBrowserTypeFromEnv(Playwright playwright) {
public static BrowserType getBrowserTypeFromEnv(Playwright playwright) {
String browserName = getBrowserNameFromEnv();
switch (browserName) {
case "webkit":
Expand Down

0 comments on commit 3417717

Please sign in to comment.