Selenium-capture-screenshots

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

Selenium-スクリーンショットのキャプチャ

この機能は、特に障害が発生した場合に、必要なときに実行時にスクリーンショットを取得するのに役立ちます。 スクリーンショットとログメッセージの助けを借りて、結果をよりよく分析できるようになります。

スクリーンショットは、ローカル実行とSelenium Grid(リモート)実行で別々に構成されます。 例を挙げてそれぞれを見てみましょう。

ローカルホストの実行

次の例では、パーセンテージを計算した後にスクリーンショットを撮ります。 スクリーンショットを保存するための有効なパスを指定してください。

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebdriverDemo {
   public static void main(String[] args) throws IOException {

      WebDriver driver = new FirefoxDriver();

     //Puts an 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/");

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

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

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

     //Enter value 10 in the first number of the percent Calculator
      driver.findElement(By.id("cpar1")).sendKeys("10");

     //Enter value 50 in the second number of the percent Calculator
      driver.findElement(By.id("cpar2")).sendKeys("50");

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

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

      File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

      FileUtils.copyFile(screenshot, new File("D:\\screenshots\\screenshots1.jpg"));

     //Print a Log In message to the screen
      System.out.println(" The Result is " + result);

     //Close the Browser.
      driver.close();
   }
}

出力

スクリプトを実行すると、下に示すように、スクリーンショットは「screenshots1.jpg」という名前で「D:\ screenshots」フォルダーに保存されます。

selenium_ide_172

Selenium Gridスクリーンショットのキャプチャ

Selenium Gridsを使用している間、リモートシステムからスクリーンショットを正しく取得していることを確認する必要があります。 拡張ドライバーを使用します。

ハブに接続されたFirefoxノードでスクリプトを実行します。 ハブとノードの構成の詳細については、リンク:/selenium/selenium_grids [Selenium Grids]の章を参照してください。

package TestNG;

import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.*;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.util.concurrent.TimeUnit;
import java.io.File;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;

public class TestNGClass {
   public WebDriver driver;
   public String URL, Node;
   protected ThreadLocal<RemoteWebDriver> threadDriver = null;

   @Parameters("browser")
   @BeforeTest
   public void launchapp(String browser) throws MalformedURLException {
      String URL = "http://www.calculator.net";

      if (browser.equalsIgnoreCase("firefox")) {
         System.out.println(" Executing on FireFox");
         String Node = "http://10.112.66.52:5555/wd/hub";
         DesiredCapabilities cap = DesiredCapabilities.firefox();
         cap.setBrowserName("firefox");
         driver = new RemoteWebDriver(new URL(Node), cap);

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

        //Launch website
         driver.navigate().to(URL);
         driver.manage().window().maximize();
      } else {
         throw new IllegalArgumentException("The Browser Type is Undefined");
      }
   }

   @Test
   public void calculatepercent() throws IOException {
     //Click on Math Calculators
      driver.findElement(By.xpath(".//*[@id = 'menu']/div[3]/a")).click();

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

     //Make use of augmented Driver to capture Screenshots.
      WebDriver augmentedDriver = new Augmenter().augment(driver);
      File screenshot = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
      FileUtils.copyFile(screenshot, new File("D:\\screenshots\\remotescreenshot1.jpg"));

     //Screenshot would be saved on the system where the script is executed and NOT on remote machine.

     //Enter value 10 in the first number of the percent Calculator
      driver.findElement(By.id("cpar1")).sendKeys("10");

     //Enter value 50 in the second number of the percent Calculator
      driver.findElement(By.id("cpar2")).sendKeys("50");

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

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

     //Print a Log In message to the screen
      System.out.println(" The Result is " + result);


      if(result.equals("5")) {
         System.out.println(" The Result is Pass");
      } else {
         System.out.println(" The Result is Fail");
      }
   }

   @AfterTest
   public void closeBrowser() {
      driver.quit();
   }
}

出力

スクリプトを実行すると、次のようにスクリーンショットがキャプチャされ、指定された場所に保存されます。

selenium_ide_173