java - What Exception to throw when a web element is found in Selenium Automation -
currently, working on 'selenium wrapper library', set of methods created in order perform automation on particular web portal. these methods includes debug logging , other logic, , called within test classes.
i've created new method check whether table cell (tag name 'td'), present inside table row (tag name 'tr'). below created method:
public void checkentryisnotintable(string elementtablename, byelementtable, string checkvisibletext1) { // rows table, using tagname <tr> list<webelement> tablerows = getelements(elementtablename, byelementtable); debug.log.info("\ttotal of [" + tablerows.size() + "] rows found in table [" + elementtablename + "]" ); int rowcount = 0; boolean isnotfound = false; findcell: for(webelement row : tablerows) { // cells table, using tagname <td> list<webelement> rowcells = row.findelements(by.tagname("td")); if(rowcells == null) { debug.log.error("\tno elements found tagname [td] in element [" + byelementtable + "]"); } else { debug.log.info("\trow [" + rowcount++ + "] contains [" + rowcells.size() + "] cells"); } // check visible text in cell, , if found, break loop. for(webelement cell : rowcells) { waitfor(2.0); if(!cell.gettext().contains(checkvisibletext1)){ debug.log.info("\t[" + checkvisibletext1 + "] not found in cell."); isnotfound = true; break findcell; } } } // kind of exception should throw here!! if(isnotfound) { debug.log.error("\t[" + checkvisibletext1 + "] found in 1 cell."); throw new nosuchelementexception(checkvisibletext1); } }
at stage, when 'checkvisibletext1' found, need throw new exception. however, method, exception 'nosuchelementexception' not reflect proper exception, since element found - , should not found.
what kind of exception think proper reflect exception versus functionality of method?
thanks lot.
ps. feel free suggest code improvements :)
you should check available exceptions and:
if have appropriate exception matches error use it, else create/use custom exception or create/use general exception custom message depending on method.
always try output exception message helpful you.
in case, assuming checkvisibletext1 text looking for, using nosuchelementexception text parameter not ok.
you should have exception elementdoesnotcontainstextexception message saying "\t element [" + elementtablename+ "] not contain text [" + checkvisibletext1 + "]."
try use appropriate exception when possible or use general exception appropriate message.
my opinion have noise/logs in method, logs , complex functionality has. methods should have exceptions you shouldn't need many logs.
some solutions: 1. text table , check if text there 2. create selector based on text xpath , check selector 3. check text in specific selector
create reusable methods like: theelementcontainstext, theelementdoesnotcontainstext
reuse as possible example above method(s) use other methods that: find element, element text
Comments
Post a Comment