2010-05-25 65 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並使用您的首選客戶端語言實現此功能。

有用的鏈接