Tuesday 31 December 2013

Events in JavaScript and HTML


Eventsin JavaScript and HTML
JavaScript's interaction with HTML is handled through events that occur when the user or browser manipulates a page.

When the page loads, that is an event. When the user clicks a button, that click, too, is an event. Another example of events are like pressing any key, closing window, resizing window etc.

Developers can use these events to execute JavaScript coded responses, which cause buttons to close windows, messages to be displayed to users, data to be validated, and virtually any other type of response imaginable to occur.

Events are a part of the Document Object Model (DOM) Level 3 and every HTML element have a certain set of events which can trigger JavaScript Code.
Without events there are no scripts. Take a look at any web page with JavaScript in it: in nearly all cases there will be an event that triggers the script. The reason is very simple. JavaScript is meant to add interactivity to your pages: the user does something and the page reacts.


Here we will see few examples to understand a relation between Event and JavaScript:
Example
Mouse Over Me and Click Me
Reacting to Events
A JavaScript can be executed when an event occurs, like when a user clicks on an HTML element.
To execute code when a user clicks on an element, add JavaScript code to an HTML event attribute:
onclick=JavaScript
Examples of HTML events:


To assign events to HTML elements you can use event attributes.

Example

Assign an onclick event to a button element:

<button onclick="displayDate()">Try it</button>

In the example above, a function named displayDate will be executed when the button is clicked.

The onload and onunload Events
The onload and onunload events are triggered when the user enters or leaves the page.

The onload event can be used to check the visitor's browser type and browser version, and load the proper version of the web page based on the information.

The onload and onunload events can be used to deal with cookies.

Example

<body onload="checkCookies()">


The onchange event are often used in combination with validation of input fields.

Below is an example of how to use the onchange. The upperCase() function will be called when a user changes the content of an input field.

Example

<input type="text" id="fname" onchange="upperCase()">

The onmouseover and onmouseout Events
The onmouseover and onmouseout events can be used to trigger a function when the user mouses over, or out of, an HTML element.

Example

A simple onmouseover-onmouseout example:




The onmousedown, onmouseup, and onclick events are all parts of a mouse-click. First when a mouse-button is clicked, the onmousedown event is triggered, then, when the mouse-button is released, the onmouseup event is triggered, finally, when the mouse-click is completed, the onclick event is triggered.

Example


Click Me



Change an image when a user holds down the mouse button.

onload
Display an alert box when the page has finished loading.

onfocus
Change the background-color of an input field when it gets focus.

Change the color of an element when the cursor moves over it.
Event               Value                     Description
onchange        script   Script runs when the element changes
onsubmit        script   Script runs when the form is submitted
onreset            script   Script runs when the form is reset
onselect           script   Script runs when the element is selected
onblur              script  Script runs when the element loses focus
onfocus           script   Script runs when the element gets focus
onkeydown    script   Script runs when key is pressed
onkeypress     script   Script runs when key is pressed and released
onkeyup         script   Script runs when key is released
onclick                          script Script runs when a mouse click
ondblclick       script   Script runs when a mouse double-click
onmousedown            script   Script runs when mouse button is pressed
onmousemove            script   Script runs when mouse pointer moves
onmouseout    script   Script runs when mouse pointer moves out of an element
onmouseover  script   Script runs when mouse pointer moves over an element
onmouseup     script   Script runs when mouse button is released

Monday 30 December 2013

Maintaining Synchronization Point


Synchronization Point instructs QTP to pause until an object property achieves a specific value.
We use “Synchronization Point” to maintain the time coordination between testing process and our application process.
Basically there are 4 ways

 “sync” is more dynamic than wait. In wait we have to wait for specified time even if the process has been completed in less time but with “sync” the page will move on after completion of a process. It is very good to apply but unfortunately we can’t apply it in desktop application.
Example:
 Browser("Google").Page("TV9 Network, Telugu").Sync


This statement instruct QTP to stay (waits) on the same page for specified 'n' seconds even though process has occurred in less than n seconds
we can apply “wait()” by editing the code manually in expert view after a test is recorded.
Example:
For Web Application: wait(10) ….. it will stay on the same page for 10 seconds
For Desktop Application: same as above

3) Wait Property
Syntax: Browser("Browser").Page("Page").WebElement("WebElement").WaitProperty "property name", property value, timeout(time in milliseconds)
For Desktop:
Window("Window").WinObject("Window Object").WaitProperty property name", property value, timeout(time in miliseconds)
 Here wait property contains 3 things as you can see above, the Property Name list contains the test object properties associated with the object. Select the property which you want to use for the Synchronization Point.
