Basic Commands of Selenium RC

In previous post, we have discussed about locator and how to do scripting in Selenium IDE. If you have done through practice with IDE then I am sure you have been familiar with multiple selenium commands. Today will talk about some basic commands of selenium which we are going to use most frequently in our test framework other than click (), type () etc. I would recommend you to go through these commands thoroughly and try to practice them with IDE.

1: SelectFrame (locator):

To learn this command we need to understand basics of HTML frame element first. Spend some time to learn HTML Frames, if don’t know much about frame elements. While performing any action to any web element, we first select the frame where it resides then we perform any action to any element inside that frame. To access other elements coded in different frame, again we need to select that frame using this command and then we can access their elements. We use frame name as locator. To select the parent frame, use “relative=parent” as a locator; to select the top frame, use “relative=top”. You can also select a frame by its 0-based index number; select the first frame with “index=0”, or the third frame with “index=2”.

E.g.     selenium.SelectFrame(“relative=up”);

selenium.SelectFrame(“al-iframe”);

selenium.SelectFrame(“fraContent”);

2: WaitForCondition (script, timeout):

This command executes ‘script’ (JavaScript/selenium command) repeatedly until it gets true and exit. The maximum duration of execution is specified as ‘timeout’. Command returns ‘false’ if script timeout without evaluating script as true. We will use this command frequently in our code to verify any expected condition be true within specified time period. For example we can use this command to execute to simple JavaScript to verify if some page element present or not etc. I would recommend you to do practice this command with variety of JavaScript or other selenium command.

Selenium.WaitForCondition (“selenium.IsElementPresent (“id=buttonID”)”, 60000); Explanation: This command will execute repeatedly until selenium.IsElementPresent () returns true or reach timeout period. Even after 60000 ms, if selenium.IsElementPresent () doesn’t find desired element, WaitForConition command returns false.

3: WaitForPageToLoad (timeout):

This is another very important selenium command that people use in testing. We need to wait for page to load either do some action or to verify if there is new page or frame being load on the browser. It works most of time but on some instances it doesn’t which I will explain later. We will create customized method using WaitForCondition command to perform waitForPageToLoad action.

4: fireEvent (locator, eventName):

This command use to explicitly simulate an event, to trigger the corresponding “onevent” handler. There are some of instances where search box perform search process as soon as we start typing in the field but that only possible if type manually. that doesn’t work if we just send selenium.Type() command to the element. In such cases we need to send fireEvent command with eventName like onChange, onFocus etc., depending upon the associated event to the search box. We will talk about this in more detail on upcoming posts.

5: AddLocationStrategy(StrategyName, Strategy):

This command is very powerful command of selenium. We will use to write a strategy to capture the web element or locator smartly. For example, if you define the strategy of name “foo”, we need to use this “foo” with ‘=’ to define locator. To use this strategy effectively we will use with “foo=blah bblah” in our code. In later section I will show you, how to use jQuery for our location strategy and start doing greedy search for every element without any hassle. Here is basic example of that but we will go in more detail in later sections. Suppose you want to click on <button> element with id=”a”, you will write command selenium.Click(“jquery=button[id=’a’]”); where ‘jquery’ is the name of the strategy. Just make sure that you run addLocationStrategy command in very beginning of you code.

There are few more commands like isElementPresent, isTextPresent, Assert, Verify, which is also an important command for testing but I don’t think we need special explanation here. You can refer them from various from or seleniumhq.org. I would recommend you to do more and more practice on these commands so you will be well acquainted.

Now we will try to finish very first step of framework in term of coding. In this section we will try to empower selenium to do greedy search for locator using AddLocationStrategy command. We will write a strategy using JavaScript and jQuery which is be best way to do search the web element without any hassle. Before going into detail let’s learn jQuery first. I am sure you just frown after reading this but I can assure you, it’s not that daunting as you are expecting.

Empowering Selenium to do greedy search for element

jQuery:

jQuery is free, open source software, dual-licensed under the MIT License or the GNU General Public License, jQuery’s syntax is designed to make it easier to navigate a document, select DOM elements, create animations, handle events, and develop Ajax applications.

The bold text in above line is more important to us. We will use this feature to search locator for the target element. I will give couple of examples to make is more clear.

jQuery Syntax:

The jQuery syntax is tailor made for selecting HTML elements and performs some action on the element(s).

Basic syntax is: $(selector).action()

  • A dollar sign to define jQuery
  • A (selector) to “query (or find)” HTML elements
  • A jQuery action() to be performed on the element(s)

Examples:

$(this).hide() – hides current element

$(“p”).hide() – hides all paragraphs

$(“p.test”).hide() – hides all paragraphs with class=”test”

$(“#test”).hide() – hides the element with id=”test”

I highlighted the selector with red. This selector is the one we need to learn for locating element. We will concentrate on selectors in detail now.

jQuery Selectors:

jQuery selectors allow you to select HTML elements (or groups of elements) by element name, attribute name or by content.

Query Element Selectors jQuery Attribute Selectors
jQuery uses CSS selectors to select HTML elements.
$(“p”) selects all <p> elements.

$(“p.second”) selects all <p> elements with class=” second “.

$(“textbox#sample”) selects all <textbox> elements with id=” sample”.

jQuery uses XPath expressions to select elements with given attributes.
$(“[href]”) select all elements with an href attribute.

$(“[href=’xyz’]”) select all elements with an href value equal to “xyz”.

$(“[href!=’ xyz ‘]”) select all elements with an href attribute NOT equal to ” xyz”.

$(“[href$=’.jpg’]”) select all elements with an href attribute that ends with “.jpg”.

$(“[href^=’fuel-input-‘]”) select all elements with an href attribute that starts with “fuel-input-“.

We will be using jQuery Attribute selector more frequently in out script as it will be make our job more easy. So again I would recommend you to learn basic of jQuery selector from jQuery Tutorial1 and Tutorial2. Don’t spend more time on that but at least one day to be confident enough. Don’t forget to bookmark those links as you may need them to search for special case and that will help your specially Tutorial2.

First Step Towards Selenium

This is a common question being search from engineers, how to start selenium Test. I am sure some of you who are reading this blog may aware of fundamentals of Selenium but I am am going to start from very basic of Selenium Scripting. So all newcomers, get ready from very beginning of selenium test. In this post I will try to explain about Selenium and how to configure IDE for testing.

First we will try to understand how Selenium works and interact with Web browser and web element. Actually Selenium is a java script library who interacts with element inside web browser but not the web browser’s elements like open, save as etc. commands. It sends HTTP commands to the server so its look like some user clicking on the element. As a result of that, we can use test machine to do other work as selenium doesn’t engage mouse for any kind of event like QTP. isn’t great?

In last paragraph we were talking about web browser elements like different menu and dialogs like “Save As” dialog that we cannot interact with Selenium. For such element we need write some external program such as Autoit, which can simulate windows events and their interaction with windows application like browser. We will discuss in detail about Autoit later.

Note: Install Firefox, firebug and go through basics of HTML and if possible do some practice on firebug to recognize elements and their values.

Lets come to the point. We will first try to understand Selenium IDE which is the basic and only UI part of Selenium. This is Add-on to the Firefox. We will first Install latest available version from Selenium website from section ‘Selenium IDE’ located here

LinkToDownloadSeleniumIDE

InstallationStart

After installation restart Firefox and you can see selenium IDE in tools menu. Click on Selenium IDE option and IDE will appear as below.

image

[youtube=http://www.youtube.com/watch?v=QIDY1gq2x_0&w=448&h=252]

Selenium IDE is record and play back tool. You can record application Now I would like you to visit following video to learn more about IDE.

[youtube=http://www.youtube.com/watch?v=5W27CPCbLSM&w=451&h=278&hd=1]
Learn Selenium IDE

Hope you understood the basic of IDE that is actually a backbone of learning phase of Selenium. There are many features available in IDE which were not discussed in last video. I would recommend you to go through the other features and options available in IDE.

I am sure this post is helpful for beginners. In next post I will discuss how to export recorded tests to different programming language as per our need.

Any suggestion please write us to learn@learnseleniumtesting.com or leave comments to this post.

qtpTesters Vs Selenium Testers

Selenium And Why Selenium is better than QTP?



This is quite common question among software professionals, which tool is better option for web testing. There are varieties of tools available in the market, either paid like QTP, Winrunner or some open source tools like Selenium, Webdriver. Selenium and Webdriver shares the same ancestor. We will discuss more in detail later in this blog. Now we are here to understand why we should go with selenium instead of other. I am pretty sure; gung-hos of QTP will hate me for this. But I will speak.

I acquainted with selenium in 2009 July when I was searching some record and playback web form application for personal use. On top of that, it should be very small to install in term of resource usage. Many people suggested me QTP for that but there was limitation that I can use that for trial period only and secondly it consumes max resources and kind of bulky for just one simple form that I wanted to automate.

After couple of hours of Google search I found Molybdenum and SeleniumIDE as an add-on for Firefox. Both are very nice, easy to install and easy to use. I tried both and was thinking to decide which one I should go with. I started learning more about both tool and found very interesting information which preluded me to decide.

Molybdenum V/s SeleniumIDE
While Selenium IDE is focusing on developers with export to different programming languages and cross browser testing. Molybdenum is focused on simple test execution, reporting, test parameterization for everybody participating in your team.

I was working on C# based in-house tool and wanted to learn some testing tools being used by other companies. I learned that in Selenium I could use my C# skills and after doing some research, I realized that Selenium/Webdriver is the future.

Mercury (now HP) did great job with their tools thus their market share is ~60% in software testing. But now open source started roaring as well. Many companies are strategically moving towards cost effective test tools because Mercury(HP) tools are still an expensive option. later studies showed that we can do >70% with open source tools of what we can do via QTP so why to spend that much money if your project scope is not that much to use all features of QTP.

I am going to give some high level idea which speaks about difference between Selenium and QTP. I am sure this will be very useful if you are looking to automate your web application testing.

Comparison Selenium/Webdriver QTP Winner
License Free Paid
Software size < 100 MB >1.8GB
Objects Recognition. Firebug(FF add-on) GUI Spy
Support Openqa, seleniumhq & other forums Dedicated from HP
Scripting language Java, .Net, Python, Ruby VB Scripting
Browsers supported IE, Firefox, Safari, Opera IE, Firefox
Operating System Windows, Mac, Linux, iOS, Android Windows
Overall

As you can see that Selenium is overall winner but keep in mind you have to depend on different forums and blogs etc. for any kind of issue. If you are about to start automation then I would recommend you to go with webdriver because webdriver is the future. Selenium RC 2 is last release of Selenium but it’s powerful to automate your web application and many people are using it so you can get support in various forums. There are many things in selenium which I personally like than webdriver but it’s just individual choice. I will explain all those differences in later posts

If you love OOPs programming then I would personally recommend you to go with selenium. Many testers want to do coding and this would be a perfect platform for them.

Job market is also shifting for selenium as compare to QTP. Sharing the latest job trend.

Selenium Vs QTP

 

qtpTesters Vs Selenium Testers

 

Blog follower EH, raised a question that first graph may be misleading so I generated another graph QTP Testers Vs Selenium Testers.  Still I noticed that QTP is going down and Selenium is moving up. 

Learn more about selenium

 

Please keep visit this site as we are going to design complete test framework for web testing in .NET and Java as well.