Subversion Repositories SmartDukaan

Rev

Rev 37432 | Rev 37434 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30209 amit.gupta 1
package com.smartdukaan.cron.scheduled;
2
 
3
import nu.pattern.OpenCV;
4
import org.apache.commons.io.FileUtils;
5
import org.apache.logging.log4j.LogManager;
6
import org.apache.logging.log4j.Logger;
7
import org.opencv.core.Mat;
34413 amit.gupta 8
import org.opencv.core.Size;
30209 amit.gupta 9
import org.opencv.imgcodecs.Imgcodecs;
10
import org.opencv.imgproc.Imgproc;
37420 amit 11
import org.json.JSONObject;
30209 amit.gupta 12
import org.openqa.selenium.*;
13
import org.openqa.selenium.chrome.ChromeDriver;
14
import org.openqa.selenium.chrome.ChromeOptions;
15
import org.openqa.selenium.interactions.Actions;
16
import org.openqa.selenium.support.ui.ExpectedConditions;
17
import org.openqa.selenium.support.ui.WebDriverWait;
30373 amit.gupta 18
import org.springframework.stereotype.Component;
30209 amit.gupta 19
 
20
import javax.imageio.ImageIO;
21
import java.awt.image.BufferedImage;
22
import java.io.File;
23
import java.time.LocalDate;
24
import java.time.format.DateTimeFormatter;
37420 amit 25
import java.time.Instant;
26
import java.time.ZoneId;
30209 amit.gupta 27
import java.util.HashMap;
28
import java.util.List;
29
import java.util.Map;
30
import java.util.concurrent.TimeUnit;
31
import java.util.stream.Collectors;
32
 
