Skip to content

Commit

Permalink
[plugin-web-app-playwright] Add screenshot taking steps (#5256)
Browse files Browse the repository at this point in the history
Co-authored-by: draker94 <noreply@github.com>
  • Loading branch information
draker94 and web-flow authored Jul 30, 2024
1 parent 68b372a commit 2c59e88
Show file tree
Hide file tree
Showing 33 changed files with 475 additions and 63 deletions.
2 changes: 2 additions & 0 deletions docs/modules/plugins/pages/plugin-web-app-playwright.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -639,3 +639,5 @@ When I execute sequence of actions:
====

include::partial$common-web-app-browser-steps.adoc[]

include::partial$common-ui-screenshot-steps.adoc[]
31 changes: 31 additions & 0 deletions docs/modules/plugins/partials/common-ui-screenshot-steps.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
=== Taking screenshot steps
==== Take screenshot

Takes a screenshot and publish it to the report.

[source,gherkin]
----
When I take screenshot
----

==== Take screenshot and save it to the file

Takes a screenshot and saves it to the file at the specified path.

TIP: The full path to the file with the taken screenshot is posted to logs and report.

[source,gherkin]
----
When I take screenshot and save it to file at path `$screenshotFilePath`
----

* `$screenshotFilePath` - The path to the file to save the screenshot, the allowed values are:
+
** an absolute path to the file, e.g. `C:\Windows\screenshot.png`;
** a relative path (it is resolved from the current working directory, e.g. `screenshot.png`.

.Take a screenshot and save it to the file
[source,gherkin]
----
When I take screenshot and save it to file at path `${screenshot-directory}/start-page.png`
----
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.vividus.selenium.IWebDriverProvider;
import org.vividus.ui.screenshot.ScreenshotFileNameGenerator;
import org.vividus.ui.screenshot.ScreenshotParameters;
import org.vividus.ui.screenshot.ScreenshotTaker;

import pazone.ashot.AShot;
import pazone.ashot.util.ImageTool;
Expand All @@ -33,12 +35,12 @@ public abstract class AbstractScreenshotTaker<T extends ScreenshotParameters>
implements ScreenshotTaker, AshotScreenshotTaker<T>
{
private final IWebDriverProvider webDriverProvider;
private final IScreenshotFileNameGenerator screenshotFileNameGenerator;
private final ScreenshotFileNameGenerator screenshotFileNameGenerator;
private final AshotFactory<T> ashotFactory;
private final ScreenshotDebugger screenshotDebugger;

protected AbstractScreenshotTaker(IWebDriverProvider webDriverProvider,
IScreenshotFileNameGenerator screenshotFileNameGenerator, AshotFactory<T> ashotFactory,
ScreenshotFileNameGenerator screenshotFileNameGenerator, AshotFactory<T> ashotFactory,
ScreenshotDebugger screenshotDebugger)
{
this.webDriverProvider = webDriverProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,16 @@
package org.vividus.selenium.screenshot;

import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Dimension;
import org.vividus.selenium.manager.IGenericWebDriverManager;
import org.vividus.ui.screenshot.ScreenshotFileNameGenerator;

import jakarta.inject.Inject;

public class ScreenshotFileNameGenerator implements IScreenshotFileNameGenerator
public class SeleniumScreenshotFileNameGenerator implements ScreenshotFileNameGenerator
{
private static final String DEFAULT_IMAGE_FORMAT = "png";

private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss_SSS")
.withZone(ZoneId.systemDefault());

@Inject private IGenericWebDriverManager webDriverManager;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,11 +30,11 @@
import org.vividus.reporter.event.AttachmentPublishEvent;
import org.vividus.reporter.model.Attachment;
import org.vividus.selenium.screenshot.AshotScreenshotTaker;
import org.vividus.selenium.screenshot.ScreenshotTaker;
import org.vividus.softassert.ISoftAssert;
import org.vividus.ui.action.BarcodeActions;
import org.vividus.ui.context.IUiContext;
import org.vividus.ui.screenshot.ScreenshotParameters;
import org.vividus.ui.screenshot.ScreenshotTaker;
import org.vividus.variable.VariableScope;

import pazone.ashot.Screenshot;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,7 +26,7 @@
import org.vividus.report.ui.ImageCompressor;
import org.vividus.reporter.model.Attachment;
import org.vividus.selenium.IWebDriverProvider;
import org.vividus.selenium.screenshot.Screenshot;
import org.vividus.ui.screenshot.Screenshot;

public abstract class AbstractPublishingScreenshotOnFailureMonitor extends AbstractPublishingAttachmentOnFailureMonitor
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,8 +23,8 @@
import org.vividus.context.RunContext;
import org.vividus.report.ui.ImageCompressor;
import org.vividus.selenium.IWebDriverProvider;
import org.vividus.selenium.screenshot.Screenshot;
import org.vividus.selenium.screenshot.ScreenshotTaker;
import org.vividus.ui.screenshot.Screenshot;
import org.vividus.ui.screenshot.ScreenshotTaker;

public class PublishingScreenshotOnFailureMonitor extends AbstractPublishingScreenshotOnFailureMonitor
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@

<bean class="org.vividus.ui.listener.UiContextListener" lazy-init="false" />

<bean class="org.vividus.selenium.screenshot.ScreenshotFileNameGenerator" />
<bean class="org.vividus.selenium.screenshot.SeleniumScreenshotFileNameGenerator" />

<bean id="genericElementSteps" class="org.vividus.steps.ui.GenericElementSteps" />
<bean id="genericWaitSteps" class="org.vividus.steps.ui.GenericWaitSteps" />
<bean id="genericSetVariableSteps" class="org.vividus.steps.ui.GenericSetVariableSteps" />
<bean id="genericSetContextSteps" class="org.vividus.steps.ui.GenericSetContextSteps" />
<bean id="genericNestedSteps" class="org.vividus.steps.ui.GenericNestedSteps" />
<bean id="navigationSteps" class="org.vividus.steps.ui.NavigationSteps" />
<bean id="screenshotTakingSteps" class="org.vividus.steps.ui.ScreenshotTakingSteps" />
<bean id="screenshotTakingSteps" class="org.vividus.ui.steps.ScreenshotTakingSteps" />
<bean id="barcodeSteps" class="org.vividus.steps.ui.BarcodeSteps" />

<bean class="org.vividus.ui.action.search.IndexElementFilter" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.vividus.selenium.IWebDriverProvider;
import org.vividus.ui.screenshot.Screenshot;
import org.vividus.ui.screenshot.ScreenshotFileNameGenerator;
import org.vividus.ui.screenshot.ScreenshotParameters;
import org.vividus.util.ResourceUtils;

Expand All @@ -59,7 +61,7 @@ class AbstractScreenshotTakerTests

@Mock private IWebDriverProvider webDriverProvider;
@Mock private TakesScreenshot takesScreenshot;
@Mock private IScreenshotFileNameGenerator screenshotFileNameGenerator;
@Mock private ScreenshotFileNameGenerator screenshotFileNameGenerator;
@Mock private ScreenshotDebugger screenshotDebugger;
@Mock private AshotFactory<ScreenshotParameters> ashotFactory;
@InjectMocks private TestScreenshotTaker testScreenshotTaker;
Expand Down Expand Up @@ -170,7 +172,7 @@ void shouldNotCutScreenshotWhenCutsIsNotSet()
private static class TestScreenshotTaker extends AbstractScreenshotTaker<ScreenshotParameters>
{
TestScreenshotTaker(IWebDriverProvider webDriverProvider,
IScreenshotFileNameGenerator screenshotFileNameGenerator,
ScreenshotFileNameGenerator screenshotFileNameGenerator,
AshotFactory<ScreenshotParameters> ashotFactory, ScreenshotDebugger screenshotDebugger)
{
super(webDriverProvider, screenshotFileNameGenerator, ashotFactory, screenshotDebugger);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,7 +30,7 @@
import org.vividus.selenium.manager.IGenericWebDriverManager;

@ExtendWith(MockitoExtension.class)
class ScreenshotFileNameGeneratorTests
class SeleniumScreenshotFileNameGeneratorTests
{
private static final String FILE_DATE_PATTERN = "\\d{4}-\\d{2}-\\d{2}_\\d{2}-\\d{2}-\\d{2}_\\d{3}-";
private static final String SEPARATOR = "-";
Expand All @@ -41,14 +41,14 @@ class ScreenshotFileNameGeneratorTests
private static final String WIDTH_X_HEIGHT_EXTENSION = SEPARATOR + WIDTH + "x" + HEIGHT + ".png";

@Mock private IGenericWebDriverManager webDriverManager;
@InjectMocks private ScreenshotFileNameGenerator screenshotFileNameGenerator;
@InjectMocks private SeleniumScreenshotFileNameGenerator seleniumScreenshotFileNameGenerator;

@Test
void testGenerateScreenshotFileNameWithoutBrowserName()
{
Dimension dimension = new Dimension(WIDTH, HEIGHT);
when(webDriverManager.getSize()).thenReturn(dimension);
String screenshotFileName = screenshotFileNameGenerator.generateScreenshotFileName(SCREENSHOT_NAME);
String screenshotFileName = seleniumScreenshotFileNameGenerator.generateScreenshotFileName(SCREENSHOT_NAME);
assertTrue(screenshotFileName.matches(FILE_DATE_PATTERN + SCREENSHOT_NAME + WIDTH_X_HEIGHT_EXTENSION));
}

Expand All @@ -61,7 +61,7 @@ void testGenerateScreenshotFileNameWithBrowserName()
when(webDriverManager.getCapabilities()).thenReturn(capabilities);
String browserName = "firefox";
when(capabilities.getBrowserName()).thenReturn(browserName);
String screenshotFileName = screenshotFileNameGenerator.generateScreenshotFileName(SCREENSHOT_NAME);
String screenshotFileName = seleniumScreenshotFileNameGenerator.generateScreenshotFileName(SCREENSHOT_NAME);
assertTrue(screenshotFileName
.matches(FILE_DATE_PATTERN + SCREENSHOT_NAME + SEPARATOR + browserName + WIDTH_X_HEIGHT_EXTENSION));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -46,10 +46,10 @@
import org.vividus.reporter.event.AttachmentPublishEvent;
import org.vividus.reporter.model.Attachment;
import org.vividus.selenium.screenshot.AshotScreenshotTaker;
import org.vividus.selenium.screenshot.ScreenshotTaker;
import org.vividus.softassert.ISoftAssert;
import org.vividus.ui.action.BarcodeActions;
import org.vividus.ui.context.IUiContext;
import org.vividus.ui.screenshot.ScreenshotTaker;
import org.vividus.variable.VariableScope;

import pazone.ashot.Screenshot;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -65,8 +65,8 @@
import org.vividus.reporter.event.AttachmentPublishEvent;
import org.vividus.reporter.model.Attachment;
import org.vividus.selenium.IWebDriverProvider;
import org.vividus.selenium.screenshot.Screenshot;
import org.vividus.softassert.event.AssertionFailedEvent;
import org.vividus.ui.screenshot.Screenshot;

@ExtendWith({ MockitoExtension.class, TestLoggerFactoryExtension.class })
class AbstractPublishingScreenshotOnFailureMonitorTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,8 +32,8 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.vividus.context.RunContext;
import org.vividus.selenium.IWebDriverProvider;
import org.vividus.selenium.screenshot.Screenshot;
import org.vividus.selenium.screenshot.ScreenshotTaker;
import org.vividus.ui.screenshot.Screenshot;
import org.vividus.ui.screenshot.ScreenshotTaker;

@ExtendWith(MockitoExtension.class)
class PublishingScreenshotOnFailureMonitorTests
Expand Down
8 changes: 8 additions & 0 deletions vividus-extension-ui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ project.description = 'VIVIDUS extension for plugins testing application with gr

dependencies {
implementation project(':vividus-engine')
implementation project(':vividus-reporter')
implementation project(':vividus-util')

implementation(group: 'com.google.guava', name: 'guava', version: '33.2.1-jre')
implementation platform(group: 'org.slf4j', name: 'slf4j-bom', version: '2.0.13')
implementation(group: 'org.slf4j', name: 'slf4j-api')

testImplementation platform(group: 'org.junit', name: 'junit-bom', version: '5.10.3')
testImplementation(group: 'org.junit.jupiter', name: 'junit-jupiter')
testImplementation platform(group: 'org.mockito', name: 'mockito-bom', version: '5.12.0')
testImplementation(group: 'org.mockito', name: 'mockito-junit-jupiter')
testImplementation(group: 'com.github.valfirst', name: 'slf4j-test', version: '3.0.1')
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.vividus.selenium.screenshot;
package org.vividus.ui.screenshot;

public class Screenshot
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,9 +14,16 @@
* limitations under the License.
*/

package org.vividus.selenium.screenshot;
package org.vividus.ui.screenshot;

public interface IScreenshotFileNameGenerator
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public interface ScreenshotFileNameGenerator
{
String DEFAULT_IMAGE_FORMAT = "png";
DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss_SSS")
.withZone(ZoneId.systemDefault());

String generateScreenshotFileName(String screenshotName);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.vividus.selenium.screenshot;
package org.vividus.ui.screenshot;

import java.awt.image.BufferedImage;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.vividus.steps.ui;
package org.vividus.ui.steps;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -28,7 +28,7 @@
import org.slf4j.LoggerFactory;
import org.vividus.reporter.event.AttachmentPublishEvent;
import org.vividus.reporter.model.Attachment;
import org.vividus.selenium.screenshot.ScreenshotTaker;
import org.vividus.ui.screenshot.ScreenshotTaker;

public class ScreenshotTakingSteps
{
Expand Down
Loading

0 comments on commit 2c59e88

Please sign in to comment.