views:

190

answers:

2

Hi all, I'm new in Selenium and testing matters. I'm trying to capture the id of an hyperlink element that is dynamically part generated. The click action is recorded like as below when i make click on in selenium, the part in bold are dynamically generated, there are many of them on my page and can differ from one site to another(I'm testing cms). I'd like to capture and click on any one. This is what I've tried to do since:

storeAttribute | //button@class onclick="setLocation(javascript{baseUrlSelection()}['/checkout/cart/add/uenc/(a-zA-Z0-9)/product/(0-9)]'')" | myid


echo | ${myid} |


clickAndWait | ${myid[0]}

It doesn't work My links looks like this when click action is recorded in selenium: buton[@onclick="setLocation('http://localhost/mydomaine/index.php/checkout/cart/add/uenc/aHR0cDovL2xvY2FsaG9zdC9NYWdlbnRvSGls

YWlyZURlbW9WMi9tYWdlbnRvZGVtb0hpbGFpcmVWMi9pbmRleC5waHAvY2F0ZWdvcmllMS5odG1sP19fX1NJRD1V/product/1/')"> Please Help.

A: 

You can use the xpath contains() function to locate the items:

storeAttribute | //input[contains(@onclick,"setLocation('http://localhost/mydomaine/index.php/checkout/cart/add/uenc/")]@class | myid
echo | ${myid}
clickAndWait | //input[contains(@onclick,"setLocation('http://localhost/mydomaine/index.php/checkout/cart/add/uenc/")]@class

It is also a good idea to reference the elements by something other than their onclick attribute. Id or name would be good choices.

s_hewitt
+1  A: 

Are you using the Selenium IDE? It would probably be much easier to do something using the other development environments, however if you really want to do looping: Get the looping user-extension from this page and run a script like this.

store | 0 | myCurrent
storeEval | var pattern=new RegExp("\\w*http://localhost/mydomaine/index.php/checkout/cart/add\\w*");var i=0;var total=0;while(i<window.document.getElementsByTagName('input').length){if (window.document.getElementsByTagName('input')[i].id.match(pattern)){window.document.getElementsByTagName('input')[i].id = 'testID_' + total;total=total+1;}i=i+1;}total; | myTotal
while | storedVars.myCurrent < storedVars.myTotal
storeAttribute | //input[contains(@id,'testID_${myCurrent}')]@class | myid
echo | ${myid}
clickAndWait | //input[contains(@id,'testID_${myCurrent}')]
store | javascript{storedVars.myCurrent++}
endWhile

What does the click on these buttons do? Does it postback the page or anything like that? If so, you'll need to move the storeEval | var pattern.... inside the while loop. What the javascript does is rename all of the inputs on the page that match the RegEx pattern (which you'll probably need to change to match your button ID's) to a sequential ID so you can loop through them easily. If there is a different pattern you can exploit, feel free to do that.

Javascript adapted from here

s_hewitt