Can I use regexp to click on this?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Can I use regexp to click on this?

GW-5
In the application I'm testing, I need to be able to click on a link that includes a generated index number.  In the sample html (below), the argument to the javascript functions changes each time I run my test:

<!-- sample 1 -->
<a title=" qa-sample1" href="javascript:selectSample('3');">qa-sample1</a>
  <a href="javascript:editSample('3');"><img src="images/edit.gif" alt="Edit" title="Edit Sample" /></a>
  <a href="javascript:deleteSample('3');"><img src="images/delete.gif" alt="Delete" title="Delete Sample" /></a>
<!-- sample 2 -->
<a title=" qa-sample2" href="javascript:selectSample('31');">qa-sample2</a>
  <a href="javascript:editSample('31');"><img src="images/edit.gif" alt="Edit" title="Edit Sample" /></a>
  <a href="javascript:deleteSample('31');"><img src="images/delete.gif" alt="Delete" title="Delete Sample" /></a>

I need to 'delete' sample 2 by clicking on it's delete.gif icon, i.e.,:

|  click  |  //a[@href="javascript:deleteSample('31');"]  |   |

I can get the value of the href for qa-sample2, but I don't know how to get the index value out of it

|  storeAttribute  |  link=qa-sample2@href  |  myHref  |

This gives me myHref = "javascript:selectSample('31');"

Any ideas how I might get the index number from myHref and use it like:

|  storeAttribute  |  link=qa-sample2@href                                      |  myHref  |
|  store              |  <get '31' from myHref>                                      |  myIdx    |
|  click               |  //a[@href="javascript:deleteSample(${myIdx});"]  |              |
 
Any other suggestions would be greatly appreciated!

Thanks,
Sandy
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=1197&messageID=3427#3427

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Can I use regexp to click on this?

David Kemp

Sandy Demi <[hidden email]> wrote on 18/02/2006 07:37:27 AM:

>
> I can get the value of the href for qa-sample2, but I don't know how
> to get the index value out of it
>
> |  storeAttribute  |  link=qa-sample2@href  |  myHref  |
>
> This gives me myHref = "javascript:selectSample('31');"
>
> Any ideas how I might get the index number from myHref and use it like:
>
> |  storeAttribute  |  link=qa-sample2@href                            
> |  myHref  |
> |  store              |  <get '31' from myHref>                      
> |  myIdx    |
> |  click               |  //a[@href="javascript:
> deleteSample(${myIdx});"]  |              |
>  

You may be able to take advantage of the fact that you can access stored variables from within javascript (it is documented towards the end of the Selenium Reference).

I have not tried it, but something like the following might work:

|  storeAttribute  |  link=qa-sample2@href    |  myHref  |
|  store              |  javascipt{storedVars['myHref']
.replace(/.*\[([0-9]+)\].*/, "$1") } |  myIdx    |
|  click               |  //a[@href="javascript:deleteSample(${myIdx});"]  |              |

Note that "foo[42]bar".replace(/.*\[([0-9]+)\].*/, "$1")  will return "42".
Try it out at : http://www.squarefree.com/shell/

Cheers,
David


David Kemp
ThoughtWorks Australia Pty Ltd
http://www.thoughtworks.com

Reply | Threaded
Open this post in threaded view
|

RE: Can I use regexp to click on this?

Sandy Demi

David,

 

I couldn’t quite get your suggestion to work, but you gave me enough help to find a solution.  Instead of trying to pull the index number out of myHref, I just changed the function name using the ‘eval’ extension, so “javascript:selectSample(‘31’);” becomes “javascript:deleteSamle(‘31’);”.  The link to the online javascript shell was very useful.

| storeAttribute | link=qa-sample2@href    | myHref  |
| eval           | storedVars['newHref'] = storedVars['myHref'].replace("select","delete"); |
| click          | //a[@href="${newHref}"] |         |

Thanks,

Sandy


From: David Kemp [mailto:[hidden email]]
Sent: Saturday, February 18, 2006 1:15 AM
To: [hidden email]
Subject: Re: [selenium-users] Can I use regexp to click on this?

 


Sandy Demi <[hidden email]> wrote on 18/02/2006 07:37:27 AM:

>
> I can get the value of the href for qa-sample2, but I don't know how
> to get the index value out of it
>
> |  storeAttribute  |  link=qa-sample2@href  |  myHref  |
>
> This gives me myHref = "javascript:selectSample('31');"
>
> Any ideas how I might get the index number from myHref and use it like:
>
> |  storeAttribute  |  link=qa-sample2@href                            
> |  myHref  |
> |  store              |  <get '31' from myHref>                      
> |  myIdx    |
> |  click               |  //a[@href="javascript:
> deleteSample(${myIdx});"]  |              |
>  

You may be able to take advantage of the fact that you can access stored variables from within javascript (it is documented towards the end of the Selenium Reference).

I have not tried it, but something like the following might work:

|  storeAttribute  |  link=qa-sample2@href    |  myHref  |
|  store              |  javascipt{storedVars['myHref']
.replace(/.*\[([0-9]+)\].*/, "$1") } |  myIdx    |
|  click               |  //a[@href="javascript:deleteSample(${myIdx});"]  |              |

Note that "foo[42]bar".replace(/.*\[([0-9]+)\].*/, "$1")  will return "42".
Try it out at : http://www.squarefree.com/shell/

Cheers,
David


David Kemp
ThoughtWorks Australia Pty Ltd
http://www.thoughtworks.com