Next comes the Property value for which QTP should wait before continuing to the next step in the test.
Then enter the Synchronization Point timeout (in milliseconds) after which QTP should continue to the next step, even if the specified property value was not achieved or if the specified value was achieved in less time even then it will wait for the specified milliseconds.
How to Apply: Synchronization point can be added while recording without writing any code from “Insert  Synchronization point” it will open your web page,
ü  here click the object  “object selection – Synchronization Point”
ü  pop up opens here
ü  select the object
ü  after clicking on any object “Add Synchronization Point” pop up opens
ü  Here select property name, enter property value and timeout.
We can also add wait property by editing in expert view after a test is recorded.
Example:
For web Application: Browser("Google").Page("Tv9 Network, Telugu").WebElement("WebElement").WaitProperty "abs_x", page, 50000
For Desktop Application: Window("Flight Reservation").WinObject("Insert Done").WaitProperty "text", text,30000

NOTE: There is a mistake done by beginners is to apply and try to find synchronization point after stopping the recording.

You can enter Exist and/or Wait statements to instruct QuickTest to wait for a window to open or an object to appear. Exist statements return a boolean value indicating whether or not an object currently exists. Wait statements instruct QuickTest to wait a specified amount of time before proceeding to the next step. You can combine these statements within a loop to instruct QuickTest to wait until the object exists before continuing with the test.
For example, the following statements instruct QuickTest to wait up to 20 seconds for the Flights Table dialog box to open.
x=Window("Flight Reservation").Dialog("Flights Table").Exist
counter=1
While Not x
Wait (2)
x=Window("Flight Reservation").Dialog("Flights Table").Exist
Counter=counter+1
If counter=10 then
x=True
End if
Wend

Wednesday 25 December 2013

Events in JavaScript

JavaScript's interaction with HTML is handled through events that occur when the user or browser manipulates a page.

When the page loads, that is an event. When the user clicks a button, that click, too, is an event. Another example of events are like pressing any key, closing window, resizing window etc.

Developers can use these events to execute JavaScript coded responses, which cause buttons to close windows, messages to be displayed to users, data to be validated, and virtually any other type of response imaginable to occur.

Events are a part of the Document Object Model (DOM) Level 3 and every HTML element have a certain set of events which can trigger JavaScript Code.
Without events there are no scripts. Take a look at any web page with JavaScript in it: in nearly all cases there will be an event that triggers the script. The reason is very simple. JavaScript is meant to add interactivity to your pages: the user does something and the page reacts.


Here we will see few examples to understand a relation between Event and JavaScript:
Example
Mouse Over Me and Click Me
Reacting to Events
A JavaScript can be executed when an event occurs, like when a user clicks on an HTML element.
To execute code when a user clicks on an element, add JavaScript code to an HTML event attribute:
onclick=JavaScript
Examples of HTML events:


HTML Event Attributes
To assign events to HTML elements you can use event attributes.

Example

Assign an onclick event to a button element:

<button onclick="displayDate()">Try it</button>

In the example above, a function named displayDate will be executed when the button is clicked.

The onload and onunload events are triggered when the user enters or leaves the page.

The onload event can be used to check the visitor's browser type and browser version, and load the proper version of the web page based on the information.

The onload and onunload events can be used to deal with cookies.

Example

<body onload="checkCookies()">


The onchange event are often used in combination with validation of input fields.

Below is an example of how to use the onchange. The upperCase() function will be called when a user changes the content of an input field.

Example

<input type="text" id="fname" onchange="upperCase()">

The onmouseover and onmouseout events can be used to trigger a function when the user mouses over, or out of, an HTML element.

Example

A simple onmouseover-onmouseout example:

Mouse Over Me

The onmousedown, onmouseup and onclick Events
The onmousedown, onmouseup, and onclick events are all parts of a mouse-click. First when a mouse-button is clicked, the onmousedown event is triggered, then, when the mouse-button is released, the onmouseup event is triggered, finally, when the mouse-click is completed, the onclick event is triggered.

Example

A simple onmousedown-onmouseup example:

Click Me

More Examples
onmousedown and onmouseup
Change an image when a user holds down the mouse button.

onload
Display an alert box when the page has finished loading.

onfocus
Change the background-color of an input field when it gets focus.

Mouse Events
Change the color of an element when the cursor moves over it.
Event               Value                     Description
onchange        script   Script runs when the element changes
onsubmit        script   Script runs when the form is submitted
onreset            script   Script runs when the form is reset
onselect           script   Script runs when the element is selected
onblur              script  Script runs when the element loses focus
onfocus           script   Script runs when the element gets focus
onkeydown    script   Script runs when key is pressed
onkeypress     script   Script runs when key is pressed and released
onkeyup         script   Script runs when key is released
onclick                          script Script runs when a mouse click
ondblclick       script   Script runs when a mouse double-click
onmousedown            script   Script runs when mouse button is pressed
onmousemove            script   Script runs when mouse pointer moves
onmouseout    script   Script runs when mouse pointer moves out of an element
onmouseover  script   Script runs when mouse pointer moves over an element
onmouseup     script   Script runs when mouse button is released