Page 1 of 1

Validate element exist using Find()

Posted: Mon Feb 25, 2019 11:14 am
by praneet
Below is my dynamic code to verify that element present or not.
If element is not present then i want boolean return because based on boolean return i will do another operation.

Below is the code not working
var elementin = doucmentDiv.FindSingle("./table["+k+"]/tbody/tr/td[3]/div/a");
bool found = Validate.Exists(elementin);
if(found){

Re: Validate element exist using Find()

Posted: Mon Feb 25, 2019 12:23 pm
by odklizec
Hi,

And what exactly is your question? Validate.Exists returns true/false, so it does exactly what you want. In my opinion, FindSingle is not even required. In fact, it's counterproductive to use it in your code, because it will always throw an exception if element is not found. So you can easily use Validate.Exists(rxpath, searchTimeout, message, exceptionOnFail) and set exceptionOnFail as false. This will do exactly what you want.

BTW, I would suggest not to use rxpath directly in code and instead to pass the repoinfo element to code. Using rxpath directly in code makes the test suite hard to understand and maintain! Once your developers change something in SUT UI, you will have to find and fix the xpath in code, possibly at many places. Hence using central repository is most recommended (and clever) way of passing elements to code.