使用 Selenium 捕获浏览器调用的输出
我需要创建一个用例(使用 Selenium),在其中通过浏览器发送带有 Cookie 的 HTTP 调用,并捕获文本文件中的返回值。
我需要做什么,我已经在命令行中使用 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..
}
}
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 encountering 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..
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,我推荐 Selenium IDE 或 Selenium RC。在 IDE 中,您只能在 Firefox 中运行测试,但它是对 Selenium 的一个很好的介绍。
您可能最感兴趣的命令是
createCookie
、open
和storeHtmlSource
。为了将 HTML 源代码保存到文本文件中,您可能需要升级到 Selenium RC 并以您首选的客户端语言实现它。有用链接
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
, andstoreHtmlSource
. 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
不确定您是否想在请求页面之前修改 cookie,但使用 Java 中的这段代码,您将捕获请求后返回的所有 HTML。
更新。稍微更改了您的代码。返回正文数据,只需将 tmpString 值保存到文本文件中,您就会从页面返回正文文本(更改为您想要的所有 html)。
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.
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.