How to Store Variables in Selenium IDE | Echo Command
So far we have seen how to find locators. Time to get in on some action. This tutorial will show you how to store variables in Selenium IDE. Echo Command will also be seen. By the end of this chapter, you will become well-versed in how to store variables in Selenium IDE and how to use echo to display stuff in your log window.
How to Store Variables in Selenium IDE
At times you might be required to store variables, and that’s where you can make use of the “store” functionality.
Step 1: Open your Firefox and then Selenium IDE.
Step 2: Click on the first line of the Editor pane which will highlight the Command box. Type store in command box and then type variable value in the “target” box. Give a name to the variable in the Value box.
Remember the data in the value box will be the variable, and not the data in the target. Target will hold the value of the variable. Confusing right? Maybe that’s why we don’t use Selenium IDE anymore.
Step 3: Run the test case by clicking on the green arrow button, and you are done.
So if you will run the above test the value “9898987895” will get stored in the variable ‘mobile’.
Kind of like:
mobile = 9898987895
But we can’t see this, right? How do we display the value of mobile?
That’s where Echo Command comes into picture.
Using Echo Command in Selenium IDE
Values inside the variables in Selenium IDE are referred by the following syntax:
${variable_name}
where variable_name can be replaced by whatever variable you have in mind.
So if you wish to just show the above mobile number you can make use of Echo command to display that. So basically echo command does what System.out.print used to do in Java.
Proceeding with our example above:
Step 4: Just click on the second line of the Editor pane and type “echo” in the command box followed by “${mobile}” in the target:
Step 5: Go ahead and run the test case. In the log section below you will notice that echo has the same value that we had passed in “mobile” variable.
An Example to Store a Text and Then Type it Some Place Else
We will use another example to learn how to store variables in Selenium IDE. It will make you more confident.
Let’s proceed with a similar example to understand this further. We will use the website of Goodreads to test this.
Step 1: Open Firefox, then Selenium IDE. Stop the recorder. We will do this the manual way this time.
Step 2: Click on the first line of the Editor pane and type “open” in the Command box. Give the target as “https://www.goodreads.com/”.
Step 3: Click on the second line of the Editor Pane and type “store” in the command box. Then put a random email id, say “abc@gmail.com” in the Target box. In the value box provide a name to the variable like “email”:
Step 4: Click on the third line of the Editor pane now and then type “Type” in the command box.
Step 5: Open “https://www.goodreads.com/” in a new tab and then open Firebug. (Press F12)
Step 6: Click on the Inspect button of Firebug and then on the Email Id Textbox at the top.
You can go back and refer to the chapter on locators if you are having a hard time understanding how to locate elements on a web page.
Typing in Text Field
Step 7: Copy the Id name “userSignInFormEmail” that refers to the Email address text box and go back to your Selenium IDE.
Step 8: Paste it in the target box. So you must have something like this:
Now comes the part where we make use of the variable. Remember how we need to specify the variable using the syntax ${*****} that’s what we are going to use here.
Step 9: Type ${email} in the Value box like this.
Step 10: Execute the test case by clicking on the green button. You will notice that the website of Goodreads opens in the background and the Email Address box at the top automatically populates with the email value “abc@gmail.com”.
Isn’t that cool?
StoreText Command
There are other cool storage commands in Selenese too. One of them which will help you to learn how to store variables in Selenium IDE is the storeText Command.
If you wish to store the inner text of some element on the web page to a variable you can make use of storeText.
Let’s take the example of Flipkart to see this.
On the website of Flipkart there’s a link text called “Sell on Flipkart”. Let’s retrieve the value of the link by using storeText command.
Step 1: Open Firefox browser and then open Selenium IDE.
Step 2: Stop the recording button since we are doing things manually here. Then click on the first line of the Editor pane.
Step 3: Click on Command box and type Open there. Then in Target box type : “https://www.flipkart.com/”
Using storeText Selenese Command
Step 4: Now click on the second line of the Editor pane and type storeText in the command box.
Time to hunt down what’s the source code of that “Link” says.
Step 5: Open Firebug (F12) and click on Inspect. Then click on the “Sell on Flipkart” button located at the top.
As you can see we can make use of data-reactid attribute here to refer to this link.
We will bring into use the css selectors lesson here, where we had seen how to locate elements using attribute and value.
Using that info we will use data-reactid = 9 value to locate the link text.
Step 6: Type “css=a[data-reactid=9]” in the target box of Selenium IDE and in the target box type “text” or any variable name you could think of:
This particular line of Editor pane will store the value of the link text inside a variable named text.
Time to print the variable value to see if it has really been copied.
Step 7: Click on the third line of Editor pane and type echo in the Command box. Then in the target type “${text}”.
Step 8: When you are done, execute the test case by pressing the green arrow button.
As you can see this has passed. The value of the link text has been printed as “Sell on Flipkart” which is correct too.
Yipee!
StoreElementPresent Command
In a similar fashion, you can make use of the readily available storeElementPresent command which returns you a boolean value of either true or false.
It just tells you whether or not an element is present in your web page. Thus making the searching job a bit easier.
Here I have simply made one simple change in the above example to test this:
Alright! I think you know how to store variables in Selenium IDE by now. Also I am assuming you have become comfortable with echo commands.
Go show them soldier!