-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[plugin-web-app] Add mobile emulation steps
- Loading branch information
Showing
5 changed files
with
242 additions
and
0 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
79 changes: 79 additions & 0 deletions
79
...-plugin-web-app/src/main/java/org/vividus/steps/ui/web/devtools/MobileEmulationSteps.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,79 @@ | ||
/* | ||
* Copyright 2019-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.vividus.steps.ui.web.devtools; | ||
|
||
import static org.apache.commons.lang3.Validate.isTrue; | ||
|
||
import java.util.Map; | ||
|
||
import org.jbehave.core.annotations.When; | ||
import org.openqa.selenium.chromium.HasCdp; | ||
import org.openqa.selenium.remote.Browser; | ||
import org.vividus.selenium.IWebDriverProvider; | ||
import org.vividus.selenium.manager.IWebDriverManager; | ||
import org.vividus.util.json.JsonUtils; | ||
|
||
public class MobileEmulationSteps | ||
{ | ||
private final IWebDriverProvider webDriverProvider; | ||
private final IWebDriverManager webDriverManager; | ||
private final JsonUtils jsonUtils; | ||
|
||
public MobileEmulationSteps(IWebDriverProvider webDriverProvider, IWebDriverManager webDriverManager, | ||
JsonUtils jsonUtils) | ||
{ | ||
this.webDriverProvider = webDriverProvider; | ||
this.webDriverManager = webDriverManager; | ||
this.jsonUtils = jsonUtils; | ||
} | ||
|
||
/** | ||
* Emulates mobile device using the provided configuration. | ||
* | ||
* <p> | ||
* <strong>The step is only supported by Chrome browser.</strong> | ||
* </p> | ||
* | ||
* @param jsonConfiguration The JSON containing device metrics to override. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
@When("I emulate mobile device with configuration:`$jsonConfiguration`") | ||
public void overrideDeviceMetrics(String jsonConfiguration) | ||
{ | ||
executeCdpCommand("Emulation.setDeviceMetricsOverride", jsonUtils.toObject(jsonConfiguration, Map.class)); | ||
} | ||
|
||
/** | ||
* Resets the mobile device emulation returning the browser to its initial state. | ||
* | ||
* <p> | ||
* <strong>The step is only supported by Chrome browser.</strong> | ||
* </p> | ||
*/ | ||
@When("I reset mobile device emulation") | ||
public void clearDeviceMetrics() | ||
{ | ||
executeCdpCommand("Emulation.clearDeviceMetricsOverride", Map.of()); | ||
} | ||
|
||
private void executeCdpCommand(String command, Map<String, Object> metrics) | ||
{ | ||
isTrue(webDriverManager.isBrowserAnyOf(Browser.CHROME), "The step is only supported by Chrome browser."); | ||
HasCdp hasCdp = webDriverProvider.getUnwrapped(HasCdp.class); | ||
hasCdp.executeCdpCommand(command, metrics); | ||
} | ||
} |
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
96 changes: 96 additions & 0 deletions
96
...in-web-app/src/test/java/org/vividus/steps/ui/web/devtools/MobileEmulationStepsTests.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,96 @@ | ||
/* | ||
* Copyright 2019-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.vividus.steps.ui.web.devtools; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.openqa.selenium.chromium.HasCdp; | ||
import org.openqa.selenium.remote.Browser; | ||
import org.vividus.selenium.IWebDriverProvider; | ||
import org.vividus.selenium.manager.IWebDriverManager; | ||
import org.vividus.util.json.JsonUtils; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class MobileEmulationStepsTests | ||
{ | ||
@Mock private IWebDriverProvider webDriverProvider; | ||
@Mock private IWebDriverManager webDriverManager; | ||
@Mock private HasCdp hasCdp; | ||
private MobileEmulationSteps steps; | ||
|
||
@BeforeEach | ||
void init() | ||
{ | ||
this.steps = new MobileEmulationSteps(webDriverProvider, webDriverManager, new JsonUtils()); | ||
} | ||
|
||
@Test | ||
void shouldOverrideDeviceMetrics() | ||
{ | ||
when(webDriverManager.isBrowserAnyOf(Browser.CHROME)).thenReturn(true); | ||
when(webDriverProvider.getUnwrapped(HasCdp.class)).thenReturn(hasCdp); | ||
|
||
String metrics = """ | ||
{ | ||
"width": 430, | ||
"height": 932, | ||
"deviceScaleFactor": 3, | ||
"mobile": true | ||
} | ||
"""; | ||
|
||
steps.overrideDeviceMetrics(metrics); | ||
|
||
verify(hasCdp).executeCdpCommand("Emulation.setDeviceMetricsOverride", Map.of( | ||
"width", 430, | ||
"height", 932, | ||
"deviceScaleFactor", 3, | ||
"mobile", true | ||
)); | ||
} | ||
|
||
@Test | ||
void shouldClearDeviceMetrics() | ||
{ | ||
when(webDriverManager.isBrowserAnyOf(Browser.CHROME)).thenReturn(true); | ||
when(webDriverProvider.getUnwrapped(HasCdp.class)).thenReturn(hasCdp); | ||
|
||
steps.clearDeviceMetrics(); | ||
|
||
verify(hasCdp).executeCdpCommand("Emulation.clearDeviceMetricsOverride", Map.of()); | ||
} | ||
|
||
@Test | ||
void shouldFailIfBrowserIsNotChrome() | ||
{ | ||
when(webDriverManager.isBrowserAnyOf(Browser.CHROME)).thenReturn(false); | ||
|
||
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, steps::clearDeviceMetrics); | ||
|
||
assertEquals("The step is only supported by Chrome browser.", thrown.getMessage()); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
vividus-tests/src/main/resources/story/integration/EmulationSteps.story
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,19 @@ | ||
Scenario: Validate steps: 'When I emulate mobile device with configuration:`$deviceMetrics`', 'When I reset mobile device emulation' | ||
Given I am on page with URL `${vividus-test-site-url}` | ||
When I execute javascript `return window.screen;` and save result to scenario variable `initialScreen` | ||
When I emulate mobile device with configuration:`{ | ||
"width": 430, | ||
"height": 932, | ||
"deviceScaleFactor": 3, | ||
"mobile": true | ||
}` | ||
|
||
When I execute javascript `return window.screen;` and save result to scenario variable `overridenScreen` | ||
Then `${overridenScreen.height}` is = `932` | ||
Then `${overridenScreen.width}` is = `430` | ||
|
||
When I reset mobile device emulation | ||
|
||
When I execute javascript `return window.screen;` and save result to scenario variable `clearedScreen` | ||
Then `${initialScreen.height}` is = `${clearedScreen.height}` | ||
Then `${initialScreen.width}` is = `${clearedScreen.width}` |