Search code examples
vbscriptqtphp-uft

Type mismatch when calling a function in qtp


I am using QTP 11.5 for automating a web application.I am trying to call an action in qtp through driverscript as below:

RFSTestPath = "D:\vf74\D Drive\RFS Automation\"

LoadAndRunAction RFStestPath & LogInApplication,"Action1",oneIteration

Inside the LogInApplication(Action1) am calling a login function as:

Call fncLogInApplication(strURL,strUsesrName,strPasssword)

Definition of fncLogInApplication is written in fncLogInApplication.vbs When I associate the fncLogInApplication.vbs file to driverscript, am able to execute my code without any errors. But when I de-associate .vbs file from driverscript and associate it to LogInApplication test am getting "Type mismatch: 'fncLogInApplication'"

Can anyone help me in the association please. I want fncLogInApplication to be executed when I associate to LogInApplication not to the main driverscript.

Please comment back if you require any more info


Solution

  • There is only one set of associated libraries that is active at any one time: That is always the outermost test's one.

    This means if test A calls test B, test B will be executed with the libraries loaded based upon test A´s associated libraries list, not B's.

    This also means that if B depends on a library, and B associated this library, but is called from test A (which does not associated this library), then B will fail to call (locate) the function since the associated libraries of B are never loaded (only those from A are). (As would A, naturally.).

    If you are still interested: "Type mismatch" is QTPs (or VBScript´s) poor way of telling you: "The function called is not known, so I bet you instead meant an array variable dereference, and the variable you specified is equal to empty, so it is not an array, and thus cannot be dereferenced as an array variable, which is what I call a 'type mismatch'." This reasoning is valid, considering the syntax tree of VB/VBScript: Function calls and array variable dereferences cannot be formally differentiated. Syntactically, they are very similar, or identical in most cases. So be prepared to handle "Type mismatch" like the "Unknown function referenced" message that VB/VBScript never display when creating VBScript code.

    You can, however, load the library you want in test B´s code (for example, using LoadFunctionLibrary), but this still allows A to call functions from that library once B loaded it and returned from A´s call. This, and all the possible variations of this procedure, however, have side-effects to aspects like debugging, forward references and visibility of global variables, so I would recommend against it.

    Additional notes:

    • There is no good reason to use CALL. Just call the sub or function.
    • If you call a function and use the result it returns, you must include the arguments in parantheses.
    • If you call a sub (or a function, and don´t use the result it returns), you must not include the arguments in parantheses. If the sub or function accepts only one argument, it might look like you are allowed to put it in parantheses, but this is not true. In this case, the argument is simply treated like a term in parantheses.
    • The argument "bracketing" aspects just listed can create very nasty bugs, especially if the argument is byRef, also due (but not limited) to the fact that VBScripts unfortunately allows you to pass values for a byRef argument (where a variable parameter is expected), so it is generally a good idea to put paranthesis only where it belongs (i.e. where absolutely needed).