Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do regular expressions work in selenium?

I want to store part of an id, and throw out the rest. For example, I have an html element with an id of 'element-12345'. I want to throw out 'element-' and keep '12345'. How can I accomplish this?

I can capture and echo the value, like this:

| storeAttribute | //pathToMyElement@id | myId |
| echo | ${!-myId-!} | |

When I run the test, I get something like this:

| storeAttribute | //pathToMyElement@id | myId |
| echo | ${myId} | element-12345 |

I'm recording with the Selenium IDE, and copying the test over into Fitnesse, using the Selenium Bridge fixture. The problem is I'm using a clean database each time I run the test, with random ids that I need to capture and use throughout my test.

like image 883
Andrew Avatar asked Dec 31 '08 17:12

Andrew


3 Answers

The solution is to use the JavaScript replace() function with storeEval:

| storeAttribute | //pathToMyElement@id                                   | elementID |
| storeEval      | '${elementID}'.replace("element-", "")                 | myID      |

Now if I echo myID I get just the ID:

| echo | ${myID} | 12345 |
like image 103
Andrew Avatar answered Sep 18 '22 07:09

Andrew


/element-(\d+)/i

That's a regular expression that would capture the numbers after the dash.

like image 45
Salty Avatar answered Sep 22 '22 07:09

Salty


Something like this might work:

| storeAttribute | fn:replace(//pathToMyElement@id,"^element-","") | myId |

To do regex requires XPath 2.0 - not sure which version Selenium implements.

like image 26
Peter Boughton Avatar answered Sep 19 '22 07:09

Peter Boughton