30373 amit.gupta 33
@Component
34
public class CheckOppoWarrantyTask {
30209 amit.gupta 35
 
32146 amit.gupta 36
    private static final Logger LOGGER = LogManager.getLogger(CheckOppoWarrantyTask.class);
30209 amit.gupta 37
 
36253 amit 38
    static {
36306 amit 39
        try {
40
            OpenCV.loadShared();
41
        } catch (Throwable t) {
42
            LogManager.getLogger(CheckOppoWarrantyTask.class).warn("OpenCV not available on this platform: {}", t.getMessage());
43
        }
36253 amit 44
    }
45
 
32146 amit.gupta 46
    public Map<String, LocalDate> checkWarranty(List<String> imeis) {
47
        Map<String, LocalDate> dateMap = new HashMap<>();
48
        LOGGER.info("Initiating webdriver...");
30209 amit.gupta 49
 
32146 amit.gupta 50
        Map<String, Object> prefsMap = new HashMap<>();
51
        prefsMap.put("profile.default_content_settings.popups", 0);
52
        prefsMap.put("download.prompt", false);
53
        prefsMap.put("download.directory_upgrade", true);
34413 amit.gupta 54
        System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
32146 amit.gupta 55
        //I need this
34413 amit.gupta 56
        //System.setProperty("webdriver.chrome.driver", "/Users/amit/cft/chromedriver/chromedriver");
32146 amit.gupta 57
        ChromeOptions options = new ChromeOptions();
34413 amit.gupta 58
        //options.setBinary("/Users/amit/cft/chrome/Google Chrome for Testing.app/Contents/MacOS/Google Chrome For Testing");
32146 amit.gupta 59
        options.setExperimentalOption("prefs", prefsMap);
60
        //options.setExperimentalOption("detach", true);
61
        options.addArguments("--headless");
62
        options.addArguments("--user-agent=\"Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 640 XL LTE) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Edge/12.10166\"");
30209 amit.gupta 63
 
32146 amit.gupta 64
        options.addArguments("--no-sandbox");
65
        options.addArguments("start-maximized");
66
        options.addArguments("disable-infobars");
67
        options.addArguments("--disable-extensions");
68
        options.addArguments("--force-device-scale-factor=1");
69
        options.addArguments("--test-type");
30209 amit.gupta 70
 
34680 amit.gupta 71
        WebDriver driver = null;
30209 amit.gupta 72
 
34680 amit.gupta 73
        try {
74
            driver = new ChromeDriver(options);
75
            driver.manage().timeouts().setScriptTimeout(6, TimeUnit.SECONDS);
76
            driver.manage().window().setSize(new Dimension(1600, 900));
77
            driver.manage().window().maximize();
78
            // Deleting all the cookies
79
            driver.manage().deleteAllCookies();
80
            // Specifiying pageLoadTimeout and Implicit wait
81
            driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
82
            driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
34415 amit.gupta 83
 
34680 amit.gupta 84
            int mainCount = 0;
85
            List<String> remainingImeis = imeis;
86
            WebDriverWait wait10Sec = new WebDriverWait(driver, 8);
87
            WebDriverWait wait5Sec = new WebDriverWait(driver, 4);
88
            WebElement slideButton;
89
            Actions actionProvider = new Actions(driver);
90
            do {
91
                driver.get("https://support.oppo.com/in/warranty-check/");
37420 amit 92
                installResponseCapture(driver);
32146 amit.gupta 93
                try {
34680 amit.gupta 94
                    driver.findElement(By.cssSelector(".cp-cookie-tip a.close")).click();
95
                } catch (Exception e) {
96
                }
30209 amit.gupta 97
 
34680 amit.gupta 98
                for (int i = 0; i < remainingImeis.size(); i++) {
99
                    String imei = remainingImeis.get(i);
100
                    try {
37410 amit 101
                        LOGGER.info("Starting fresh with new IMEI " + imei);
34680 amit.gupta 102
                        if (driver.findElement(By.className("el-input__inner")).getAttribute("value").length() > 0) {
37410 amit 103
                            LOGGER.info("Darn.. leaving" + driver.findElement(By.className("el-input__inner")).getAttribute("value"));
32146 amit.gupta 104
                            driver.get("https://support.oppo.com/in/warranty-check/");
37432 amit 105
                            installResponseCapture(driver);
32146 amit.gupta 106
                            break;
107
                        }
34680 amit.gupta 108
                        driver.findElement(By.className("el-input__inner")).sendKeys(imei);
30209 amit.gupta 109
 
34680 amit.gupta 110
                        //driver.findElement(By.className("el-button--primary")).click();
111
                        driver.findElement(By.cssSelector("div.searchBtn")).click();
112
 
113
                        // Perform click-and-hold action on the element
114
                        int counter = 1;
115
                        boolean captchaNotBroken = true;
116
                        do {
117
                            String fileName = "/tmp/oppo-simple-i-" + counter + " " + System.currentTimeMillis() + ".png";
118
                            try {
37410 amit 119
                                LOGGER.info("Starting Do");
36583 amit 120
                                wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_wrapper_')]")));
121
                                wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]")));
122
                                slideButton = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]"));
34680 amit.gupta 123
                                //System.out.println("Slide button - " + slideButton);
37410 amit 124
                                LOGGER.info("Margin Left before- " + slideButton.getCssValue("margin-left"));
34680 amit.gupta 125
                                actionProvider.moveToElement(slideButton).clickAndHold()
126
                                        .moveByOffset(1, 0)//.pause(2000)
127
                                        .perform();
128
 
129
                                //actionProvider.moveByOffset(-1,0).pause(2000).perform();
37410 amit 130
                                LOGGER.info("Margin Left after - " + slideButton.getCssValue("margin-left"));
34680 amit.gupta 131
                                //Thread.sleep(5000);
132
                            } catch (Exception e) {
37410 amit 133
                                LOGGER.warn("Slider captcha widget (dx_captcha_basic_*) not found or not draggable"
134
                                        + " - reloading. Cause: " + e.getMessage());
34680 amit.gupta 135
                                driver.get("https://support.oppo.com/in/warranty-check/");
37432 amit 136
                                installResponseCapture(driver);
34680 amit.gupta 137
                                break;
138
                            }
139
                            double[] circles = getMatCircles2(driver, fileName);
140
                            if (circles != null) {
141
                                double firstCircleX = circles[0];
142
                                double secondCircleX = circles[1];
143
                                double distance = Math.abs(firstCircleX - secondCircleX);
37410 amit 144
                                LOGGER.info("Distance is " + distance);
34680 amit.gupta 145
 
37422 amit 146
                                double[] cal = calibrate(driver, actionProvider, distance);
147
                                int moveByOffset = cal == null ? 0 : (int) cal[0];
34680 amit.gupta 148
                                if (moveByOffset > 0) {
37422 amit 149
                                    // Move, then measure again and close the remainder BEFORE
150
                                    // releasing. Releasing on the first computed offset committed
151
                                    // the whole calibration error to the final position.
152
                                    actionProvider.moveByOffset(moveByOffset, 0).perform();
153
                                    refine(driver, actionProvider, cal[1]);
154
                                    actionProvider.release().perform();
155
                                    LOGGER.info("Move click performed (main {}px)", moveByOffset);
32146 amit.gupta 156
                                    try {
34680 amit.gupta 157
                                        try {
158
                                            wait5Sec.until(ExpectedConditions.visibilityOfElementLocated(By.className("dx_captcha_basic_bar-inform")));
37433 amit 159
                                            // realme logs the widget's own verdict here and oppo did not,
160
                                            // which is why realme's failures name POSITION_MISMATCH and
161
                                            // oppo's 85% say nothing at all. Same line, oppo's variables.
162
                                            LOGGER.warn("Failed = {} (captcha said: {})", counter, captchaSignal(driver));
34680 amit.gupta 163
                                        } catch (Exception notFailedException) {
37410 amit 164
                                            LOGGER.info("Success  at attempt " + counter);
37420 amit 165
 
166
                                            // Read the JSON the page itself received rather than the
167
                                            // rendered text. The DOM value is built from two text nodes -- the
168
                                            // date plus a "(UTC+x)" suffix derived from the browser timezone --
169
                                            // and the old locator tested only the first, so it matched nothing.
170
                                            // The API carries regDate as epoch millis, which has no format or
171
                                            // timezone ambiguity at all.
37429 amit 172
                                            String json = awaitResponse(driver, 8000);
37420 amit 173
                                            LocalDate activationDate = regDateFrom(json);
174
 
175
                                            if (json == null) {
37429 amit 176
                                                LOGGER.warn("No getDeviceInfo response for imei {} (captcha said: {})",
177
                                                        imei, captchaSignal(driver));
34680 amit.gupta 178
                                            } else {
37420 amit 179
                                                LOGGER.info("Activation lookup for {} -> {}", imei,
180
                                                        activationDate != null ? activationDate : "no activation date");
32146 amit.gupta 181
                                            }
37420 amit 182
                                            // Always record the imei, even with a null date: findElement used to
183
                                            // throw here, so the row was never written and came back every run.
184
                                            dateMap.put(imei, activationDate);
185
                                            clearCapture(driver);
186
 
34680 amit.gupta 187
                                            if (dateMap.size() == imeis.size()) {
188
                                                return dateMap;
189
                                            }
190
                                            WebElement checkAgainButton = driver.findElement(By.className("c-btn"));
191
                                            captchaNotBroken = false;
192
                                            checkAgainButton.click();
193
                                            break;
32146 amit.gupta 194
                                        }
34680 amit.gupta 195
                                    } catch (Throwable e) {
37410 amit 196
                                        LOGGER.warn("Failed = " + counter + " ----------" + e.getMessage());
32146 amit.gupta 197
                                    }
198
                                }
199
                            }
37410 amit 200
                            LOGGER.info("After circles conditions");
36583 amit 201
                            wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_wrapper_')]")));
202
                            wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]")));
203
                            slideButton = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]"));
34680 amit.gupta 204
                            actionProvider.moveToElement(slideButton).doubleClick().perform();
37410 amit 205
                            LOGGER.info("Ending Do" + counter++);
30209 amit.gupta 206
 
34680 amit.gupta 207
                        } while (captchaNotBroken && counter <= 20);
37410 amit 208
                        LOGGER.warn("Counter reached upto 20");
34680 amit.gupta 209
                    } catch (Exception e) {
37410 amit 210
                        LOGGER.error("Oppo warranty check failed for imei " + imei, e);
34680 amit.gupta 211
                    }
212
                    driver.get("https://support.oppo.com/in/warranty-check/");
37432 amit 213
                    // Every navigation destroys the JS context, taking the capture hook
214
                    // with it. Miss one of these and the page is blind from the second
215
                    // imei onwards: __oppoResp and __oppoCaptcha both come back null,
216
                    // which reads as "the captcha failed" when it may well have passed.
217
                    installResponseCapture(driver);
34680 amit.gupta 218
                }
