-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example code for taking screenshots (#141)
* dependency and plugin versions updated in pom.xml * updated docker command in workflow yaml file * added screenshot tests, updated github workflow to create screenshot folder in ci env, updated drivermanager to handle profile password leak message in chrome * added screenshots folder to gitignore * removed screenshot tests from running in CI env * added https to the the-internet website link * updated workflow actions versions to v4
- Loading branch information
1 parent
e2fb859
commit adbcaf8
Showing
23 changed files
with
288 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
/test-output/ | ||
/logs/ | ||
/reports/ | ||
/screenshots | ||
|
||
# Mac OSX | ||
.DS_Store | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/test/java/io/github/mfaisalkhatri/tests/screenshottests/FullPageScreenshotTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package io.github.mfaisalkhatri.tests.screenshottests; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.openqa.selenium.*; | ||
import org.openqa.selenium.chrome.ChromeDriver; | ||
import org.openqa.selenium.firefox.FirefoxDriver; | ||
import org.openqa.selenium.interactions.Actions; | ||
import org.testng.annotations.AfterMethod; | ||
import org.testng.annotations.AfterTest; | ||
import org.testng.annotations.Test; | ||
import ru.yandex.qatools.ashot.AShot; | ||
import ru.yandex.qatools.ashot.Screenshot; | ||
import ru.yandex.qatools.ashot.shooting.ShootingStrategies; | ||
|
||
import javax.imageio.ImageIO; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.time.Duration; | ||
|
||
public class FullPageScreenshotTest { | ||
|
||
WebDriver driver; | ||
|
||
@Test | ||
public void testTakeFullPageScreenshotFirefox() { | ||
|
||
driver = new FirefoxDriver(); | ||
|
||
|
||
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30)); | ||
driver.manage().window().maximize(); | ||
|
||
driver.get("https://ecommerce-playground.lambdatest.io/"); | ||
|
||
JavascriptExecutor js = (JavascriptExecutor) driver; | ||
Actions actions = new Actions(driver); | ||
|
||
WebElement topTrendingItemList = driver.findElement(By.className("swiper-wrapper")); | ||
js.executeScript("arguments[0].scrollIntoView(true);", topTrendingItemList); | ||
actions.pause(2000).build().perform(); | ||
|
||
WebElement topProducts = driver.findElement(By.cssSelector("#entry_217978 > h3")); | ||
js.executeScript("arguments[0].scrollIntoView(true);",topProducts); | ||
actions.pause(2000).build().perform(); | ||
|
||
WebElement bottom = driver.findElement(By.className("article-thumb")); | ||
js.executeScript("arguments[0].scrollIntoView(true);", bottom); | ||
actions.pause(2000).build().perform(); | ||
|
||
File src = ((FirefoxDriver) driver).getFullPageScreenshotAs(OutputType.FILE); | ||
try { | ||
FileUtils.copyFile(src, new File("./screenshots/fulpagescreenshot.png")); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Test | ||
public void testTakeScreenshotUsingAShot() throws IOException { | ||
driver = new ChromeDriver(); | ||
|
||
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30)); | ||
|
||
driver.get("https://ecommerce-playground.lambdatest.io/"); | ||
|
||
Object devicePixelRatio = ((JavascriptExecutor)driver).executeScript("return window.devicePixelRatio"); | ||
float windowDPR = Float.parseFloat(devicePixelRatio.toString()); | ||
|
||
Screenshot screenshot = new AShot() | ||
.shootingStrategy(ShootingStrategies.viewportPasting(ShootingStrategies.scaling(windowDPR),1000)) | ||
.takeScreenshot(driver); | ||
|
||
ImageIO.write(screenshot.getImage(), "png", new File("./screenshots/AshotFullPageScreen.png")); | ||
|
||
} | ||
|
||
@AfterMethod | ||
public void tearDown() { | ||
driver.quit(); | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
src/test/java/io/github/mfaisalkhatri/tests/screenshottests/ScreenshotWithSeleniumTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.github.mfaisalkhatri.tests.screenshottests; | ||
|
||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.WebElement; | ||
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext; | ||
import org.openqa.selenium.chrome.ChromeDriver; | ||
import org.openqa.selenium.chrome.ChromeOptions; | ||
import org.openqa.selenium.remote.RemoteWebElement; | ||
import org.testng.annotations.AfterMethod; | ||
import org.testng.annotations.AfterTest; | ||
import org.testng.annotations.BeforeTest; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.util.Base64; | ||
|
||
public class ScreenshotWithSeleniumTest { | ||
private WebDriver driver; | ||
|
||
@BeforeTest | ||
public void setup() { | ||
ChromeOptions options = new ChromeOptions(); | ||
options.setCapability("webSocketUrl", true); | ||
driver = new ChromeDriver(options); | ||
} | ||
|
||
@Test | ||
public void testTakeScreenshot() throws IOException { | ||
BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); | ||
|
||
driver.get("https://ecommerce-playground.lambdatest.io/"); | ||
|
||
String screenshot = browsingContext.captureScreenshot(); | ||
byte[] imgByteArray = Base64.getDecoder().decode(screenshot); | ||
FileOutputStream imgOutFile = new FileOutputStream("./screenshots/screenshot_homepage.png"); | ||
imgOutFile.write(imgByteArray); | ||
imgOutFile.close(); | ||
|
||
} | ||
|
||
@Test | ||
public void testTakeElementScreenshot() throws IOException { | ||
BrowsingContext browsingContext = new BrowsingContext(driver,driver.getWindowHandle()); | ||
driver.get("https://ecommerce-playground.lambdatest.io/index.php?route=account/register"); | ||
|
||
WebElement firstName = driver.findElement(By.id("input-firstname")); | ||
String screenshot = browsingContext.captureElementScreenshot(((RemoteWebElement) firstName).getId()); | ||
|
||
byte[] imgByteArray = Base64.getDecoder().decode(screenshot); | ||
FileOutputStream imgOutFile = new FileOutputStream("./screenshots/screenshot_webelement.png"); | ||
imgOutFile.write(imgByteArray); | ||
imgOutFile.close(); | ||
|
||
} | ||
|
||
|
||
@AfterTest | ||
public void tearDown() { | ||
driver.quit(); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/test/java/io/github/mfaisalkhatri/tests/screenshottests/ViewableScreenshotExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package io.github.mfaisalkhatri.tests.screenshottests; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.openqa.selenium.*; | ||
import org.openqa.selenium.chrome.ChromeDriver; | ||
import org.openqa.selenium.interactions.Actions; | ||
import org.testng.annotations.AfterTest; | ||
import org.testng.annotations.BeforeTest; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.time.Duration; | ||
|
||
public class ViewableScreenshotExample { | ||
|
||
WebDriver driver; | ||
|
||
@BeforeTest | ||
public void setup() { | ||
|
||
driver = new ChromeDriver(); | ||
|
||
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30)); | ||
driver.manage().window().maximize(); | ||
} | ||
|
||
@Test | ||
public void testTakeViewableScreenshot() { | ||
|
||
driver.get("https://ecommerce-playground.lambdatest.io/"); | ||
|
||
WebElement blogMenu = driver.findElement(By.cssSelector("div.entry-section div.entry-widget ul > li:nth-child(3) > a > div > span")); | ||
blogMenu.click(); | ||
|
||
WebElement firstArticleImage = driver.findElement(By.className("article-thumb")); | ||
Actions actions = new Actions(driver); | ||
actions.scrollToElement(firstArticleImage).build().perform(); | ||
|
||
WebElement secondArticleImage = driver.findElement(By.cssSelector(".swiper-wrapper div[aria-label='2 / 10']")); | ||
actions.scrollToElement(secondArticleImage).build().perform(); | ||
|
||
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); | ||
try { | ||
FileUtils.copyFile(src, new File("./screenshots/blogpage.png")); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@AfterTest | ||
public void tearDown() { | ||
driver.quit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.