Subversion Repositories SmartDukaan

Rev

Rev 37426 | Rev 37430 | 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;
37423 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;
25
import java.util.HashMap;
26
import java.util.List;
27
import java.util.Map;
28
import java.util.concurrent.TimeUnit;
29
import java.util.stream.Collectors;
30
 
30373 amit.gupta 31
@Component
34416 amit.gupta 32
public class CheckRealmeWarrantyTask {
30209 amit.gupta 33
 
34416 amit.gupta 34
    private static final Logger LOGGER = LogManager.getLogger(CheckRealmeWarrantyTask.class);
30209 amit.gupta 35
 
36253 amit 36
    static {
36306 amit 37
        try {
38
            OpenCV.loadShared();
39
        } catch (Throwable t) {
40
            LogManager.getLogger(CheckRealmeWarrantyTask.class).warn("OpenCV not available on this platform: {}", t.getMessage());
41
        }
36253 amit 42
    }
43
 
32146 amit.gupta 44
    public Map<String, LocalDate> checkWarranty(List<String> imeis) {
45
        Map<String, LocalDate> dateMap = new HashMap<>();
46
        LOGGER.info("Initiating webdriver...");
30209 amit.gupta 47
 
32146 amit.gupta 48
        Map<String, Object> prefsMap = new HashMap<>();
49
        prefsMap.put("profile.default_content_settings.popups", 0);
50
        prefsMap.put("download.prompt", false);
51
        prefsMap.put("download.directory_upgrade", true);
34413 amit.gupta 52
        System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
32146 amit.gupta 53
        //I need this
34413 amit.gupta 54
        //System.setProperty("webdriver.chrome.driver", "/Users/amit/cft/chromedriver/chromedriver");
32146 amit.gupta 55
        ChromeOptions options = new ChromeOptions();
34413 amit.gupta 56
        //options.setBinary("/Users/amit/cft/chrome/Google Chrome for Testing.app/Contents/MacOS/Google Chrome For Testing");
32146 amit.gupta 57
        options.setExperimentalOption("prefs", prefsMap);
58
        //options.setExperimentalOption("detach", true);
59
        options.addArguments("--headless");
34419 amit.gupta 60
        options.addArguments("--user-agent=\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36\"");
30209 amit.gupta 61
 
32146 amit.gupta 62
        options.addArguments("--no-sandbox");
63
        options.addArguments("start-maximized");
64
        options.addArguments("disable-infobars");
65
        options.addArguments("--disable-extensions");
66
        options.addArguments("--force-device-scale-factor=1");
67
        options.addArguments("--test-type");
34420 amit.gupta 68
        options.addArguments(
69
                "--disable-blink-features=AutomationControlled",
70
                "--disable-infobars",
71
                "--disable-popup-blocking",
72
                "--disable-notifications",
73
                "--disable-extensions"
74
        );
30209 amit.gupta 75
 
34679 amit.gupta 76
        WebDriver driver = null;
30209 amit.gupta 77
 
34679 amit.gupta 78
        try {
79
            driver = new ChromeDriver(options);
80
            driver.manage().timeouts().setScriptTimeout(6, TimeUnit.SECONDS);
81
            driver.manage().window().setSize(new Dimension(1600, 900));
82
            driver.manage().window().maximize();
83
            // Deleting all the cookies
84
            driver.manage().deleteAllCookies();
85
            // Specifiying pageLoadTimeout and Implicit wait
86
            driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
87
            driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
88
 
89
            int mainCount = 0;
90
            List<String> remainingImeis = imeis;
91
            WebDriverWait wait10Sec = new WebDriverWait(driver, 15);
92
            WebDriverWait wait5Sec = new WebDriverWait(driver, 4);
93
            WebElement slideButton;
94
            Actions actionProvider = new Actions(driver);
95
            do {
96
                driver.get("https://www.realme.com/in/support/phonecheck");
37423 amit 97
                installResponseCapture(driver);
34424 amit.gupta 98
            /*try {
34415 amit.gupta 99
                driver.findElement(By.cssSelector(".cp-cookie-tip a.close")).click();
100
            } catch (Exception e) {
101
                try {
37423 amit 102
                    LOGGER.info("Cookie not found");
34415 amit.gupta 103
                } catch (Exception e1) {
104
                }
34424 amit.gupta 105
            }*/
34415 amit.gupta 106
 
34679 amit.gupta 107
                for (int i = 0; i < remainingImeis.size(); i++) {
108
                    String imei = remainingImeis.get(i);
109
                    try {
37423 amit 110
                        LOGGER.info("Starting fresh with new IMEI " + imei);
111
                        // realme rebuilt this page: the field is no longer .sn-input and the
112
                        // submit is no longer div.check-btn but an icon inside the same box.
113
                        if (driver.findElement(By.cssSelector(IMEI_INPUT)).getAttribute("value").length() > 0) {
34679 amit.gupta 114
                            //System.out.println("Darn.. leaving" + driver.findElement(By.className("el-input__inner")).getAttribute("value"));
115
                            driver.get("https://www.realme.com/in/support/phonecheck");
37423 amit 116
                installResponseCapture(driver);
34679 amit.gupta 117
                            break;
118
                        }
37423 amit 119
                        driver.findElement(By.cssSelector(IMEI_INPUT)).sendKeys(imei);
30209 amit.gupta 120
 
34679 amit.gupta 121
                        //driver.findElement(By.className("el-button--primary")).click();
37423 amit 122
                        driver.findElement(By.cssSelector(SUBMIT_ICON)).click();
34413 amit.gupta 123
 
34679 amit.gupta 124
                        // Perform click-and-hold action on the element
125
                        int counter = 1;
126
                        boolean captchaNotBroken = true;
127
                        do {
128
                            String fileName = "/tmp/oppo-simple-i-" + counter + " " + System.currentTimeMillis() + ".png";
129
                            try {
37423 amit 130
                                LOGGER.info("Starting Do");
34679 amit.gupta 131
                                //Thread.sleep(5000);
132
                                wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("canvas")));
34420 amit.gupta 133
 
34679 amit.gupta 134
                                wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_bg_')]")));
135
                                wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_wrapper_')]")));
136
                                wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]")));
137
                                slideButton = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]"));
138
                                //System.out.println("Slide button - " + slideButton);
37423 amit 139
                                LOGGER.info("Margin Left before- " + slideButton.getCssValue("margin-left"));
34679 amit.gupta 140
                                actionProvider.moveToElement(slideButton).clickAndHold()
141
                                        .moveByOffset(1, 0)//.pause(2000)
142
                                        .perform();
34413 amit.gupta 143
 
34679 amit.gupta 144
                                //actionProvider.moveByOffset(-1,0).pause(2000).perform();
37423 amit 145
                                LOGGER.info("Margin Left after - " + slideButton.getCssValue("margin-left"));
34679 amit.gupta 146
                                //Thread.sleep(5000);
147
                            } catch (Exception e) {
37423 amit 148
                                LOGGER.info(e.getMessage());
34679 amit.gupta 149
                                driver.get("https://www.realme.com/in/support/phonecheck");
37423 amit 150
                installResponseCapture(driver);
34679 amit.gupta 151
                                break;
152
                            }
153
                            double[] circles = getMatCircles2(driver, fileName);
154
                            if (circles != null) {
155
                                double firstCircleX = circles[0];
156
                                double secondCircleX = circles[1];
157
                                double distance = Math.abs(firstCircleX - secondCircleX);
37423 amit 158
                                LOGGER.info("Distance is " + distance);
30209 amit.gupta 159
 
37423 amit 160
                                double[] cal = calibrate(driver, actionProvider, distance);
161
                                int moveByOffset = cal == null ? 0 : (int) cal[0];
34679 amit.gupta 162
                                if (moveByOffset > 0) {
37423 amit 163
                                    // Move, measure again, close the remainder, THEN release.
164
                                    actionProvider.moveByOffset(moveByOffset, 0).perform();
165
                                    refine(driver, actionProvider, cal[1]);
166
                                    actionProvider.release().perform();
167
                                    LOGGER.info("Move click performed (main {}px)", moveByOffset);
32146 amit.gupta 168
                                    try {
37426 amit 169
                                        // Success used to mean "the dx_captcha_basic_bar-inform error element
170
                                        // did not appear". realme rebuilt this page, so that element may no
171
                                        // longer exist -- in which case the wait always times out and EVERY
172
                                        // attempt reports success while the captcha was never solved. That is
173
                                        // exactly what happened: 27 successes, 27 missed responses, 0 rows.
174
                                        //
175
                                        // The lookup response arriving is the only signal that actually proves
176
                                        // the captcha passed, so wait for that instead.
177
                                        String json = awaitResponse(driver, 8000);
178
                                        if (json == null) {
37428 amit 179
                                            LOGGER.warn("Failed = {} (captcha said: {})",
180
                                                    counter, captchaSignal(driver));
37426 amit 181
                                        } else {
37423 amit 182
                                            LOGGER.info("Success  at attempt " + counter);
34679 amit.gupta 183
                                            //List<WebElement> warrantyDateElements = driver.findElements(By.className("warranty-service--result_info__dateValue"));
184
                                            //WebElement activationTimeElement = driver.findElement(By.xpath("//*[contains(text(), 'UTC+5.5')  or contains(text(), 'Non-Activate')]"));
37423 amit 185
                                            // Read the JSON the page already received. The old locator
186
                                            // keyed off a label that no longer exists, and its "| //h1" branch
187
                                            // would silently grab a page heading and try to parse it as a date.
188
                                            // The API is explicit: expiryDate (yyyy.MM.dd) plus an isActivation
189
                                            // flag, so nothing has to be inferred from message text.
190
                                            LocalDate activationDate = activationFrom(json);
37426 amit 191
                                            LOGGER.info("Activation lookup for {} -> {}", imei,
192
                                                    activationDate != null ? activationDate : "not activated");
37423 amit 193
                                            // Always record, even with a null date. findElement used to THROW
194
                                            // when the element was missing, so nothing was written and the imei
195
                                            // came back on every run indefinitely.
196
                                            dateMap.put(imei, activationDate);
197
                                            clearCapture(driver);
198
 
34679 amit.gupta 199
                                            if (dateMap.size() == imeis.size()) {
200
                                                return dateMap;
201
                                            }
202
                                            WebElement checkAgainButton = driver.findElement(By.className("recheck-btn"));
203
                                            captchaNotBroken = false;
204
                                            checkAgainButton.click();
205
                                            break;
32146 amit.gupta 206
                                        }
34679 amit.gupta 207
                                    } catch (Throwable e) {
37423 amit 208
                                        LOGGER.warn("Failed = " + counter + " ----------" + e.getMessage());
32146 amit.gupta 209
                                    }
210
                                }
211
                            }
37423 amit 212
                            LOGGER.info("After circles conditions");
34679 amit.gupta 213
                            wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_wrapper_')]")));
214
                            wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]")));
215
                            slideButton = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_slider_')]"));
216
                            actionProvider.moveToElement(slideButton).doubleClick().perform();
37423 amit 217
                            LOGGER.info("Ending Do" + counter++);
30209 amit.gupta 218
 
34679 amit.gupta 219
                        } while (captchaNotBroken && counter <= 20);
37423 amit 220
                        LOGGER.warn("Counter reached upto 20");
34679 amit.gupta 221
                    } catch (Exception e) {
37423 amit 222
                        LOGGER.info("Caught unknown - for imei " + imei);
223
                        LOGGER.error("Realme warranty check failed for imei " + imei, e);
34679 amit.gupta 224
                    }
