navigation

Automate browser actions using selenium

Automate browser actions using selenium

Annotation

For a long time, I was looking for some tool that can automate some of my actions regarding a web browser. Like opening a browser, going to some address, login, do some actions, logout, exit. This way I could automate some activities which most of the time are done via the web browser and do not require much of a thinking to do them, but require a lot of attention of the data you enter.

Current problem

My current project has two modes: running on my local machine, or running on a dedicated server – and for accessing both deployments I go to a single portal. When I am developing I want to be able to check the application against my local server, but when I am doing final tests I want to check it from the dedicated server. We are a team of around 10 people using the same dedicated server so those two modes of the application are changing quite often. Switching between them require a set of steps:

  • Opening a browser and going to the WebLogic configuration page
  • Login
  • Searching for my application, then some more searching for the module
  • Going to the administration tab of the module and changing a set of 4 fields
  • Values of those fields are not so simple so often I have to check in some other place their values just to be on the safe side.

So, only if I have some kind of one click  script which performs these things for me.

Research

After some searching, I didn’t find a simple tool designed exactly for this purpose. There were some nice tools like AutoHotKey, which is intended to make various kind of automated scripts for Windows in general and has a support of Browser automation but at first it seemed to me like a lot of research to do it.

I went then for Selenium. I know it is used for making automated UI tests, so why not use it here. It has a nice plugin for Firefox for generating the script. But if I want to run the script, I will have to open Firefox, open the plugin, navigate to the correct script and play it. So I started wondering if this could be made one click runnable.

Solution

Let’s start with creating a selenium script which will be run in Firefox. For setup the selenium IDE please refer to its official documentation here. Selenium IDE provides the recording option so, in general, you don’t have to write the script yourself, but it will record your actions over the browser and then generate the script for you. But as one can expect there are always exceptions you have to deal manually. So here is how a playback of the script looks like:

acho real

 

So you have two windows: one with the browser, and the second one with the script. When you run the script you will see the browser changing and responding to the scripts` actions. On the browser screen, you can see the 5 fields that have to be changed in order to switch between the two modes I mentioned above. One problem I had to resolve is having the script wait for an HTML element to be present before trying to perform an action on it. This was resolved easily with “waitForElementPresent” command. So just before every action I add this command with the same target as the action’s target. And that’s it! I guess there will be more complicated examples where you will have to make other modifications to the script as well, but then refer to the selenium documentation.

So we created one test-case inside a test-suite. Now we have to save the whole test suite from the file menu (again, save the whole test suite). It will be saved as HTML file. And now to see how we can run this test suite easily with a single (in the worst case double) click.

Selenium offers a way that scripts can be run from Java, C# or another language using a dedicated server and a web driver. So this dedicated server can be run with Java code, for example, and then perform the script (a browser will be opened automatically, then the actions will be performed over it and it will be closed). Its worth mentioning that the script is always executed in the browser, so a browser is always opened (correct me if I am wrong here). So here is our first solution:

Solution 1 – Create a JAVA (or if you are not experienced enough you can use another language like C#) program (class) which executes the script. Then this class can be made runnable with some bash/batch OS script with a single click (for example in Windows create a .bat file with one line like “java MyClass” + adding the required classpath). This a powerful solution because you can make all kinds of modifications to the java program (like running a different script depending on the weather forecast). But then It requires some initial setup of the java project (setting the selenium libraries, running the test for the first time) which can make some impatient people to give up.

Solution 2 – Download the selenium server from here. Then providing you have saved the test suite as myTestSuite.html you can use the following command line command to execute the script (my server’s jar is named selenium-server-standalone-2.42.2.jar):


java -jar selenium-server-standalone-2.42.2.jar -htmlSuite “*firefox” “http://localhost” “myTestSuite.html” “results.html”


Even though we reached our aim we can go even further and make the test case parametrized. In my use case, I have to change the Hostname, Port and URI for the local machine and dedicated server deployment. I can make two different test cases, but since the only difference is the values entered in these 3 fields it is better to have one parametrized. In selenium, we can accomplish this by using user extension JavaScript.This will start the selenium server on localhost, bring up the firefox browser along with another window showing the status of the execution and perform the script. Then on the result.html we will have info about the outcome. Now we can put this line inside a .bat file (for Windows) and here is your double-click shortcut for performing the automation script.

First we will create a file called user-extensions.js with content:


var _url=”localhost”;
var _port =”7101”;
var _URI = “/MyTestUri”;


Then to access these variables in the script itself we have to modify it like:

acho2

 

Where storeEval command reads the javascript variable and store it under another name in the script (in our case we use the same name). Then using ${<name_of_var>} we can reference it on the typing step. We do the same for the rest of the variables.

The last thing to do is modify the command line that calls the server to have this javascript file passed:


java -jar selenium-server-standalone-2.42.2.jar -htmlSuite “*firefox” “http://localhost” “myTestSuite.html” “results.html”
-userExtensions user-extensions.js
 


 

Any questions about my tips? Share your views in the comments section below.

Angel Gruev

IT professional focused on software development having background with software architecture, project management, JAVA, J2EE, Oracle, ADF, BPEL, Integration specialist. Very focused, highly motivated, proactive and quality/detail oriented with strong troubleshooting and problem investigation skills. Passionate about finding new methods for planning and design, project management, training and education.

More Posts - Website

Follow Me:
LinkedInGoogle Plus

Do you want more great blogs like this?

Subscribe for Dreamix Blog now!

  • Can Selenium support Visual Basic type of coding? Is Selenium more broad and better than AutoIt? I am currently using Autoit but found there are some limitations especially with browsers updating all the time