Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, windows and java based applications. Get your 30 day free trial.

Discuss your Sahi Pro usage patterns, best practices, problems and solutions. Help others solve their problems and seek help from the community when needed. If you need specific support on your application, please email support @ sahipro.com

How to know whether or not a element exist on page

ziscloudziscloud Members
edited November -1 in Sahi - Open Source
Hi All,

I try to use this code to verify whether a element exist on the page:

if (_condition(_link($label) != null)) {
_click(_radio(0, _near(_link($label))));
} else {
_click(_cell(0, _near(_cell($label))));
}
}

Some times it is work fine, but some times even there is a link element, it also clicks the cell element!

I have used the sahi controller to verity _assertExists(_link("xxxx")), it return true, but in the script

_condition(_link($label) != null)

return false and click the cell element.

is it a bug of sahi, please verify!

Comments

  • narayannarayan Administrators
    If this check is done just after a particular page is loaded, try and add an assertion before this condition to make sure that you have moved to the correct page before it verifies this condition. You can also add a wait, but an assertion is better.
  • Thanks a lot for your reply!

    are you mean the code should like this:

    _assertExists(_link($label)); or _wait(10000);

    if (_condition(_link($label) != null)) {
    _click(_radio(0, _near(_link($label))));
    } else {
    _click(_cell(0, _near(_cell($label))));
    }
    }


    ??
  • _wait(10000,_link($label));
    This will wait till given amout of time or provided link was found. whichever occurs first. Assert is much better in your case.
  • narayannarayan Administrators
    Infact, since you do not know if the link will exist or not, assert for something else which will ensure that you are on the right page before you check for your link existence.
  • JohnPJohnP Members
    edited September 2014
    --
  • JohnPJohnP Members
    edited September 2014
    --
  • This should work:

    if(_assertExists(_link("clickme"))){
    _alert("it exists on the page");
    }
  • Sorry, this is how to fix the above example, which does work:
    if(_condition(_assertExists(_link("clickme")))){
    _alert("it exists on the page");
    }
  • This is the best, not adding an "assertion" in the test logs:

    if(_condition(_link("clickme"))){
    _alert("it exists on the page");
    }
Sign In or Register to comment.