Skip to content
This repository was archived by the owner on Dec 7, 2020. It is now read-only.

Commit 44bb1e0

Browse files
author
Severi Haverila
committed
screenshot refactoring
1 parent 9800c4e commit 44bb1e0

File tree

1 file changed

+46
-50
lines changed

1 file changed

+46
-50
lines changed

src/main/java/AbstractAppiumTest.java

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public abstract class AbstractAppiumTest {
2727
public static final int LONG_SLEEP = 10;
2828

2929
protected static Logger logger = LoggerFactory.getLogger(AbstractAppiumTest.class);
30-
private static long timeDifferenceStartTest;
31-
private static long startTime;
30+
31+
3232

3333
protected static AppiumDriver<MobileElement> driver;
3434
protected static int defaultWaitTime = 120;
@@ -129,58 +129,54 @@ public static AppiumDriver getAndroidDriver() throws Exception {
129129
}
130130

131131
public static void takeScreenshot(String screenshotName) throws IOException, InterruptedException {
132-
if (idevicescreenshotExists) {
133-
// Keep Appium session alive between multiple non-driver screenshots
134-
driver.manage().window().getSize();
135-
}
136-
timeDifferenceStartTest = (int) ((System.nanoTime() - startTime) / 1e6 / 1000);
137-
long start_time = System.nanoTime();
138-
//
139-
// if (new_step) {
140-
// counter = counter + 1;
141-
// retry_counter = 1;
142-
// } else {
143-
// retry_counter = retry_counter + 1;
144-
// }
145-
//
146-
// searchedImage = screenshotsFolder + getScreenshotsCounter() + "_" + screenshotName + getRetryCounter() + "_" + timeDifferenceStartTest + "sec";
147-
String fullFileName = System.getProperty("user.dir") + "/" + screenshotsFolder + screenshotName + ".png";
132+
if (idevicescreenshotExists) {
133+
// Keep Appium session alive between multiple non-driver screenshots
134+
driver.manage().window().getSize();
135+
}
136+
137+
long start_time = System.nanoTime();
138+
String fullFileName = System.getProperty("user.dir") + "/" + screenshotsFolder + screenshotName + ".png";
148139

149140
if (platformName.equalsIgnoreCase("iOS") && idevicescreenshotExists) {
150-
String[] cmd = new String[]{"idevicescreenshot", "-u", udid, fullFileName};
151-
Process p = Runtime.getRuntime().exec(cmd);
152-
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
153-
String line;
154-
while ((line = in.readLine()) != null)
155-
log(line);
156-
157-
int exitVal = p.waitFor();
158-
if (exitVal != 0) {
159-
log("idevicescreenshot process exited with value: " + exitVal);
160-
}
161-
cmd = new String[]{"sips", "-s", "format", "png", fullFileName, "--out", fullFileName};
162-
p = Runtime.getRuntime().exec(cmd);
163-
exitVal = p.waitFor();
164-
if (exitVal != 0) {
165-
log("sips process exited with value: " + exitVal);
166-
}
141+
takeIDeviceScreenshot(fullFileName);
167142
} else {
168-
// idevicescreenshot not available, using driver.getScreenshotAs()
169-
File scrFile = driver.getScreenshotAs(OutputType.FILE);
170-
try {
171-
File testScreenshot = new File(fullFileName);
172-
FileUtils.copyFile(scrFile, testScreenshot);
173-
logger.info("Screenshot stored to {}", testScreenshot.getAbsolutePath());
174-
return;
175-
} catch (IOException e) {
176-
e.printStackTrace();
177-
}
143+
takeAppiumScreenshot(fullFileName);
178144
}
179-
180-
long end_time = System.nanoTime();
181-
int difference = (int) ((end_time - start_time) / 1e6 / 1000);
182-
logger.info("==> Taking a screenshot took " + difference + " secs.");
183-
}
145+
long end_time = System.nanoTime();
146+
int difference = (int) ((end_time - start_time) / 1e6 / 1000);
147+
logger.info("==> Taking a screenshot took " + difference + " secs.");
148+
}
149+
150+
private static void takeAppiumScreenshot(String fullFileName) {
151+
File scrFile = driver.getScreenshotAs(OutputType.FILE);
152+
try {
153+
File testScreenshot = new File(fullFileName);
154+
FileUtils.copyFile(scrFile, testScreenshot);
155+
logger.info("Screenshot stored to {}", testScreenshot.getAbsolutePath());
156+
} catch (IOException e) {
157+
e.printStackTrace();
158+
}
159+
}
160+
161+
private static void takeIDeviceScreenshot(String fullFileName) throws IOException, InterruptedException {
162+
String[] cmd = new String[]{"idevicescreenshot", "-u", udid, fullFileName};
163+
Process p = Runtime.getRuntime().exec(cmd);
164+
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
165+
String line;
166+
while ((line = in.readLine()) != null)
167+
log(line);
168+
169+
int exitVal = p.waitFor();
170+
if (exitVal != 0) {
171+
log("idevicescreenshot process exited with value: " + exitVal);
172+
}
173+
cmd = new String[]{"sips", "-s", "format", "png", fullFileName, "--out", fullFileName};
174+
p = Runtime.getRuntime().exec(cmd);
175+
exitVal = p.waitFor();
176+
if (exitVal != 0) {
177+
log("sips process exited with value: " + exitVal);
178+
}
179+
}
184180

185181
//On a test run on the local machine this method will save the Reports folder in different folders on every test run.
186182
public static void savePreviousRunReports() {

0 commit comments

Comments
 (0)