225
                    driver.get("https://www.realme.com/in/support/phonecheck");
37423 amit 226
                installResponseCapture(driver);
34679 amit.gupta 227
                }
228
                mainCount++;
229
                remainingImeis = imeis.stream().filter(x -> !dateMap.containsKey(x)).collect(Collectors.toList());
36253 amit 230
                if (remainingImeis.isEmpty()) break;
34679 amit.gupta 231
            } while (mainCount < 5);
232
        } finally {
233
            if (driver != null) {
234
                try {
34681 amit.gupta 235
                    driver.quit();  // Always shuts everything down
32146 amit.gupta 236
                } catch (Exception e) {
37423 amit 237
                    LOGGER.warn("Error closing driver: " + e.getMessage());
32146 amit.gupta 238
                }
239
            }
34679 amit.gupta 240
        }
241
 
32146 amit.gupta 242
        return dateMap;
243
    }
30352 amit.gupta 244
 
34679 amit.gupta 245
 
37423 amit 246
    /** realme rebuilt the page; the old .sn-input / div.check-btn no longer render. */
247
    private static final String IMEI_INPUT  = ".imei-input input";
248
    private static final String SUBMIT_ICON = ".imei-input .search-icon";
30209 amit.gupta 249
 
37423 amit 250
    /**
37424 amit 251
     * Calibration nudge, in slider px, sized from the gap rather than fixed.
252
     *
253
     * A fixed 15px was far too small: it closed only ~25px, so the ratio came from a
254
     * measurement ~20% noise and the aim landed ~30px out. A fixed 60px then proved too
255
     * large in the other direction -- production data showed 85% of attempts with a gap
256
     * under 60px ended up UNUSABLE (the piece shot past the hole and the measured gap
257
     * grew), and 45% overall.
258
     *
259
     * Aim to close about half the gap: the piece travels roughly ASSUMED_RATIO px per
260
     * slider px, so nudge ~= gap / (2 * ratio), clamped. The real ratio is still derived
261
     * from the measurement; this only sizes the probe.
37423 amit 262
     */
37424 amit 263
    private static final double ASSUMED_RATIO = 1.7;
264
    private static final int MIN_NUDGE = 10;
265
    private static final int MAX_NUDGE = 60;
37423 amit 266
    private static final double ALIGNED_PX = 2.0;
267
 
37424 amit 268
    private static int nudgeFor(double gap) {
269
        int n = (int) Math.round(gap / (2 * ASSUMED_RATIO));
270
        return Math.max(MIN_NUDGE, Math.min(MAX_NUDGE, n));
271
    }
272
 
37423 amit 273
    /** Stash the active/check response on window as the page receives it. */
274
    private void installResponseCapture(WebDriver driver) {
275
        try {
276
            ((JavascriptExecutor) driver).executeScript(
277
                "if (!window.__rmCapInstalled) {"
37428 amit 278
              + "  window.__rmCapInstalled = true; window.__rmResp = null; window.__rmCaptcha = null;"
37425 amit 279
              // axios can sit on either transport, and realme's page uses fetch --
280
              // an XHR-only hook silently captures nothing, which is exactly what
281
              // 'no active/check response captured' was reporting.
37423 amit 282
              + "  var O = XMLHttpRequest.prototype.open;"
283
              + "  XMLHttpRequest.prototype.open = function(m,u){"
284
              + "    this.addEventListener('load', function(){"
37428 amit 285
              + "      try { var s=String(u);"
286
              + "        if (s.indexOf('active/check') !== -1) window.__rmResp = this.responseText;"
287
              + "        else if (s.indexOf('captcha') !== -1) window.__rmCaptcha = s + ' -> ' + (this.responseText||'').slice(0,200);"
288
              + "      } catch(e){}"
37423 amit 289
              + "    });"
290
              + "    return O.apply(this, arguments);"
291
              + "  };"
37425 amit 292
              + "  var F = window.fetch;"
293
              + "  if (F) { window.fetch = function(){"
294
              + "    var u = arguments[0]; u = (u && u.url) ? u.url : String(u);"
295
              + "    return F.apply(this, arguments).then(function(r){"
296
              + "      try { if (u.indexOf('active/check') !== -1) { r.clone().text().then(function(t){ window.__rmResp = t; }); } } catch(e){}"
297
              + "      return r;"
298
              + "    });"
299
              + "  }; }"
37423 amit 300
              + "}");
301
        } catch (Exception e) {
302
            LOGGER.warn("Could not install the response capture hook: " + e.getMessage());
303
        }
304
    }
