-
Notifications
You must be signed in to change notification settings - Fork 35
Mobile Emulation settings support for chromium-based browsers #158
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
afa6113
Mobile Emulation settings support for chromium-based browsers
mialeska 524acbe
Change ChromiumSettings constructor visibility to "protected"
mialeska be0d2d6
Add clientHints into the README example
mialeska 7af12cc
Fix coding issues in ScreenshotListener
mialeska c5a7edf
Fix coding issues in ScreenshotListener, stabilize geolocation test
mialeska File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
src/main/java/aquality/selenium/configuration/driversettings/ChromiumSettings.java
This file contains hidden or 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 @@ | ||
| package aquality.selenium.configuration.driversettings; | ||
|
|
||
| import aquality.selenium.core.utilities.ISettingsFile; | ||
| import org.openqa.selenium.MutableCapabilities; | ||
| import org.openqa.selenium.chromium.ChromiumOptions; | ||
| import org.openqa.selenium.logging.LoggingPreferences; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public abstract class ChromiumSettings extends DriverSettings { | ||
|
|
||
| private static final String MOBILE_EMULATION_CAPABILITY_KEY = "mobileEmulation"; | ||
| private static final String DEVICE_METRICS_CAPABILITY_KEY = "deviceMetrics"; | ||
| private static final String CLIENT_HINTS_CAPABILITY_KEY = "clientHints"; | ||
|
|
||
| protected ChromiumSettings(ISettingsFile settingsFile){ | ||
| super(settingsFile); | ||
| } | ||
|
|
||
| protected <T extends ChromiumOptions<T>> void setupDriverOptions(T options) { | ||
| setPrefs(options); | ||
| setCapabilities(options); | ||
| getBrowserStartArguments().forEach(options::addArguments); | ||
| setExcludedArguments(options); | ||
| options.setPageLoadStrategy(getPageLoadStrategy()); | ||
| } | ||
|
|
||
| void setLoggingPreferences(MutableCapabilities options, String capabilityKey) { | ||
| if (!getLoggingPreferences().isEmpty()) { | ||
| LoggingPreferences logs = new LoggingPreferences(); | ||
| getLoggingPreferences().forEach(logs::enable); | ||
| options.setCapability(capabilityKey, logs); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| void setCapabilities(MutableCapabilities options) { | ||
| getBrowserCapabilities().forEach((key, value) -> { | ||
| if (key.equals(MOBILE_EMULATION_CAPABILITY_KEY)) { | ||
| Map<String, Object> mobileOptions = getMapOrEmpty(getDriverSettingsPath(CapabilityType.CAPABILITIES.getKey(), MOBILE_EMULATION_CAPABILITY_KEY)); | ||
| if (mobileOptions.containsKey(DEVICE_METRICS_CAPABILITY_KEY)) { | ||
| Map<String, Object> deviceMetrics = getMapOrEmpty(getDriverSettingsPath(CapabilityType.CAPABILITIES.getKey(), MOBILE_EMULATION_CAPABILITY_KEY, DEVICE_METRICS_CAPABILITY_KEY)); | ||
| mobileOptions.put(DEVICE_METRICS_CAPABILITY_KEY, deviceMetrics); | ||
| } | ||
| if (mobileOptions.containsKey(CLIENT_HINTS_CAPABILITY_KEY)) { | ||
| Map<String, Object> clientHints = getMapOrEmpty(getDriverSettingsPath(CapabilityType.CAPABILITIES.getKey(), MOBILE_EMULATION_CAPABILITY_KEY, CLIENT_HINTS_CAPABILITY_KEY)); | ||
| mobileOptions.put(CLIENT_HINTS_CAPABILITY_KEY, clientHints); | ||
| } | ||
| ((ChromiumOptions<?>)options).setExperimentalOption(key, mobileOptions); | ||
| } else { | ||
| options.setCapability(key, value); | ||
| } | ||
| }); | ||
| } | ||
mialeska marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| protected <T extends ChromiumOptions<T>> void setPrefs(T options){ | ||
| HashMap<String, Object> prefs = new HashMap<>(); | ||
| Map<String, Object> configOptions = getBrowserOptions(); | ||
| configOptions.forEach((key, value) -> { | ||
| if (key.equals(getDownloadDirCapabilityKey())) { | ||
| prefs.put(key, getDownloadDir()); | ||
| } else { | ||
| prefs.put(key, value); | ||
| } | ||
| }); | ||
| options.setExperimentalOption("prefs", prefs); | ||
| } | ||
|
|
||
|
|
||
| protected <T extends ChromiumOptions<T>> void setExcludedArguments(T chromiumOptions) { | ||
| chromiumOptions.setExperimentalOption("excludeSwitches", getExcludedArguments()); | ||
| } | ||
|
|
||
| @Override | ||
| public String getDownloadDirCapabilityKey() { | ||
| return "download.default_directory"; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 testreport; | ||
|
|
||
| import aquality.selenium.browser.AqualityServices; | ||
| import org.openqa.selenium.OutputType; | ||
| import org.openqa.selenium.TakesScreenshot; | ||
| import org.testng.ITestResult; | ||
| import org.testng.Reporter; | ||
| import org.testng.TestListenerAdapter; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.time.LocalDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
|
|
||
| public class ScreenshotListener extends TestListenerAdapter { | ||
| @Override | ||
| public void onTestFailure(ITestResult result) { | ||
| String dateString = LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd_MM_yyyy_HH_mm_ss")); | ||
| String methodName = result.getName(); | ||
| if (AqualityServices.isBrowserStarted()) { | ||
| try { | ||
| File scrFile = ((TakesScreenshot) AqualityServices.getBrowser().getDriver()).getScreenshotAs(OutputType.FILE); | ||
| String reportDirectory = String.format("%s/target/surefire-reports", new File(System.getProperty("user.dir")).getAbsolutePath()); | ||
| File destFile = new File(String.format("%s/failure_screenshots/%s_%s.png", reportDirectory, methodName, dateString)); | ||
| Files.createDirectories(destFile.getParentFile().toPath()); | ||
| Files.copy(scrFile.toPath(), destFile.toPath()); | ||
| Reporter.log(String.format("<a href='%s'> <img src='%s' height='100' width='100'/> </a>", destFile.getAbsolutePath(), destFile.getAbsolutePath())); | ||
| } catch (IOException e) { | ||
| AqualityServices.getLogger().fatal("An IO exception occurred while tried to save a screenshot", e); | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.