java - EDITED: My code does not work the catch block during exception handling -


edited

i have changed code bit, have following function in page object.

public void kenshosearch(string searchterm) throws exception {     driver.findelement(kenshosearchbox).sendkeys(searchterm);     try {         new webdriverwait(driver, 10).until(expectedconditions.visibilityofelementlocated(kenshosearchverify));     }     catch (nosuchelementexception e) {         system.out.println("no results found");     }    } 

when run test keyword not have results, should print out "no results found" in actual throws nosuchelementexception on console along stack trace.

what doing wrong here?

there 2 ways:

1.) can catch nosuchelementexception , throw new assertexception text message.

2.) instead of catching exception should use:

list<webelement> elements = getdriver().findelements(by.xpath("your xpath")); assert.asserttrue(elements.size()>0, "no results found \"search term\" "); 

here try fill list found elements using findelements instead of findelement. if findelements doesn't find webelement doesn't throw exception. can easilly check size of list if element found.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -