github.com/bazelbuild/rules_webtesting@v0.2.0/javatests/com/google/testing/web/screenshotter/ScreenshotterTest.java (about)

     1  // Copyright 2017 Google Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // //////////////////////////////////////////////////////////////////////////////
    16  //
    17  package com.google.testing.web.screenshotter;
    18  
    19  import static org.junit.Assert.assertEquals;
    20  
    21  import com.google.testing.bazel.Bazel;
    22  import com.google.testing.web.WebTest;
    23  import java.awt.Image;
    24  import java.nio.file.Files;
    25  import java.nio.file.Path;
    26  import org.junit.After;
    27  import org.junit.Before;
    28  import org.junit.BeforeClass;
    29  import org.junit.Test;
    30  import org.junit.runner.RunWith;
    31  import org.junit.runners.JUnit4;
    32  import org.openqa.selenium.By;
    33  import org.openqa.selenium.WebDriver;
    34  
    35  @RunWith(JUnit4.class)
    36  public class ScreenshotterTest {
    37  
    38    private static Path tmpDir;
    39  
    40    private WebDriver driver;
    41    private Screenshotter screenshotter;
    42  
    43    @BeforeClass
    44    public static void createTmpDir() throws Exception {
    45      tmpDir = Bazel.getInstance().newTmpDir(ScreenshotterTest.class.getSimpleName());
    46    }
    47  
    48    @Before
    49    public void createDriver() throws Exception {
    50      driver = new WebTest().newWebDriverSession();
    51      screenshotter = new Screenshotter(driver);
    52      driver.get(Bazel.getInstance().runfile("testdata/testpage.html").toUri().toString());
    53    }
    54  
    55    @After
    56    public void quitDriver() throws Exception {
    57      try {
    58        driver.quit();
    59      } finally {
    60        driver = null;
    61        screenshotter = null;
    62      }
    63    }
    64  
    65    @Test
    66    public void basicScreenshot() throws Exception {
    67      Screenshot ss = screenshotter.takeScreenshot();
    68      Path out = tmpDir.resolve("basicScreenshot.png");
    69      Files.write(out, ss.asBytes());
    70      System.out.println("image written to: " + out);
    71      // No assertions. Check output manually.
    72    }
    73  
    74    @Test
    75    public void ofInputElement() throws Exception {
    76      Screenshot ss = screenshotter.of(driver.findElement(By.tagName("input"))).takeScreenshot();
    77      Path out = tmpDir.resolve("ofInputElement.png");
    78      Files.write(out, ss.asBytes());
    79      System.out.println("image written to: " + out);
    80      Image img = ss.asImage();
    81      assertEquals(200, img.getHeight(null));
    82      assertEquals(200, img.getWidth(null));
    83    }
    84  
    85    @Test
    86    public void excludingElements() throws Exception {
    87      Screenshot ss =
    88          screenshotter
    89              .of(driver.findElement(By.id("outer-div")))
    90              .excluding(driver.findElement(By.id("inner-div1")), driver.findElement(By.id("b")))
    91              .takeScreenshot();
    92      Path out = tmpDir.resolve("excludingElements.png");
    93      Files.write(out, ss.asBytes());
    94      System.out.println("image written to: " + out);
    95      // No assertions. Check output manually.
    96    }
    97  }