219
                mainCount++;
220
                remainingImeis = imeis.stream().filter(x -> !dateMap.containsKey(x)).collect(Collectors.toList());
36253 amit 221
                if (remainingImeis.isEmpty()) break;
34680 amit.gupta 222
            } while (mainCount < 5);
223
        } finally {
224
            if (driver != null) {
225
                try {
226
                    driver.quit(); //Ensures cleanup even on return or error
32146 amit.gupta 227
                } catch (Exception e) {
37410 amit 228
                    LOGGER.warn("Error closing driver: " + e.getMessage());
32146 amit.gupta 229
                }
230
            }
34680 amit.gupta 231
        }
232
 
32146 amit.gupta 233
        return dateMap;
234
    }
30352 amit.gupta 235
 
34680 amit.gupta 236
 
37420 amit 237
    /** Stash the getDeviceInfo response on window as the page receives it. */
238
    private void installResponseCapture(WebDriver driver) {
239
        try {
240
            ((JavascriptExecutor) driver).executeScript(
241
                "if (!window.__oppoCapInstalled) {"
37428 amit 242
              + "  window.__oppoCapInstalled = true; window.__oppoResp = null; window.__oppoCaptcha = null;"
37425 amit 243
              // axios can sit on either transport, and realme's page uses fetch --
244
              // an XHR-only hook silently captures nothing, which is exactly what
245
              // 'no getDeviceInfo response captured' was reporting.
37420 amit 246
              + "  var O = XMLHttpRequest.prototype.open;"
247
              + "  XMLHttpRequest.prototype.open = function(m,u){"
248
              + "    this.addEventListener('load', function(){"
37428 amit 249
              + "      try { var s=String(u);"
250
              + "        if (s.indexOf('getDeviceInfo') !== -1) window.__oppoResp = this.responseText;"
251
              + "        else if (s.indexOf('captcha') !== -1) window.__oppoCaptcha = s + ' -> ' + (this.responseText||'').slice(0,200);"
252
              + "      } catch(e){}"
37420 amit 253
              + "    });"
254
              + "    return O.apply(this, arguments);"
255
              + "  };"
37425 amit 256
              + "  var F = window.fetch;"
257
              + "  if (F) { window.fetch = function(){"
258
              + "    var u = arguments[0]; u = (u && u.url) ? u.url : String(u);"
259
              + "    return F.apply(this, arguments).then(function(r){"
260
              + "      try { if (u.indexOf('getDeviceInfo') !== -1) { r.clone().text().then(function(t){ window.__oppoResp = t; }); } } catch(e){}"
261
              + "      return r;"
262
              + "    });"
263
              + "  }; }"
37431 amit 264
              // A window.fetch wrapper only catches clients that resolve fetch at call
265
              // time. Anything that captured globalThis.fetch when its bundle loaded --
266
              // Nuxt's $fetch/ofetch does exactly this, and it is what hid realme's
267
              // lookup from us -- sails straight past it. Response.prototype is resolved
268
              // per call, so it catches every client regardless.
269
              // Read the clone with the ORIGINAL reader; the patched one recurses forever.
270
              + "  ['json','text'].forEach(function(name){"
271
              + "    var orig = Response.prototype[name];"
272
              + "    if (!orig) return;"
273
              + "    Response.prototype[name] = function(){"
274
              + "      try { var u = this.url || '';"
275
              + "        if (u.indexOf('getDeviceInfo') !== -1 && !this.__oppoSeenBody) {"
276
              + "          var c = this.clone(); c.__oppoSeenBody = 1;"
277
              + "          orig.call(c).then(function(t){ window.__oppoResp = (typeof t === 'string') ? t : JSON.stringify(t); });"
278
              + "        } } catch(e){}"
279
              + "      return orig.apply(this, arguments);"
280
              + "    };"
281
              + "  });"
37420 amit 282
              + "}");
283
        } catch (Exception e) {
284
            LOGGER.warn("Could not install the response capture hook: " + e.getMessage());
285
        }
286
    }
