Skip to content

Commit

Permalink
Merge pull request #8 from elaichenkov/main
Browse files Browse the repository at this point in the history
add(transformation): for the manage(), navigate() commands
  • Loading branch information
christian-bromann authored Apr 23, 2021
2 parents 2f3ef65 + 89c6a17 commit dc94c2c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
17 changes: 16 additions & 1 deletion protractor/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,22 @@ exports.REPLACE_WINDOW_COMMANDS = {
minimize: 'minimizeWindow',
getPosition: 'getWindowRect',
setSize: 'setWindowRect',
deleteAllCookies: 'deleteCookies'
getSize: 'getWindowRect',
}

exports.REPLACE_MANAGE_COMMANDS = {
deleteAllCookies: 'deleteCookies',
deleteCookie: 'deleteCookie',
addCookie: 'addCookie',
getCookie: 'getCookie',
getCookies: 'getCookies'
}

exports.REPLACE_NAVIGATE_COMMANDS = {
forward: 'forward',
back: 'back',
refresh: 'refresh',
to: 'url'
}

exports.UNSUPPORTED_CONFIG_OPTION_ERROR = '' +
Expand Down
43 changes: 38 additions & 5 deletions protractor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const {
UNSUPPORTED_COMMAND_ERROR,
INCOMPATIBLE_COMMAND_ERROR,
REPLACE_WINDOW_COMMANDS,
REPLACE_TIMEOUTS
REPLACE_TIMEOUTS,
REPLACE_MANAGE_COMMANDS,
REPLACE_NAVIGATE_COMMANDS
} = require('./constants')
const {
isCustomStrategy,
Expand Down Expand Up @@ -394,11 +396,17 @@ module.exports = function transformer(file, api) {
root.find(j.CallExpression)
.filter((path) => (
path.value.callee.property &&
['get', ...Object.keys(REPLACE_TIMEOUTS), ...Object.keys(REPLACE_WINDOW_COMMANDS)].includes(path.value.callee.property.name) &&
[
'get',
...Object.keys(REPLACE_TIMEOUTS),
...Object.keys(REPLACE_WINDOW_COMMANDS),
...Object.keys(REPLACE_MANAGE_COMMANDS),
...Object.keys(REPLACE_NAVIGATE_COMMANDS)
].includes(path.value.callee.property.name) &&
path.value.callee.object &&
path.value.callee.object.callee &&
path.value.callee.object.callee.property &&
['logs', 'timeouts', 'window', 'manage'].includes(path.value.callee.object.callee.property.name)
['logs', 'timeouts', 'window', 'manage', 'navigate'].includes(path.value.callee.object.callee.property.name)
))
.replaceWith((path) => {
const scope = path.value.callee.object.callee.property.name
Expand Down Expand Up @@ -451,12 +459,37 @@ module.exports = function transformer(file, api) {
args
)
} else if (scope === 'manage') {
const args = []

if (command === 'addCookie') {
args.push(path.value.arguments[0])
} else if (command === 'deleteCookie' || command === 'getCookie') {
const cookieValue = path.value.arguments[0].value;

args.push(j.literal(cookieValue));
}

return j.callExpression(
j.memberExpression(
j.identifier('browser'),
j.identifier(REPLACE_WINDOW_COMMANDS[command])
j.identifier(REPLACE_MANAGE_COMMANDS[command])
),
[]
args
)
} else if (scope === 'navigate') {
const args = [];

if (command === 'to') {
const urlValue = path.value.arguments[0].value;

args.push(j.literal(urlValue));
}
return j.callExpression(
j.memberExpression(
j.identifier('browser'),
j.identifier(REPLACE_NAVIGATE_COMMANDS[command])
),
args
)
}

Expand Down
10 changes: 10 additions & 0 deletions test/__fixtures__/protractor/source/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,13 @@ browser.manage().deleteAllCookies();
var titleIsNotFoo = protractor.ExpectedConditions.not(
protractor.ExpectedConditions.titleIs('Foo')
);

browser.manage().window().getSize();
browser.manage().addCookie({ name: "version", value: "v1" });
browser.manage().deleteCookie("enableMock");
browser.manage().getCookie("name");
browser.manage().getCookies();
browser.navigate().back();
browser.navigate().forward();
browser.navigate().refresh();
browser.navigate().to("https://webdriver.io");
10 changes: 10 additions & 0 deletions test/__fixtures__/protractor/transformed/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,13 @@ browser.deleteCookies();
var titleIsNotFoo = require("wdio-wait-for").not(
require("wdio-wait-for").titleIs('Foo')
);

browser.getWindowRect();
browser.addCookie({ name: "version", value: "v1" });
browser.deleteCookie("enableMock");
browser.getCookie("name");
browser.getCookies();
browser.back();
browser.forward();
browser.refresh();
browser.url("https://webdriver.io");

0 comments on commit dc94c2c

Please sign in to comment.