Sending multiple requests simultaneously to the Server using Selenium with Java
Posted
by gagneet
on Stack Overflow
See other posts from Stack Overflow
or by gagneet
Published on 2010-05-27T09:24:27Z
Indexed on
2010/05/30
15:52 UTC
Read the original article
Hit count: 300
I wish to send multiple requests to the server, simultaneously. The problem statement will be:
- Read a text file containing multiple URL’s.
- Open each URL in the web browser.
- Collect the Cookie information for each call, and store it to a file.
- Send another call: http://myserver.com:1111/cookie?out=text
- Store the output (body text) of this file to a separate file for each call made in 4
- Open the next URL in the text file given in 1 and repeat steps 1-6.
The above is to be run with multi-threading, so that I can send around 5-10 URL requests simultaneously. I have implemented something in Selenium using Java, but have not been able to do the multi-threading approach.
Code is given below:
package com.cookie.selenium;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import com.thoughtworks.selenium.*;
public class ReadURL extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://www.myserver.com/", "*chrome");
}
public static void main(String args[]) {
Selenium selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://myserver");
selenium.start();
selenium.setTimeout("30000000");
try {
BufferedReader inputfile = new BufferedReader(new FileReader("C:\\url.txt"));
BufferedReader cookietextfile = new BufferedReader(new FileReader("C:\\text.txt"));
BufferedWriter cookiefile = new BufferedWriter(new FileWriter("C:\\cookie.txt"));
BufferedWriter outputfile = null;
String str;
String cookiestr = "http://myserver.com:1111/cookie?out=text";
String filename = null;
int i = 0;
while ((str = inputfile.readLine()) != null) {
selenium.createCookie("T=222redHyt345&f=5&r=fg&t=100","");
selenium.open( str );
selenium.waitForPageToLoad("120000");
String urlcookie = selenium.getCookie();
System.out.println( "URL :" + str );
System.out.println( "Cookie :" + urlcookie );
cookiefile.write( urlcookie );
cookiefile.newLine();
selenium.open( cookiestr );
selenium.waitForPageToLoad("120000");
String bodytext = selenium.getBodyText();
System.out.println("Body Text :" + bodytext);
filename = "C:\\cookies\\" + i + ".txt";
outputfile = new BufferedWriter(new FileWriter( filename ));
outputfile.write( bodytext );
outputfile.newLine();
i++;
}
inputfile.close();
outputfile.close();
cookiefile.close();
selenium.stop();
} catch (IOException e) {
}
}
}
What basically I am trying to do here is, open the first set of URL from a text file (which has list given of all the URL's i wish to open). Then when I capture the cookie information from here and store it, I open another window to output all the cookie information for that server to my browser window. This works fine when I do outside of Selenium code, but when I do it within the above code, it opens a "Save As..." popup and my tests stop. :-(
I wish to save the contents of that second call to a new file, but have not been able to do the same. Also, if I have to send multiple such requests to the server, how would that be possible in Java using a Selenium Framework. Currently, I am opening multiple instances of the framework and running them with different parameters :-(
© Stack Overflow or respective owner