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.

how can we put _getText value in a variable?

edited November -1 in Sahi - Open Source
I tried using _getText value to store a string obtained from UI in a var but returned

here's is the code -
var $isMFAEnable = "";
$isMFAEnable = _getText(_link("Where do I enter my password?"));
PrintOnCommandPrompt($isMFAEnable);

it printed [object Object]

i also tried _set($isEnable, _getText(_link("Where do I enter my password?")));
it works fine; but if the link is not present the scripts stops there.
Where as i want the script to execute into the 'else' statement 'if' the above link is not found

Can someone give me a solution?

Comments

  • dpdp Members
    You certainly have to use _set to store the value of a UI element to a variable. And to handle the error condition, you could enclose it in a try...catch block as below.

    var $isEnable;
    try{
    _set($isEnable, _getText(_link("Where do I enter my password?")));
    $isEnable = true;
    }catch(e){
    $isEnable = false;
    }
  • Hey...it did work...sweet!!!
    thanks a ton :-)
  • narayannarayan Administrators
    First, thanks dp for answering a lot on the forums. Your contribution is deeply appreciated.

    Maverick:

    That answer is technically correct, but not really a good test. You are identifying an element by a string and then verifying that that string is present in the element. Instead you should identify the span by some other property and then check whether its content is right. One good thing would be to use _span("span_id"), if that span has an id.

    Another would be to look for the text in the document body itself.
    _set($enable, _containsText(document.body, "Where do I enter my password?"));

    Let me know what you guys think.

    Regards,
    Narayan
  • dpdp Members
    That is true. I realise that I got biased towards trapping that error condition than looking at a better solution

    Thanks a lot for your input

    dp
Sign In or Register to comment.