Synchronization - Wait|Exist|Do while|Sync
1. Wait Statement: It will wait for the specified amount of time. We can specify the time in both seconds and milliseconds. But its necessary to define time in the wait statement even it is zero otherwise it will give you an error.
Example: wait, wait () both will give errors
while wait(0), wait 0, wait 0, 10000 are valid statements.
also wait (0, 10000) will give an error and wait 0,10000 is a valid statement.
The disadvantage of wait statements is that it is not applied on a condition. If this statements is encountered by the compiler then it will wait for the specified amount of time.
2. Exist Property: A very useful property when dealing with the dynamic objects which takes time to download on the web pages.As the name suggests this property checks if a objects exist before doing operations on the object. This property can be used almost all the objects. It is used as follows:
A=Browser("").Page("").Frame("").webedit("Login").exist(09)
The above exist statement will check if the "Login" object exists on the WebPage or not. If it exist then it return True to "A: and if not then it will return False to "A". In real time we generally checks if some objects exist or not before doing some operation on it.
Example:
If Browser("JobCenter").Page("JobCenter").WebButton(strKeywordName).Exist(10) then Browser("JobCenter").Page("JobCenter").WebButton(strKeywordName).Click end if
Difference between wait and Exist Statement:
1. Execution will wait specifically for the amount specified in Wait statement. If wait(10) is define then the QTP will wait for 10 seconds. No matter what happens. So if we have a script which run 100 time then naturally it is going to increase the run time significantly.
Exist: Execution will move to next statement as soon as Exist return a True. So if Exist(10) is specified and the condition become true on the very first second then QTP will move to next statement. Thus we have directly saved the 9 second.
2. Wait is a statement while Exist is a property. By then help of wait we pause the execution for certain amount of time while Exist checks if a specified objects exists or not for the specified time. Thus we can use it in the loops and take various decisions.
Note:
1. There is no point of using the exist property when we are taking it value back in a variable. So following statement will throw Error.
B("").P("").WebEdit("").Exist - Error
B("").P("").WebEdit("").Exist(10) - Error
while following statements will work fine
2.Exist can't be used with the empty braces. It will give u error.However we can use it with the "0" in Exist which says don't wait if the objects Exist or don't exist.
a=B("").P("").WebEdit("").Exist() - Error
a=B("").P("").WebEdit("").Exist(0) - Good
3. Exist statement with Empty braces will wait for the object synchronization time i.e. if the time is not specified in the exist statement then it will take the time from object synchronization timeout.
a=B("").P("").WebEdit("").Exist - will wait for Object synchronization time by default 20 secs
a=B("").P("").WebEdit("").Exist(0) - will wait for zero secs
a=B("").P("").WebEdit("").Exist (10) - will wait for 10 secs
3. Do While: Do while loops are best suited when objects ares dynamics, objects are visible based upon state of some other objects. For example generally in application we have submit forms/submit details pages/upload attachments etc; in these cases the next page generally depends upon the size, number, connection speed & server response time. So we have a simple Do while loop here which will wait till the next page is not visible and we can specify the timeout period in the loop to; just in case something goes wrong.
Example:
Dim Counter:Counter=0
Browser("").Page("").Frame("").Link("").Click
Do
If Browser("").Page("").Frame("").WebTable(""").Exist(5) Then
If trim(Browser("").Page("").Frame("").WebTable("").getcelldata(1,1))= Then
Exit Do
end if
end if
Counter= Counter+1 'counter for tracking the no of attempts
If Counter=5 Then
Reporter.ReportEvent micFail,"", ""
ExitActionIteration
elseif Counter=2 then
Browser("").Page("").Frame("").Link("").Click ' submit upload webtable request again
end If
Loop until Counter > 5 ' exit in case webtable is not found after 5 attempts
The above loop wait for the webtable to upload after clicking on a particular link. The loop keeps on checking some specific value ( Header of the webtable) till specified amount of time. If the webtable has not appeared the script click on the link again to do submit the upload webtable request again. If the webtable is not uploaded after 5 attempts then it will report the fail and skips to the next action iteration.
4 Sync Method: We can use the Browser and Page sync methods to synchronize the Browser and Page navigation
The following sync statements will wait for the browser to complete its navigation i.e. till the browser status bar displays "Done"
Browser("").sync
The following sync statements will wait for the page to download i.e. displayed. Whenever we navigate to a page the page gets download, the page sync wait till the page is displayed
Browser("").Page("").sync
Example: wait, wait () both will give errors
while wait(0), wait 0, wait 0, 10000 are valid statements.
also wait (0, 10000) will give an error and wait 0,10000 is a valid statement.
The disadvantage of wait statements is that it is not applied on a condition. If this statements is encountered by the compiler then it will wait for the specified amount of time.
2. Exist Property: A very useful property when dealing with the dynamic objects which takes time to download on the web pages.As the name suggests this property checks if a objects exist before doing operations on the object. This property can be used almost all the objects. It is used as follows:
A=Browser("").Page("").Frame("").webedit("Login").exist(09)
The above exist statement will check if the "Login" object exists on the WebPage or not. If it exist then it return True to "A: and if not then it will return False to "A". In real time we generally checks if some objects exist or not before doing some operation on it.
Example:
If Browser("JobCenter").Page("JobCenter").WebButton(strKeywordName).Exist(10) then Browser("JobCenter").Page("JobCenter").WebButton(strKeywordName).Click end if
Difference between wait and Exist Statement:
1. Execution will wait specifically for the amount specified in Wait statement. If wait(10) is define then the QTP will wait for 10 seconds. No matter what happens. So if we have a script which run 100 time then naturally it is going to increase the run time significantly.
Exist: Execution will move to next statement as soon as Exist return a True. So if Exist(10) is specified and the condition become true on the very first second then QTP will move to next statement. Thus we have directly saved the 9 second.
2. Wait is a statement while Exist is a property. By then help of wait we pause the execution for certain amount of time while Exist checks if a specified objects exists or not for the specified time. Thus we can use it in the loops and take various decisions.
Note:
1. There is no point of using the exist property when we are taking it value back in a variable. So following statement will throw Error.
B("").P("").WebEdit("").Exist - Error
B("").P("").WebEdit("").Exist(10) - Error
while following statements will work fine
a=B("").P("").WebEdit("").Exist - Good
a= B("").P("").WebEdit("").Exist(10) - Good
2.Exist can't be used with the empty braces. It will give u error.However we can use it with the "0" in Exist which says don't wait if the objects Exist or don't exist.
a=B("").P("").WebEdit("").Exist() - Error
a=B("").P("").WebEdit("").Exist(0) - Good
3. Exist statement with Empty braces will wait for the object synchronization time i.e. if the time is not specified in the exist statement then it will take the time from object synchronization timeout.
a=B("").P("").WebEdit("").Exist - will wait for Object synchronization time by default 20 secs
a=B("").P("").WebEdit("").Exist(0) - will wait for zero secs
a=B("").P("").WebEdit("").Exist (10) - will wait for 10 secs
3. Do While: Do while loops are best suited when objects ares dynamics, objects are visible based upon state of some other objects. For example generally in application we have submit forms/submit details pages/upload attachments etc; in these cases the next page generally depends upon the size, number, connection speed & server response time. So we have a simple Do while loop here which will wait till the next page is not visible and we can specify the timeout period in the loop to; just in case something goes wrong.
Example:
Dim Counter:Counter=0
Browser("").Page("").Frame("").Link("").Click
Do
If Browser("").Page("").Frame("").WebTable(""").Exist(5) Then
If trim(Browser("").Page("").Frame("").WebTable("").getcelldata(1,1))=
Exit Do
end if
end if
Counter= Counter+1 'counter for tracking the no of attempts
If Counter=5 Then
Reporter.ReportEvent micFail,"", ""
ExitActionIteration
elseif Counter=2 then
Browser("").Page("").Frame("").Link("").Click ' submit upload webtable request again
end If
Loop until Counter > 5 ' exit in case webtable is not found after 5 attempts
The above loop wait for the webtable to upload after clicking on a particular link. The loop keeps on checking some specific value ( Header of the webtable) till specified amount of time. If the webtable has not appeared the script click on the link again to do submit the upload webtable request again. If the webtable is not uploaded after 5 attempts then it will report the fail and skips to the next action iteration.
4 Sync Method: We can use the Browser and Page sync methods to synchronize the Browser and Page navigation
The following sync statements will wait for the browser to complete its navigation i.e. till the browser status bar displays "Done"
Browser("").sync
The following sync statements will wait for the page to download i.e. displayed. Whenever we navigate to a page the page gets download, the page sync wait till the page is displayed
Browser("").Page("").sync
Comments
Post a Comment