I need to create a use case (using Selenium) in which I send HTTP calls with a Cookie through the browser and capture the return value in a text file.
What do I need to do this, I have run this using CURL in the command line, but we are encou开发者_开发知识库ntering issues with the same, and hence wish to verify using a real UI browser.
Another thing to this is that I need to get the URL's to be in a test file from which I can read and send to the browser. Then for each call, I need to capture the cookie and the header for the same. I have the following code/logic for this, could someone elaborate?
---> read a file....
File aFile = new File("../blah.txt");
BufferedReader input = new BufferedReader( new FileReader( aFile ));
String line = null; //not declared within while loop
while (( line = input.readLine()) != null){
callsel(line);
System.out.println(line);
}
--> call selenium .. Open the url.. Pass cookies
public void callsel(String url) {
selenium.open(url);
selenium.waitForPageToLoad("120000");
selenium.createCookie("","");
selenium.createCookie("","");
selenium.open(url);
selenium.waitForPageToLoad("120000");
---> ur page is open now..
}
}
I would recommend Selenium IDE or Selenium RC for this. In the IDE you can run tests back in Firefox only, but it is a good introduction to Selenium.
The commands you might be most interested in are createCookie
, open
, and storeHtmlSource
. For saving the HTML source to a text file you'll probably want to progress to Selenium RC and implement this in your preferred client language.
Useful links
- Introduction to Selenium IDE
- Introduction to Selenium RC
- Selenium Command Reference
Not sure if you want to modify the cookie before requesting a page but with this code in Java you will capture all HTML coming back after the request.
String url = "http://host/";
HttpCommandProcessor proc;
proc = new HttpCommandProcessor("localhost", 4444, "*iexplore", url);
Selenium selenium = new DefaultSelenium(proc);
selenium.start();
selenium.open("pageToOpen.htm");
String HTMLOutput = selenium.getHtmlSource();
String BodyOutput = selenium.getBodyText();
Update. Changed your code a bit.. Returning back the body data, just save the tmpString value to a text file and you will have the Body Text(change this is you want all html) back from the page.
---> read a file....
File aFile = new File("../blah.txt");
BufferedReader input = new BufferedReader( new FileReader( aFile ));
String line = null; //not declared within while loop
while (( line = input.readLine()) != null){
String tmpString = callsel(line);
System.out.println("Line: " + line + " HTML:" + tmpString);
}
--> call selenium .. Open the url.. Pass cookies
public string callsel(String url) {
selenium.open(url);
selenium.waitForPageToLoad("120000");
selenium.createCookie("","");
selenium.createCookie("","");
selenium.open(url);
selenium.waitForPageToLoad("120000");
return selenium.getBodyText();
---> ur page is open now..
}
}
精彩评论