|
2 | 2 | import library.AkazeImageFinder;
|
3 | 3 | import library.ImageRecognition;
|
4 | 4 |
|
| 5 | +import org.apache.commons.io.FileUtils; |
5 | 6 | import org.opencv.core.Point;
|
6 | 7 | import org.openqa.selenium.By;
|
7 | 8 | import org.openqa.selenium.Dimension;
|
| 9 | +import org.openqa.selenium.OutputType; |
8 | 10 | import org.openqa.selenium.interactions.touch.TouchActions;
|
9 | 11 | import org.slf4j.Logger;
|
10 | 12 | import org.slf4j.LoggerFactory;
|
@@ -105,44 +107,23 @@ public Point[] findImageOnScreen(String image) throws Exception {
|
105 | 107 | return findImageOnScreen(image, defaultSettings);
|
106 | 108 | }
|
107 | 109 |
|
108 |
| - //Searches for an image until it disappears from the current view. Good for checking if a loading screen has disappeared. |
109 |
| - public void waitForImageToDisappearFromScreen(String image) throws Exception { |
110 |
| - boolean first_time = true; |
111 |
| - boolean check = true; |
112 |
| - long start, present; |
113 |
| - start = System.nanoTime(); |
114 |
| - present = start; |
115 |
| - |
| 110 | + public boolean waitForImageToDisappearFromScreen(String image) throws Exception { |
| 111 | + long start = System.nanoTime(); |
116 | 112 | log("==> Trying to find image: " + image);
|
117 |
| - |
118 |
| - while ((check) && ((present - start) / 1e6 / 1000 < 300)) { |
119 |
| - |
120 |
| - if (first_time) { |
121 |
| - first_time = false; |
122 |
| - takeScreenshot(image + "_screenshot", true); |
123 |
| - if ((findImage(image, image + "_screenshot" + getRetryCounter())) == null) { |
124 |
| - log("Loading screen not found. Moving on"); |
125 |
| - check = false; |
126 |
| - } else { |
127 |
| - sleep(3); |
128 |
| - } |
129 |
| - } else { |
130 |
| - takeScreenshot(image + "_screenshot", false); |
131 |
| - if ((findImage(image, image + "_screenshot" + getRetryCounter())) == null) { |
132 |
| - log("Loading screen not found. Moving on"); |
133 |
| - check = false; |
134 |
| - } else { |
135 |
| - sleep(3); |
136 |
| - } |
137 |
| - } |
138 |
| - |
139 |
| - present = System.nanoTime(); |
140 |
| - |
141 |
| - if ((present - start) / 1e6 / 1000 >= 300) { |
142 |
| - fail("Application takes too long to load: Stopping tests....."); |
143 |
| - check = false; |
144 |
| - } |
| 113 | + int retry_counter=0; |
| 114 | + String queryImageFile = "queryimages/" + queryimageFolder + image; |
| 115 | + Dimension screenSize = getScreenSizeADB(); |
| 116 | + |
| 117 | + while (((System.nanoTime() - start) / 1e6 / 1000 < 300)) { |
| 118 | + String screenShotFile = takeScreenshot(image + "_screenshot_"+retry_counter); |
| 119 | + if ((ImageRecognition.findImage(queryImageFile, screenShotFile, platformName, screenSize)) == null) { |
| 120 | + log("Image has successfully disappeared from screen."); |
| 121 | + return true; |
| 122 | + } |
| 123 | + sleep(3); |
145 | 124 | }
|
| 125 | + logger.warn("Image did not disappear from screen"); |
| 126 | + return false; |
146 | 127 | }
|
147 | 128 |
|
148 | 129 |
|
@@ -247,6 +228,59 @@ public String grabTextFromImage(String image) throws Exception {
|
247 | 228 | String text = ImageRecognition.getTextStringFromImage(imageSearch.getScreenshotFile());
|
248 | 229 | return text;
|
249 | 230 | }
|
| 231 | + |
| 232 | + |
| 233 | + public String takeScreenshot(String screenshotName) throws IOException, InterruptedException { |
| 234 | + if (idevicescreenshotExists) { |
| 235 | + // Keep Appium session alive between multiple non-driver screenshots |
| 236 | + driver.manage().window().getSize(); |
| 237 | + } |
| 238 | + |
| 239 | + long start_time = System.nanoTime(); |
| 240 | + String screenshotFile = screenshotsFolder + screenshotName + ".png"; |
| 241 | + String fullFileName = System.getProperty("user.dir") + "/" + screenshotFile; |
| 242 | + |
| 243 | + if (platformName.equalsIgnoreCase("iOS") && idevicescreenshotExists) { |
| 244 | + takeIDeviceScreenshot(fullFileName); |
| 245 | + } else { |
| 246 | + takeAppiumScreenshot(fullFileName); |
| 247 | + } |
| 248 | + long end_time = System.nanoTime(); |
| 249 | + int difference = (int) ((end_time - start_time) / 1e6 / 1000); |
| 250 | + logger.info("==> Taking a screenshot took " + difference + " secs."); |
| 251 | + return screenshotFile; |
| 252 | + } |
| 253 | + |
| 254 | + private void takeAppiumScreenshot(String fullFileName) { |
| 255 | + File scrFile = driver.getScreenshotAs(OutputType.FILE); |
| 256 | + try { |
| 257 | + File testScreenshot = new File(fullFileName); |
| 258 | + FileUtils.copyFile(scrFile, testScreenshot); |
| 259 | + logger.info("Screenshot stored to {}", testScreenshot.getAbsolutePath()); |
| 260 | + } catch (IOException e) { |
| 261 | + e.printStackTrace(); |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + private static void takeIDeviceScreenshot(String fullFileName) throws IOException, InterruptedException { |
| 266 | + String[] cmd = new String[]{"idevicescreenshot", "-u", udid, fullFileName}; |
| 267 | + Process p = Runtime.getRuntime().exec(cmd); |
| 268 | + BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); |
| 269 | + String line; |
| 270 | + while ((line = in.readLine()) != null) |
| 271 | + log(line); |
| 272 | + |
| 273 | + int exitVal = p.waitFor(); |
| 274 | + if (exitVal != 0) { |
| 275 | + log("idevicescreenshot process exited with value: " + exitVal); |
| 276 | + } |
| 277 | + cmd = new String[]{"sips", "-s", "format", "png", fullFileName, "--out", fullFileName}; |
| 278 | + p = Runtime.getRuntime().exec(cmd); |
| 279 | + exitVal = p.waitFor(); |
| 280 | + if (exitVal != 0) { |
| 281 | + log("sips process exited with value: " + exitVal); |
| 282 | + } |
| 283 | + } |
250 | 284 |
|
251 | 285 |
|
252 | 286 |
|
|
0 commit comments