Skip to content

Commit

Permalink
feat(java): ✨ added new screenshot method to get content (#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
WasiqB authored Oct 17, 2024
1 parent 743b364 commit 7b1b596
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 239 deletions.
1 change: 1 addition & 0 deletions .release-it.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"release": true,
"tokenRef": "PUSH_TOKEN",
"assets": ["core-java/target/*.jar"],
"discussionCategoryName": "Announcements",
"comments": {
"submit": true,
"issue": ":rocket: _This issue has been resolved in v${version}. See [${releaseName}](${releaseUrl}) for release notes._",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static org.apache.commons.io.FileUtils.copyFile;
import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.apache.logging.log4j.LogManager.getLogger;
import static org.openqa.selenium.OutputType.BASE64;
import static org.openqa.selenium.OutputType.FILE;

import java.io.File;
Expand All @@ -41,6 +42,7 @@
import com.google.common.truth.StringSubject;
import io.github.boykaframework.actions.interfaces.drivers.IWindowActions;
import io.github.boykaframework.actions.interfaces.listeners.drivers.IWindowActionsListener;
import io.github.boykaframework.config.ui.UISetting;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.TakesScreenshot;
Expand All @@ -67,9 +69,12 @@ public static IWindowActions onWindow () {
}

private final IWindowActionsListener listener;
private final UISetting setting;

private WindowActions () {
this.listener = getSession ().getListener (WINDOW_ACTION);
this.setting = getSession ().getSetting ()
.getUi ();
}

@Override
Expand Down Expand Up @@ -103,6 +108,22 @@ public void fullScreen () {
LOGGER.traceExit ();
}

@Override
public String getScreenshot () {
LOGGER.traceEntry ();
LOGGER.info ("Getting the screenshot content...");
final var screenshotSetting = this.setting.getScreenshot ();
if (!screenshotSetting.isEnabled ()) {
return EMPTY;
}
ofNullable (this.listener).ifPresent (IWindowActionsListener::onGetScreenshot);
final var content = getDriverAttribute (
driver -> ((TakesScreenshot) requireNonNull (driver, DRIVER_CANNOT_BE_NULL)).getScreenshotAs (BASE64),
EMPTY);
LOGGER.traceExit ();
return content;
}

@Override
public String getTitle () {
LOGGER.traceEntry ();
Expand Down Expand Up @@ -176,15 +197,13 @@ public void switchToNew (final WindowType type) {
@Override
public void takeScreenshot () {
LOGGER.info ("Taking the screenshot...");
final var setting = getSession ().getSetting ()
.getUi ()
.getScreenshot ();
if (!setting.isEnabled ()) {
final var screenshotSetting = this.setting.getScreenshot ();
if (!screenshotSetting.isEnabled ()) {
return;
}
final var path = setting.getPath ();
final var prefix = setting.getPrefix ();
final var extension = setting.getExtension ();
final var path = screenshotSetting.getPath ();
final var prefix = screenshotSetting.getPrefix ();
final var extension = screenshotSetting.getExtension ();
final var date = new SimpleDateFormat ("yyyyMMdd-HHmmss");
final var timeStamp = date.format (getInstance ().getTime ());
final var fileName = "%s/%s-%s.%s";
Expand All @@ -195,10 +214,8 @@ public void takeScreenshot () {
public void takeScreenshot (final String fileName) {
LOGGER.traceEntry ();
LOGGER.info ("Taking screenshot and saving at [{}]...", fileName);
final var setting = getSession ().getSetting ()
.getUi ()
.getScreenshot ();
if (!setting.isEnabled ()) {
final var screenshotSetting = this.setting.getScreenshot ();
if (!screenshotSetting.isEnabled ()) {
return;
}
performDriverAction (driver -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ public interface IWindowActions {
*/
void fullScreen ();

/**
* Gets the base 64 content of the screenshot.
*
* @return Image content in Base64 encoded string
*/
String getScreenshot ();

/**
* Title of the browser.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ default void onFullScreen () {
// not implemented.
}

/**
* Handles Get screenshot method.
*/
default void onGetScreenshot () {
// not implemented.
}

/**
* Handles get title method.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static io.github.boykaframework.testng.ui.jiomeet.pages.SignInPage.signInPage;
import static io.github.boykaframework.testng.ui.jiomeet.pages.StartMeetingPage.startMeetingPage;
import static org.openqa.selenium.support.ui.ExpectedConditions.invisibilityOfElementLocated;
import static org.testng.Reporter.log;

import io.github.boykaframework.enums.PlatformType;
import org.testng.ITestResult;
Expand All @@ -49,6 +50,7 @@ public class JioMeetTest {

@AfterMethod
public void afterMethod (final ITestResult result) {
log (onWindow ().getScreenshot ());
if (!result.isSuccess ()) {
onWindow ().takeScreenshot ();
}
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
"@release-it-plugins/lerna-changelog": "^7.0.0",
"@stylistic/eslint-plugin-js": "^2.9.0",
"@stylistic/eslint-plugin-ts": "^2.9.0",
"@types/node": "^22.7.5",
"@typescript-eslint/eslint-plugin": "^8.8.1",
"@typescript-eslint/parser": "^8.8.1",
"@types/node": "^22.7.6",
"@typescript-eslint/eslint-plugin": "^8.9.0",
"@typescript-eslint/parser": "^8.9.0",
"commitlint": "^19.5.0",
"eslint": "^9.12.0",
"eslint-config-google": "^0.14.0",
Expand All @@ -56,14 +56,14 @@
"lerna-version": "^6.6.2",
"lint-staged": "^15.2.10",
"lodash": "^4.17.21",
"nx": "^20.0.0",
"nx": "^20.0.1",
"prettier": "^3.3.3",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"release-it": "^17.8.2",
"release-it": "^17.10.0",
"ts-node": "^10.9.2",
"typescript": "^5.6.3",
"typescript-eslint": "^8.8.1"
"typescript-eslint": "^8.9.0"
},
"scripts": {
"preinstall": "npx only-allow pnpm",
Expand Down
Loading

0 comments on commit 7b1b596

Please sign in to comment.