-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support device name in playwright fixtures #1472
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yury-s this is returning null for me and so a null pointer exception is being thrown. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, that's odd. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, do you have a test where this is happening? This should never happen if playwright is initialized. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I assume it is just the new test. It passes on the bots and locally. Could it be that you have some stale driver? We changed the way device descriptors pushed to the client some time ago. Try downloading current driver and see if it still fails. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just running There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it was failing on main for me too but you figured it out. downloaded the current driver and it's all good. apologies for that. i'll make running that command part of my workflow. |
||
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); | ||
} | ||
|
||
} |
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")); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's unfortunate that we cannot read back the actual values here, only the device name. However, that's better than nothing.