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

WIP: Control flow fork #198

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 21 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
32 changes: 32 additions & 0 deletions packages/selenium-ide/src/content/commands-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ function doCommands(request, sender, sendResponse) {
} else if (request.commands == "domWait") {
selenium["doDomWait"]("", selenium.preprocessParameter(""));
sendResponse({ dom_time: window.sideex_new_page });
} else if (isConditinal(request.commands)) {
executeConditional(request, sendResponse);
} else {
const upperCase = request.commands.charAt(0).toUpperCase() + request.commands.slice(1);
if (selenium["do" + upperCase] != null) {
Expand Down Expand Up @@ -112,6 +114,36 @@ function doCommands(request, sender, sendResponse) {
}
}

function isConditinal(commandName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is unnecessary, just add Selenium.prototype.doConditional, make it something like,
return !!eval(conditional).
This will give you more control, we just need a new class of commands, I believe.
Currently we have 2:

  • Content script commands
  • Ext commands

We also have type which is in the middle, because it's both, it has to wait like a content command, but it needs the elevated background script.
IMO just create an isFlowCommand (like isExtCommand), and execute them in that place (similarly to how I execute type, after the waiting).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is being reconsidered while I'm working on sandboxing.

return ["if", "while", "elseIf", "repeatIf", "times"].includes(commandName);
}

function executeConditional(request, sendResponse) {

const upperCase = request.commands.charAt(0).toUpperCase() + request.commands.slice(1);
if (selenium["do" + upperCase] != null) {
try {
document.body.setAttribute("SideeXPlayingFlag", true);
let returnValue = selenium["do"+upperCase](request.target,selenium.preprocessParameter(request.value));
// conditional command executed!
if (returnValue) {
document.body.removeAttribute("SideeXPlayingFlag");
sendResponse({result: "truthy"});
} else {
document.body.removeAttribute("SideeXPlayingFlag");
sendResponse({result: "falsy"});
}
} catch(e) {
// Synchronous command failed
document.body.removeAttribute("SideeXPlayingFlag");
sendResponse({result: e.message});
}
} else {
sendResponse({ result: "Unknown command: " + request.commands });
}

}

if (!window._listener) {
window._listener = doCommands;
browser.runtime.onMessage.addListener(doCommands);
Expand Down
41 changes: 41 additions & 0 deletions packages/selenium-ide/src/content/selenium-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,47 @@ Selenium.prototype.doVerifyElementPresent = function(locator) {
}
};

Selenium.prototype.doIf = function(string) {
return eval(string);
};

Selenium.prototype.doWhile = function(string) {
return eval(string);
};

Selenium.prototype.doTimes = function(counter) {
// return to it when 'eval' and stored variables properly figured out
return true;
};

Selenium.prototype.doElseIf = function(string) {
return eval(string);
};

Selenium.prototype.doRepeatIf = function(string) {
return eval(string);
};

Selenium.prototype.doElse = function() {
return true;
};

Selenium.prototype.doEnd = function() {
return true;
};

Selenium.prototype.doDo = function() {
return true;
};

Selenium.prototype.doContinue = function() {
return true;
};

Selenium.prototype.doBreak = function() {
return true;
};

Selenium.prototype.doVerifyElementNotPresent = function(locator) {
try {
this.browserbot.findElement(locator);
Expand Down
45 changes: 39 additions & 6 deletions packages/selenium-ide/src/neo/IO/SideeX/playback.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import PlaybackState, { PlaybackStates } from "../../stores/view/PlaybackState";
import UiState from "../../stores/view/UiState";
import ExtCommand, { isExtCommand } from "./ext-command";
import { xlateArgument } from "./formatCommand";
import PlaybackTree from "./playbackTree";

export const extCommand = new ExtCommand();
// In order to not break the separation of the execution loop from the state of the playback
Expand All @@ -37,6 +38,7 @@ let ignoreBreakpoint = false;

function play(currUrl) {
baseUrl = currUrl;
PlaybackTree.processCommands(PlaybackState.runningQueue);
prepareToPlay()
.then(executionLoop)
.then(finishPlaying)
Expand All @@ -51,7 +53,9 @@ function playAfterConnectionFailed() {
}

function executionLoop() {
(PlaybackState.currentPlayingIndex < 0) ? PlaybackState.setPlayingIndex(0) : PlaybackState.setPlayingIndex(PlaybackState.currentPlayingIndex + 1);
if (PlaybackState.currentPlayingIndex < 0) {
PlaybackState.setPlayingIndex(0);
}
// reached the end
if (PlaybackState.currentPlayingIndex >= PlaybackState.runningQueue.length && PlaybackState.isPlaying) return false;
const { id, command, target, value, isBreakpoint } = PlaybackState.runningQueue[PlaybackState.currentPlayingIndex];
Expand All @@ -68,11 +72,14 @@ function executionLoop() {
(extCommand["do" + upperCase](parsedTarget, value))
.then(() => {
PlaybackState.setCommandState(id, PlaybackStates.Passed);
}).then(executionLoop)
return doControlFlow(true);
}).then(doDelay).then(executionLoop)
));
} else if (isImplicitWait(command)) {
notifyWaitDeprecation(command);
return executionLoop();
return doControlFlow(true)
.then(doDelay)
.then(executionLoop);
} else {
return doPreparation()
.then(doPrePageWait)
Expand All @@ -81,10 +88,32 @@ function executionLoop() {
.then(doDomWait)
.then(doDelay)
.then(doCommand)
.then(doControlFlow)
.then(executionLoop);
}
}

function doControlFlow(result) {
let nextPlayingIndex;
if (result) {
// right
if (PlaybackState.runningQueue[PlaybackState.currentPlayingIndex].right) {
nextPlayingIndex = PlaybackState.runningQueue.indexOf(PlaybackState.runningQueue[PlaybackState.currentPlayingIndex].right);
} else {
nextPlayingIndex = PlaybackState.runningQueue.length + 1;
}
} else {
// left
if (PlaybackState.runningQueue[PlaybackState.currentPlayingIndex].left) {
nextPlayingIndex = PlaybackState.runningQueue.indexOf(PlaybackState.runningQueue[PlaybackState.currentPlayingIndex].left);
} else {
nextPlayingIndex = PlaybackState.runningQueue.length + 1;
}
}
PlaybackState.setPlayingIndex(nextPlayingIndex);
return Promise.resolve();
}

function prepareToPlay() {
PlaybackState.setPlayingIndex(PlaybackState.currentPlayingIndex - 1);
return extCommand.init();
Expand All @@ -101,7 +130,6 @@ function finishPlaying() {
function catchPlayingError(message) {
if (isReceivingEndError(message)) {
setTimeout(function() {
PlaybackState.setPlayingIndex(PlaybackState.currentPlayingIndex - 1);
playAfterConnectionFailed();
}, 100);
} else {
Expand Down Expand Up @@ -142,7 +170,7 @@ reaction(
function doPreparation() {
return extCommand.sendMessage("waitPreparation", "", "")
.then(function() {
return true;
return Promise.resolve();
});
}

Expand Down Expand Up @@ -238,7 +266,11 @@ function doCommand(res, implicitTime = Date.now(), implicitCount = 0) {
: extCommand.doType(xlateArgument(target), xlateArgument(value))
))
.then(function(result) {
if (result.result !== "success") {
if (result.result === "truthy") {
return true;
} else if (result.result === "falsy") {
return false;
} else if (result.result !== "success") {
// implicit
if (result.result.match(/Element[\s\S]*?not found/)) {
if (implicitTime && (Date.now() - implicitTime > 30000)) {
Expand All @@ -258,6 +290,7 @@ function doCommand(res, implicitTime = Date.now(), implicitCount = 0) {
PlaybackState.setCommandState(id, PlaybackStates.Failed, result.result);
} else {
PlaybackState.setCommandState(id, PlaybackStates.Passed);
return true;
}
});
}
Expand Down
Loading