piątek, 11 listopada 2011

RANDOM NAMES AND SURNAMES


Continuing work on our script – it’s still just a rough prototype and needs improvement.



























We commenced works on artificial lover. At the moment it’s just a rough prototype.

Cen-sor-ed.





Reminder: our script searches for eastwood in every label, if there are results it saves screenshot…

What to improve? Maybe it would be more realistic if script will test Ebay categories using not just one surname but random surnames of famous actors.

Generating random surnames is almost as easy as generating random number.

In Selenium IDE you can create your own commands in separate javascript files.

Open notepad, save the file wherever you want with “.js” extension. I’ll save it as surnames.js. Write these words:

Selenium.prototype means that file will create new Selenium command. Name this command, e.g. “storeRandomSurname”:
  
Notice: add “do” before command name and remember to write first letter of command in capital, although in Selenium first letter of this command will appear as small.

Our “storeRandomSurname” command will return value of function, let's call it SURNAME:
Now we have to create an array of surnames:

“row = new Array” is defining that this is an array. It’s your choice how many surnames will be in the file. In our example there are three.

Generating random surname is done by generating random number. Our random number is called “randno” and it can have value 0, 1 or 2:

Last thing is to assign random number to the appropriate row and surname:

Speaking in general names used in file (including name of the file) are up to you. Below there is whole file where I marked words/ names which are freely to choose but have to be exactly the same in whole file.

Notice for Polish users: sadly obtaining words from external javascript file doesn’t support polish letters. Instead of: ć ą you will get: æ ê etc.

Once file with surnames is ready you must install it in Selenium. Open Selenium and “options”:

Add path to your file in “Selenium Core extensions”:


Click OK, close Selenium, and open it again. There should be two new commands:
storeRandomSurname
storeRandomSurnameAndWait

We can use freshly created command in our Ebay script. storeRandomSurname command stores random surname to variable called, for example, “surname”. Then script puts this variable in a textbox instead of eastwood.

If you want to generate random surnames, names, street names etc. you don’t have to create many files. You can keep all of them in one file, e.g.:


easy to copy version:
Selenium.prototype.doStoreRandomSurname = function(SURNAME){

row = new Array
row[0]="eastwood"
row[1]="belluci"
row[2]="schwarzenegger"

randno = (Math.round((Math.random()*2)));

storedVars[SURNAME] = row[randno];
}



Selenium.prototype.doStoreRandomName = function(NAME){

actors = new Array
actors[0]="arnold"
actors[1]="monica"
actors[2]="clint"
actors[3]="sandra"

randomnumber = (Math.round((Math.random()*3)));

storedVars[NAME] = actors[randomnumber];
}














Because we used four names, string generating random number also changed, now it generates numbers form 0 to 3.

Now this file will create in Selenium these commands:
storeRandomSurname
storeRandomName

(+ of course “…AndWait” commands)

If you improve your file in that way remember to use different names for function, random number, array etc.

If you move your file to a different location remember to delete it from Selenium options and add it again.

czwartek, 3 listopada 2011

Testing every label in a loop.

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):

  1. Imagine that you want your script to seek for eastwood not in random but in every category available on Ebay.
  2. 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:

wtorek, 25 października 2011

SELECTING RANDOM LABEL from a DROPDOWN

So you’ve just recorded your first test – Selenium’s selecting label from a dropdown. Playback works! But what if you like to test, let say… more randomly?


For instance you’ve got script that opens Ebay, selects label “Books” from a dropdown (dropdown id in this case is “_sacat”); in nearby textbox (its id is “_nkw”) script types “eastwood” and clicks “Search” button:

Now you want to seek for eastwood but randomly choose label instead of choosing always “Books”.

First you need to generate random number using, for example, this javascript string:
javascript{Math.floor(Y*Math.random()+X)}

This string will generate integer in the range from X to (X +Y – 1).

In our case we’ve got 36 labels in a dropdown. Important thing you must know is that they don’t count from 1 to 36 but from 0 to 35. So we would like to generate random number from 0 to 35 (36 possible choices in total).

In Selenium we should use below command:
store || javascript{Math.floor(36*Math.random()+0)} || randomLabel

It stores result of javascript to variable with name of our choice (we would like to be meaningful “randomLabel” but it could be “abc123” as well):


Why we used 0 and 36 in javascript string?

Because, according to definition mentioned above, range of numbers used to generate random one would be from X to (X +Y – 1),
so: from 0 to (0 + 36 – 1) gives us range from 0 to 35 (36 possibilities – including 0 – and our dropdown has 36 labels).

Of course if you know Javascript you can use any other javascript string resulting with random number.


Next step is to replace “label” with “index” so that you can use numbers instead of label names (index=4, index=22 and so on instead of: label=Books, label=Music etc.):
But we won’t use numbers directly – we’ll use our variable.


And this is the last thing to do – replace index value with a variable – in our case it’s “randomLabel”. Remember to use variable in brackets and with dollar symbol at the beginning – ${randomLabel}:

In result script will search for eastwood using random label. If generated number is 24 then selected label will be this one placed on 25th position from the top of the dropdown (in our example it’s “Pet Supplies”). If random number was 0 then selected label would be this on the first position etc.






















open | http://www.ebay.com/
store | javascript{Math.floor(36*Math.random()+0)} | randomLabel
select | id=_sacat | index=${randomLabel}
type | id=_nkw | eastwood
clickAndWait | id=ghSearch