3

I am using http client POST method to perform specific action in a website , for that it uses javascript of ajax connection which produces a requestID i.e var reqID = Math.floor(Math.random()*1000001); to post . I want to access that reqID to make the action performed . can anyone help me regarding how to access a java script in HttpClient ? Or can we access the specific reqID variable of javascript using HtmlUnit ?

My JS includes:

ajaxConnection.prototype.execute = function() {
    var reqID = Math.floor(Math.random()*1000001);
    var params = "reqID=" + reqID ;
    for (var key in this.connection_parameters) {
        params += "&" + key + "=" + this.connection_parameters[key];
    }

and i am calling POST to get the action performed i.e

String Src = PageSource_Post("http://www.example.com/ajax/ratingClient.php", new String[][]{{"reqID",""},{"id", "329602"},{"cmd", "rate"},{"rating", "2"},}, null);

as of now i have kept the reqID blank and other parameters are getting from pagesource !
I am trying with HtmlUnit also like

webClient.setJavaScriptEnabled(true);
HtmlPage firstPage = webClient.getPage("http://www.example.com/");
HTMLScriptElement script = new HTMLScriptElement();

From here do i have to access the particular reqID variable?

Rob W
  • 341,306
  • 83
  • 791
  • 678
Aspirant
  • 2,238
  • 9
  • 31
  • 43
  • post your code so the community can help you better – Luiggi Mendoza Mar 31 '12 at 05:36
  • @LuiggiMendoza my js includes `ajaxConnection.prototype.execute = function(){ var reqID = Math.floor(Math.random()*1000001); var params = "reqID=" + reqID ; for( var key in this.connection_parameters ){ params += "&" + key + "=" + this.connection_parameters[key];}` and i am calling POST to get the action performed i.e `String Src = PageSource_Post("http://www.example.com/ajax/ratingClient.php", new String[][]{{"reqID",""},{"id", "329602"},{"cmd", "rate"},{"rating", "2"},}, null);` as of now i have kept the reqID blank and other parameters are getting from pagesource !! – Aspirant Mar 31 '12 at 07:11
  • @LuiggiMendoza i am trying with HtmlUnit also like `webClient.setJavaScriptEnabled(true); HtmlPage firstPage = webClient.getPage("http://www.example.com/"); HTMLScriptElement script = new HTMLScriptElement(); ` from here do i have to access the particular `var reqID = Math.floor(Math.random()*1000001);` or the total js to get the exact value – Aspirant Mar 31 '12 at 07:43
  • @Aspirant: You should EDIT your post to include your code (NOT add the code as a comment). It's more readable and that's what editing is for. :) – Stefan Mar 31 '12 at 08:13
  • This is a php question, not a Java question (http://www.example.com/ajax/ratingClient.php), or you want help to achieve this in Java? – Luiggi Mendoza Mar 31 '12 at 15:02
  • Hey @LuiggiMendoza !! this is a java question , the only thing is that the website i am accessing is of .php !! anyway the question is while posting the request it gives reqID as request values which i got through LiveHttpHeaders , but the thing is the reqID which makes the request process is in javascript !! Can you help me regarding how to call javascript in HtmlUnit .... – Aspirant Apr 02 '12 at 12:30

2 Answers2

0

It looks like you want to execute javascript from within Java. Personally, I've found HTMLUnit, and HTTPUnit's javascript support a bit lacking. We have had similar issues, and had great success using Selenium. You can create a quick test case using the Selenium IDE which is a firefox plugin(http://docs.seleniumhq.org/projects/ide/) . It allows you to record your steps from within Firefox, and export the test case to a JUnit test. Here is a good link for the steps. http://university.utest.com/selenium-basics-part-3-record-test-case-and-export-in-junit-format/

Rhino
  • 101
  • 8
0

you mean web browser?

firebug for firefox and F12 for IE

These tools will help you to debug javascript, you should toggle a break point in your javascript codes, and run step by step to see the value of your reqID

Neysor
  • 3,893
  • 11
  • 34
  • 66
yahz
  • 41
  • 1
  • 4
  • I assume when he said [HTTPClient](http://hc.apache.org/httpclient-3.x/) or [HTML Unit](http://htmlunit.sourceforge.net/), then he meant "HTTPClient or HTML Unit" and not "a web browser". – Quentin Mar 31 '12 at 05:39
  • @yahz bro i am using HttpClient , so i have done the debugging process using firebug and found the reqID is firing from ajaxconnection.js from my pagesource !! now since i need the reqID to be POSTED which is in javascript i need guidance regarding how to access that reqID variable from javascript using HttpClient or Html UNIT ..... – Aspirant Mar 31 '12 at 07:22