2010-05-25 5 views
1

브라우저를 통해 HTTP 호출을 보내고 리턴 값을 텍스트 파일로 캡처하는 유스 케이스 (Selenium 사용)를 작성해야합니다.Selenium을 사용하여 브라우저 호출의 출력을 캡처하십시오.

이 작업을 수행하려면 무엇이 필요합니까? 명령 줄에서 CURL을 사용하여 실행했지만 실제 UI 브라우저를 사용하여 문제를 확인했습니다.

또 다른 한가지는 내가 읽을 수 있고 브라우저로 보낼 수있는 테스트 파일에 URL을 가져와야한다는 것입니다. 그런 다음 각 호출에 대해 쿠키와 헤더를 캡처해야합니다. 나는 이것을 위해 다음과 같은 코드/논리를 가지고있다. 누군가 정교 할 수 있는가? 당신이 페이지를 요청하기 전에하지만 다시 요청 이후에 오는 모든 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){ 
    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

.

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 값을 저장하면 페이지에서 Body Text (모든 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

3

나는 이것을 위해 Selenium IDE 또는 Selenium RC를 권하고 싶습니다. IDE에서는 Firefox에서만 테스트를 실행할 수 있지만 Selenium에 대한 좋은 소개입니다.

사용자가 가장 관심을 가질만한 명령은 createCookie, openstoreHtmlSource입니다. HTML 소스를 텍스트 파일로 저장하려면 Selenium RC로 진행하여 원하는 클라이언트 언어로 구현해야합니다.

유용한 링크

관련 문제