2017-05-09 9 views
0

Selenium Webdriver를 사용하여 http 상태를 확인하여 내 웹 사이트/CMS의 모든 링크가 작동하는지 확인하려고합니다. 이를 위해 Selenium Webdriver에서 HttpURLConnection 클래스를 사용하고 있습니다. 여기에 내가 실행하고있어 코드입니다 :HttpURLConnection 잘못된 상태 표시

import java.net.HttpURLConnection; 
import java.net.URL; 
import java.util.List; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class checkingLinks { 

public static void main(String[] args) 
{ 
    WebDriver driver=new FirefoxDriver(); 

    driver.manage().window().maximize(); 

    driver.get("link_to_cms"); 

    driver.findElement(By.id("username")).clear(); 
    driver.findElement(By.id("username")).sendKeys("my_login"); 
    driver.findElement(By.id("password")).clear(); 
    driver.findElement(By.id("password")).sendKeys("my_passwod"); 
    driver.findElement(By.xpath("//button[@type='submit']")).click(); 

    List<WebElement> links=driver.findElements(By.tagName("a")); 

    System.out.println("Total links are "+links.size()); 

    for(int i=0;i<links.size();i++) 
    { 

     WebElement ele= links.get(i); 

     String url=ele.getAttribute("href"); 

     verifyLinkActive(url); 

    } 

} 

public static void verifyLinkActive(String linkUrl) 
{ 
    try 
    { 
     URL url = new URL(linkUrl); 

     HttpURLConnection httpURLConnect=(HttpURLConnection)url.openConnection(); 

     httpURLConnect.setConnectTimeout(3000); 

     httpURLConnect.connect(); 

     System.out.println(linkUrl+" - "+httpURLConnect.getResponseCode()); 


    } catch (Exception e) { 

    } 
} 

} 

문제는 결과에 모든 링크는 HTTP 상태 200지고 있다는 것입니다,하지만 난 그들 중 몇은 HTTP 상태에 있다는 것을 알고는 (500) 당신은 아마 그것이 얼마나 알고 계십니까 가능한? 위의 코드에서 실수로 잘못된 결과를 얻었습니까?

+0

정직하게도 코드에 오류가 표시되지 않습니다. 모든 것이 완벽 해 보입니다. In-case의 공개 URL은 잘못된 결과를 가져 오는 URL을 공유 할 수 있습니까? – DebanjanB

+0

@Dev 안타깝게도 공개 URL이 아닙니다. CMS는 내 사무실 네트워크에서만 볼 수 있습니다. 그래서 당신의 의견으로는 코드 자체가 적절한 것입니까? 좋은 사실 :) –

답변

0

HttpURLConnection에서 HTTP 요청을 보내지는 않습니다. 그냥 연결을 엽니 다.

또한 인증 (로그인) 할 때 수신하는 쿠키는 이후 요청에서 헤더로 보내야합니다.

+0

답해 주셔서 감사합니다. 그렇다면 코드를 수정/추가하여 꼭 필요한대로 작동하도록 제안 하시겠습니까? –