![]()
API Guide - Browser |
Courses in c#, ruby, shell scripting, Software testing courses and more
|
|||||||||||||||||||||||||
|
||||||||||||||||||||||||||
A Browser object is the reference of the internet explorer window (currently IE is the only supported browser). This is probably the most common object that you would use in all your scripts. There are two ways to create a Browser object: a) Create a new empty browser - In this case a new browser is launched b) Attach to an existing browser - In this case the scripts attach to a browser that is already running on the system.
Common methods for all web elements. This module is inserted into all Web objects as a mixin.
Checks to see if a Browser with the given parameters is running on the system. The parameters is a hashtable of expected name value pairs. For example, if you want to check if a browser with the title of ‘google’ is running, you have to say
Browser.is_exist('title' => 'Google')
Create a new instance of the browser. The most common way of using it is by not specifying any parameters :
Browser.new()
In the above case, a new browser is launched. If the optional boolean value is set to ‘true’, then a WET Browser object is created but a real browser is not started.
1) Create a new ruby script with the following lines
##################################### require 'WET' include WET ie = Browser.new() #####################################
2) Save the ruby script and execute it.
A new blank IE window opens up.
Everytime you run the above script, a new browser window is opened. Close this browser window manually – or in a short time, you’ll have 10s of IE windows open on your desktop.
Get a modal dialog that is owned by this browser. The Dialog can be
retreived by searching for one or more parameters. The argument
supplied is a name value pair of expected properties of the modal dialog
in the form of "name:=value". Currently you can only specify the 'title'
property - that is, "title:=my_dialog_title"
Modal Dialogs are the popups that are shown within applications - eg. Are You Sure?, Security Information, etc., When these popups are displayed, you will not be allowed to work on the parent window unless the popus are closed. ModalDialogs are referenced by calling the Browser's Dialog method.
(1) Getting a reference of the required Modal Dialog
The syntax to get the reference of a Modal Dialog is :
Browser.Dialog("name:=value")
Where name is the name of the attribute to be checked.
value is the value of that attribute.
Example
Browser('title:=Controls').Dialog('title:=Microsoft Internet Explorer')
In the current version, the only attribute that can be used is:title
The title displayed on the Modal Dialog
1)Open a new script and type the following:
#####################################
require 'WET'
include WET
ie = Browser.new()
ie.goto "http://wet.qantom.org/userguide/browser_sample.html"
ie.Button("value:=Alert").click_without_blocking
dlg = ie.Dialog("title:=Microsoft Internet Explorer")
puts dlg
#####################################
After running this script, the sample browser page is opened, the ‘Alert’ button is automatically clicked and the sample popup window is opened as a result. The following text is printed out on the console:
Dialog(text:=Microsoft Internet Explorer)
If you notice, the text printed is nothing but the title displayed on the popup window.
Tip : In the above script observe that you typed Button.click_without_blocking and not Button.click. The reason for doing this is that if use ‘click’, then as soon as the popup window is opened, it puts the browser into a ‘wait’ state and nothing else can work, till the popup window is dismissed. You probably would have put code in the subsequent lines to handle this popup window. Unfortunately since the browser is waiting (and since your test scripts are the ones that are driving the browser, your script is waiting in turn) forever and nothing happens. When you use click_without_blocking, then the click operation is carried out in a separate process and therefore allows your script to immediately move to the next line. If you expect that peforming a click operation is likely to open a modal popup dialog, then use click_without_blocking() and not click()
Get a frame with the specified properties. The properties are
specified as name-value pairs using the syntax:
"property_name:=value"
The value can either be a string literal or a string representing
a regular expression. To emulate a regular expressioin, the value
starts and ends with the '/' character. For example,
"name:=/some.*/"
For those web pages that are displayed in a frameset, WET's Frame object can be used to get a reference of the Frame. To find out all the frames shown in a Browser, you need to use the Browser's show_frame method.
1) Getting a reference of the required Frame
The syntax to get the reference of a Frame is :
Browser.Frame("name:=value")
Where name is the name of the attribute to be checked.
value is the value of that attribute.
Browser('title:=Controls').Frame('name:=fr001')
The commonly used attributes to identify an Image are:
text The text of this Frame
name The HTML name attribute of the frame
id The HTML id attribute of the frame
1) Write the following script in a new ruby file
#####################################
require 'WET'
include WET
ie = Browser.new()
ie.goto "http://www.wet.qantom.org/userguide/frameset.html"
tf = ie.Frame('name:=top')
puts tf.text
#####################################
2) Execute this script
The following output should be printed on the console
THIS IS THE TOP FRAME
The different properties that can be used to identify a frame
are:
* name - The html name attribute of the frame
* id - The html id attribute of the frame
* index - The order in which this frame appears on the web page.
Find all the children of this Browser. When doing so, recurse into in any other top level containers (Frames / IFrames) and get the children of these containers too. The boolean parameters dictates if a refresh tree is required. If not specified or specified as false, then the previously cached array of children (If cached earlier) is returned. Otherwise, the whole tree is traversed again.
Check to see if a modal dialog exists for this browser. The method can be called without any parameters or with a hashtable of expected properties. If called without any argument, then it checks to see if the browser has any open modal dialog at all. If called with a hashtable of parameters, then check to see if there is a modal dialog with the properties matching the hashtable. Example. If called as ie.check_dialog_exists({"title" => "Microsoft Internet Explorer"}) then this method checks if a modal dialog with the title of ‘Microsoft Internet Explorer’ is active. Note: You can also call the method with a series of ‘name:=value’ parameters instead of a hashtable. However this method is deprecated and should not be used in new scripts.
Check to see if the expected text matches the text of the control.The text of a browser is the full text displayed in the document of the browser.The search_text can either be a string literal or a regular expression.
If the check is successful prints a success statement to the report,otherwise reports failure.
Check to see if the text displayed by the browser does not match expected_text.This kind of check is useful either when you need to ensure that a certain text doesn’t exist.For example,when doing a negative test or after doing a delete operaion,you need to confirm that the text actually has been deleted.
If the check is successful (that is,the text confirmed to be not found) Pritns a success statement to the report,otherwise reports a failure.
Get the count of the total number of elements in this browser. The count is the sum of all children of this browser (recursively) and the browser itself
Get properties of dom elements as a hashtable starting off from the ‘start’ parameter and a total of no more than ‘length’ parameter. This method is used by the remote C# layer when it needs to asynchronously update the browser view. To make the update appear much faster, the client requests the dom elements in multiple parts, rather than as a single block. For example, if there are 100 objects on the page, the c# client requests for 10 at a time. When it recieves 10, it updates the information on its simulated browser. It then goes and makes a request for another 10 and updates. This continues till all the objects are updated. Since the user can start using some part of the application even before all the objects are downloaded, working with the application appears to be much faster.
Is the browser actually displaying a page without any errors? Return true if it does exist. False otherwise.
The goto command is used to navigate to a web page. The parameters to be passed to this method are:
(1) url – The url of the web page that you want the browser to navigate to (2) process_type (optional) – You can specify whether the script should wait for the page to load before moving on to the next line or not. The default (when parameter is not specified) is to wait for the page to completely load. In certain cases, it may be required to just make a request for the browser to navigate to a page and then carry on with the next lines of script. This is usually true when the first result upon navigating to a web page is to see a modal popup window. In such a case, you can set the second parameter to OUTOFPROCESS and the subsequent lines of scripts wont be blocked
Example:
ie.goto "page_to_be_opened" ie.goto "a_page_that_opens_dialog_upon_navigating", OUTOFPROCESS
1) Open the previous script and add the following line to the end of the script
ie.goto "http://www.google.com"
The complete script looks like:
##################################### require 'WET' include WET ie = Browser.new() ie.goto "http://www.google.com" #####################################
Now run this ruby script
This time, a new IE window is opened and this window automatically navigates to the google web site. (As before, close the IE window manually, after script execution)
Print all the ‘wet’ objects displayed in the browser. Not only are the direct children of the
browser displayed, but also the children of top level parents like frames & iframes.
| container | [R] |
Find a Button with the specified properties, The properties are specified as name-value pairs using the syntax:
"property_name:=value"
The value can either be a string literal or a string representing a regular expression. To emulate a regular expressioin, the value starts and ends with the ’/’ character. For example, "name:=/some.*/"
The common properties used to identify a button are :
Find a Web checkbox with the specified properties. The properties are specified as name-value pairs using the syntax:
"property_name:=value"
The value can either be a string literal or a string representing a regular expression. To emulate a regular expressioin, the value starts and ends with the ’/’ character. For example, "name:=/some.*/"
The properties used to identify a checkfield are:
displayed to the left of the checkbox.
Find a Web File with the specified properties. The properties are specified as name-value pairs using the syntax:
"property_name:=value"
The value can either be a string literal or a string representing a regular expression. To emulate a regular expressioin, the value starts and ends with the ’/’ character. For example, "name:=/some.*/"
The properties used to identify a File are:
text displayed to the left of the file.
Find an IFrame with the specified properties. The properties are specified as name-value pairs using the syntax:
"property_name:=value"
The value can either be a string literal or a string representing a regular expression. To emulate a regular expressioin, the value starts and ends with the ’/’ character. For example, "name:=/some.*/"
The properties used to identify a Textarea are:
Find an Image with the specified properties. The properties are specified as name-value pairs using the syntax:
"property_name:=value"
The value can either be a string literal or a string representing a regular expression. To emulate a regular expressioin, the value starts and ends with the ’/’ character. For example, "name:=/some.*/"
The common properties for an image are
Get a web Link with the specified properties. The properties are specified as name-value pairs using the syntax:
"property_name:=value"
The value can either be a string literal or a string representing a regular expression. To emulate a regular expressioin, the value starts and ends with the ’/’ character. For example, "name:=/some.*/"
For a link the following are the commonly used properties:
Find a Web List with the specified properties. The properties are specified as name-value pairs using the syntax:
"property_name:=value"
The value can either be a string literal or a string representing a regular expression. To emulate a regular expressioin, the value starts and ends with the ’/’ character. For example, "name:=/some.*/"
The properties used to identify a List are:
displayed to the left of the weblist
Find a Web radio button with the specified properties. The properties are specified as name-value pairs using the syntax:
"property_name:=value"
The value can either be a string literal or a string representing a regular expression. To emulate a regular expressioin, the value starts and ends with the ’/’ character. For example, "name:=/some.*/"
The properties used to identify a radio button are:
label is considered to be the textimmediately after the radio.
Find a HTML Table with the specified properties. The properties are specified as name-value pairs using the syntax:
"property_name:=value"
The value can either be a string literal or a string representing a regular expression. To emulate a regular expressioin, the value starts and ends with the ’/’ character. For example, "name:=/some.*/"
The properties with which a table can be identified are:
info that is available about a table is the text contained in the table. The table's text is the aggregate of the contents of the cells of the table.
Find a Web textfield with the specified properties. The properties are specified as name-value pairs using the syntax:
"property_name:=value"
The value can either be a string literal or a string representing a regular expression. To emulate a regular expressioin, the value starts and ends with the ’/’ character. For example, "name:=/some.*/"
The properties used to identify a textfield are:
displayed to the left of the textfield.
Find a Textarea with the specified properties. The properties are specified as name-value pairs using the syntax:
"property_name:=value"
The value can either be a string literal or a string representing a regular expression. To emulate a regular expressioin, the value starts and ends with the ’/’ character. For example, "name:=/some.*/"
The properties used to identify a Textarea are:
Get an element which is of the ‘div’ tag.It is equivalent of saying - element_by_tagname(‘div’, props)
Get an element based on the tagname. This is useful for those cases where there is no predefined WET class for a particular tag but a scripter needs to use it.
For example if you want to get a <div> element whose id is ‘myId’, then you can say
element_by_tag_name("div", "id" => "myId")
Get an element which is of the ‘p’ tag.It is equivalent of saying - element_by_tagname(‘p’, props)
Get an element which is of the ‘span’ tag.It is equivalent of saying - element_by_tagname(‘span’, props)
Check to see if the object exists or not at runtime.
Returns true if the object exists, false if it doesn’t
| WET is a opensource automated web testing tool which uses Watir as the library to drive web pages. WET drives an IE Browser directly and so the automated testing done using WET is equivalent to how a user would drive the web pages. WET extends the scripting abilities of Watir and also offers the convenience of recorders. It is licensed under LGPL and BSD style open source licenses. |