2017-01-10 20 views
0

enter image description here壊れた画像は

私は、www.google.comのために壊れたイメージのコードを実行しようとしています。実行時に初期化エラーが発生します。コードをコンパイルするときに、55行目でエラーが発生します。

エラーは、配列またはjava.lang.Iterableのインスタンスに対してのみ反復処理されます。実行時の障害トレースは次のとおりです。

(1)java.lang.Exception:メソッドSetUp()は静的でなければなりません。

(2.)java.lang。例外:Method tear Down()は静的でなければなりません。

参考のためにコードを以下に示します。参照用にスクリーンショットも添付しています。

CODE

enter image description here

package fb_project; 


    import org.apache.http.client.HttpClient; 
    import org.apache.http.client.methods.HttpGet; 
    import org.apache.http.impl.client.HttpClientBuilder; 
    import org.jboss.netty.handler.codec.http.HttpResponse; 
    import org.junit.AfterClass; 
    import org.junit.BeforeClass; 
    import org.junit.Test; 
    import org.openqa.selenium.By; 
    import org.openqa.selenium.WebDriver; 
    import org.openqa.selenium.WebElement; 
    import org.openqa.selenium.firefox.FirefoxDriver; 

    import java.util.List; 

     /** 
     * @author Prerna 
     * 
     */ 
     public class Broken_image { 

     /** 
    * @param args 
    */ 

     private WebDriver driver; 
     private int invalidImageCount; 

     @BeforeClass 
     public void setUp() { 

    String exePathGecko=("C:\\Users\\Prerna\\Downloads  \\geckodriver.exe"); 
      System.setProperty("webdriver.gecko.driver",exePathGecko); 
      WebDriver driver=new FirefoxDriver(); 
      driver.get("http://google.com"); 
     } 
    @Test 
     public void validateInvalidImages() { 
      try { 
       invalidImageCount = 0; 
       List imagesList = (List) driver.findElements(By.tagName("img")); 
       System.out.println("Total no. of images are " + imagesList.size()); 
       for (WebElement imgElement : imagesList) { 
        if (imgElement != null) { 
         verifyimageActive(imgElement); 
        } 
       } 
       System.out.println("Total no. of invalid images are " + invalidImageCount); 
      } catch (Exception e) { 
       e.printStackTrace(); 
       System.out.println(e.getMessage()); 
      } 
     } 
     @AfterClass 
     public void tearDown() { 
      if (driver != null) 
       driver.quit(); 
     } 



    public void verifyimageActive(WebElement imgElement) { 
       try { 
        HttpClient client = HttpClientBuilder.create().build(); 
        HttpGet request = new HttpGet(imgElement.getAttribute("src")); 
        HttpResponse response = (HttpResponse) client.execute(request); 
        // verifying response code he HttpStatus should be 200 if not, 
        // increment as invalid images count 
        if (response.getStatus().getCode() != 200) 
         invalidImageCount++; 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
+0

あなたが添付したイメージは、あなたが求めているエラーとは異なるランタイムエラーを示しています。 – ProgrammersBlock

+0

エラーについて、その解決方法を教えてください。 – agusta

答えて

0

あなたは間違ってインポートしたので、それがエラーを示しているListパッケージ 変更

import java.awt.List; 

そしてへのあなたの構文を変更する - あなたはjava.awt.List魔女からListを使用している

List<WebElement>imagesList = driver.findElements(By.tagName("img")); 
+0

あなたのアイデアも試してみました。実行時に同じエラーが発生することがあります。初期化エラー。私はこれを解決する方法を教えてください。 – agusta

0

Iterable<E>インタフェースを実装していません。このため、imagesList.size()は使用できません。代わりにList

List<WebElement> imagesList = driver.findElements(By.tagName("img")); 
+0

私もこの変更を試みましたが、initialization.pleaseと同じエラーが発生して解決方法がわかりましたか? – agusta

+0

@agusta 'import java.awt.List;'を削除します。開発プラットフォームを再起動します。 – Guy

+0

私はすでに、インポートjava.awt.Listを削除しました。私は新しい問題のための上のスクリーンショットをつけている.kindlyそれを通って、私に知らせてください。 – agusta

0

/** * */ パッケージfb_projectを使用します。

 import org.apache.http.client.HttpClient; 
     import org.apache.http.client.methods.HttpGet; 
     import org.apache.http.impl.client.HttpClientBuilder; 
     import org.jboss.netty.handler.codec.http.HttpResponse; 
     import org.junit.AfterClass; 
     import org.junit.BeforeClass; 
     import org.junit.Test; 
     import org.openqa.selenium.By; 
     import org.openqa.selenium.WebDriver; 
     import org.openqa.selenium.WebElement; 
     import org.openqa.selenium.firefox.FirefoxDriver; 

     import java.util.List; 
     /** 
     * @author Prerna 
      * 
     */ 
      public class Broken_image { 

      /** 
      * @param args 
      */ 




       private static WebDriver driver; 
       private int invalidImageCount; 

      @BeforeClass 
      public static void setUp(){ 

      String exePathGecko=("C:\\Users\\Prerna\\Downloads\\geckodriver.exe"); 
     System.setProperty("webdriver.gecko.driver",exePathGecko); 
     WebDriver driver=new FirefoxDriver(); 
     driver.get("http://google.com"); 
    } 

    @Test 
    public void validateInvalidImages() { 
     try { 
      invalidImageCount = 0; 



      List<WebElement> imagesList = (List<WebElement>) driver.findElements(By.tagName("img")); 


      System.out.println("Total no. of images are " + imagesList.size()); 
      for (WebElement imgElement : imagesList) { 
       if (imgElement != null) { 
        verifyimageActive(imgElement); 
       } 
      } 
      System.out.println("Total no. of invalid images are " + invalidImageCount); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      System.out.println(e.getMessage()); 
     } 
    } 
    @AfterClass 

    public static void tearDown() 
    { 
     if (driver != null) 
      driver.quit(); 
    } 

    public void verifyimageActive(WebElement imgElement) { 
     try { 
      HttpClient client = HttpClientBuilder.create().build(); 
      HttpGet request = new HttpGet(imgElement.getAttribute("src")); 
      HttpResponse response = (HttpResponse) client.execute(request); 
      // verifying response code he HttpStatus should be 200 if not, 
      // increment as invalid images count 
      if (response.getStatus().getCode() != 200) 
       invalidImageCount++; 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
関連する問題