2017-09-13 8 views
0

私の要件として、5秒ごとにhtmlファイルを読み込み、そこからテストケース名を見つけてステータスを渡す必要があります新しいファイルがあり、htmlレポートを作成してそこに書き込む必要があります。 私は以下のコードで行っています。私は正しい方法をとっています。コアJavaを使用してhtmlファイルを読み込み、ページから属性値を取得します

ここでは、htmlファイルをテキストファイルに変換した後、テストケース名を配列に入れました。そのテキストファイルのテキストケース名を検索しています。テキストファイルの中にテキストケースがありますこの形式では "RAP_45 1"です。ここでRAP_45はテストケース名です。 "1"はパスを意味し、 "0"は失敗を意味します。エラーは配列 "0"または "1"入力ファイルに基づいて.Kindly私がその下で動作しないことを試みた状態を助けてください。提案を与える。

public class HtmlReport { 

     public static final String FOLDER = ("C:\\Users\\jdutta\\files"); 

     public static void main(String[] args) throws Exception { 

      System.out.println("Searching for new file...."); 
      final long pollingInterval = 5 * 1000; 

      File folder = new File(FOLDER); 

      if (!folder.exists()) { 

       throw new RuntimeException("Directory not found: " + FOLDER); 
      } 

      FileAlterationObserver observer = new FileAlterationObserver(folder); 
      FileAlterationMonitor monitor = 
        new FileAlterationMonitor(pollingInterval); 
      FileAlterationListener listener = new FileAlterationListenerAdaptor() { 

       @Override 
       public void onFileCreate(File file) { 
        try { 
         System.out.println("File created: " + file.getCanonicalPath()); 
          FileReader reader = new FileReader(file.getCanonicalPath()); 
          // System.out.println(extractText(reader)); 
          extractText(reader); 

        } 

        catch (IOException e) { 
         e.printStackTrace(System.err); 
        } 
       } 

       // Is triggered when a file is deleted from the monitored folder 
       @Override 
       public void onFileDelete(File file) { 
        try { 
         // "file" is the reference to the removed file 
         System.out.println("File removed: " 
           + file.getCanonicalPath()); 
         // "file" does not exists anymore in the location 
         System.out.println("File still exists in location: " 
           + file.exists()); 
        } catch (IOException e) { 
         e.printStackTrace(System.err); 
        } 
       } 
      }; 

      observer.addListener(listener); 
      monitor.addObserver(observer); 
      monitor.start(); 

     } 


     public static String extractText(Reader reader) throws IOException { 

      String s[]={"RAP_45 1","RAP_50 0","RAP_75 0","RAP_PayAsYouGo 0","RAP_Refill_Coupon 0"}; 

      StringBuilder sb = new StringBuilder(); 
      BufferedReader br = new BufferedReader(reader); 
      FileWriter writeReport = new FileWriter("C:\\Users\\jdutta\\files\\ResultSummary.html"); 
      BufferedWriter bw = new BufferedWriter(writeReport); 
      String line; 
      bw.write("<html><Body font=\"14px/1.4 Georgia\" ><br><table border=\"2%\" width=\"50%\"><h3 >Summary:</h3><tr bgcolor=\"black\">");   
      bw.write("<div border=\"1px solid\" color=\" #ccc\" padding=\"50px\"> <th><font color=\"white\">Test</th> <th><font color=\"white\">Passed</th><th><font color=\"white\">Failed</th> <th><font color=\"white\">Execution time</th>"); 

      while ((line=br.readLine()) != null) { 

       sb.append(line); 
      } 
      String textOnly = Jsoup.parse(sb.toString()).text(); 
      int pass=0; 
      int fail=0; 
      for(int i=0;i<s.length;i++) { 
       String[] s1=s[i].split(" "); 
      System.out.println("s1.........."+s1[1]); 

      if(textOnly.contains(s1[0])&& s1[1].contains("1")){ 

        System.out.println(s[i]+"pass"); 
        bw.write("<tr><td>" + s1[0] + "</td><td>" + "1" + "</td><td>" + "0" + "</td><td>" 
         + "200sec" + "</td></td></tr>"); 

        ++pass; 

      } 
      else{ 
       System.out.println("fail"+s1[0]); 
        bw.write("<tr><td>" + s1[0] + "</td><td>" + "0" + "</td><td>" + "1" + "</td><td>" 
          + "200sec" + "</td></tr>"); 
        ++fail; 
      } 

      } 
      bw.write("<tr><td>" + "Total" + "</td><td>" + pass + "</td><td>" + fail + "</td></tr>"); 

      bw.write("</tr></table></Body></html>"); 
      bw.close(); 
      return textOnly; 
     } 


     } 

答えて

0

あなたは基本的にHTMLファイルから属性を読み込み、それに応じて行動しようとしています。 HTMLは単なるXMLファイルです。 JAXBを使用してタグを非整列化して使用することができます。 まだ試したことはありませんが、試してみるべきです。

それとも、JSoup

jsoup

+0

のYaaを使用することができますが、ここで私はJsoupを使用しています...あなたは私がmistakをした状態を確認してくださいすることができかもしれません。 –

関連する問題