-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support device name in playwright fixtures (#1472)
- Loading branch information
Showing
7 changed files
with
128 additions
and
13 deletions.
There are no files selected for viewing
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
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
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
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
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
34 changes: 34 additions & 0 deletions
34
playwright/src/main/java/com/microsoft/playwright/junit/impl/DeviceDescriptor.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,34 @@ | ||
package com.microsoft.playwright.junit.impl; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.microsoft.playwright.Playwright; | ||
import com.microsoft.playwright.impl.PlaywrightImpl; | ||
import com.microsoft.playwright.options.ViewportSize; | ||
|
||
class DeviceDescriptor { | ||
public String userAgent; | ||
public ViewportSize viewport; | ||
public Double deviceScaleFactor; | ||
public Boolean isMobile; | ||
public Boolean hasTouch; | ||
public String defaultBrowserType; | ||
|
||
static DeviceDescriptor findByName(Playwright playwright, String name) { | ||
JsonArray devices = ((PlaywrightImpl) playwright).deviceDescriptors(); | ||
JsonObject descriptor = null; | ||
for (JsonElement item : devices) { | ||
if (name.equals(item.getAsJsonObject().get("name").getAsString())) { | ||
descriptor = item.getAsJsonObject().getAsJsonObject("descriptor"); | ||
break; | ||
} | ||
} | ||
if (descriptor == null) { | ||
return null; | ||
} | ||
return new Gson().fromJson(descriptor, DeviceDescriptor.class); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
playwright/src/test/java/com/microsoft/playwright/TestPlaywrightDeviceOption.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,35 @@ | ||
package com.microsoft.playwright; | ||
|
||
import com.microsoft.playwright.junit.Options; | ||
import com.microsoft.playwright.junit.OptionsFactory; | ||
import com.microsoft.playwright.junit.UsePlaywright; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static com.microsoft.playwright.ServerLifecycle.serverMap; | ||
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@FixtureTest | ||
@UsePlaywright(TestPlaywrightDeviceOption.CustomOptions.class) | ||
public class TestPlaywrightDeviceOption { | ||
|
||
public static class CustomOptions implements OptionsFactory { | ||
@Override | ||
public Options getOptions() { | ||
return new Options().setDeviceName("iPhone 14"); | ||
} | ||
} | ||
|
||
private Server server() { | ||
return serverMap.get(this.getClass()); | ||
} | ||
|
||
@Test | ||
public void testPredifinedDeviceParameters(Page page) { | ||
page.navigate(server().EMPTY_PAGE); | ||
assertEquals("webkit", page.context().browser().browserType().name()); | ||
assertEquals(3, page.evaluate("window.devicePixelRatio")); | ||
assertEquals(980, page.evaluate("window.innerWidth")); | ||
assertEquals(1668, page.evaluate("window.innerHeight")); | ||
} | ||
} |