Selenium-log4j-logging

提供:Dev Guides
移動先:案内検索

Selenium-Log4jロギング

Log4jは、実行中に発生したことに関する情報を提供する監査ログフレームワークです。 それは次の利点を提供します-

  • アプリケーションの実行を理解できるようにします。
  • 後で分析できるログ出力を保存できます。
  • テスト自動化に失敗した場合のデバッグに役立ちます。
  • また、監査目的でアプリケーションの状態を確認するために使用できます。

コンポーネント

{空} 1 Loggerクラスのインスタンス。

{空} 2。 次のいずれかとしてメッセージを記録するために使用されるログレベルの方法-

  • エラー
  • warn
  • info
  • デバッグ
  • log

このデモにも同じパーセント計算機を使用してみましょう。

selenium_ide_158

  • ステップ2 *-[ファイル]メニューに移動して、「新しいJavaプロジェクト」を作成します。

selenium_ide_159

  • ステップ3 *-プロジェクトの名前を「log4j_demo」として入力し、「次へ」をクリックします。

selenium_ide_160

  • ステップ4 *-[外部Jarの追加]をクリックし、「Log4j-1.2.17.jar」を追加します。

selenium_ide_161

  • ステップ5 *-[外部Jarの追加]をクリックして、Selenium WebDriver Librariesを追加します。

selenium_ide_162

  • ステップ6 *-[外部Jarの追加]をクリックし、LibsフォルダーにあるSelenium WebDriver JARを追加します。

selenium_ide_163

  • ステップ7 *-Log4jプロパティを指定できる新しいXMLファイルを追加します。

selenium_ide_164

  • ステップ8 *-「Log4j.xml」としてログファイル名を入力します。

selenium_ide_165

  • ステップ9 *-最終的なフォルダー構造を以下に示します。

selenium_ide_166

  • ステップ10 *-実行中に取得されるLog4jのプロパティを追加します。
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j = "http://jakarta.apache.org/log4j/" debug = "false">

   <appender name = "fileAppender" class = "org.apache.log4j.FileAppender">
      <param name = "Threshold" value = "INFO"/>
      <param name = "File" value = "percent_calculator.log"/>

      <layout class = "org.apache.log4j.PatternLayout">
         <param name = "ConversionPattern"
            value = "%d{yyyy-MM-dd HH:mm:ss}  [%c] (%t:%x) %m%n"/>
      </layout>
   </appender>

   <root>
      <level value = "INFO"/>
      <appender-ref ref = "fileAppender"/>
   </root>

</log4j:configuration>
  • ステップ11 *-デモンストレーションの目的で、log4jを実行したのと同じテストに組み込みます(パーセント計算)。 'Main’関数にクラスファイルを追加します。
package log4j_demo;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class log4j_demo {
   static final Logger logger = LogManager.getLogger(log4j_demo.class.getName());

   public static void main(String[] args) {

      DOMConfigurator.configure("log4j.xml");
      logger.info("# # # # # # # # # # # # # # # # # # # # # # # # # # # ");
      logger.info("TEST Has Started");
      WebDriver driver = new FirefoxDriver();

     //Puts a Implicit wait, Will wait for 10 seconds before throwing exception
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

     //Launch website
      driver.navigate().to("http://www.calculator.net/");
      logger.info("Open Calc Application");

     //Maximize the browser
      driver.manage().window().maximize();

     //Click on Math Calculators
      driver.findElement(By.xpath(".//*[@id = 'menu']/div[3]/a")).click();
      logger.info("Clicked Math Calculator Link");

     //Click on Percent Calculators
      driver.findElement(By.xpath(".//*[@id = 'menu']/div[4]/div[3]/a")).click();
      logger.info("Clicked Percent Calculator Link");

     //Enter value 10 in the first number of the percent Calculator
      driver.findElement(By.id("cpar1")).sendKeys("10");
      logger.info("Entered Value into First Text Box");

     //Enter value 50 in the second number of the percent Calculator
      driver.findElement(By.id("cpar2")).sendKeys("50");
      logger.info("Entered Value into Second Text Box");

     //Click Calculate Button
      driver.findElement(By.xpath(".//*[@id = 'content']/table/tbody/tr/td[2]/input")).click();
      logger.info("Click Calculate Button");

     //Get the Result Text based on its xpath
      String result =
         driver.findElement(By.xpath(".//*[@id = 'content']/p[2]/span/font/b")).getText();
      logger.info("Get Text Value");

     //Print a Log In message to the screen
      logger.info(" The Result is " + result);

      if(result.equals("5")) {
         logger.info("The Result is Pass");
      } else {
         logger.error("TEST FAILED. NEEDS INVESTIGATION");
      }

      logger.info("# # # # # # # # # # # # # # # # # # # # # # # # # # # ");
     //Close the Browser.
      driver.close();
   }
}

実行

実行すると、以下に示すように、ログファイルがルートフォルダーに作成されます。 Eclipseでファイルを見つけることはできません。 同じように表示するには、「Windows Explorer」を開く必要があります。

selenium_ide_167

ファイルの内容を以下に示します。

selenium_ide_168