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”.
2: WaitForCondition (script, timeout):
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.
Empowering Selenium to do greedy search for element
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.
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)
$(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 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.