287
 
37428 amit 288
    /** Last response from the captcha widget itself -- says WHY a drag was refused. */
289
    private String captchaSignal(WebDriver driver) {
290
        try {
291
            Object v = ((JavascriptExecutor) driver).executeScript("return window.__oppoCaptcha || null;");
292
            return v == null ? null : v.toString();
293
        } catch (Exception e) {
294
            return null;
295
        }
296
    }
297
 
37429 amit 298
    /** Poll for the response instead of reading once: the success signal can fire
299
     *  before the lookup request has completed, which loses the payload. */
300
    private String awaitResponse(WebDriver driver, long timeoutMs) {
301
        long deadline = System.currentTimeMillis() + timeoutMs;
302
        while (System.currentTimeMillis() < deadline) {
303
            String json = capturedResponse(driver);
304
            if (json != null && !json.trim().isEmpty()) return json;
305
            try {
306
                Thread.sleep(300);
307
            } catch (InterruptedException ie) {
308
                Thread.currentThread().interrupt();
309
                return null;
310
            }
311
        }
312
        return null;
313
    }
314
 
37420 amit 315
    private String capturedResponse(WebDriver driver) {
316
        try {
317
            Object v = ((JavascriptExecutor) driver).executeScript("return window.__oppoResp || null;");
318
            return v == null ? null : v.toString();
319
        } catch (Exception e) {
320
            return null;
321
        }
322
    }
