8

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.

OMG Ponies
  • 300,587
  • 73
  • 490
  • 482
Andrew
  • 196,883
  • 184
  • 487
  • 673

3 Answers3

11

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 |
Andrew
  • 196,883
  • 184
  • 487
  • 673
2

/element-(\d+)/i

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

Salty
  • 6,450
  • 3
  • 30
  • 30
  • Thanks! Now where would that go in my code? | storeAttribute | //pathToMyElement@id | myId | – Andrew Dec 31 '08 at 21:41
  • I'm really sorry, but I've never used Selenium before. I posted the regular expression hoping someone who had used it before could come along and help you use it. I'll do some research and see if I can come up with an answer though :) – Salty Dec 31 '08 at 22:03
2

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.

Peter Boughton
  • 102,341
  • 30
  • 116
  • 172