QTP Automation Test Practice: "Software test automation-QTP Series Lectures 46" = = The ultimate study of JScript in QTP

Source: Internet
Author: User
Tags define object end error handling execution object model try catch

Last lecture we briefly introduced how to execute JavaScript in the next QTP, in fact, that is, the use of QTP encapsulation method to perform JavaScript, mainly to take care of the novice so a brief introduction of this piece of content, may be for some of the test experts are insignificant, But today this lecture is sure to make your eyes bright, it should be said that this course is the climax of the leading technology lectures. Polish your eyes to see how the real meaning is to execute JavaScript directly in the QTP editor and apply try catch to the actual application. Get rid of the same bug trap as the VBS.
Before that, allow me to introduce a component, which is the mshtml component that we can use to execute JS
JavaScript execution Script
Set ohtml = CreateObject ("Htmlfile")
Set Js = Ohtml.parentwindow
Js.execscript "alert (" Test ")"
Analysis: Through the above script we can very easily execute JavaScript, the results will be directly pop-up Test text box.
Results:

So can we execute the following code in QTP?
Set ohtml = CreateObject ("Htmlfile")
Set Js = Ohtml.parentwindow
Js.execscript _
Browser ("Baidu, You Know"). Page ("Baidu, You Know"). Webedit ("WD"). Set ("Iquicktest"); "
Analysis: What we're going to do now is to control the QTP object model through the above JavaScript, because that's the only way to make sense, isn't it? But when you execute the above code, you will find a very annoying thing, QTP directly threw a mistake:

Why does it appear as above, because the JavaScript script layer is in a different two space from the VBScript script layer, although the browser object can be directly controlled in the VBScript script layer. But in JavaScript, the object is empty, there is no such object, so it is wrong to throw it back, so the question now is how do we pass the test object model in the VBScript script layer directly to the JavaScript script layer, which needs to be traversed, Only the spatial distortion can do, hehe, a joke, in fact very simple, look at the following script:
The crossing of any object in the QTP
Set ohtml = CreateObject ("Htmlfile")
Set Js = Ohtml.parentwindow
The Declare Browser object
Js.execscript "var Browser;"
"Put the browser object layer
Set Js.browser = Browser
Js.execscript _
Browser ("Baidu, You Know"). Page ("Baidu, You Know"). Webedit ("WD"). Set ("Iquicktest"); "
Analysis: After executing the above script, you will find that Iquicktest was successfully entered into the Baidu text box (don't forget to add objects to the object Library first. This I do not explain), do not worry, has not played, this is only a small start, there are many heavyweight not onstage, how can this end it. So I said at the beginning of this article, to get QTP to really execute JavaScript, now it's time to get into our climax. Maybe for some of the pros I believe you should have thought of what I need to do next, to see the next script.
On Error Resume Next
Call Initjsengine
Function Initjsengine ()
The Create Htmlfile component
Set ohtml = CreateObject ("Htmlfile")
"Get the QTP automation model instance through GetObject
Set Qtapp = GetObject ("", "quicktest.application")
"Define JS, and some objects that need to be traversed.
Set JS = Ohtml.parentwindow
Js.execscript "var browser,setting,print;"
Set Js.browser = Browser
Set Js.print = Print
Set JS. Setting = Setting
"Gets the script content in the QTPACTION1 and executes
Js.execscript Qtapp. Test.actions ("Action1"). Getscript
End Function
Analysis: The above script we mainly define a function, this function is actually very simple, JS implementation of the same as before, the main is the author through the AOM Way to QTP Script Editor in the script content directly captured and through JS way to execute, This is an easy thing to think about when you're familiar with AOM's friends. Then name the file JS.QFL into the resources:

We then enter the JavaScript script directly in the QTP Script Editor:
function Testflow ()
{
var p = Browser ("Baidu, You Know"). Page ("Baidu, You Know");
P.webedit ("WD"). Set ("Iquicktest");
P.webbutton ("Baidu"). Click ();
}
Testflow ();
Effect Chart:

Then if you execute the script directly, QTP will first verify that the script conforms to the VBS syntax, and if an error pops up the error box, causing the execution to break:

As shown in the figure, qtp an invalid string, so we need to filter this type of error-handling box.
After entering test settings, select the Run option

As shown in figure, change when error occurs during Run session: option is stop run, why do you set this?
Cause Analysis: The order in which the QTP executes the test scripts is to execute the function libraries in the resources first and then execute the Script Editor.
Once the rewrite is complete, execute the script again, and you'll be amazed to see that you can successfully execute JavaScript directly in the QTP editor. Hey, don't worry, it's not over yet!!
How can the climax end so soon, try catch has not yet come out, this is the most heavyweight, the script is as follows:
function Testflow ()
{
Set timeout time to 100
Setting.item ("defaulttimeout") = 100;
Print ("********************* Javascript in Qtp-report *********************");
Error handling for QTP scripts using a try Catch statement
try{
var p = Browser ("Baidu, You Know").
Page ("Baidu, You Know");
P.webedit ("WD"). Set ("Iquicktest"); This article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20121126/34400.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.