323
 
324
    private void clearCapture(WebDriver driver) {
325
        try {
326
            ((JavascriptExecutor) driver).executeScript("window.__oppoResp = null;");
327
        } catch (Exception ignored) {
328
        }
329
    }
330
 
331
    /** data.regDate is epoch millis; absent or blank means the device is not activated yet. */
332
    private LocalDate regDateFrom(String json) {
333
        if (json == null) return null;
334
        try {
335
            JSONObject data = new JSONObject(json).optJSONObject("data");
336
            if (data == null) return null;
337
            String reg = data.optString("regDate", "");
338
            if (reg == null || reg.trim().isEmpty() || "null".equals(reg)) return null;
339
            return Instant.ofEpochMilli(Long.parseLong(reg.trim()))
340
                    .atZone(ZoneId.of("Asia/Kolkata")).toLocalDate();
341
        } catch (Exception e) {
342
            LOGGER.warn("Could not read regDate from the Oppo response: " + e.getMessage());
343
            return null;
344
        }
345
    }
346
 
37422 amit 347
    /**
37424 amit 348
     * Calibration nudge, in slider px, sized from the gap rather than fixed.
37422 amit 349
     *
37424 amit 350
     * A fixed 15px was far too small: it closed only ~25px, so the ratio came from a
351
     * measurement ~20% noise and the aim landed ~30px out. A fixed 60px then proved too
352
     * large in the other direction -- production data showed 85% of attempts with a gap
353
     * under 60px ended up UNUSABLE (the piece shot past the hole and the measured gap
354
     * grew), and 45% overall.
37422 amit 355
     *
37424 amit 356
     * Aim to close about half the gap: the piece travels roughly ASSUMED_RATIO px per
357
     * slider px, so nudge ~= gap / (2 * ratio), clamped. The real ratio is still derived
358
     * from the measurement; this only sizes the probe.
37422 amit 359
     */
