2010-05-25 66 views
1

我需要创建一个使用案例(使用Selenium),其中我通过浏览器使用Cookie发送HTTP调用,并在文本文件中捕获返回值。使用Selenium从浏览器调用捕获输出

我需要做什么,我已经在命令行中使用CURL来运行此操作,但是我们遇到了相同的问题,因此希望使用真实的UI浏览器进行验证。

另一件事情是,我需要将URL放在测试文件中,我可以从中读取并发送到浏览器。然后,对于每个电话,我需要捕获cookie和标题相同。我有这样的代码/逻辑,可以有人详细说明吗?

---> 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.. 
    } 
} 

回答

2

不知道,如果你想请求页面之前,但与此代码在Java中,你将捕获所有HTML来的请求后回修改的cookie。

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(); 

更新。改变了你的代码。返回正文数据,只需将tmpString值保存到一个文本文件中,并且您将从页面返回正文文本(更改这是你想要的所有html)。

---> 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.. 
    } 
} 
+0

嗨史蒂芬,我添加了一些更高的要求和一些代码,你可以请检查并详细说明上述解决方案? – gagneet 2010-05-25 12:39:11

3

我会为此推荐Selenium IDE或Selenium RC。在IDE中,您只能在Firefox中运行测试,但这是对Selenium的一个很好的介绍。

您可能最感兴趣的命令是createCookie,openstoreHtmlSource。为了将HTML源文件保存为文本文件,您可能需要进入Selenium RC并使用您的首选客户端语言实现此功能。

有用的链接