I've ported the existing control-flow extension to work in Selenium IDE. The image below shows a sample test case using goto, gotoIf and a while loop, all running successfully in Selenium IDE.

The file linked at the end of this post should be saved as "goto_sel_ide.js" to your hard drive. Then the Options settings in Selenium IDE should set the Selenium Core extensions to include the path of this file, similar to the image below.
Link:Download goto_sel_ide.js here
Copy & Paste the contents into a new file and save it as "goto_sel_ide.js" in a location such as C:\Documents and Settings\goto_sel_ide.js and configure the Selenium Options tab as shown.
Update 26 February, 2006:
This mod of the FlowControl extension works only with the Selenium IDE. If you want to use the TestRunner component or Selenium RC, please use the original FlowControl extension.


72 comments:
Sorry for the dumb question - but how would you use this extension to compare 2 URL's (or page Titles) for example?
E.g. if you are keep trying to open the same URL1 until the response changes from URL2a to URL2b?
Sounds like you're trying to poll a page until something in the response changes. I should point out though that if you query URL1, the response will always be for URL1. You could get an HTTP 304 response that would trigger a redirect, where the browser issues a new HTTP request. Anyway, if all you want to do is poll a page until the title changes, your test case would look something like this:
openAndWait | http://arstechnica.com/index.ars
storeTitle | pageTitle
storeTitle | newPageTitle
while | storedVars['pageTitle']==storedVars['newPageTitle']
pause | 1000
openAndWait | http://arstechnica.com/index.ars
storeTitle | newPageTitle
endWhile
getEval | alert("The title has changed");
this script throws:
"missing = in XML attribute"
in the IDE
this is with selenium 8.7
:(
Hi anonymous,
Please see http://www.webmasterworld.com/forum89/13847.htm
The flow control extension doesn't throw this error message. This one is generated by the JavaScript engine and is reporting an error in the page under test.
The flow control extension throws only the following error messages:
- non-matching while/endWhile found
- invalid row)nim specified
- specified label xyz is not found
- corresponding 'endWhile' is not found
- Corresponding 'While' is not found
Cheers...
D
Yours flowControl extension block IDE start command or other command if any comments exists.
The bugged row is #27
switch( command_rows[i].command.toLowerCase() ) {
Error message: command_rows[i].command has no properties
flowControl extensions seem doesn't work with xpath eval expression
count(//table//td) = 10
or
xpath=count(//table//td) = 10
Anonymous #1
I've noted that the script doesn't handle comments as a feature request.
Anonymous #2
Your eval expression doesn't appear to be valid javascript. Note that the expression is passed directly to the eval() function and must be valid javascript / DOM code. This is not the same syntax that's used for Selenium locators.
The easiest solution is to set up your variables before a while or goto using the Selenium getEval command. The parameter to getEval is the Javascript needed to do your xpath lookup. For example:
getEval |
var currentWin = this.browserbot.getCurrentWindow(); storedVars['myLinks'] = new Array(); myLinks = document.evaluate("//a[starts-with(@href, '/mylink')]", currentWin.document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null); storedVars['myLinkCount'] = myLinks.length;
This can be followed by a conditional while or goto, ie:
gotoIf | ${myLinkCount} > 10 | myTarget
Hope that helps.
Also, please not that for support questions you should use the forums at http://www.openQA.org
to handle comments in Flow Control.
Add at line 27
if (command_rows[i].type == 'command') {
switch....
...
}
anonymous, thanks for the tip, I've tested your patch and added it to the file linked here.
I'm sure I'm missing something basic here, but when I open Selenium after adding the extension, I get the following error:
Failed to load user-extensions.js!
files=C:\Documents and Settings\Administrator\Desktop\goto_sel_ide.js
error=SyntaxError: illegal character
This is with version 0.8.7. I've tried saving the file several ways, but always get the same error
Thanks!
Zach,
There's something hokey about the way google docs saves a file. Just highlight the entire text of the file and copy-n-paste it into a new text file and save it as goto.js or whatever.
D
Darren, thanks for the tip. Turns out that I was saving in the wrong encoding. Switched to ANSI and it worked fine.
Hi,
I use Selenium Core with goto_sel_ide.js file. I added it to core\scripts and I have run my script with Tomcat. The 'while' loop in test script doesn't execute. And I change its name to user-extension.js. the error javascript is occurred. please help me
This goto / flow-control script is only for Selenium IDE, the Firefox plugin. I know that it doesn't work with Selenium Core. For that, you need to use the original flow control extension (which you can find on the OpenQA website). Unfortunately I just haven't had the time (or the personal need) to modify my version of the code to work with both IDE and Core. Perhaps some L337 H4X0RZ out there will take up the challenge?
Hi,
I'm trying to use this FlowControl extension for one of my tests and I'm running into problems.
Basically if my test is dependent on importing data. So, the first time I import the data I just hit next and proceed. But if for any reason I want to run the test again the first page is a replace page. Replace the existing data (yes/no). I want to set up my test to work either way so I don't have to code it to expect to say yes and replace like it is now.
Example.
First time it imports the data I want to execute this:
selenium.Click("ctl00_bottomButtons_lbNext");
Second time it imports the data I want to execute this:
selenium.Click("ctl00_contentHolder_lnkYes");
selenium.Click("ctl00_bottomButtons_lbNext");
I don't know how to get it to check and see if the yes control is there and if it is hit it else hit the next control. Make sense?
Anthony,
Use any of the Selenium selectors to test for the presence of your reset element; assign the boolean result to a stored variable. Place a goto target in front of the "click next" line. Place an if statement in front of the "click yes" line, and use the boolean as the condition. See my Selenium Test Tips post for more information. What you're trying to do is fairly simple, I'm sure you'll get it going if you perservere... :-)
Is there a more complete syntax document for the conditions used by while and gotoIf? I'd like to:
|while|assertTextNotPresent|error
|click|//a[2]/img
|endwhile|
or something to that effect.
any suggestions?
The conditions used by goto, if and while are simply snippets of JavaScript that get passed to eval() for execution and should return a value that can be interpreted as a boolean. Selenium itself provides a number of functions (eg. assertTextNotPresent, etc.) that return booleans and are documented on both the OpenQA website and within Selenium IDE itself. The underlying Prototype library provides syntax "shortcuts" like ${x}, although I recommend using the storedVars['x'] syntax (see my post on Selenium IDE test tips: http://51elliot.blogspot.com/2008/03/selenium-ide-test-tips.html .
Hi
Re: Selenium Flow Control
How can I create an Array and Iterate through it Selenium IDE or Core?
Thanks in advance
Hi AB1971
If you use Selenium IDE, use my version of the goto extension, if you use Selenium core, use the original goto extension.
In Selenium IDE, to create and iterate over an array, you could do this:
getEval | delete storedVars['myarray']
storeEval | new Array('a','b','c') | myarray
store | 0 | x
while | storedVars['x'] < 3
getEval | alert(storedVars['myarray'][storedVars['x']])
getEval | tmp = storedVars['x'];tmp++;storedVars['x']=tmp
endWhile
Notice that I access the internal "storedVars" directly, rather than using the ${x} syntax, for reasons cited in my post "Selenium IDE Test Tips". Also note that you might want to grab an Array like document.images rather than the simple "abc" array I created in this example.
Hi Darren,
First, thanks for the quick respond. Second, your solution worked for me..THANK U SO MUCH, I AM MOST GRATEFUL !!!
Hi Darren
Just wonder, if you could tell what's wrong here, this loop works as expected in the selenium IDE, but when I try to run it in the selenium core. the first element is not pick up until the second loop. Also the If statement that pick up the desire html, appears to be picking up the wrong html (html A or htmlB or htmlC) . This is the skeleton step to test multilanguage website
Grateful for your insight..
storeEval | 0 | LoopCounter
storeEval | new Array('en','de','da','cs','fi','fr','it','ja','ko','nl','no','pl','pt','ru','sv','tr') | myarray
while | storedVars['LoopCounter'] < 10
storeEval | storedVars['myarray'][storedVars['LoopCounter']] | Lang
echo | ${LoopCounter
echo | ${Lang}
gotoIf | (storedVars['Lang'] || storedVars['LoopCounter'] )== " " | SkipLoop
store | | html
storeEval | if ( storedVars['Lang'] == "en" ) {html ='../tests/htmlA.html'} else if ( storedVars['Lang'] == "de ") {html ='../tests/htmlB.html'} else if ( storedVars['Lang'] == "da ") {html ='../tests/htmlC.html'} | html
echo | ${html} |
label | SkipLoop
store | javascript{storedVars.LoopCounter++}
endWhile
ab1971,
I haven't tried running your script but the first thing that popped into my head was this sentence from my post on Selenium IDE test tips:
"Within a single Selenium command, if you modify storedVars['x'], the value of ${x} won't get updated until the next command."
I notice you're using a mix of the ${x} and "storedVars" ways of accessing variables -- try sticking with storedVars. Also, I'm not sure how Core uses storedVars; my version of the extension is for S-IDE, so if you need help with the Core version, please check out the OpenQA website forums.
Hi Darren
I would love to have flow control in the Selenium IDE but with 1.0b2 version, I get the following error after I load the goto_sel_ide.js extension:
"error loading Selenium IDE extensions: ReferenceError: Selenium is not defined"
Any ideas?
Blake, please refer back to the original post and pay special attention to how you add the extension in the Selenium IDE Options panel. If you add it as an IDE extension rather than a Core extension, you will get the error you're seeing.
If you are getting an error you may need to add single quotes around your condition
e.g.
'storedVars{"companyRegisteredLegalNameVar"} = ""'
[error] Unexpected Exception: message -> missing ; before statement, fileName -> chrome://selenium-ide/content/tools.js -> file:///C:/selenium-extension/goto_sel_ide.js, lineNumber -> 82, stack -> (.....
, name -> SyntaxError
Actually you need to place the single quotes around any variables you are using in the condition check
e.g.
'${entityType}' == 'selectedType1'
Brillian, Darren, just brilliant. Why this isn't in the core Selenium IDE is anyone's guess... Thanks for taking the trouble to make this work under the IDE!
Thanks Richard. I think the webdriver team plans to integrate selenium into their work; maybe flow control will be part of it. Webdriver is geared towards coders, though, so we'll have to see if they get uptake.
Hi Darren
Thanks for a useful article.
I have had some success with your example, however the gotoIf does not find the second target, it reports [error] Unexpected Exception: message -> Specified label 'target2' is not found.
If the label is before the goto it works. Any advice please?
thanks
Mike (Poole, UK)
Hi Mike,
I'd have to put your item in the "could not reproduce" category, since the example in the screen capture worked for me. You might want to check for typos, capitalized letters, or spaces in your label that you might not have noticed before. I really hope its just a typo and not really a bug, because furkelling around in the guts of Selenium isn't how I want to spend Christmas :-)
cheers
I have combined the Flow-Control extention (IDE and RC versions combined)) and Data-Driven extention and created a unified user-extentions.js. Now I'm trying to run a few data driven tests that are part of a test suite. The suite work perfectly in IDE. It is working perfectly in *chrome through RC.
java -jar D:\Selenium\selenium-server.jar -multiwindow -userExtensions "D:\MaheshN\Projects\LLP FTAF-Sel feasibility\userExtentions\user-extensions.js" -htmlsuite *chrome http://www.google.com/ "D:\MaheshN\Projects\LLP FTAF-Sel feasibility\Selenium IDE scripts\SuiteDataDriven.html" "D:\MaheshN\Projects\LLP FTAF-Sel feasibility\Result\Result.html"
But when i try to run in *iehta with the following command
java -jar D:\Selenium\selenium-server.jar -multiwindow -userExtensions "D:\MaheshN\Projects\LLP FTAF-Sel feasibility\userExtentions\user-extensions.js" -htmlsuite *iehta http://www.google.com/ "D:\MaheshN\Projects\LLP FTAF-Sel feasibility\Selenium IDE scripts\SuiteDataDriven.html" "D:\MaheshN\Projects\LLP FTAF-Sel feasibility\Result\Result.html"
the following happens:
1) I get a dialog box with title "Internet explorer script error" and Message "error 'XML is undefined' do you want to continue running script on this page?" and buttons yes & no. I click the yes Button
2) It execute the open command (this happens to be the first command in my test) and then fails at the 'loadTestData' command. The result file contains the following error Unknown command: 'loadTestData'
Following is the content of the user-extentions.js file
//the content of the js is not accepted by the blog editor.
Am I doing anything wrong. It seems to work perfectly in *chrome.
Hi Daren,
I have added the *.js file to my hard drive and reflected in Selenium Options. When I try to run 'gotoif' command, i get the message:
[error] Unknown command: 'gotoif'
I'm running this on mac.
Any advise please?
Regards,
Abe
Hi Abe,
This does work on a Mac, but your text-editing program might not save the code as plain ascii text. I opened up a terminal and used vi, but if you're not comfy in Unix-land, what I suggest is to take the CD of "extras" that came with your Mac and install the Developer Tools. Then under /Developer/Applications you can find Dashcode, a pretty decent text editor for coding. Btw I just tested out with Firefox 3, latest Selenium IDE, and Mac OS X 10, no problems.
The other thing to check is make sure you add the extensions under the Selenium Core section (see screenshot).
hope this helps,
Darren
I have a test in which I hit a URL that redirects to one of three pages.
After I get to one of those pages I want to assert of verify element on one of those pages. Can I use gotoIF to do that? The page I'm redirected to will be random.
Can you give me an example of selenium html code/syntax that will accomplish this task. Thanks
Hi,
I am new to Selenium IDE. I have to create a script for detecting cursor focus on any control when a page is visited. For that i have taken ID's of all controls into a variable using storeAllFields Command. I too have got length of that variable and using while loop, i have been able to move till the length of that variable.
Question is :- I am unable to get each value separately as we get in C or JAVA. How can i do that using Selenium IDE?
jtbaca.
I'm not sure if I understand your question. I think what you want to do is jump to another section of your test if a certain element is on the page. For that, use "storeElementPresent" and use the result as the condition to "gotoIf".
Eg.
storeElementPresent|myElement|foundIt
gotoIf|foundIt|myTarget
Hope this helps,
Darren
Abhishek,
You have to use Javascript syntax to access stored variables. I recommend using the storedVars['varibleName'] style of accessing stored variables. In your case you'll iterate as:
storedVars['myFields'][currentIndex]
That should give you access to each element in your array of IDs. Also see my post on Selenium IDE test tips for more info on variables.
Hope this helps.
Darren
Thanks Darren... it helped..... thanks a lot....
Hi Darren:
I have one of three pages that I will be randomly be redirected to as soon as I hit a URL for what I call a control page. THis page allows random redirection to any one of three pages.
When I get to one of those pages then I want to make many assertions on that page. I tried something like the following:
I want to do something like the following, but am not quite sure how to pull it off:
StoreLocation Store the URL's of all three possible pages that I could be redirected to as variables.
If redirected to page A assert elements on that page.
elseif redirected to page B assert elements on page B
elseif redirected to page C assert elements on page C
Then do it all over again a few times.
Thanks
Hi D,
I'm a newbie to Selenium. Its been a couple of days since i made my hands wet in Selenium.
As you have mentioned in your other blog, Selenium does not have any kind of tech documentation so I'm finding it very very difficult to understand this tool.
Since I'm damn new I'm unable to say how great your tool is. But the comments from others makes me to give you 5 out of 5.
Great job...!!!!
jtbaca,
You'll have to get the document.location to check the landing url against your stored URLs. Other than that, it sounds like your test is fairly straightforward. I you have questions related to Selenium syntax, check out the QA forums. They're a lot more active in answering questions than I am. :-)
Swami... good, hope it helps you out.
D.
are there any extension (js files) that deal with do-while loop or if statement or for loop.. in selenium ide???
Thanks,
Abhishek
Abhishek - I'm not aware of any extensions that implement a do-while of foreach loop in Selenium IDE.
I think I've spotted a small bug in the javascript.
Line 98 reads:
var while_row = whileLabels.ends[ last_row ] - 1;
but I'm fairly sure the '- 1' is wrong, otherwise the flow is passed back to one line *before* the start of the while loop.
Otherwise, if you have a while loop starting on the first line (which is line 0 in in the whileLabels object created by initialiseLabels) then it tries to pass control back to line '-1' which fails, obviously.
At any rate, changing the line so that it reads just:
var while_row = whileLabels.ends[ last_row ];
gives the expected behaviour for me.
Hi,
I just started using Selenium and ran into your blog when I was looking for online help. Thank you for the information you have shared. It's a blessing for beginners like me.
The following question may be retarded but I am gonna ask you anyway...
I am trying to capture the (current) page title and compare it with a title I had captured earlier...
storeTitle||currentTitle
gotoIf|storedVars['currentTitle']==storedVars['classicTitle']|label1
WILL THIS WORK???
Also, How do I use the 'echo' command to make it print the value/text stored in the "currentTitle"???
Hey Suraj, the only way to know if it'll work for sure is to try it and see! I also consider myself a beginner at Selenium, by the way. I used it for a few weeks and then moved on to other projects, so I'm probably not the best one to ask about all the various functions. What I've done in the past is use a Javascript alert call to pop up a message containing whatever text I wanted to see. You can find examples of that in my blog posts about Selenium.
Darren,
Thank you for your response. The Javascript alert suggestion helped debug the issue I was facing.
Also, can you point me to any good online Selenium websites/ discussion forums??? [apart from your blog :)]
Thanks,
Suraj
Visit openqa.org for Selenium related questions.
I tried to use the gotoif command and it doesn't seem working for me. My Selenium IDE version is 1.0B2.
My issue is no matter the condition is met or not, Selenium keeps going to the step with the label "clickrenew".
Any help with this will be appreciated.
Here are the commands I have:
gotoIf |'${AutoRenew}' == true|clickrenew
label |clickrenew
click |CheckBox_RenewText
${} is syntax from the Prototype library and doesn't return a boolean. See my other posts on Selenium as to why and how to use the stored variable cache instead.
Excelente ayuda tanto para novatos como para expertos...gracias por compartir tus conocimientos y experiencia. Saludos desde Uruguay.
Hi,
First want to say thank you for your post and all comments. They are very helpful.
Could you please tell me where i'm wrong? I was trying to use one of your example from comments above to solve next problem:
I have an array with 3 link names
and i need to click them one by one
open| http://localhost/
storeEval| new Array('Worldwide','Asia','Europe')| myarray
store| 0| x
while| storedVars['x']<3| |
getEval| alert(storedVars['myarray'][storedVars['x']])|
store| storedVars['myarray'][storedVars['x']]| t
click| link=storedVars['t']|
getEval| tmp = storedVars['x'];tmp++;storedVars['x']=tmp|
endWhile|
Regards,
Oleg
Ukraine
Hey Oleg
First of all, I would try to populate the array by looking up the items on the page (ie. by using XPATH) instead of hard-coding them.
Secondly, if you see an [error] link="Worldwide" no found, you have a problem with your locator. Google 'Selenium locator'.
Here's a suggestion: use the automatic recording feature on Selenium IDE to perform the actions you want to automate. Save the test and study it to see what kind of actions and locators you should be using in your hand-crafted testcase.
Keep working on it, you'll get it.
Hi Darren.
First whant to say thanks for quick reply.
I've resolved the problem in next way:
I was using:
click|link=storedVars['myarray'][storedVars['x']]
And it wasn't working.
Then I tried to use following:
click|javascript{'link='+storedVars['myarray'][storedVars['x']]}
And it works!
Apparently the problem was that Selenium didn't understand that storedVars['myarray'][storedVars['x']] is a variable and processed it as a text line.
Thanks a lot for your time.
It seems strange to me that you had to use the javascript{} specifier. I've written a number of similar tests without it, but I didn't actually try to reproduce your test case. Maybe something has changed in the more recent versions of Selenium IDE. Thanks for sharing your solution.
I'm trying to get the IDE flow control code to work with the include code - includeCommand4IDE_1_1.zip (http://wiki.openqa.org/pages/viewpageattachments.action?pageId=283).
There are references to getting these to work together, but these seem to be for RC, not IDE - (http://wiki.openqa.org/display/SEL/flowControl) recommends:
if( htmlTestRunner.currentTest.initialiseLabels ) { htmlTestRunner.currentTest.initialiseLabels();
})
When running the test in IDE there is no htmlTestRunner.currentTest.initialiseLabels.
Is there an IDE equivalent reference I can use?
Thanks!
Brian
Brian, welcome to the world of Selenium hacking ;-)
Anonymous, check openqa.org for intros and tutorials.
Yeah, the hacking part is alright - it's just tough to find stuff in the doc. I would think (and maybe it's there and I'm missing it) that it should be simple to find the equivalent IDE call for the RC "htmlTestRunner.currentTest.[whatever]", but I can't seem to find it anywhere. I've tried removing the htmlTestRunner. and added this. or Selenium. but to no avail...
I'll keep searching. Thanks,
Brian
See my post on Selenium IDE tricks; there are a few scripts to expose S-IDE internals there.
Finally figured it out. As you've referred to in the past, documentation...there was a post in the wiki that had the call in the wrong function so this.initialiseLabels() call wasn't working. Thanks for the help. Time for a hair transplant since I just pulled out all of mine.
Brian
I currently started using Molybdenum. Can I use this user-extension in Molyb. I tried but it is not working. any idea??
No.
hey,
would someone tell me where the default commands in selenium ide are stored? if at all i have to modify them, how do i go about it?
thanks.
See the post "selenium IDE tricks". There's some information on Selenium internals.
hi darren
would you tell me how can we generate random numbers in seleium IDE. actually I want to create unique Mail Id's to test a site. and give me an example of For loop in selinum IDE..
Regards
Suruchi
Hi Suruchi,
If you haven't found a solution already, you'll just need to run a javascript statement to generate the random number. There's not Selenium command for it, per se. You can use eval or storeEval though, with the random-number generating script as a paramter.
cheers,
Darren
Hello,
This was a great extension but we are trying to implement our test cases in selenium-rc and we are wondering if it is possible to use the while loops and gotos still. Please let us know if this is possible.
Please visit the openqa site for the original flow control extension, which works with Selenium RC. My port of the extension is just for Selenium IDE (the Firefox plugin).
Thank you - this is great!
When I store javascript{category} cat
I can then reference the variable and add additional text to it like this:
${cat} is the description of this item
But if I use getEval category=categoryName[index], as in the example, it won't resolve the variable and I get this:
javascript{category} is the desc...
Interesting and I'd love to know why.
Darren - in your Selenium IDE hacks do you have a way to click Javascript links? standard "click" does in fact click the link, but then it sits there waiting for acknowledgment that apparently never arrives. I found the beatnikClick method but it doesn't always work, esp where the javascript link destination is written dynamically on pageload (so i don't know the ID until after the page is loaded). This is a Selenium bug. any work arounds?
Post a Comment