305
 
37426 amit 306
    /**
307
     * Wait for the lookup response to arrive, which is the only reliable proof that
308
     * the captcha was actually solved. Polls rather than blocking so a solved captcha
309
     * is picked up as soon as the request completes.
310
     */
311
    private String awaitResponse(WebDriver driver, long timeoutMs) {
312
        long deadline = System.currentTimeMillis() + timeoutMs;
313
        while (System.currentTimeMillis() < deadline) {
314
            String json = capturedResponse(driver);
315
            if (json != null && !json.trim().isEmpty()) return json;
316
            try {
317
                Thread.sleep(300);
318
            } catch (InterruptedException ie) {
319
                Thread.currentThread().interrupt();
320
                return null;
321
            }
322
        }
323
        return null;
324
    }
325
 
37428 amit 326
    /** Last response from the captcha widget itself -- says WHY a drag was refused. */
327
    private String captchaSignal(WebDriver driver) {
328
        try {
329
            Object v = ((JavascriptExecutor) driver).executeScript("return window.__rmCaptcha || null;");
330
            return v == null ? null : v.toString();
331
        } catch (Exception e) {
332
            return null;
333
        }
334
    }
335
 
37423 amit 336
    private String capturedResponse(WebDriver driver) {
337
        try {
338
            Object v = ((JavascriptExecutor) driver).executeScript("return window.__rmResp || null;");
339
            return v == null ? null : v.toString();
340
        } catch (Exception e) {
341
            return null;
342
        }
343
    }
