Q:

Create an select option with selenium IDE

I have 2 selects for setting the expiration date on a credit card and I want to test that the error messaging comes back correctly when the card is expired.  The only problem I have is that right now I can't pick a value that's expired since the list starts at Jan 2011 which isn't expired yet.  What I'm thinking about doing is using javascript in selenium to inject a known expired value.  Anyone have any idea on how to do this or suggestions on a better way to accomplish?

A:

You can use the storeEval method to run snippets of JavaScript using Selenium. The JavaScript is run in the context of the Selenium object so you need window.document to get the document reference (hence the reason for the first line).
This snippet is the JavaScript for adding a new option the select element: 
var d = window.document;
var select = d.getElementsByName("selectName")[0];
var option = d.createElement('option');
option.value='Value';
option.innerHTML='My new option';
select.appendChild(option);

Here's the copy/paste of my Selenium IDE entry for you.
<tr>
 <td>storeEval</td>
 <td>var d = window.document;var select = d.getElementsByName(&quot;selectName&quot;)[0];var option = d.createElement('option');option.value='Value';option.innerHTML='My new option';select.appendChild(option);</td>
 <td></td>
</tr>

Then you can just have the standard select method in Selenium to select the option. Of course, you can always do option.setAttribute('selected',true) in the JavaScript snippet but having Selenium do it will ensure the new value has been selected (since it will fail if it can't find it).

