java - HtmlUnit on Moodle: FileUpload fails despite success message -


i try upload file moodle2 container. done via yui. skip stuff disable java script in webclient (browser) "noscript" elements. after analysing these page filepicker.php of moodle called.

the page looks this.

<form action="http://demo.moodle.net/repository/filepicker.php?ctx_id=41&amp;itemid=861552155&amp;env=editor&amp;course=2&amp;maxbytes=67108864&amp;areamaxbytes=-1&amp;maxfiles=-1&amp;subdirs=1&amp;sesskey=chtrokyblc&amp;action=browse&amp;draftpath=%2f&amp;savepath=%2f&amp;repo_id=3" method="post" enctype="multipart/form-data" style="display:inline">     <label>attachment: </label><input name="repo_upload_file" type="file"><br>     <input name="action" value="upload" type="hidden"><br>     <input name="draftpath" value="/" type="hidden"><br>     <input name="savepath" value="/" type="hidden"><br>     <input name="repo_id" value="3" type="hidden"><br>     <input value="upload file" type="submit"> </form> 

complete case against demo.moodle.org (manually create folder name "test" in mooodle-course 'my first course' id=2 first):

import com.gargoylesoftware.htmlunit.webclient; import com.gargoylesoftware.htmlunit.html.htmlanchor; import com.gargoylesoftware.htmlunit.html.htmlelement; import com.gargoylesoftware.htmlunit.html.htmlfileinput; import com.gargoylesoftware.htmlunit.html.htmlform; import com.gargoylesoftware.htmlunit.html.htmlpage; import com.gargoylesoftware.htmlunit.html.htmlpasswordinput; import com.gargoylesoftware.htmlunit.html.htmlspan; import com.gargoylesoftware.htmlunit.html.htmlsubmitinput; import com.gargoylesoftware.htmlunit.html.htmltextinput; import java.util.list; import java.util.logging.level; import java.util.logging.logger;   public class moodleupload {      /** instance of webclient */     private static final webclient browser = new webclient();      /** current htmlpage */     private htmlpage currentpage;      public moodleupload(){          //intialize webclient         browser.getoptions().setjavascriptenabled(true);         browser.getoptions().setredirectenabled(true);         browser.getoptions().setthrowexceptiononscripterror(false);         browser.getoptions().setcssenabled(false);         browser.getoptions().setuseinsecuressl(true);         browser.getoptions().setthrowexceptiononfailingstatuscode(false);         browser.getcookiemanager().setcookiesenabled(true);          try{             login();                         currentpage = (htmlpage) browser.getpage("http://demo.moodle.net/course/view.php?id=2");             modifyfolder(getfolderbyname("test"));          } catch (exception e){              logger.getlogger(moodleupload.class.getname()).log(level.severe, null, e);         }      }      //perform login on moodle     private void login() throws exception{         currentpage = (htmlpage) browser.getpage("http://demo.moodle.net/");          //get login-form, fill in required fields , perform click         ((htmltextinput) currentpage.getelementbyid("login_username")).setvalueattribute("manager");         ((htmlpasswordinput) currentpage.getelementbyid("login_password")).settext("sandbox");          list<htmlsubmitinput> inputs;         inputs = (list) currentpage.getbyxpath("//form/div/input[@type='submit']");         inputs.get(0).click();     }      //finds link folder given name     private string getfolderbyname (string name) throws exception{         list<htmlspan> spans;         spans = (list) currentpage.getbyxpath("//a/span[@class='instancename' , text()='"+name+"']");           system.out.println(((htmlanchor) spans.get(0).getenclosingelement("a")).gethrefattribute());          return ((htmlanchor) spans.get(0).getenclosingelement("a")).gethrefattribute();     }      //trys upload file moodle folder     private void modifyfolder(string path) throws exception{          //disable javascript noscript elements         browser.getoptions().setjavascriptenabled(false);         currentpage = (htmlpage) browser.getpage(path);          //click 'edit settings'         list<htmlanchor> anchors;         anchors = (list) currentpage.getbyxpath("//ul/li/p[@class='tree_item leaf']/a[text()='edit settings']");         currentpage = (htmlpage) browser.getpage(anchors.get(0).gethrefattribute());          //get file uploader         list<htmlelement> up;         = (list) currentpage.getbyxpath("//noscript/div/object");                 currentpage = (htmlpage) browser.getpage(up.get(0).getattribute("data"));          //get "add file"-page         list<htmlelement> elements;         elements = (list) currentpage.getbyxpath("//div[@class='filemanager-toolbar']");         currentpage = (htmlpage) browser.getpage(((htmlanchor) elements.get(0).getfirstchild()).gethrefattribute());         elements = (list) currentpage.getbyxpath("//a[text()='upload file']");         currentpage = (htmlpage) browser.getpage(((htmlanchor) elements.get(0)).gethrefattribute());          //get file upload form         elements = (list) currentpage.getbyxpath("//form");         htmlform uploadform = (htmlform) elements.get(0);          htmlfileinput fileinput = uploadform.getinputbyname("repo_upload_file");          fileinput.setvalueattribute("file:///f:/test.xlsx");         fileinput.setcontenttype("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");          //submit         list<htmlsubmitinput> inputs;         inputs = (list) currentpage.getbyxpath("//form/input[@type='submit']");         currentpage = inputs.get(0).click();          system.out.println(currentpage.astext());       }       public static void main(string[] args){         moodleupload tl = new moodleupload();     } } 

if check current page success message moodle. file not in destinated container. tried several modifications code , action parameter of form corresponding 'documentation' of filepicker.php no avail. if fill out form in firefox, works fine. if skip setvalue-call, failure message. seems happen.

maybe associated additional hidden fields? appreciated. use javascript , yui-functionality, if feasible solution.

is strange javascript needs disabled able upload file.

anyhow, real chrome:

  1. go http://demo.moodle.net/course/modedit.php?update=1&return=1
  2. click 'add'
  3. 'upload file'
  4. upload actual file
  5. 'the file has been uploaded successfully'
  6. refresh page (http://demo.moodle.net/course/modedit.php?update=1&return=1)
  7. "no files available"

so, how 1 can check if file correctly uploaded? , there way upload file javascript enabled.

as side note, better click() hyperlink, so:

currentpage = anchors.get(0).click(); 

instead of

currentpage = (htmlpage) browser.getpage(anchors.get(0).gethrefattribute()); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -