使用 Selenium 捕获浏览器调用的输出

发布于 2024-09-03 08:10:10 字数 888 浏览 2 评论 0原文

我需要创建一个用例(使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的故事 2024-09-10 08:10:10

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

您可能最感兴趣的命令是 createCookieopenstoreHtmlSource。为了将 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, 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

避讳 2024-09-10 08:10:10

不确定您是否想在请求页面之前修改 cookie,但使用 Java 中的这段代码,您将捕获请求后返回的所有 HTML。

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

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..
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文