diff --git a/protractor/constants.js b/protractor/constants.js index 8b7f994..48c1cb1 100644 --- a/protractor/constants.js +++ b/protractor/constants.js @@ -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 = '' + diff --git a/protractor/index.js b/protractor/index.js index 8734f45..4bd9ea7 100644 --- a/protractor/index.js +++ b/protractor/index.js @@ -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, @@ -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 @@ -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 ) } diff --git a/test/__fixtures__/protractor/source/spec.js b/test/__fixtures__/protractor/source/spec.js index 4d1b45c..3737709 100644 --- a/test/__fixtures__/protractor/source/spec.js +++ b/test/__fixtures__/protractor/source/spec.js @@ -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"); diff --git a/test/__fixtures__/protractor/transformed/spec.js b/test/__fixtures__/protractor/transformed/spec.js index 49b1618..1774a2b 100644 --- a/test/__fixtures__/protractor/transformed/spec.js +++ b/test/__fixtures__/protractor/transformed/spec.js @@ -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");