Skip to content

Commit

Permalink
Added example code for taking screenshots (#141)
Browse files Browse the repository at this point in the history
* 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
mfaisalkhatri authored Sep 1, 2024
1 parent e2fb859 commit adbcaf8
Show file tree
Hide file tree
Showing 23 changed files with 288 additions and 42 deletions.
24 changes: 12 additions & 12 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ jobs:

steps:
- name: checkout Git repository
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Install Java and Maven
uses: actions/setup-java@v2
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'adopt'
cache: maven

- name: Setup Juice Shop using docker compose
run: docker-compose -f ${{ github.workspace }}/docker-compose-v3-juiceshop.yml up -d
run: docker compose -f ${{ github.workspace }}/docker-compose-v3-juiceshop.yml up -d

- name: Start Selenium Grid 4
run: docker compose -f ${{ github.workspace }}/docker-compose-v3-seleniumgrid.yml up --scale chrome=4 -d
Expand All @@ -52,10 +52,10 @@ jobs:
mvn org.jacoco:jacoco-maven-plugin:prepare-agent install -Pcoverage-per-test -Dheadless=true -DLT_USERNAME=$LMT_USER -DLT_ACCESS_KEY=$LMT_ACCESS
- name: Stop docker compose for Juice Shop and Selenium grid
run: docker-compose -f ${{ github.workspace }}/docker-compose-v3-juiceshop.yml down --remove-orphans
run: docker compose -f ${{ github.workspace }}/docker-compose-v3-juiceshop.yml down --remove-orphans

- name: Upload target folder
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: target
path: |
Expand All @@ -71,19 +71,19 @@ jobs:

steps:
- name: checkout Git repository
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis

- name: Install Java and Maven
uses: actions/setup-java@v2
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'adopt'
cache: maven

- name: Setup Juice Shop using docker compose
run: docker-compose -f ${{ github.workspace }}/docker-compose-v3-juiceshop.yml up -d
run: docker compose -f ${{ github.workspace }}/docker-compose-v3-juiceshop.yml up -d

- name: Start Selenium Grid 4
run: docker compose -f ${{ github.workspace }}/docker-compose-v3-seleniumgrid.yml up --scale chrome=4 -d
Expand All @@ -95,21 +95,21 @@ jobs:
uses: browser-actions/setup-firefox@latest

- name: Cache SonarCloud packages
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar

- name: Cache Maven packages
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2

- name: Download target folder
uses: actions/download-artifact@v2
uses: actions/download-artifact@v4
with:
name: target

Expand All @@ -125,7 +125,7 @@ jobs:
-Dsonar.projectKey=$SONAR_KEY -Dheadless=true -DLT_USERNAME=$LMT_USER -DLT_ACCESS_KEY=$LMT_ACCESS
- name: Stop docker compose for Juice Shop and Selenium grid
run: docker-compose -f ${{ github.workspace }}/docker-compose-v3-juiceshop.yml down --remove-orphans
run: docker compose -f ${{ github.workspace }}/docker-compose-v3-juiceshop.yml down --remove-orphans

- name: Test Report
uses: dorny/test-reporter@v1
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
/test-output/
/logs/
/reports/
/screenshots

# Mac OSX
.DS_Store
Expand Down
20 changes: 14 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<selenium.java.version>4.21.0</selenium.java.version>
<selenium.java.version>4.24.0</selenium.java.version>
<testng.version>7.10.2</testng.version>
<webdrivermanager.version>5.8.0</webdrivermanager.version>
<webdrivermanager.version>5.9.2</webdrivermanager.version>
<commons-io.version>2.16.1</commons-io.version>
<log4jcore.version>2.23.1</log4jcore.version>
<log4japi.version>2.23.1</log4japi.version>
<orgjson.version>20240303</orgjson.version>
<seleniumdevtools.version>4.21.0</seleniumdevtools.version>
<lombok.version>1.18.32</lombok.version>
<datafaker.version>2.2.2</datafaker.version>
<seleniumdevtools.version>4.23.1</seleniumdevtools.version>
<lombok.version>1.18.34</lombok.version>
<datafaker.version>2.3.1</datafaker.version>
<ashot.version>1.5.4</ashot.version>
<maven-enforcer-plugin.version>3.5.0</maven-enforcer-plugin.version>
<maven.compiler.version>3.13.0</maven.compiler.version>
<surefire-version>3.2.5</surefire-version>
<surefire-version>3.5.0</surefire-version>
<java.release.version>17</java.release.version>
<maven.source.encoding>UTF-8</maven.source.encoding>
<suite-xml>test-suite/testng.xml</suite-xml>
Expand Down Expand Up @@ -109,6 +110,13 @@
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<!--https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot-->
<dependency>
<groupId>ru.yandex.qatools.ashot</groupId>
<artifactId>ashot</artifactId>
<version>${ashot.version}</version>
</dependency>

</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ private static void setupChromeDriver () {
chromePrefs.put ("download.prompt_for_download", "false");
chromePrefs.put ("download.default_directory",
String.valueOf (Paths.get (System.getProperty ("user.home"), "Downloads")));
chromePrefs.put("profile.password_manager_leak_detection", false);

final var options = new ChromeOptions ();
options.addArguments (NO_SANDBOX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void logoutFromWebSite () {
}

private WebElement logoutLink () {
return this.wait.until (ExpectedConditions.elementToBeClickable ((By.cssSelector ("#logout_sidebar_link"))));
return this.wait.until (ExpectedConditions.elementToBeClickable ((By.id ("logout_sidebar_link"))));
}

private WebElement menuBtn () {
Expand Down
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();
}

}
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();
}
}
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/
public class BrowserTests extends BaseSuiteSetup {

private static final String websiteLink = "http://the-internet.herokuapp.com/";
private static final String websiteLink = "https://the-internet.herokuapp.com/";

@Test
public void browserNavigationTests () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class CheckboxTests extends BaseSuiteSetup {

@BeforeClass
public void testSetup () {
final String websiteLink = "http://the-internet.herokuapp.com/";
final String websiteLink = "https://the-internet.herokuapp.com/";
getDriver().get(websiteLink);
final MainPage mainPage = new MainPage();
mainPage.clickLink("Checkboxes");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class DataTableTests extends BaseSuiteSetup {

@BeforeClass
public void testSetup () {
final String websiteLink = "http://the-internet.herokuapp.com/";
final String websiteLink = "https://the-internet.herokuapp.com/";
getDriver().get(websiteLink);
final MainPage mainPage = new MainPage();
mainPage.clickLink("Sortable Data Tables");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class DigestAuthTest extends BaseSuiteSetup {

@BeforeClass
public void testSetup() {
final String websiteLink = "http://the-internet.herokuapp.com/";
final String websiteLink = "https://the-internet.herokuapp.com/";
getDriver().get(websiteLink);
final MainPage mainPage = new MainPage();
mainPage.clickLink("Basic Auth");
Expand Down
Loading

0 comments on commit adbcaf8

Please sign in to comment.