multithreading - Multi-threaded C# Selenium WebDriver automation with Uris not known beforehand -
i need perform simultaneous webdrivers manipulation, uncertain how this.
what asking here is:
- what correct way achieve ?
- what reason exception getting (revealed below)
after research ended with:
1. way see people doing (and 1 ended using after playing api, before searching) loop on window handles webdriver has @ hand, , perform switch , out of window handle want process, closing when finished.
2. selenium grid not seem option fore me - wrong or intended parallel processing ? since running in single computer, of no use me.
in trying 1st option, have following scenario (a code sample available below, skipped stuff not relevant/repeat (where ever added 3 dots:
i have html page, several submit buttons, stacked.
clicking each of them open new browser/tab (interestingly enough, using chromedriver opens tabs, while firefoxdriver opens separate windows each.)
as side note: can't determine uris of each submit beforehand (they must determined javascript, , @ point, let's assume want handle knowing nothing client code.
now, after looping on submit buttons, , issuing webelement.click() on corresponding elements, tabs/windows open. code flows create list of tasks executed, 1 each new tab/window.
the problem is: since tasks depend upon same instance of webdriver switch window handles, seems need add resource sharing locks/control. uncertain whether correct, since saw no mention of locks/resource access control in searching multi-threaded web driver examples.
on other hand, if able determine tabs/windows uris beforehand, able skip automation steps needed reach point, , creating webdriver instance each thread, via navigate().gotourl()
straightforward. looks deadlock! don't see webdriver's api providing access newly opened tab/window without performing switch. , want switch if not have repeat automation steps lead me current window !
...
in case, keep getting exception:
element belongs different frame current 1 - switch containing frame use it
at
iwebelement element = cell.findelement
inside todictionary()
block.
i checked selectors returning results, in chrome's console.
foreach (webelement resultset in resultsets) resultset.click(); foreach(string windowhandle in webdriver.windowhandles.skip(1)) { datacollectiontasks.add(task.factory.startnew<list<datatable>>(obj => { list<datatable> collecteddata = new list<datatable>(); string window = obj string; if (window != null) { webdriver.switchto().window(windowhandle); list<webelement> datasets = webdriver.findelements(by.jqueryselector(utils.getappsetting("selectors.resultsetdata"))).tolist(); datatable data = null; (int = 0; < datasets.count; += 2) { data = new datatable(); data.columns.add("col1", typeof(string)); data.columns.add("col2", typeof(string)); data.columns.add("col3", typeof(string)); ///... //data set header if (i % 2 != 0) { iwebelement headerelement = datasets[i].findelement(openqa.selenium.by.cssselector(utils.getappsetting("selectors.resultsetdataheader"))); data.tablename = string.join(" ", headerelement.text.split().take(3)); } //data set records else { dictionary<string, string> cells = datasets[i] .findelements(openqa.selenium.by.cssselector(utils.getappsetting("selectors.resultsetdatacell"))) .todictionary( cell => { iwebelement element = cell.findelement(openqa.selenium.by.cssselector(utils.getappsetting("selectors.resultsetdataheadercolumn"))); return element == null ? string.empty : element.text; }, cell => { return cell == null ? string.empty : cell.text; }); string col1value, col2value, col3value; //... cells.trygetvalue("col1", out col1value); cells.trygetvalue("col2", out col2value); cells.trygetvalue("col3", out col3value); //... data.rows.add(col1value, col2value, col3value /*...*/); } } collecteddata.add(data); } webdriver.switchto().window(mainwindow); webdriver.close(); return collecteddata; }, windowhandle)); } //foreach task.waitall(datacollectiontasks.toarray()); foreach (task<list<datatable>> datacollectiontask in datacollectiontasks) { results.addrange(datacollectiontask.result); } return results;
Comments
Post a Comment