c# - Click on Save or SaveAs dialog of file download -
i have seen couple of question posted on topic none of solution worked that's why posting again.
i using watin automate testing of website. using ie 11. have download , save file, not able click save or saveas button of download window. tried filedownloadhandler of watin didn't worked. not limited watin solutions. thing can use in c# code acceptable
filedownloadhandler filedownloadhandler = new filedownloadhandler(); browser.adddialoghandler(filedownloadhandler); browser.image(find.byalt("download.csv")).ancestor("a").clicknowait(); filedownloadhandler.waituntilfiledownloaddialogishandled(40); filedownloadhandler.waituntildownloadcompleted(200);
watin not interact windows controls , in windows little. had same problem in handling windows controls had multiple versions of ie. ie 9.0 , higher have different file handling when compared <= ie 8.0 version. below works fine ie 9.0 , higher. please make sure proper references added (refer using's).
using system.threading; using system.windows.automation; using watin.core; using watin.core.native.windows; namespace testframework.util { public static class windowshelper { #region public methods /// <summary> /// download ie file. /// </summary> /// <param name="action">action can save/save as/open/cancel.</param> /// <param name="path">path file needs saved (for save function).</param> public static void downloadiefile(string action, string path = "", string regexpatterntomatch = "") { browser browser = null; if (utility.browser != null) // utility.browser watin browser instance. { if (string.isnullorempty(regexpatterntomatch)) { browser = utility.browser; } else { utility.wait(() => (browser = browser.attachto<ie>(find.byurl(new system.text.regularexpressions.regex(regexpatterntomatch)))) != null); } } else { return; } // if doesn't work try increase sleep interval or write own waituntill method thread.sleep(3000); // see information here (http://msdn.microsoft.com/en-us/library/windows/desktop/ms633515(v=vs.85).aspx) window windowmain = null; utility.wait(() => (windowmain = new window(nativemethods.getwindow(browser.hwnd, 5))).processid != 0); treewalker trw = new treewalker(condition.truecondition); automationelement mainwindow = trw.getparent(automationelement.fromhandle(browser.hwnd)); window windowdialog = null; utility.wait(() => (windowdialog = new window(nativemethods.getwindow(windowmain.hwnd, 5))).processid != 0); windowdialog.setactivate(); automationelementcollection amc = null; utility.wait(() => (amc = automationelement.fromhandle(windowdialog.hwnd).findall(treescope.children, condition.truecondition)).count > 1); foreach (automationelement element in amc) { // can use "save ", "open", ''cancel', or "close" find necessary button or write own enum if (element.current.name.equals(action)) { // if doesn't work try increase sleep interval or write own waituntil method // waituntilbuttonexsist(element,100); thread.sleep(1000); automationpattern[] pats = element.getsupportedpatterns(); // replace each if need 'save as' code bellow foreach (automationpattern pat in pats) { // '10000' button click event id if (pat.id == 10000) { invokepattern click = (invokepattern)element.getcurrentpattern(pat); click.invoke(); } } } else if (element.current.name.equals("save") && action == "save as") { automationelementcollection bmc = element.findall(treescope.children, automation.controlviewcondition); invokepattern click1 = (invokepattern)bmc[0].getcurrentpattern(automationpattern.lookupbyid(10000)); click1.invoke(); thread.sleep(1000); automationelementcollection main = mainwindow.findall(treescope.children, condition.truecondition); foreach (automationelement el in main) { if (el.current.localizedcontroltype == "menu") { // first array element 'save', second array element 'save as', third second array element 'save , open' invokepattern clickmenu = (invokepattern) el.findall(treescope.children, condition.truecondition)[1].getcurrentpattern(automationpattern.lookupbyid(10000)); clickmenu.invoke(); thread.sleep(1000); controlsavedialog(mainwindow, path); break; } } } } } /// <summary> /// control save dialog. /// </summary> /// <param name="mainwindow">main window.</param> /// <param name="path">path.</param> private static void controlsavedialog(automationelement mainwindow, string path) { // obtain save dialog var saveasdialog = mainwindow .findfirst(treescope.descendants, new propertycondition(automationelement.nameproperty, "save as")); // file name box var saveastext = saveasdialog .findfirst(treescope.descendants, new andcondition( new propertycondition(automationelement.nameproperty, "file name:"), new propertycondition(automationelement.controltypeproperty, controltype.edit))) .getcurrentpattern(valuepattern.pattern) valuepattern; // fill filename box saveastext.setvalue(path); thread.sleep(500); utility.presskey("left"); utility.presskey("left"); thread.sleep(1000); // find save button var savebutton = saveasdialog.findfirst(treescope.descendants, new andcondition( new propertycondition(automationelement.nameproperty, "save"), new propertycondition(automationelement.controltypeproperty, controltype.button))); // invoke button var pattern = savebutton.getcurrentpattern(invokepattern.pattern) invokepattern; pattern.invoke(); } #endregion } } public static class utility { public static ie browser { get; set; } // wait specified number of seconds public static void wait(int seconds) { system.threading.thread.sleep(seconds * 1000); } // wait condition evaluate true, timeout after 30 seconds public static void wait(func<bool> condition) { int count = 0; while (!condition() && count < 30) { system.threading.thread.sleep(1000); count++; } } //send tab key press browser public static void presstab() { system.windows.forms.sendkeys.sendwait("{tab}"); system.threading.thread.sleep(300); } //send specified key press browser public static void presskey(string keyname) { system.windows.forms.sendkeys.sendwait("{" + keyname.toupper() + "}"); system.threading.thread.sleep(300); } }
hope helps.
Comments
Post a Comment