Parsing URL for querystring values with Selenium I

2019-05-12 17:44发布

问题:

I'm new to integration testing, but have had great success so far buiding up a suite of tests using Se:IDE. As I've been running my tests, it has occurred to me that I'm generating a substantial amount of data and I'd like to clean up after myself.

Most of my tests involve creating a new 'page', and the id is available in the querystring. I'd like to have Se:IDE store a querystring value and pass it to another page that calls a delete method to tidy up after I have run my verifications.

I see that I can use the command storeLocation, but I'm not sure how I would go about parsing that value for the id in the querystring, and then pass it to another page using Open.

Have I reached the point where I need to migrate my tests to c#, or is this possible using the IDE?

回答1:

If you keep all your test cases inside the same test suite. They can share variables between executions without problems. So, all you have to do is to store the desired value:

storeLocation | variable | |

and in a future test, you have to use the variable as the following:

open | ${variable} | |

Note: for more info on test suites, take a look at: http://seleniumhq.org/docs/03_selenium_ide.html#writing-a-test-suite

Update:

You can now use javascript regular expressions to get a substring from a variable:

storeEval | reg = /substring pattern/;reg.exec(${variable}) | substring
open | ${substring} | |

Example:

store | "012la4la" | a
storeEval | re = /[0-3]*la/;re.exec(${a}) | new
echo | ${new} | 

output:

[info] echo: 012la 


回答2:

I had a similar issue at work, and this Q&A blog helped me out a lot. In my case, I had to strip query string parameters from an aspx URL, and verify their existence.

And I used a 2 stage filter approach for verification (1) storeLocation, storeEval and verifyExpression. (2) verifyHTMLsource and globbing the string

<tr>
    <td>verifyLocation</td>
    <td>http://qa.clockstock.com/confirmation.aspx?exrc=90210&amp;csrc=</td>
    <td></td>
</tr>
<tr>
    <td>storeLocation</td>
    <td>urlconf</td>
    <td></td>
</tr>
<tr>
    <td>echo</td>
    <td>${urlconf}</td>
    <td></td>
</tr>
<tr>
    <td>storeEval</td>
    <td>storedVars['urlconf'].indexOf('exrc=90210');</td>
    <td>exrcurlconf</td>
</tr>
<tr>
    <td>verifyExpression</td>
    <td>javascript{(storedVars['CIDurlconf']&gt;0)}</td>
    <td>true</td>
</tr>
<tr>
    <td>storeEval</td>
    <td>storedVars['urlconf'].indexOf('csrc=');</td>
    <td>CSRCurlconf</td>
</tr>
<tr>
    <td>verifyExpression</td>
    <td>javascript{(storedVars['CSRCurlconf']&gt;0)}</td>
    <td>true</td>
</tr>
<tr>
    <td>verifyHtmlSource</td>
    <td>glob:*confirmation.aspx*exrc=90210*csrc=*</td>
    <td></td>
</tr>


回答3:

A quick example for extracting an id parameter from a query string would be:

storeLocation | myLocation
store | javascript{ storedVars['myLocation'].substring(storedVars['myLocation'].indexOf('id=')+3, storedVars['myLocation'].length); } | idValue

This assumes that the id parameter is the last in the query string. If it's not then you might be best splitting the location on '&' and looping through the resulting array for the 'id' parameter value.