HRVHackers
HRVHackers

Reputation: 2873

Selenium split date string working example?

I'm trying to split a date string (04.05.2012) into 3 sub-strings for later string manipulation. When I run the below code in a regular javascript editor (in Eclipse), it works. In other words, dString[2] correctly returns 2012 . When I run the below code in Selenium IDE though, it splits the date string into 9 chars, instead of the desired 3 sub-strings. What changes do I need to make to the below code? Or is it a bug with Selenium's implementation...?

Thanks All, -Sam

<tr>
    <td>store</td>
    <td>04.05.2012</td>
    <td>flight_date</td>
</tr>
<tr>
    <td>storeEval</td>
    <td>dList = '${flight_date}'.split('.'); </td>
    <td>dsplit1 </td>
</tr>
<tr>
    <td>echo</td>
    <td>${dsplit1}</td>
    <td></td>
</tr>
<tr>
    <td>storeEval</td>
    <td>day = '${dsplit1}'[9]</td>
    <td>dsplit2</td>
</tr>
<!--this returns 2-->
<tr>
    <td>echo</td>
    <td>${dsplit2}</td>
    <td></td>
</tr>

Upvotes: 1

Views: 4287

Answers (2)

Aleh Douhi
Aleh Douhi

Reputation: 1978

Try

<tr>
    <td>storeEval</td>
    <td>day = storedVars.dsplit1[2]</td>
    <td>dsplit2</td>
</tr>

instead of

<tr>
    <td>storeEval</td>
    <td>day = '${dsplit1}'[9]</td>
    <td>dsplit2</td>
</tr>

it will give you a year (2012). storedVars.dsplit1[0] and storedVars.dsplit1[1] will give you 04 and 05 correspondingly. Or you can use simple storeEval | dList[2] | dsplit2. In this case you can use getEval | dList = '${flight_date}'.split('.'); instead of storeEval | dList = '${flight_date}'.split('.'); | dsplit1

Upvotes: 2

epascarello
epascarello

Reputation: 207501

Is it treating . as any character as in a regular expression?

Try escaping it

'${flight_date}'.split('\.');

or

'${flight_date}'.split(/\./g);

Upvotes: 1

Related Questions