Skip to content

Instantly share code, notes, and snippets.

@luzeduardo
Last active July 14, 2016 20:21
Show Gist options
  • Save luzeduardo/4679626 to your computer and use it in GitHub Desktop.
Save luzeduardo/4679626 to your computer and use it in GitHub Desktop.
Selenium loop example with Selenium IDE + https://github.com/darrenderidder/sideflow
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="http://localhost" />
<title>ExampleSeleniumLoop</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">ExampleSeleniumLoop</td></tr>
</thead><tbody>
<tr>
<td>getEval</td>
<td>index = 0;</td>
<td></td>
</tr>
<tr>
<td>while</td>
<td>index &lt; 10;</td>
<td></td>
</tr>
<tr>
<td>open</td>
<td>/</td>
<td></td>
</tr>
<tr>
<td>clickAndWait</td>
<td>//img[contains(@src,'http://localhost/')]</td>
<td></td>
</tr>
<tr>
<td>clickAndWait</td>
<td>css=img[alt=&quot;Bloco de cortiça&quot;]</td>
<td></td>
</tr>
<tr>
<td>getEval</td>
<td>index++;</td>
<td></td>
</tr>
<tr>
<td>endWhile</td>
<td></td>
<td></td>
</tr>
</tbody></table>
</body>
</html>
var gotoLabels= {};
var whileLabels = {};
// overload the original Selenium reset function
Selenium.prototype.reset = function() {
// reset the labels
this.initialiseLabels();
// proceed with original reset code
this.defaultTimeout = Selenium.DEFAULT_TIMEOUT;
this.browserbot.selectWindow("null");
this.browserbot.resetPopups();
}
/*
* --- Initialize Conditional Elements --- *
* Run through the script collecting line numbers of all conditional elements
* There are three a results arrays: goto labels, while pairs and forEach pairs
*
*/
Selenium.prototype.initialiseLabels = function()
{
gotoLabels = {};
whileLabels = { ends: {}, whiles: {} };
var command_rows = [];
var numCommands = testCase.commands.length;
for (var i = 0; i < numCommands; ++i) {
var x = testCase.commands[i];
command_rows.push(x);
}
var cycles = [];
var forEachCmds = [];
for( var i = 0; i < command_rows.length; i++ ) {
if (command_rows[i].type == 'command')
switch( command_rows[i].command.toLowerCase() ) {
case "label":
gotoLabels[ command_rows[i].target ] = i;
break;
case "while":
case "endwhile":
cycles.push( [command_rows[i].command.toLowerCase(), i] )
break;
case "foreach":
case "endforeach":
forEachCmds.push( [command_rows[i].command.toLowerCase(), i] )
break;
}
}
var i = 0;
while( cycles.length ) {
if( i >= cycles.length ) {
throw new Error( "non-matching while/endWhile found" );
}
switch( cycles[i][0] ) {
case "while":
if( ( i+1 < cycles.length ) && ( "endwhile" == cycles[i+1][0] ) ) {
// pair found
whileLabels.ends[ cycles[i+1][1] ] = cycles[i][1];
whileLabels.whiles[ cycles[i][1] ] = cycles[i+1][1];
cycles.splice( i, 2 );
i = 0;
} else ++i;
break;
case "endwhile":
++i;
break;
}
}
}
Selenium.prototype.continueFromRow = function( row_num )
{
if(row_num == undefined || row_num == null || row_num < 0) {
throw new Error( "Invalid row_num specified." );
}
testCase.debugContext.debugIndex = row_num;
}
// do nothing. simple label
Selenium.prototype.doLabel = function(){};
Selenium.prototype.doGotoLabel = function( label )
{
if( undefined == gotoLabels[label] ) {
throw new Error( "Specified label '" + label + "' is not found." );
}
this.continueFromRow( gotoLabels[ label ] );
};
Selenium.prototype.doGoto = Selenium.prototype.doGotoLabel;
Selenium.prototype.doGotoIf = function( condition, label )
{
if( eval(condition) ) this.doGotoLabel( label );
}
Selenium.prototype.doWhile = function( condition )
{
if( !eval(condition) ) {
var last_row = testCase.debugContext.debugIndex;
var end_while_row = whileLabels.whiles[ last_row ];
if( undefined == end_while_row ) throw new Error( "Corresponding 'endWhile' is not found." );
this.continueFromRow( end_while_row );
}
}
Selenium.prototype.doEndWhile = function()
{
var last_row = testCase.debugContext.debugIndex;
var while_row = whileLabels.ends[ last_row ] - 1;
if( undefined == while_row ) throw new Error( "Corresponding 'While' is not found." );
this.continueFromRow( while_row );
}
Selenium.prototype.doPush= function(value, varName)
{
if(!storedVars[varName]) {
storedVars[varName] = new Array();
}
if(typeof storedVars[varName] !== 'object') {
throw new Error("Cannot push value onto non-array " + varName);
} else {
storedVars[varName].push(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment