From: Casey C. <cr...@na...> - 2005-03-28 21:06:37
|
I'm not sure exactly how much I can disclose due to NDA, I'll try to be lucid. It works like this: * Login to the application (basic auth) * Navigate to their area (simple link clicking) * Create a document in their area (click on an "action link") This will open a new window with a wizard-like interface for entering data such as the file name and description; the field for the actual file. The user enters all this information and clicks "Next". This does a form submit and uploads the file to a temporary location. * On the next page of the wizard, permissions can be set. Users sets permissions however they want and clicks "OK". This does a form submit. The page that loads has it's body onload= set to a method that will close the wizard window and refresh the parent window using a window.opener.location.reload(). Along the way we are setting and using javascript global variables for flow control. (For some actions we don't want to close the wizard, we only want to refresh the parent page; sometimes we don't want to refresh the parent at all) Catching that window.opener.location.reload()'s result is what I'm trying to do. Here's the example; below are the helper function definitions: // Create our Client // I've defined my own BrowserVersion; this should be independent WebClient webClient = new WebClient(bv); webClient.setRedirectEnabled(true); // Set our authentication credentials for this client DefaultCredentialsProvider dcp = (DefaultCredentialsProvider) webClient.getCredentialsProvider(); dcp.addCredentials("billg", "billg"); // Load up a page. URL url = new URL("http://obscured"); HtmlPage page = (HtmlPage)webClient.getPage(url); // Start clicking links; Here we make sure we're in the Access Control Testing Project's Folders page. page = clickLink(page, "Home"); page = clickLink(page, "Project"); page = clickLink(page, "Projects List"); page = clickLink(page, "Access Control Testing"); page = clickLink(page, "Folders"); // for debugging; write out the result to disk in a file called page1. writePage("page1", page); // Open up a new window for the Create Document wizard HtmlPage wizard = clickLink(page, "Create Document"); // Grab the form and set some values HtmlForm mainform = wizard.getFormByName("mainform"); setTextField(mainform, "name", "My first file"); HtmlFileInput fileField = (HtmlFileInput)mainform.getInputByName("file"); // the fileField's value should be set to a path to a file. fileField.setValueAttribute("Hello.java"); // Done with the first step; move on to the next wizard = clickButton(mainform, "nextButton"); mainform = wizard.getFormByName("mainform"); // We don't care about the additional information step; so we just move on. wizard = clickButton(mainform, "nextButton"); mainform = wizard.getFormByName("mainform"); // for debugging; write out the result to disk in a file called permWizard writePage("permWizard", wizard); // now lets give members read access only. HtmlRadioButtonInput memberRead = (HtmlRadioButtonInput) wizard.getHtmlElementById("PJLAll_ASC32_Members__read"); memberRead.click(); // we're done! wizard = clickOK(mainform); writePage("finalWizard", wizard); // At this point I'm not sure what object has the refreshed value; as page doesn't seem to. // refresh doesn't seem to automatically work; so we have to refresh the page manually. page = clickLink(page, "Folders"); writePage("page2", page); Helper functions follow: /** * Convenience method for click a link on a page * * @param p The <code>HtmlPage</code> the link is on * @param linkText the literal text of the link (What you see in the Web Browser) * @return a new <code>HtmlPage</code> result * @exception Exception if an error occurs */ public static HtmlPage clickLink(HtmlPage p, String linkText) throws Exception { HtmlAnchor link = p.getFirstAnchorByText(linkText); return (HtmlPage)link.click(); } /* Convenience method for setting the value of a text field */ public static void setTextField(HtmlForm form, String fieldName, String value) throws Exception { HtmlTextInput textField = (HtmlTextInput)form.getInputByName(fieldName); textField.setValueAttribute(value); } /* Convenience method for clicking a button on a wizard */ /* buttonName is the value of the field "name" on the "button" html object */ /* For example, most "Next" buttons are named "nextButton" */ public static HtmlPage clickButton(HtmlForm form, String buttonName) throws Exception { HtmlButtonInput button = (HtmlButtonInput) form.getInputByName(buttonName); return (HtmlPage)button.click(); } /* Convenience method for clicking the OK button on a wizard */ public static HtmlPage clickOK(HtmlForm form) throws Exception { HtmlSubmitInput ok = (HtmlSubmitInput) form.getInputByName("okButton"); return (HtmlPage)ok.click(); } /* A quick and dirty way to output most of the page. */ public static void writePage(String name, HtmlPage p) throws Exception { FileWriter fw = new FileWriter(name); fw.write(p.asXml()); fw.flush(); fw.close(); } -- Casey On Mon, Mar 28, 2005 at 12:42:52PM -0800, Brad Clarke wrote: > My application does things such as this all the time with no problems. In order to > help you I'd need to know more about what you're doing (self contained, failing test > cases being the best examples). > > Brad C > > --- Casey Crabb <cra...@na...> wrote: > > Hey all, > > > > I'm working on an application that uses pop-up wizards to perform > > actions. After these wizards are done (when you fully submit them) > > they trigger a refresh on the main browser page and close > > themselves. This doesn't seem to be automatically supported by > > htmlunit. Is there a way I can get this to work; or can someone point > > me in the correct direction in the source? > > > > -- > > Casey > > > > > ------------------------------------------------------- > SF email is sponsored by - The IT Product Guide > Read honest & candid reviews on hundreds of IT Products from real users. > Discover which products truly live up to the hype. Start reading now. > http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click > _______________________________________________ > Htmlunit-user mailing list > Htm...@li... > https://lists.sourceforge.net/lists/listinfo/htmlunit-user |