18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

Modularization of the Test Scripts

n_narayanann_narayanan Members
edited November -1 in Sahi - Open Source
Hi

I was trying to make a test suite by modularizing. Here is the summary

I have created 3 test scripts

1) MainScript.sah
2) Login.sah
3) IndustryMasterAdd.sah (Add a record to one master screen)

Here is the name and script

MainScript.sah
===========
_include("Login.sah");
_setGlobal("LoginSuccess","0"); //Set to false initially. This will set to true after successful login
_setGlobal("productname","My Application v3.9.5.0");
Login("admin","admine4#",_getGlobal("productname"));
if (_getGlobal("LoginSuccess")=="1")
{
_include("IndustryMasterAdd.sah");
IndustryMasterAdd('ABCIND','ABC INDUSTRIES LIMITED');
}

In the above script I am using LoginSuccess as a global variable. Once a login is successful I will set 1 to this variable
Also since the product version keep changing I want to use a global variable called productname and use it in the other scripts whereever required

Login.sah
========

function Login($username="admin",$userpwd="admine4#",$pname="My App v3.9.5.0")
{
//Script by Narayanan Neelakantan 07-Jul-2009
//The below 4 lines to test login screen is visible
_popup($pname)._assertExists(_div("User Id"));
_popup($pname)._assertContainsText("User Id", _div("User Id"));
_popup($pname)._assertExists(_div("Password"));
_popup($pname)._assertContainsText("Password", _div("Password"));
//The above 4 lines to test login screen is visible
_popup($pname)._setValue(_textbox("loginName"), $username);
_popup($pname)._setValue(_password("password"), $userpwd);
_popup($pname)._click(_link("Login"));
_popup($pname)._assertExists(_link("Main page")); // If login is success then the main page comes
if (_popup($pname)._assertExists(_link("Main page")))
{
_log("Login Success","success");
_setGlobal("LoginSuccess","1");
}
else
{
_log("Login Failure","Failure");
_setGlobal("LoginSuccess","0");
}
}

IndustryMasterAdd.sah
=================

function IndustryMasterAdd($sname, $lname)
{
_popup(_getGlobal("productname"))._click(_div("el2"));
_popup(_getGlobal("productname"))._click(_div("el75"));
_popup(_getGlobal("productname"))._click(_link("Add"));
_popup(_getGlobal("productname"))._setValue(_textbox("txtIndustryName"), $sname);
_popup(_getGlobal("productname"))._setValue(_textbox("txtIndustryDesc"), $lname);
_popup(_getGlobal("productname"))._click(_link("Save"));
_popup(_getGlobal("productname"))._assertExists(_cell("Add Delete Back"));
_log("Industry Addition Success","success");
}

Now when I play the MainScript.sah, I am getting the following message

--Stopped Playback: SUCCESS--

What should be the problem?

The following is the output of the log file
Test Total Steps Failures Errors Success Rate

MainScript.sah 0 0 0 100%


Starting script
Stopping script


I would appreciate if you can help me.

Regards
Narayanan

Regards
Narayanan

