0

I'm trying set item in select using javascript (in variable I have date)

Here is how I'm trying set this item

<tr>
    <td>select</td>
    <td>Recent-Year</td>
    <td>value=javascript{d=new Date();d.getYear()}</td>
</tr>

I get:

[error] Option with value 'javascript{d=new Date();d.getYear()}' not found 

of cource when I'm trying by this:

<tr>
    <td>select</td>
    <td>Recent-Year</td>
    <td>value=2011</td>
</tr>

it works.

What I must to change in this code to set this item?

EDIT

I don't want to split this command into few commands, because in another testcase I have stored variable birthday

<tr>
    <td>storeEval</td>
    <td>new Date('1966,09,16')</td>
    <td>dateOfBirth</td>
</tr>

and I have 3 selects : Birth-Day, Birth-Month and Birth-Yearand I don't want to create 3 variables.

I want to set items in select like this:

    <tr>
        <td>select</td>
        <td>Birth-Year</td>
        <td>value =${dateOfBirth.getFullYear()}</td>
    </tr>

1 Answer 1

3

Store the JavaScript evaluation first (using storeEval) and then use this variable as your option value:

<tr>
  <td>storeEval</td>
  <td>new Date().getYear()</td>
  <td>varYear</td>
</tr>
<tr>
    <td>select</td>
    <td>Recent-Year</td>
    <td>value=${varYear}</td>
</tr>

That should work


Update: In response to your edit: you can't use the value= with JavaScript but you can use the storedVars variable available in the JavaScript environment to create all the variables you need. For example, storedVars['myVar'] can be accessed directly through Selenium as ${myVar}. Below, I use verifyEval to run some JavaScript that will create all the variables I need.

<tr>
    <td>storeEval</td>
    <td>new Date()</td>
    <td>myDate</td>
</tr>
<tr>
    <td>verifyEval</td>
    <td>var date=storedVars['myDate']; storedVars['day']=date.getDate(); storedVars['month']=date.getMonth(); storedVars['year']=date.getFullYear();</td>
    <td></td>
</tr>

Then you can select the options based on:

<tr>
    <td>select</td>
    <td>Birth-Day</td>
    <td>value=${day}</td>
</tr>
<tr>
    <td>select</td>
    <td>Birth-Month</td>
    <td>value=${month}</td>
</tr>
<tr>
    <td>select</td>
    <td>Birth-Year</td>
    <td>value=${year}</td>
</tr>

I know it's not the ideal solution, but it does help a bit since you can't do value=javascript{...} (not that I know of anyway but if anyone else knows otherwise then please say).

Sign up to request clarification or add additional context in comments.

4 Comments

yes, that works and thanks for that, but It's not I want, because in another testcase I have stored variable birthday (new Date('1974,09,16')) and I have 3 selects : dayOfBirth, monthOfBirth and yearOfBirth and I don't want to create 3 variables. I want to set items in select eg value =${dateOfBirth.getFullYear()}
@user278618 I've updated my answer. Unfortunately I don't think there's a way to do what you're looking for but I've provided a solution that at least minimises the amount you need to do.
@Downvoter - any reason why you downvoted? If there's anything you suggest then please help by leaving a comment.
Yours solution is what I did before in my code. I had hope that there is other simpler solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.