Test complete : ReferenceError Driver1 is not defined

243 Views Asked by At

I'm using Test complete for automation. I create class : "Common" with code :

function Read_Excel_Login(){
    var Driver1 = DDT.ExcelDriver("C:/Users/NVA/Downloads/leave.xls","login",true);
}

function Login()
{
    if( Driver1.Value(3)==2)
    {
        page = Aliases.browser.pageHrmtestSpsSymfonyWebIndexPhp2;
        page.Wait(5000);
    }
    else
    {
        page= Aliases.browser.pageHrmtestSpsSymfonyWebIndexPhp;
        page.Wait(5000);
    }

    page.contentDocument.Script.$("#txtUsername").val(Driver1.Value(0)).change();
    page.contentDocument.Script.$("#txtPassword").val(Driver1.Value(1)).change();
    page.contentDocument.Script.$("#btnLogin").click();
}

I Create other class : "Main" for login user and call class : "common" with code:

var Common = require("Common");

function Main()
{   
    Common.Read_Excel_Login();
    while(!Driver1.EOF())
    { 
        Common.Login();
        Driver1.Next();                      
    }
}

Error appear after run:

ReferenceError

Driver1 is not defined

How to fix this problem ? Thanks.

1

There are 1 best solutions below

2
On

If you put a breakpoint at the start of your code and step through it, it might help you find the issue.

The issue is that you've declared Driver1 in the function Read_Excel_Login() and then that function goes out of scope and you reference Driver1 in the function Login() where it's not defined.

I would define Driver1 in Main() and then have Read_Excel_Login() return a driver instance. Then you can pass Driver1 to Login() where it can be used.

function Read_Excel_Login()
{
    return DDT.ExcelDriver("C:/Users/NVA/Downloads/leave.xls", "login", true);
}

function Main()
{   
    var ExcelDriver = Common.Read_Excel_Login();
    while(!ExcelDriver.EOF())
    { 
        Common.Login(ExcelDriver);
        ExcelDriver.Next();
    }
}

function Login(ExcelDriver) { ... }

I also changed the name of the driver to be more specific, ExcelDriver instead of Driver1.

It's been a long time since I've done anything with TestComplete so I think this syntax is mostly correct.