344
 
345
    private void clearCapture(WebDriver driver) {
346
        try {
347
            ((JavascriptExecutor) driver).executeScript("window.__rmResp = null;");
348
        } catch (Exception ignored) {
349
        }
350
    }
351
 
352
    /**
353
     * realme returns the WARRANTY EXPIRY, not the activation date, so a year is
354
     * subtracted -- warranty is 12 months as standard. Verified against our own
355
     * records: 41,267 of 41,289 stored Realme activation dates precede the date we
356
     * checked them, averaging 13 days before, which is only consistent with the
357
     * subtraction being applied. isActivation false means the device is not
358
     * activated yet and there is no date to derive.
359
     */
360
    private LocalDate activationFrom(String json) {
361
        if (json == null) return null;
362
        try {
363
            JSONObject root = new JSONObject(json);
364
            JSONObject data = root.optJSONObject("data");
365
            if (data == null) return null;
366
            if (!data.optBoolean("isActivation", false)) return null;
367
 
368
            String expiry = data.optString("expiryDate", "");
369
            if (expiry == null || expiry.trim().isEmpty() || "null".equals(expiry)) return null;
370
 
371
            return LocalDate.parse(expiry.trim().split(" ")[0],
372
                    DateTimeFormatter.ofPattern("yyyy.MM.dd")).minusYears(1);
373
        } catch (Exception e) {
374
            LOGGER.warn("Could not read expiryDate from the realme response: " + e.getMessage());
375
            return null;
376
        }
377
    }
378
 
379
    /** @return {sliderPxToMove, sliderPxPerCirclePx}, or null if the circles were unreadable. */
380
    private double[] calibrate(WebDriver driver, Actions actionProvider, double distance) throws Exception {
37424 amit 381
        int nudge = nudgeFor(distance);
382
        actionProvider.moveByOffset(nudge, 0).perform();
37423 amit 383
 
32146 amit.gupta 384
        String fileName2 = "/tmp/" + Thread.currentThread().getName() + "-moved.png";
34413 amit.gupta 385
        double[] movedCircles = getMatCircles2(driver, fileName2);
37423 amit 386
        if (movedCircles == null) return null;
387
 
388
        double movedDistance = Math.abs(movedCircles[0] - movedCircles[1]);
389
        double closed = distance - movedDistance;
390
        LOGGER.info("Calibration: gap {} -> {} after {}px, closed {}",
37424 amit 391
                distance, movedDistance, nudge, closed);
37423 amit 392
        if (closed <= 0) return null;
393
 
37424 amit 394
        double sliderPerCircle = nudge / closed;
37423 amit 395
        return new double[]{Math.round(movedDistance * sliderPerCircle), sliderPerCircle};
32146 amit.gupta 396
    }
30209 amit.gupta 397
 
37423 amit 398
    /** Measure again after the main move and close the remainder before releasing. */
399
    private int refine(WebDriver driver, Actions actionProvider, double sliderPerCircle) throws Exception {
400
        String fileName = "/tmp/" + Thread.currentThread().getName() + "-refine.png";
401
        double[] circles = getMatCircles2(driver, fileName);
402
        if (circles == null) return 0;
403
 
404
        double remaining = Math.abs(circles[0] - circles[1]);
405
        if (remaining <= ALIGNED_PX) return 0;
406
 
407
        int correction = (int) Math.round(remaining * sliderPerCircle);
408
        if (correction == 0) return 0;
409
        LOGGER.info("Refine: {}px of gap left -> nudging {}px", remaining, correction);
410
        actionProvider.moveByOffset(correction, 0).perform();
411
        return correction;
412
    }
413
 
