Skip to content

Commit

Permalink
Use local url for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
uchagani committed Jan 21, 2024
1 parent 535c51e commit 198afc3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,38 @@
import com.microsoft.playwright.junit.Options;
import com.microsoft.playwright.junit.OptionsFactory;
import com.microsoft.playwright.junit.UsePlaywright;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.regex.Pattern;

import static com.microsoft.playwright.Utils.nextFreePort;
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

@UsePlaywright(TestPlaywrightCustomFixtures.CustomOptions.class)
public class TestPlaywrightCustomFixtures {
private static Server server;

public static class CustomOptions implements OptionsFactory {
@Override
public Options getOptions() {
return new Options().setBaseUrl("https://bing.com").setBrowserName("firefox");
return new Options().setBaseUrl(server.EMPTY_PAGE).setBrowserName("firefox");
}
}

@BeforeAll
static void beforeAll() throws IOException {
server = Server.createHttp(nextFreePort());
}

@AfterAll
static void afterAll() {
if (server != null) {
server.stop();
server = null;
}
}

Expand All @@ -27,6 +46,6 @@ public void testCustomBrowser(Browser browser) {
@Test
public void testBaseUrl(Page page) {
page.navigate("/");
assertThat(page).hasURL(Pattern.compile("bing"));
assertThat(page).hasURL(Pattern.compile("localhost"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,50 @@
import com.microsoft.playwright.junit.Options;
import com.microsoft.playwright.junit.OptionsFactory;
import com.microsoft.playwright.junit.UsePlaywright;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.regex.Pattern;

import static com.microsoft.playwright.Utils.nextFreePort;
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;

@UsePlaywright(TestPlaywrightCustomOptionFixtures.CustomOptions.class)
public class TestPlaywrightCustomOptionFixtures {
private static Server server;

public static class CustomOptions implements OptionsFactory {
@Override
public Options getOptions() {
return new Options().setChannel("chrome").setApiRequestOptions(new APIRequest.NewContextOptions().setBaseURL("https://bing.com")).setContextOption(new Browser.NewContextOptions().setBaseURL("https://microsoft.com"));
return new Options().setChannel("chrome").setApiRequestOptions(new APIRequest.NewContextOptions().setBaseURL(server.EMPTY_PAGE)).setContextOption(new Browser.NewContextOptions().setBaseURL(server.EMPTY_PAGE));
}
}

@BeforeAll
static void beforeAll() throws IOException {
server = Server.createHttp(nextFreePort());
}

@AfterAll
static void afterAll() {
if (server != null) {
server.stop();
server = null;
}
}

@Test
void testCustomBrowserContext(Page page) {
page.navigate("/");
assertThat(page).hasURL(Pattern.compile("microsoft"));
assertThat(page).hasURL(Pattern.compile("localhost"));
}

@Test
void testCustomAPIRequestOptions(APIRequestContext apiRequestContext) {
APIResponse response = apiRequestContext.get("/");
assertTrue(response.url().contains("bing"));
assertTrue(response.url().contains("localhost"));
}
}

0 comments on commit 198afc3

Please sign in to comment.