Skip to content

Mobile screenshot plugin fix #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,4 @@ public static void close() {

DISPOSED.set(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@
import org.openqa.selenium.TakesScreenshot;
import plugins.screenshots.ScreenshotPlugin;
import plugins.screenshots.ScreenshotPluginEventArgs;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
import solutions.bellatrix.android.configuration.AndroidSettings;
import solutions.bellatrix.core.configuration.ConfigurationService;
import solutions.bellatrix.core.utilities.Log;
import solutions.bellatrix.core.utilities.PathNormalizer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.UUID;

public class MobileScreenshotPlugin extends ScreenshotPlugin {
Expand All @@ -42,38 +48,44 @@ public String takeScreenshot(String name) {
var screenshotSaveDir = getOutputFolder();
var filename = getUniqueFileName(name);

var screenshot = ((TakesScreenshot)DriverService.getWrappedAndroidDriver()).getScreenshotAs(OutputType.BASE64);
var screenshot = new AShot()
.shootingStrategy(ShootingStrategies.simple())
.takeScreenshot(DriverService.getWrappedAndroidDriver());

var path = Paths.get(screenshotSaveDir, filename) + ".png";

var file = new File(path);

try (FileWriter writer = new FileWriter(file)) {
writer.write(screenshot);
var path = Paths.get(screenshotSaveDir, filename).toString();
var destFile = new File(path);
Log.info("Saving screenshot with path: " + destFile);
try {
ImageIO.write(screenshot.getImage(), "png", destFile);
} catch (IOException e) {
e.printStackTrace();
Log.error(e.toString());
}

SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, screenshot));
return screenshot;
var base64image = bufferedImageToBase64(screenshot.getImage());

SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, base64image));
return base64image;
}

@Override
public String takeScreenshot(String screenshotSaveDir, String filename) {
var screenshot = ((TakesScreenshot)DriverService.getWrappedAndroidDriver()).getScreenshotAs(OutputType.BASE64);

var path = Paths.get(screenshotSaveDir, filename) + ".png";

var file = new File(path);

try (FileWriter writer = new FileWriter(file)) {
writer.write(screenshot);
var screenshot = new AShot()
.shootingStrategy(ShootingStrategies.simple())
.takeScreenshot(DriverService.getWrappedAndroidDriver());

var path = Paths.get(screenshotSaveDir, filename).toString();
var destFile = new File(path);
Log.info("Saving screenshot with path: " + destFile);
try {
ImageIO.write(screenshot.getImage(), "png", destFile);
} catch (IOException e) {
e.printStackTrace();
Log.error(e.toString());
}

SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, screenshot));
return screenshot;
var base64image = bufferedImageToBase64(screenshot.getImage());

SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, base64image));
return base64image;
}

@Override
Expand All @@ -93,4 +105,23 @@ protected String getOutputFolder() {
protected String getUniqueFileName(String testName) {
return testName.concat(UUID.randomUUID().toString()).concat(".png");
}
}

private static String bufferedImageToBase64(BufferedImage image) {
String base64String = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
ImageIO.write(image, "png", outputStream);
byte[] imageBytes = outputStream.toByteArray();
base64String = Base64.getEncoder().encodeToString(imageBytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return base64String;
}
}