37433 amit 360
    /*
361
     * These are OPPO's tuning values and are deliberately independent of realme's.
362
     * The two classes drive the same dingxiang widget but convert very differently
363
     * (oppo ~12%, realme ~60% of completed drags), so a change proven on one is NOT
364
     * evidence for the other. Tune and measure them separately.
365
     *
366
     * 1.7 was a guess carried over from a single hand measurement. Measured over 3,694
367
     * real oppo calibrations in production the ratio is 0.67 (realme's is 0.70, on its
368
     * own 1,683). At 1.7 the probe closes about a fifth of the gap instead of the half
369
     * it is meant to, so the ratio is derived from a needlessly small, noisy sample.
370
     */
371
    private static final double ASSUMED_RATIO = 0.67;
37424 amit 372
    private static final int MIN_NUDGE = 10;
373
    private static final int MAX_NUDGE = 60;
37422 amit 374
    private static final double ALIGNED_PX = 2.0;
375
 
37424 amit 376
    private static int nudgeFor(double gap) {
377
        int n = (int) Math.round(gap / (2 * ASSUMED_RATIO));
378
        return Math.max(MIN_NUDGE, Math.min(MAX_NUDGE, n));
379
    }
380
 
37422 amit 381
    /**
382
     * @return {sliderPxToMove, sliderPxPerCirclePx}, or null when the circles could not
383
     *         be read. Returned rather than stored on a field: this is a singleton and
384
     *         the Oppo secondary and tertiary jobs run it concurrently.
385
     */
386
    private double[] calibrate(WebDriver driver, Actions actionProvider, double distance) throws Exception {
37424 amit 387
        int nudge = nudgeFor(distance);
388
        actionProvider.moveByOffset(nudge, 0).perform();
37422 amit 389
 
32146 amit.gupta 390
        String fileName2 = "/tmp/" + Thread.currentThread().getName() + "-moved.png";
34413 amit.gupta 391
        double[] movedCircles = getMatCircles2(driver, fileName2);
37422 amit 392
        if (movedCircles == null) return null;
393
 
394
        double movedDistance = Math.abs(movedCircles[0] - movedCircles[1]);
395
        double closed = distance - movedDistance;
396
        LOGGER.info("Calibration: gap {} -> {} after {}px, closed {}",
37424 amit 397
                distance, movedDistance, nudge, closed);
37422 amit 398
 
399
        // A non-positive value means the piece did not move, or moved away from the
400
        // hole -- either way the ratio is meaningless and guessing would waste the try.
401
        if (closed <= 0) return null;
402
 
37424 amit 403
        double sliderPerCircle = nudge / closed;
37422 amit 404
        int moveBy = (int) Math.round(movedDistance * sliderPerCircle);
405
        return new double[]{moveBy, sliderPerCircle};
32146 amit.gupta 406
    }
30209 amit.gupta 407
 
37422 amit 408
    /**
409
     * Measure again after the main move and close whatever is left. The old code
410
     * committed its single computed offset blind, so any calibration error went
411
     * straight into the final position with nothing to catch it.
412
     */
413
    private int refine(WebDriver driver, Actions actionProvider, double sliderPerCircle) throws Exception {
414
        String fileName = "/tmp/" + Thread.currentThread().getName() + "-refine.png";
415
        double[] circles = getMatCircles2(driver, fileName);
416
        if (circles == null) return 0;
417
 
418
        double remaining = Math.abs(circles[0] - circles[1]);
419
        if (remaining <= ALIGNED_PX) return 0;
420
 
421
        int correction = (int) Math.round(remaining * sliderPerCircle);
422
        if (correction == 0) return 0;
423
        LOGGER.info("Refine: {}px of gap left -> nudging {}px", remaining, correction);
424
        actionProvider.moveByOffset(correction, 0).perform();
425
        return correction;
426
    }
427
 
32146 amit.gupta 428
    private Mat getMatCircles(WebDriver driver, String fileName) throws Exception {
30209 amit.gupta 429
 
32146 amit.gupta 430
        File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
431
        BufferedImage fullImg = ImageIO.read(screenshot);
30209 amit.gupta 432
 
32146 amit.gupta 433
        // Get the location of element on the page
36583 amit 434
        WebElement imageElement = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_bg_')]"));
32146 amit.gupta 435
        Point point = imageElement.getLocation();
30209 amit.gupta 436
 
32146 amit.gupta 437
        // Get width and height of the element
438
        int eleWidth = imageElement.getSize().getWidth();
439
        int eleHeight = imageElement.getSize().getHeight();
30209 amit.gupta 440
 
32146 amit.gupta 441
        // Crop the entire page screenshot to get only element screenshot
442
        BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(),
443
                eleWidth, eleHeight);
