Welcome to lesson number two…
Yes, I’m aware – opening random label is not as reliable as testing all options that application under test (AUT) contains. Bearing this in mind we will focus on following issues (let’s stick to script we created last time):
- Imagine that you want your script to seek for eastwood not in random but in every category available on Ebay.
- Moreover you demand to save a screenshot of page containing search results, but only if there were any results for eastwood indeed (in chosen category).
Before we do anything in Selenium IDE please install necessary add-on: Flow Control. I’ll mark commands added by Flow Control by (FC) so you can realize how important they are.
Ok, as I wrote we want to test every category available on Ebay. It means we need our script to repeat itself in a loop only changing search categories. We’ll use commands “while” (FC) and “endWhile” (FC) to achieve that. “While” marks beginning of a loop and “endWhile” – its end. Please add them to the script as follows:
Now we must provide condition to “While” command to specify how many times we want to run our script. Required number is 35 because that’s the number of categories/ labels in a dropdown we deal with.
So, before “While” add simple “store” command.
This will create variable “loop” (name it different if you like) and assign it initial value to 0.
Then in “While” command place condition that test (or loop to be more specific) must be repeated if “loop” variable has value less than 35:
We must only use something that will increase value of “loop” variable after every test run:
Notice: When you use variable in Selenium commands you have to use dollar sign and brackets ${variablename}. When you use variable in Javascript strings use this pattern: storedVars.variablename.
Here’s the whole path. First script assigns value 0 to variable named „loop”:
store | 0 | loop
Then it checks if value of „loop” is less than 35.
while | ${loop} < 35 |
Because loop=0 script will move on. In next step script will increase value of “loop” by 1:
store | javascript{storedVars.loop++} |
Now loop=1
Script will go through next steps and when it reaches endWhile it’ll come back to:
while | ${loop} < 35 |
Loop=1, so less than 35 so script will move on. Next step will increase value of “loop” by 1…
It’ll be repeated untill loop=35.
Note that assigning value of 0 to “loop” takes place only once.
We can use “loop” variable not only to repeat the scenario specified number of times but also to select the desired category on Ebay. Just change the name of variable which is used to pick up option from the dropdown from “randomLabel” to “loop”. Because in first run loop=1 script will search for eastwood in first category, in second run in second category and so on.
We no longer need store command generating random number – delete it.
As I mentioned script will search for eastwood and will take a screenshot if there are results, but if there are no results screenshot won’t be taken. So…
Choose a folder where screenshots are supposed to be saved by Selenium. Add another command to our script:
You have to specify whole path to desired file including its extension. You don’t have to create that file earlier – Selenium will take care of that. In the name of our jpg file we’ll use variable “loop” because if the name would be specified directly (e.g. C:\Users\x\Desktop\screenshots\abc.jpg) Selenium would simply overwrite one file after every test run.
In our script after every run file’s name would be unique: run1.jpg, run2.jpg, run3.jpg; Selenium will create many files.
Hmm, probably it would be more useful if name of every jpg file will contain also name of chosen category – like: run11_Collectibles.jpg so that you instantly know what screenshot it is.
To get the name of chosen category use below command:
This command saves name of selected label and stores it to the variable (we called it “label”). Needless to say that name saved by the script in this command results from label picked up one step earlier.
Remember to modify name of screenshot file:
captureEntirePageScreenshot | C:\Users\x\Desktop\screenshots\run${loop}_${label}.jpg |
No, I didn’t forget about condition I mentioned at the beginning: “screenshot must be taken only if search produces results”.
Little research – Ebay informes you about lack of results in this way:
As long as I’m concerned these words ("0 results found") are not present on Ebay web page in different situations than lack of results, so it’s a good indicator for us. Place these two commands in script:
gotoIf command (FC) will take execution of script to the place that we choose if condition is fulfilled. In our case if text "0 results found" is present on a web page, script will go to place called “NO_SCREENSHOT” (name freely to choose). You mark this “place” in script by “label” command (FC).
So if text "0 results found” is present script jumps to label that we called “NO_SCREENSHOT” omitting capturing screenshot step.
Notice: “Label” doesn’t do anything by itself. It just indicates place in the script. E.g. if text "0 results found” is not present, script will take a screenshot, then it’ll pass “label” but it make no harm to execution of our test, results or anything else.
Well, it’s mostly everything. Maybe one more thing. You can add at the very beginning of script this command: “set speed” (FC):
setSpeed | 1
store | 0 | loop
while | ${loop} < 35
store | javascript{storedVars.loop++}
open | http://www.ebay.com/
select | id=_sacat | index=${loop}
storeSelectedLabel | id=_sacat | label
type | id=_nkw | eastwood
clickAndWait | id=ghSearch
gotoIf | javascript{selenium.isTextPresent("0 results found")} | NO_SCREENSHOT
captureEntirePageScreenshot|C:\Users\x\Desktop\screensho\run${loop}_${label}.jpg
label | NO_SCREENSHOT
endWhile |
“setSpeed” lets you control speed of execution of script. Selenium will wait before executing every step of script for specified number of milliseconds, except commands that loads or reloads web page. “setSpeed” is in fact replacement for Fast-Slow slider in top left corner of Selenium IDE. Setting speed for 1 millisecond makes you sure that execution will be as fast as possible.
After running your, pardon – our, script you should see results like below:
Brak komentarzy:
Prześlij komentarz