Browse Category: Selenium WebDriver

Selenium Commands That You must know.

Pay attention to following commands as these are going to play pivotal role in selenium.

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”);



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 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.