Skip to content

Commit

Permalink
- add ScreenShotUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanAksionau committed Aug 2, 2023
1 parent b0f57dc commit e02d384
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/test/java/com/ea/springbasic/test/SpringTestNGTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ea.springbasic.test;

import com.ea.springbasic.pages.MainPage2;
import com.ea.springbasic.util.ScreenShotUtil;
import org.openqa.selenium.WebDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -23,6 +24,9 @@ public class SpringTestNGTest extends AbstractTestNGSpringContextTests {
@Autowired
private MainPage2 mainPage2;

@Autowired
private ScreenShotUtil screenShotUtil;

@BeforeMethod(alwaysRun = true)
protected void setupWebDriver() {
webDriver.navigate().to(appUrl);
Expand All @@ -38,6 +42,7 @@ protected void springTestContextPrepareTestInstance() throws Exception {
@Test
public void test() {
mainPage2.performLogin();
screenShotUtil.takeScreenShot();
}

@AfterMethod
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/com/ea/springbasic/util/ScreenShotUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.ea.springbasic.util;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.UUID;

@Component
public class ScreenShotUtil {

@Autowired
protected WebDriver webDriver;

@Value("${screenshot.path}")
private Path screenshotPath;

public void takeScreenShot() {
File screenshot = ((TakesScreenshot) this.webDriver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenshot, new File(
screenshotPath.toString() + "\\" + UUID.randomUUID() + ".png"));
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println("Take screenshot");
}
}
1 change: 1 addition & 0 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
app.url=http://eaapp.somee.com
env=prod
browser=chrome
screenshot.path=target/screenshots
#driver.options=

0 comments on commit e02d384

Please sign in to comment.