Best Testsigma code snippet using com.testsigma.automator.exceptions.TestsigmaScreenShotException
Source:ScreenCaptureUtil.java
...3import com.fasterxml.jackson.databind.node.ObjectNode;4import com.testsigma.automator.constants.EnvSettingsConstants;5import com.testsigma.automator.constants.StorageConstants;6import com.testsigma.automator.entity.TestDeviceSettings;7import com.testsigma.automator.exceptions.TestsigmaScreenShotException;8import com.testsigma.automator.runners.EnvironmentRunner;9import lombok.RequiredArgsConstructor;10import lombok.extern.log4j.Log4j2;11import org.apache.commons.io.FileUtils;12import org.apache.commons.io.FilenameUtils;13import org.openqa.selenium.*;14import ru.yandex.qatools.ashot.AShot;15import ru.yandex.qatools.ashot.Screenshot;16import ru.yandex.qatools.ashot.shooting.ShootingStrategies;17import javax.imageio.ImageIO;18import java.awt.Rectangle;19import java.awt.*;20import java.awt.image.BufferedImage;21import java.io.File;22import java.io.IOException;23import java.net.MalformedURLException;24import java.util.List;25import java.util.Map;26@Log4j227@RequiredArgsConstructor28public class ScreenCaptureUtil {29 public static String JPEG = "jpeg";30 public List<ObjectNode> screenshots;31 JsonNodeFactory jnf = JsonNodeFactory.instance;32 public ScreenCaptureUtil(List<ObjectNode> screenshots) {33 this.screenshots = screenshots;34 }35 public void takeScreenShot(WebDriver webdriver, String loalFolderPath, String relativePath) throws Exception {36 try {37 byte[] srcFile = ((TakesScreenshot) webdriver).getScreenshotAs(OutputType.BYTES);38 saveScreenshotFile(srcFile, loalFolderPath, relativePath);39 } catch (WebDriverException e) {40 log.debug("Exception in taking screenshot using WebDriver. Details :: " + e.getMessage());41 log.error(e.getMessage(), e);42 if (e instanceof UnhandledAlertException) {43 log.debug("The Exception is caused by Unhandled Alert.");44 takeScreenShot(loalFolderPath, relativePath);45 }46 } catch (Exception e) {47 log.debug("Exception while Tacking screenshot" + e);48 log.error(e.getMessage(), e);49 }50 }51 public void takeScreenShot(String localFolderPath, String relativePath) throws Exception {52 Integer SCREEN_WIDTH = (int) Toolkit.getDefaultToolkit()53 .getScreenSize().getWidth();54 Integer SCREEN_HEIGHT = (int) Toolkit.getDefaultToolkit()55 .getScreenSize().getHeight();56 BufferedImage img = new Robot().createScreenCapture(new Rectangle(SCREEN_WIDTH, SCREEN_HEIGHT));57 saveScreenshotFile(img, localFolderPath, relativePath);58 }59 public void createScreenshotsFolder() {60 TestDeviceSettings settings = EnvironmentRunner.getRunnerEnvironmentEntity().getEnvSettings();61 Long envRunId = EnvironmentRunner.getRunnerEnvironmentRunResult().getId();62 StringBuffer screenshots = new StringBuffer(PathUtil.getInstance().getScreenshotsPath());63 File f = new File(PathUtil.getInstance().getScreenshotsPath());64 f.mkdirs();65 String path = screenshots.append(File.separator).append(envRunId).toString();66 settings.setScreenshotLocalPath(path);67 }68 public void deleteScreenshotsFolder(Map<String, String> settings) {69 String path = settings.get(EnvSettingsConstants.KEY_SCREENSHOT_LOCAL_PATH);70 try {71 FileUtils.forceDelete(new File(path));72 } catch (IOException e) {73 log.error("Exception while deleting files :: " + path, e);74 }75 }76 private void saveScreenshotFile(BufferedImage img, String localFolderPath, String relativePath) {77 try {78 String path = getLocalScreenShotPath(localFolderPath, relativePath);79 File f = new File(path);80 f.createNewFile();81 ImageIO.write(img, JPEG, f);82 saveScreenshotPaths(path, relativePath);83 } catch (IOException e) {84 log.error("Exception while saving screenshot :: " + relativePath, e);85 }86 }87 private void saveScreenshotFile(byte[] srcFile, String localFolderPath, String relativePath) {88 File f;89 try {90 String path = getLocalScreenShotPath(localFolderPath, relativePath);91 f = new File(path);92// f.createNewFile();93 FileUtils.writeByteArrayToFile(f, srcFile);94 saveScreenshotPaths(path, relativePath);95 } catch (IOException e) {96 log.error("Exception while saving screenshot :: " + relativePath, e);97 }98 }99 private void saveScreenshotPaths(String srcFile, String relativePath) {100 ObjectNode image = jnf.objectNode();101 image.put(StorageConstants.LOCAL_FILE_PATH, srcFile);102 image.put(StorageConstants.STORAGE_FILE_PATH, relativePath);103 this.screenshots.add(image);104 }105 public void screenShotWithURL(String localFolderPath, String relativePath, WebDriver webdriver) throws TestsigmaScreenShotException {106 try {107 String url = webdriver.getCurrentUrl();108 String path = getLocalScreenShotPath(localFolderPath, relativePath);109 File file = new File(path);110 Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.simple()).takeScreenshot(webdriver);111 saveImageWithUrl(url, file, screenshot);112 saveScreenshotPaths(path, relativePath);113 } catch (Exception e) {114 log.error(e.getMessage(), e);115 throw new TestsigmaScreenShotException("Unable to take ScreenShot. Details :" + e.getMessage());116 }117 }118 public void fullPageScreenshotWithURL(String localFolderPath, String relativePath, WebDriver webdriver) throws TestsigmaScreenShotException {119 try {120 log.debug("Fetching url");121 String url = webdriver.getCurrentUrl();122 log.debug("Fetched url" + url);123 String path = getLocalScreenShotPath(localFolderPath, relativePath);124 log.debug("LocalScreenshot path" + path);125 File file = new File(path);126 log.debug("Before taking screenshot");127 Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(webdriver);128 log.debug("After taking screenshot");129 saveImageWithUrl(url, file, screenshot);130 log.debug("before saving image");131 saveScreenshotPaths(path, relativePath);132 log.debug("save screenshot path");133 } catch (Exception e) {134 log.error(e.getMessage(), e);135 throw new TestsigmaScreenShotException("Unable to take ScreenShot. Details :" + e.getMessage());136 }137 }138 private void saveImageWithUrl(String url, File file, Screenshot screenshot) {139 try {140 BufferedImage bufferedImage = ensureOpaque(screenshot.getImage());141 Graphics graphics = bufferedImage.getGraphics();142 graphics.setColor(Color.RED);143 graphics.setFont(new Font("Arial Black", Font.CENTER_BASELINE, 14));144 graphics.drawString(url, 20, 25);145 ImageIO.write(bufferedImage, JPEG, file);146 } catch (Exception e) {147 log.error(e.getMessage(), e);148 }149 }...
Source:TestsigmaScreenShotException.java
...4import lombok.extern.log4j.Log4j2;5@Log4j26@Getter7@Setter8public class TestsigmaScreenShotException extends AutomatorException {9 private Integer errorCode;10 private String message;11 private String dispMessage;12 public TestsigmaScreenShotException(Integer errorCode) {13 super(errorCode);14 this.errorCode = errorCode;15 log.error(errorCode);16 }17 public TestsigmaScreenShotException(Exception ex) {18 super(ex);19 this.dispMessage = ex.getLocalizedMessage();20 this.message = ex.getMessage();21 log.error(ex);22 }23 public TestsigmaScreenShotException(String msg, Exception ex) {24 super(msg, ex);25 this.dispMessage = msg;26 this.message = msg;27 log.error(msg, ex);28 }29 public TestsigmaScreenShotException(String exceptionMessage) {30 super(exceptionMessage);31 errorCode = 0;32 this.message = exceptionMessage;33 log.error(message);34 }35 public TestsigmaScreenShotException(Integer errorCode, String message) {36 super(errorCode, message);37 this.errorCode = errorCode;38 this.message = message;39 this.dispMessage = message;40 log.error(message);41 }42}...
TestsigmaScreenShotException
Using AI Code Generation
1package com.testsigma.automator.exceptions;2import java.io.File;3import org.openqa.selenium.OutputType;4import org.openqa.selenium.TakesScreenshot;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.io.FileHandler;7import com.testsigma.automator.utils.TestsigmaLogger;8public class TestsigmaScreenShotException extends Exception {9 private static final long serialVersionUID = 1L;10 private String exceptionMessage;11 private String screenShotPath;12 public TestsigmaScreenShotException(String message, WebDriver driver) {13 this.exceptionMessage = message;14 this.screenShotPath = getScreenShot(driver);15 }16 public String getExceptionMessage() {17 return exceptionMessage;18 }19 public String getScreenShotPath() {20 return screenShotPath;21 }22 private String getScreenShot(WebDriver driver) {23 try {24 File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);25 File destFile = new File("screenshots/" + System.currentTimeMillis() + ".png");26 FileHandler.copy(srcFile, destFile);27 return destFile.getAbsolutePath();28 } catch (Exception e) {29 TestsigmaLogger.logError("Error while taking screenshot");30 return null;31 }32 }33}34package com.testsigma.automator.exceptions;35import org.openqa.selenium.WebDriver;36public class TestsigmaException extends Exception {37 private static final long serialVersionUID = 1L;38 private String exceptionMessage;39 public TestsigmaException(String message) {40 this.exceptionMessage = message;41 }42 public String getExceptionMessage() {43 return exceptionMessage;44 }45}46package com.testsigma.automator.exceptions;47import org.openqa.selenium.WebDriver;48public class TestsigmaException extends Exception {49 private static final long serialVersionUID = 1L;50 private String exceptionMessage;51 public TestsigmaException(String message) {52 this.exceptionMessage = message;53 }54 public String getExceptionMessage() {55 return exceptionMessage;56 }57}58package com.testsigma.automator.exceptions;59import org.openqa.selenium.WebDriver;
TestsigmaScreenShotException
Using AI Code Generation
1package com.testsigma.automator.exceptions;2public class TestsigmaScreenShotException extends Exception {3private static final long serialVersionUID = 1L;4private String message;5public TestsigmaScreenShotException(String message) {6super(message);7this.message = message;8}9public String getMessage() {10return message;11}12}13package com.testsigma.automator.exceptions;14public class TestsigmaScreenShotException extends Exception {15private static final long serialVersionUID = 1L;16private String message;17public TestsigmaScreenShotException(String message) {18super(message);19this.message = message;20}21public String getMessage() {22return message;23}24}25package com.testsigma.automator.exceptions;26public class TestsigmaScreenShotException extends Exception {27private static final long serialVersionUID = 1L;28private String message;29public TestsigmaScreenShotException(String message) {30super(message);31this.message = message;32}33public String getMessage() {34return message;35}36}37package com.testsigma.automator.exceptions;38public class TestsigmaScreenShotException extends Exception {39private static final long serialVersionUID = 1L;40private String message;41public TestsigmaScreenShotException(String message) {42super(message);43this.message = message;44}45public String getMessage() {46return message;47}48}49package com.testsigma.automator.exceptions;50public class TestsigmaScreenShotException extends Exception {51private static final long serialVersionUID = 1L;52private String message;53public TestsigmaScreenShotException(String message) {54super(message);55this.message = message;56}57public String getMessage() {58return message;59}60}61package com.testsigma.automator.exceptions;62public class TestsigmaScreenShotException extends Exception {63private static final long serialVersionUID = 1L;64private String message;65public TestsigmaScreenShotException(String message) {66super(message);67this.message = message;68}
TestsigmaScreenShotException
Using AI Code Generation
1import com.testsigma.automator.exceptions.TestsigmaScreenShotException;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.chrome.ChromeDriver;4import org.testng.annotations.Test;5public class TestsigmaScreenShotExceptionTest {6 public void testScreenshot() throws TestsigmaScreenShotException {7 System.setProperty("webdriver.chrome.driver", "path-to-chromedriver");8 WebDriver driver = new ChromeDriver();9 throw new TestsigmaScreenShotException(driver, "This is a test");10 }11}12import com.testsigma.automator.exceptions.TestsigmaScreenShotException;13import org.openqa.selenium.WebDriver;14import org.openqa.selenium.chrome.ChromeDriver;15import org.testng.annotations.Test;16public class TestsigmaScreenShotExceptionTest {17 public void testScreenshot() throws TestsigmaScreenShotException {18 System.setProperty("webdriver.chrome.driver", "path-to-chromedriver");19 WebDriver driver = new ChromeDriver();20 throw new TestsigmaScreenShotException(driver, "This is a test", true);21 }22}23import com.testsigma.automator.exceptions.TestsigmaScreenShotException;24import org.openqa.selenium.WebDriver;25import org.openqa.selenium.chrome.ChromeDriver;26import org.testng.annotations.Test;27public class TestsigmaScreenShotExceptionTest {28 public void testScreenshot() throws TestsigmaScreenShotException {29 System.setProperty("webdriver.chrome.driver", "path-to-chromedriver");30 WebDriver driver = new ChromeDriver();31 throw new TestsigmaScreenShotException(driver, "This is a test", true, "Google");32 }33}
TestsigmaScreenShotException
Using AI Code Generation
1import com.testsigma.automator.exceptions.TestsigmaScreenShotException;2import com.testsigma.automator.core.TestsigmaAutomator;3public class TestsigmaScreenShotExceptionExample {4public static void main(String[] args) throws TestsigmaScreenShotException {5 TestsigmaAutomator automator = new TestsigmaAutomator();6 automator.start();7 try {8 automator.click("adsf");9 } catch (Exception e) {10 throw new TestsigmaScreenShotException("Element not found", e);11 } finally {12 automator.stop();13 }14}15}16import com.testsigma.automator.exceptions.TestsigmaScreenShotException;17import com.testsigma.automator.core.TestsigmaAutomator;18public class TestsigmaScreenShotExceptionExample {19public static void main(String[] args) throws TestsigmaScreenShotException {20 TestsigmaAutomator automator = new TestsigmaAutomator();21 automator.start();22 try {23 automator.click("adsf");24 } catch (Exception e) {25 throw new TestsigmaScreenShotException("Element not found", e);26 } finally {27 automator.stop();28 }29}30}31import com.testsigma.automator.exceptions.TestsigmaScreenShotException;32import com.testsigma.automator.core.TestsigmaAutomator;33public class TestsigmaScreenShotExceptionExample {34public static void main(String[] args) throws TestsigmaScreenShotException {35 TestsigmaAutomator automator = new TestsigmaAutomator();36 automator.start();37 try {38 automator.click("adsf");39 } catch (Exception e) {40 throw new TestsigmaScreenShotException("Element not found", e);41 } finally {42 automator.stop();43 }44}45}46import com.testsigma.automator.exceptions.TestsigmaScreenShotException;47import com.testsigma.automator.core.TestsigmaAutomator;
TestsigmaScreenShotException
Using AI Code Generation
1import com.testsigma.automator.exceptions.TestsigmaScreenShotException;2public class ScreenShotException {3 public static void main(String[] args) {4 try {5 throw new TestsigmaScreenShotException("Screen shot exception");6 } catch (TestsigmaScreenShotException e) {7 e.printStackTrace();8 }9 }10}11import com.testsigma.automator.exceptions.TestsigmaScreenShotException;12public class ScreenShotException {13 public static void main(String[] args) {14 try {15 throw new TestsigmaScreenShotException("Screen shot exception");16 } catch (TestsigmaScreenShotException e) {17 e.printStackTrace();18 }19 }20}21import com.testsigma.automator.exceptions.TestsigmaScreenShotException;22public class ScreenShotException {23 public static void main(String[] args) {24 try {25 throw new TestsigmaScreenShotException("Screen shot exception");26 } catch (TestsigmaScreenShotException e) {27 e.printStackTrace();28 }29 }30}31import com.testsigma.automator.exceptions.TestsigmaScreenShotException;32public class ScreenShotException {33 public static void main(String[] args) {34 try {35 throw new TestsigmaScreenShotException("Screen shot exception");36 } catch (TestsigmaScreenShotException e) {37 e.printStackTrace();38 }39 }40}41import com.testsigma.automator.exceptions.TestsigmaScreenShotException;42public class ScreenShotException {43 public static void main(String[] args) {44 try {45 throw new TestsigmaScreenShotException("Screen shot exception");46 } catch (TestsigmaScreenShotException e) {47 e.printStackTrace();48 }49 }50}51import com.testsigma.automator.exceptions.TestsigmaScreenShotException;52public class ScreenShotException {
TestsigmaScreenShotException
Using AI Code Generation
1package com.testsigma.automator.exceptions;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import com.testsigma.automator.utils.TestsigmaScreenShotException;5public class TestsigmaScreenShotExceptionTest {6 public static void main(String[] args) {7 WebDriver driver = null;8 WebElement element = null;9 try {10 element.click();11 } catch (Exception e) {12 throw new TestsigmaScreenShotException("TestsigmaScreenShotException", e);13 }14 }15}16package com.testsigma.automator.exceptions;17import org.openqa.selenium.WebDriver;18import org.openqa.selenium.WebElement;19import com.testsigma.automator.utils.TestsigmaScreenShotException;20public class TestsigmaScreenShotExceptionTest {21 public static void main(String[] args) {22 WebDriver driver = null;23 WebElement element = null;24 try {25 element.click();26 } catch (Exception e) {27 throw new TestsigmaScreenShotException("TestsigmaScreenShotException", e);28 }29 }30}31package com.testsigma.automator.exceptions;32import org.openqa.selenium.WebDriver;33import org.openqa.selenium.WebElement;34import com.testsigma.automator.utils.TestsigmaScreenShotException;35public class TestsigmaScreenShotExceptionTest {36 public static void main(String[] args) {37 WebDriver driver = null;38 WebElement element = null;39 try {40 element.click();41 } catch (Exception e) {42 throw new TestsigmaScreenShotException("TestsigmaScreenShotException", e);43 }44 }45}46package com.testsigma.automator.exceptions;47import org.openqa.selenium.WebDriver;48import org.openqa.selenium.WebElement;49import com.testsigma.automator.utils.TestsigmaScreenShotException;50public class TestsigmaScreenShotExceptionTest {51 public static void main(String[] args) {52 WebDriver driver = null;53 WebElement element = null;54 try {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!