444
        ImageIO.write(eleScreenshot, "png", screenshot);
30209 amit.gupta 445
 
32146 amit.gupta 446
        // Copy the element screenshot to disk
447
        File screenshotLocation = new File(fileName);
448
        FileUtils.copyFile(screenshot, screenshotLocation);
30209 amit.gupta 449
 
32146 amit.gupta 450
        Imgcodecs imageCodecs = new Imgcodecs();
451
        Mat grayMatrix = imageCodecs.imread(fileName, Imgcodecs.IMREAD_COLOR);
452
        Imgproc.cvtColor(grayMatrix, grayMatrix, Imgproc.COLOR_BGR2GRAY);
453
        Mat medianBlur = new Mat();
454
        Imgproc.medianBlur(grayMatrix, medianBlur, 5);
455
        Mat circles = new Mat();
456
        Imgproc.HoughCircles(medianBlur, circles, Imgproc.HOUGH_GRADIENT, 1, 15, 50, 20, 17, 25);
457
        return circles;
458
    }
30209 amit.gupta 459
 
34413 amit.gupta 460
    private double[] getMatCircles2(WebDriver driver, String fileName) throws Exception {
461
        File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
462
        BufferedImage fullImg = ImageIO.read(screenshot);
463
 
464
        // Get the location of element on the page
36583 amit 465
        WebElement imageElement = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_bg_')]"));
34413 amit.gupta 466
        Point point = imageElement.getLocation();
467
 
468
        // Get width and height of the element
469
        int eleWidth = imageElement.getSize().getWidth();
470
        int eleHeight = imageElement.getSize().getHeight();
471
 
472
        // Crop the entire page screenshot to get only element screenshot
473
        BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(),
474
                eleWidth, eleHeight);
475
        ImageIO.write(eleScreenshot, "png", screenshot);
476
 
477
        // Copy the element screenshot to disk
478
        File screenshotLocation = new File(fileName);
479
        FileUtils.copyFile(screenshot, screenshotLocation);
480
 
481
        Mat src = Imgcodecs.imread(fileName);
482
        if (src.empty()) {
483
            System.err.println("Cannot read image: " + fileName);
36253 amit 484
            new File(fileName).delete();
34413 amit.gupta 485
            return null;
486
        }
487
 
488
        Mat gray = new Mat();
489
        Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
490
        Imgproc.GaussianBlur(gray, gray, new Size(9, 9), 2, 2);
491
 
492
        Mat circles = new Mat();
493
        Imgproc.HoughCircles(gray, circles, Imgproc.HOUGH_GRADIENT, 1.0,
494
                20, // min distance between centers
495
                100, 20, // param1 (Canny high threshold), param2 (accumulator threshold)
496
                10, 50); // minRadius, maxRadius (adjust as needed)
497
 
36253 amit 498
        if (circles.cols() < 2) {
499
            new File(fileName).delete();
500
            return null;
501
        }
34413 amit.gupta 502
 
503
        for (int i = 0; i < circles.cols(); i++) {
504
            double[] c1 = circles.get(0, i);
505
            for (int j = i + 1; j < circles.cols(); j++) {
506
                double[] c2 = circles.get(0, j);
507
                if (Math.abs(c1[2] - c2[2]) < 3) { // check similar radius
36253 amit 508
                    new File(fileName).delete();
34413 amit.gupta 509
                    return new double[] {
510
                            c1[0], c2[0] // x-coordinates
511
                    };
512
                }
513
            }
514
        }
515
 
36253 amit 516
        new File(fileName).delete();
34413 amit.gupta 517
        return null;
518
    }
519
 
520
 
30209 amit.gupta 521
}