c# - Selenium find element -


so created 2 generic functions findelement , findelements:

public class find {     public static iwebelement element(iwebdriver driver, func<iwebdriver, iwebelement> expectedcondtions,          locator, iwebelement finder = null, int timeoutinseconds = 120)     {         webdriverwait webdriverwait = createwebdriverwait(driver, timeoutinseconds);         webdriverwait.until(expectedcondtions);          if (finder != null)             return finder.findelement(locator);         return driver.findelement(locator);     }      public static readonlycollection<iwebelement> elements(iwebdriver driver, func<iwebdriver, readonlycollection<iwebelement>> expectedcondtions,          locator, iwebelement finder = null, int timeoutinseconds = 120)     {         webdriverwait webdriverwait = createwebdriverwait(driver, timeoutinseconds);         webdriverwait.until(expectedcondtions);          if (finder == null)             return driver.findelements(locator);         return finder.findelements(locator);      }      private static webdriverwait createwebdriverwait(iwebdriver driver, int timeoutinseconds)     {         webdriverwait webdriverwait = new webdriverwait(driver, timespan.fromseconds(timeoutinseconds));         webdriverwait.ignoreexceptiontypes(typeof(nosuchelementexception));         return webdriverwait;     } } 

usage:

iwebelement element =     find.element(     driver,     expectedconditions.elementisvisible(by.cssselector("bla bla")),     by.cssselector("bla bla")); 

as can see send locator twice function question there way send once ?

if understand intent write applicable functions make code cleaner. thing in concept think in case doesn't achieve you've hoped. see number of people wanting things this. want create wrapper around simple methods provided selenium in end haven't simplified code, have made more complicated, added layer of stuff in call stack, potentially introduced bugs, , created proprietary api uses code base have learn instead of using basic selenium commands. when every element find goes through single function, bugs or intermittency issues introduced function seen in every single script in suite.

a simple comparison:

using find method

iwebelement e = find.element(driver, expectedconditions.elementtobeclickable(by.cssselector("#checkboxes > input")), by.cssselector("#checkboxes > input"), null, 10); 

the selenium way

iwebelement e = new webdriverwait(driver, timespan.fromseconds(10)).until(expectedconditions.elementtobeclickable(by.cssselector("#checkboxes > input"))); 

using methods isn't cleaner in end. go further in selenium-only way , reuse wait makes code cleaner.

webdriverwait wait = new webdriverwait(driver, timespan.fromseconds(10)); iwebelement e = wait.until(expectedconditions.elementtobeclickable(by.cssselector("#checkboxes > input"))); iwebelement e2 = wait.until(expectedconditions.elementtobeclickable(by.id("checkboxes"))); 

and because until() returns element, can chain actions .click()

wait.until(expectedconditions.elementtobeclickable(by.cssselector("#checkboxes > input"))).click(); 

if use page object model (which, in general, should), store locators , use them below makes code cleaner.

wait.until(expectedconditions.elementtobeclickable(checkboxlocator)).click(); 

having said that, rewrite these this

public class find {     public static iwebelement element(webdriverwait wait, func<iwebdriver, iwebelement> expectedcondition)     {         return wait.until(expectedcondition);     }     public static ireadonlycollection<iwebelement> elements(webdriverwait wait, func<iwebdriver, ireadonlycollection<iwebelement>> expectedcondition)     {         return wait.until(expectedcondition);     } } 

the until() method returns found element(s), can return return saves having find things twice , eliminates need sending locator twice.

i removed webdriverwait() creating in methods because should reusing single instance. don't know don't need 10 different wait times, use maybe couple. declare in test script , pass them around.

i removed finder element because shouldn't needed. can create single locator using css selectors finds child elements.

in createwebdriverwait(), don't need ignore nosuchelementexception, it's built in. so, function can reduced to

private static webdriverwait createwebdriverwait(iwebdriver driver, int timeoutinseconds) {     return new webdriverwait(driver, timespan.fromseconds(timeoutinseconds)); } 

which @ point have ask why find.createwebdriverwait() better new webdriverwait()? gaining writing separate method? having separate method create webdriverwait, creating new instance each time if timeout 10s, true in element() , elements(). so, omitted code.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -