Skip to content
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: Added custom chrome option in scenario configuration #180

Merged
merged 3 commits into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ Unlike the onReady and onBefore script options, onBeforeSuite script does not ha

For scenarios where you need to use a mobile emulator, pass in the device name to the property `mobileDeviceName` on your config. Note that at the moment, this will only work with the chrome browser.

## Chrome Custom Options

For scenarios where you would like to add chrome custom options for example like different user-agent etc. pass the whole json configuration to the property `chromeCustomCapabilites` on your config. Note this will only work with the chrome browser.
```
"chromeCustomCapabilites": {
"mobileEmulation": {
"deviceMetrics": { "width": 360, "height": 1600, "pixelRatio": 3.0 },
"userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"
},
"args": ["incognito"]
}
```
## Running

### Supported Browsers: Firefox | Chrome
Expand Down
7 changes: 5 additions & 2 deletions src/configValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ function isMobileConfigValid(config) {
let isMobileConfigCorrect = true;

config.scenarios.forEach(scenario => {
if (config.browser !== 'chrome' && scenario.mobileDeviceName) {
if (
config.browser !== 'chrome' &&
(scenario.mobileDeviceName || scenario.chromeCustomCapabilites)
) {
logger.info(
'configValidator',
`❗️ ${
config.browser
} not supported on the mobile emulator. Please change your browser to chrome.`
} not supported on the mobile emulator / custom capabilities. Please change your browser to chrome.`
);
isMobileConfigCorrect = false;
}
Expand Down
28 changes: 28 additions & 0 deletions src/configValidator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ describe('The Config Validator', () => {
expect(isMobileConfigValid(config)).toBe(true);
});

it('config returns true for valid chrome custom capabilities', () => {
const config = {
browser: 'chrome',
scenarios: [
{
url: 'http:/google.com/',
label: 'homepage',
chromeCustomCapabilites: '{args:["incognito"]}}'
}
]
};
expect(isMobileConfigValid(config)).toBe(true);
});

it('config returns false for invalid mobile configs', () => {
const config = {
browser: 'firefox',
Expand All @@ -127,4 +141,18 @@ describe('The Config Validator', () => {
};
expect(isMobileConfigValid(config)).toBe(false);
});

it('config returns false for invalid custom google capabilities', () => {
const config = {
browser: 'firefox',
scenarios: [
{
url: 'http:/google.com/',
label: 'homepage',
chromeCustomCapabilites: '{args:["incognito"]}}'
}
]
};
expect(isMobileConfigValid(config)).toBe(false);
});
});
1 change: 1 addition & 0 deletions src/getScreenshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const generateSnapShotPromises = (SnapShotter, config) => {
label: scenario.label,
latest: config.latest,
browser: config.browser,
chromeCustomCapabilites: scenario.chromeCustomCapabilites,
mobileDeviceName: scenario.mobileDeviceName,
gridUrl: config.gridUrl,
height: viewport.height,
Expand Down
17 changes: 14 additions & 3 deletions src/snapshotter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class SnapShotter {
width = 700,
height = 1024,
browser = 'chrome',
chromeCustomCapabilites,
mobileDeviceName,
cookies,
cropToSelector,
Expand All @@ -38,6 +39,7 @@ export default class SnapShotter {
this._width = width;
this._height = height;
this._browser = browser;
this._chromeCustomCapabilites = chromeCustomCapabilites;
this._mobileDeviceName = mobileDeviceName;
this._cookies = cookies;
this._cropToSelector = cropToSelector;
Expand All @@ -61,9 +63,10 @@ export default class SnapShotter {
? this._webdriver.Capabilities.chrome
: this._webdriver.Capabilities.firefox;

this._capability = mobileDeviceName
? this.getMobileBrowserCapability()
: browserCapability();
if (mobileDeviceName) this._capability = this.getMobileBrowserCapability();
else if (chromeCustomCapabilites)
this._capability = this.getCustomGoogleCapability();
else this._capability = browserCapability();
}

get driver() {
Expand All @@ -83,6 +86,14 @@ export default class SnapShotter {
};
}

getCustomGoogleCapability() {
return {
browserName: 'chrome',
version: '*',
'goog:chromeOptions': this._chromeCustomCapabilites
};
}

async snooze(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
Expand Down