32146 amit.gupta 414
    private Mat getMatCircles(WebDriver driver, String fileName) throws Exception {
30209 amit.gupta 415
 
32146 amit.gupta 416
        File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
417
        BufferedImage fullImg = ImageIO.read(screenshot);
30209 amit.gupta 418
 
32146 amit.gupta 419
        // Get the location of element on the page
420
        WebElement imageElement = driver.findElement(By.id("dx_captcha_basic_bg_1"));
421
        Point point = imageElement.getLocation();
30209 amit.gupta 422
 
32146 amit.gupta 423
        // Get width and height of the element
424
        int eleWidth = imageElement.getSize().getWidth();
425
        int eleHeight = imageElement.getSize().getHeight();
30209 amit.gupta 426
 
32146 amit.gupta 427
        // Crop the entire page screenshot to get only element screenshot
428
        BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(),
429
                eleWidth, eleHeight);
430
        ImageIO.write(eleScreenshot, "png", screenshot);
30209 amit.gupta 431
 
32146 amit.gupta 432
        // Copy the element screenshot to disk
433
        File screenshotLocation = new File(fileName);
434
        FileUtils.copyFile(screenshot, screenshotLocation);
30209 amit.gupta 435
 
32146 amit.gupta 436
        Imgcodecs imageCodecs = new Imgcodecs();
437
        Mat grayMatrix = imageCodecs.imread(fileName, Imgcodecs.IMREAD_COLOR);
438
        Imgproc.cvtColor(grayMatrix, grayMatrix, Imgproc.COLOR_BGR2GRAY);
439
        Mat medianBlur = new Mat();
440
        Imgproc.medianBlur(grayMatrix, medianBlur, 5);
441
        Mat circles = new Mat();
442
        Imgproc.HoughCircles(medianBlur, circles, Imgproc.HOUGH_GRADIENT, 1, 15, 50, 20, 17, 25);
443
        return circles;
444
    }
30209 amit.gupta 445
 
34413 amit.gupta 446
    private double[] getMatCircles2(WebDriver driver, String fileName) throws Exception {
447
        File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
448
        BufferedImage fullImg = ImageIO.read(screenshot);
449
 
450
        // Get the location of element on the page
36583 amit 451
        WebElement imageElement = driver.findElement(By.xpath("//*[starts-with(@id, 'dx_captcha_basic_bg_')]"));
34413 amit.gupta 452
        Point point = imageElement.getLocation();
453
 
454
        // Get width and height of the element
455
        int eleWidth = imageElement.getSize().getWidth();
456
        int eleHeight = imageElement.getSize().getHeight();
457
 
458
        // Crop the entire page screenshot to get only element screenshot
459
        BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(),
460
                eleWidth, eleHeight);
461
        ImageIO.write(eleScreenshot, "png", screenshot);
462
 
463
        // Copy the element screenshot to disk
464
        File screenshotLocation = new File(fileName);
465
        FileUtils.copyFile(screenshot, screenshotLocation);
466
 
467
        Mat src = Imgcodecs.imread(fileName);
468
        if (src.empty()) {
36583 amit 469
            LOGGER.warn("Cannot read image: {}", fileName);
36253 amit 470
            new File(fileName).delete();
34413 amit.gupta 471
            return null;
472
        }
473
 
474
        Mat gray = new Mat();
475
        Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
476
        Imgproc.GaussianBlur(gray, gray, new Size(9, 9), 2, 2);
477
 
478
        Mat circles = new Mat();
479
        Imgproc.HoughCircles(gray, circles, Imgproc.HOUGH_GRADIENT, 1.0,
480
                20, // min distance between centers
481
                100, 20, // param1 (Canny high threshold), param2 (accumulator threshold)
482
                10, 50); // minRadius, maxRadius (adjust as needed)
483
 
36253 amit 484
        if (circles.cols() < 2) {
485
            new File(fileName).delete();
486
            return null;
487
        }
34413 amit.gupta 488
 
489
        for (int i = 0; i < circles.cols(); i++) {
490
            double[] c1 = circles.get(0, i);
491
            for (int j = i + 1; j < circles.cols(); j++) {
492
                double[] c2 = circles.get(0, j);
493
                if (Math.abs(c1[2] - c2[2]) < 3) { // check similar radius
36253 amit 494
                    new File(fileName).delete();
34413 amit.gupta 495
                    return new double[] {
496
                            c1[0], c2[0] // x-coordinates
497
                    };
498
                }
499
            }
500
        }
501
 
36253 amit 502
        new File(fileName).delete();
34413 amit.gupta 503
        return null;
504
    }
505
 
506
 
30209 amit.gupta 507
}