Comments

  • narayannarayan Administrators
    Hi Narayanan,

    Could you change
    function Login($username="admin",$userpwd="admine4#",$pname="My App v3.9.5.0")
    to
    function Login($username, $userpwd, $pname)
    and try again.

    Regards,
    Narayan
  • Hi Narayan,

    Yes I did try this, Now I am getting the following error.

    Error in script: _setGlobal("LoginSuccess","0");
    Logs may have details.

    I forgot to mention I am using the stable version V2 Build: 2009-05-21

    Regards
    Narayanan
  • narayannarayan Administrators
    And what do you see in the logs?
  • This is what is shown in the log file

    Test Total Steps Failures Errors Success Rate
    MainScript.sah 1 0 1 0%


    Starting script
    JSERROR [object Object] at Jul 8, 2009 9:51:23 AM
    Stopping script
  • narayannarayan Administrators
    You can click on that JSERROR line and get more details. Do you see anything?
  • narayannarayan Administrators
    Making a few changes to the scripts. You do not need _getGlobal, _setGlobal like you have used here.
    
    // MainScript.sah
    
    _include("Login.sah");
    _include("IndustryMasterAdd.sah");
    
    var $LoginSuccess = 0; //Set to false initially. This will set to true after successful login
    var $productname = "My Application v3.9.5.0";
    Login("admin", "admine4#", $productname);
    if ($LoginSuccess == 1)
    {
        IndustryMasterAdd('ABCIND','ABC INDUSTRIES LIMITED');
    }
    
    
    // Login.sah
    
    function Login($username, $userpwd, $pname)
    {
        //Script by Narayanan Neelakantan 07-Jul-2009
        //The below 4 lines to test login screen is visible
        _popup($pname)._assertExists(_div("User Id"));
        _popup($pname)._assertContainsText("User Id", _div("User Id"));
        _popup($pname)._assertExists(_div("Password"));
        _popup($pname)._assertContainsText("Password", _div("Password"));
        //The above 4 lines to test login screen is visible
        _popup($pname)._setValue(_textbox("loginName"), $username);
        _popup($pname)._setValue(_password("password"), $userpwd);
        _popup($pname)._click(_link("Login"));
        _popup($pname)._assertExists(_link("Main page")); // If login is success then the main page comes
    
    
        // This is the only tricky part. We need to evaluate the condition in the popup and 
        // then pass it to the script. For non-popup windows you could have used: 
        // if (_condition(_link("Main page") != null)) {}
    
        _popup($pname)._set($exists, (_link("Main page") != null))
        if ($exists)
        {
            _log("Login Success","success");
            $LoginSuccess = 1;
        }
        else
        {
            _log("Login Failure","Failure");
            $LoginSuccess = 0;
        }
    }
    

    
    // IndustryMasterAdd.sah
    
    function IndustryMasterAdd($sname, $lname)
    {
        _popup($productname)._click(_div("el2"));
        _popup($productname)._click(_div("el75"));
        _popup($productname)._click(_link("Add"));
        _popup($productname)._setValue(_textbox("txtIndustryName"), $sname);
        _popup($productname)._setValue(_textbox("txtIndustryDesc"), $lname);
        _popup($productname)._click(_link("Save"));
        _popup($productname)._assertExists(_cell("Add Delete Back"));
        _log("Industry Addition Success","success");
    }
    

    Let me know what you find.

    Regards,
    Narayan
  • I tried to remove the _setGlobal etc.. and tried to run, I am getting the following error

    function main()
    2 {
    3 _include("KF_Login.sah");
    4 _include("IndustryMasterAdd.sah");
    5 _log("Start Script","info");
    6 Login("loranne","e4#","KASTLE Factoring v3.9.5.0");
    7 }
    8 main();

    Then I removed the _log("Start Script","info"); and ran it again,

    I am getting the following error

    --Stopped Playback: FAILURE--

    i checked the log and getting the following error

    Test Total Steps Failures Errors Success Rate
    MainScript.sah 1 0 1 0%


    Starting script
    JSERROR TypeError: Cannot call method "_assertContainsText" of undefined at Jul 8, 2009 10:08:18 AM
    Stopping script

    On clicking the error

    1 function main()
    2 {
    3 _include("KF_Login.sah");
    4 _include("IndustryMasterAdd.sah");
    5 Login("loranne","e4#","KASTLE Factoring v3.9.5.0");
    6 }
    7 main();

    main() is highlighted
  • narayannarayan Administrators
    OK, I am looking at this.
    One point to note: do not use _include inside if conditions or other functions if the included file itself has functions. Make your includes at the top and invoke necessary functions when needed.

    You can email me at narayan@sahi.co.in to get this fixed.
  • Excellent! I have done the changes are per your instruction and it is logging into the application and getting the following error

    Test Total Steps Failures Errors Success Rate
    MainScript.sah 4 0 1 75%


    Starting script
    _popup("KASTLE Factoring v3.9.5.0")._setValue(_textbox("loginName"), "loranne"); at Jul 8, 2009 11:11:36 AM
    _popup("KASTLE Factoring v3.9.5.0")._setValue(_password("password"), "e4#"); at Jul 8, 2009 11:11:37 AM
    _popup("KASTLE Factoring v3.9.5.0")._click(_link("Login")); at Jul 8, 2009 11:11:37 AM
    _popup("KASTLE Factoring v3.9.5.0")._set(undefined, (_link("Main page") != null))
    TypeError: Object expected
    No trace available Click for browser script [object Object] at Jul 8, 2009 11:11:52 AM
    Stopping script

    When I clicked on the error I get the following,

    1 function Login($username, $userpwd, $pname)
    2 {
    3 //Script by Narayanan Neelakantan 07-Jul-2009
    4 _popup($pname)._setValue(_textbox("loginName"), $username);
    5 _popup($pname)._setValue(_password("password"), $userpwd);
    6 _popup($pname)._click(_link("Login"));
    7
    8 // This is the only tricky part. We need to evaluate the condition in the popup and
    9 // then pass it to the script. For non-popup windows you could have used:
    10 // if (_condition(_link("Main page") != null)) {}
    11 var $exists;
    12 _popup($pname)._set($exists, (_link("Main page") != null))
    13 if ($exists)
    14 {
    15 _log("Login Success","success");
    16 $LoginSuccess = 1;
    17 }
    18 else
    19 {
    20 _log("Login Failure","Failure");
    21 $LoginSuccess = 0;
    22 }
    23
    24 }

    The line number 12 is highlighted now. ( I also defined the variable var $exists;)
  • narayannarayan Administrators
    Hi Narayanan,

    Thanks for pointing that out. _set has a bug when used with _popup and so it is failing. I will fix it in a coming build.
    Meanwhile, you can use this

    12 _popup($pname)._setGlobal("exists", (_link("Main page") != null)) ;
    13 if (_getGlobal("exists")){

    Regards,
    Narayan
  • Hi Narayan,

    No luck. I have done the changes and here is the output in the controller

    _popup("KASTLE Factoring v3.9.5.0")._setValue(_textbox("loginName"), "admin");
    _popup("KASTLE Factoring v3.9.5.0")._setValue(_password("password"), "e4#");
    _popup("KASTLE Factoring v3.9.5.0")._click(_link("Login"));
    _popup("KASTLE Factoring v3.9.5.0")._setGlobal("exists", (_link("Main page") != null)) ;
    Error in script: _log("Login Success","success");
    Logs may have details.

    Test Total Steps Failures Errors Success Rate
    MainScript.sah 5 0 1 80%


    Starting script
    _popup("KASTLE Factoring v3.9.5.0")._setValue(_textbox("loginName"), "admin"); at Jul 8, 2009 1:24:31 PM
    _popup("KASTLE Factoring v3.9.5.0")._setValue(_password("password"), "e4#"); at Jul 8, 2009 1:24:31 PM
    _popup("KASTLE Factoring v3.9.5.0")._click(_link("Login")); at Jul 8, 2009 1:24:31 PM
    _popup("KASTLE Factoring v3.9.5.0")._setGlobal("exists", (_link("Main page") != null)) ; at Jul 8, 2009 1:24:39 PM
    JSERROR [object Object] at Jul 8, 2009 1:29:42 PM
    Stopping script

    On clicking JSERROR it takes me to the script but nothing is highlighted.

    But the entire script worked perfected after I removed the following from KF_Login.sah

    _log("Login Success","success");

    Now for the benefit of everyone I will post the modified working script in my next post here.

    Regards
    Narayanan
  • narayannarayan Administrators
    Hey,

    Do you have 1 base window and 1 popup window? And are they both open while the tests are running?

    Just try with

    _popup($pname)._log("Login Success","success");

    It feels to me that when we call _log("Login Success","success"), Sahi will try to execute it in the base window but the base window is somehow not accessible, and so the script fails there. Try to send the log statement to the popup via _popup($pname)._log as mentioned above and check.

    Regards,
    Narayan
  • Yes, you are right. It worked now. Here is the working version

    //MainScript.sah
    //===========

    function main()
    {
    _include("KF_Login.sah");
    _include("IndustryMasterAdd.sah");
    var $LoginSuccess = 0; //Set to false initially. This will set to true after successful login
    var $productname = "KASTLE Factoring v3.9.5.0";
    Login("loranne","e4#","KASTLE Factoring v3.9.5.0");
    if ($LoginSuccess == 1)
    {
    IndustryMasterAdd('ABCIND','ABC INDUSTRIES LIMITED');
    }

    }
    main();
    //KF_Login.sah
    //========

    function Login($username, $userpwd, $pname)
    {
    //Script by Narayanan Neelakantan 07-Jul-2009
    _popup($productname)._setValue(_textbox("loginName"), $username);
    _popup($productname)._setValue(_password("password"), $userpwd);
    _popup($productname)._click(_link("Login"));

    // This is the only tricky part. We need to evaluate the condition in the popup and
    // then pass it to the script. For non-popup windows you could have used:

    _popup($productname)._setGlobal("exists", (_link("Main page") != null)) ;

    if (_getGlobal("exists"))
    {
    $LoginSuccess = 1;
    _popup($pname)._log("Login Success","success");
    }
    else
    {
    $LoginSuccess = 0;
    _popup($pname)._log("Login Failed","failure");
    }
    }


    //IndustryMasterAdd.sah
    //=================

    function IndustryMasterAdd($sname, $lname)
    {
    _popup($productname)._click(_div("el2"));
    _popup($productname)._click(_div("el75"));
    _popup($productname)._click(_link("Add"));
    _popup($productname)._setValue(_textbox("txtIndustryName"), $sname);
    _popup($productname)._setValue(_textbox("txtIndustryDesc"), $lname);
    _popup($productname)._click(_link("Save"));
    _popup($productname)._assertExists(_cell("Add Delete Back"));
    }


    Thanks a lot Narayan for helping me out.

    Regards
    Narayanan
  • narayannarayan Administrators
    Btw, why is everything in a popup? Is your application designed to work on a popup only?
  • It is in the same main window but not a seperate popup.
Sign In or Register to comment.