github.com/bazelbuild/rules_webtesting@v0.2.0/scalatests/com/google/testing/web/screenshotter/ScreenshotterTest.scala (about) 1 // Copyright 2018 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 com.google.testing.bazel.Bazel 20 import com.google.testing.web.WebTest 21 import java.awt.Image 22 import java.nio.file.Files 23 import java.nio.file.Path 24 import org.openqa.selenium.By 25 import org.openqa.selenium.WebDriver 26 import org.scalatest._ 27 28 class ScreenshotterTest 29 extends WordSpec 30 with BeforeAndAfter 31 with BeforeAndAfterAll 32 with Matchers { 33 34 var tmpDir: Path = _ 35 36 private var driver: WebDriver = _ 37 private var screenshotter: Screenshotter = _ 38 39 override def beforeAll() = { 40 tmpDir = Bazel.getInstance.newTmpDir(this.getClass.getSimpleName) 41 } 42 43 before { 44 driver = new WebTest().newWebDriverSession() 45 screenshotter = new Screenshotter(driver) 46 driver.get(Bazel.getInstance.runfile("testdata/testpage.html").toUri.toString) 47 } 48 49 50 after { 51 try { 52 driver.quit() 53 } finally { 54 driver = null 55 screenshotter = null 56 } 57 } 58 59 "A WebDriver" should { 60 "take a screenshot" in { 61 val ss = screenshotter.takeScreenshot() 62 val out = tmpDir.resolve("basicScreenshot.png") 63 Files.write(out, ss.asBytes()) 64 System.out.println("image written to: " + out) 65 // No assertions. Check output manually. 66 } 67 68 "take a screenshot of an element" in { 69 val ss = screenshotter.of(driver.findElement(By.tagName("input"))).takeScreenshot() 70 val out = tmpDir.resolve("ofInputElement.png") 71 Files.write(out, ss.asBytes()) 72 System.out.println("image written to: " + out) 73 val img = ss.asImage() 74 200 shouldEqual img.getHeight(null) 75 200 shouldEqual img.getWidth(null) 76 } 77 78 "exclude elements from a screenshot" in { 79 val ss = screenshotter 80 .of(driver.findElement(By.id("outer-div"))) 81 .excluding(driver.findElement(By.id("inner-div1")), driver.findElement(By.id("b"))) 82 .takeScreenshot() 83 val out = tmpDir.resolve("excludingElements.png") 84 Files.write(out, ss.asBytes()) 85 System.out.println("image written to: " + out) 86 // No assertions. Check output manually. 87 } 88 } 89 }