Subversion Repositories SmartDukaan

Rev

Rev 34423 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 34423 Rev 34680
Line 59... Line 59...
59
        options.addArguments("disable-infobars");
59
        options.addArguments("disable-infobars");
60
        options.addArguments("--disable-extensions");
60
        options.addArguments("--disable-extensions");
61
        options.addArguments("--force-device-scale-factor=1");
61
        options.addArguments("--force-device-scale-factor=1");
62
        options.addArguments("--test-type");
62
        options.addArguments("--test-type");
63
 
63
 
64
        WebDriver driver = new ChromeDriver(options);
-
 
65
        driver.manage().timeouts().setScriptTimeout(6, TimeUnit.SECONDS);
-
 
66
        driver.manage().window().setSize(new Dimension(1600, 900));
-
 
67
        driver.manage().window().maximize();
-
 
68
        // Deleting all the cookies
-
 
69
        driver.manage().deleteAllCookies();
-
 
70
        // Specifiying pageLoadTimeout and Implicit wait
-
 
71
        driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
-
 
72
        driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
-
 
73
 
-
 
74
        int mainCount = 0;
-
 
75
        List<String> remainingImeis = imeis;
-
 
76
        WebDriverWait wait10Sec = new WebDriverWait(driver, 8);
-
 
77
        WebDriverWait wait5Sec = new WebDriverWait(driver, 4);
-
 
78
        WebElement slideButton;
64
        WebDriver driver = null;
79
        Actions actionProvider = new Actions(driver);
-
 
80
        do {
-
 
81
            driver.get("https://support.oppo.com/in/warranty-check/");
-
 
82
            try {
-
 
83
                driver.findElement(By.cssSelector(".cp-cookie-tip a.close")).click();
-
 
84
            } catch (Exception e) {
-
 
85
            }
-
 
86
 
65
 
-
 
66
        try {
87
            for (int i = 0; i < remainingImeis.size(); i++) {
67
            driver = new ChromeDriver(options);
-
 
68
            driver.manage().timeouts().setScriptTimeout(6, TimeUnit.SECONDS);
-
 
69
            driver.manage().window().setSize(new Dimension(1600, 900));
-
 
70
            driver.manage().window().maximize();
-
 
71
            // Deleting all the cookies
-
 
72
            driver.manage().deleteAllCookies();
-
 
73
            // Specifiying pageLoadTimeout and Implicit wait
-
 
74
            driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
-
 
75
            driver.manage().timeouts().implicitlyWait(9, TimeUnit.SECONDS);
-
 
76
 
-
 
77
            int mainCount = 0;
88
                String imei = remainingImeis.get(i);
78
            List<String> remainingImeis = imeis;
-
 
79
            WebDriverWait wait10Sec = new WebDriverWait(driver, 8);
-
 
80
            WebDriverWait wait5Sec = new WebDriverWait(driver, 4);
-
 
81
            WebElement slideButton;
-
 
82
            Actions actionProvider = new Actions(driver);
-
 
83
            do {
-
 
84
                driver.get("https://support.oppo.com/in/warranty-check/");
89
                try {
85
                try {
90
                    System.out.println("Starting fresh with new IMEI " + imei);
-
 
91
                    if (driver.findElement(By.className("el-input__inner")).getAttribute("value").length() > 0) {
86
                    driver.findElement(By.cssSelector(".cp-cookie-tip a.close")).click();
92
                        System.out.println("Darn.. leaving" + driver.findElement(By.className("el-input__inner")).getAttribute("value"));
-
 
93
                        driver.get("https://support.oppo.com/in/warranty-check/");
-
 
94
                        break;
87
                } catch (Exception e) {
95
                    }
88
                }
96
                    driver.findElement(By.className("el-input__inner")).sendKeys(imei);
-
 
97
 
-
 
98
                    //driver.findElement(By.className("el-button--primary")).click();
-
 
99
                    driver.findElement(By.cssSelector("div.searchBtn")).click();
-
 
100
 
89
 
101
                    // Perform click-and-hold action on the element
90
                for (int i = 0; i < remainingImeis.size(); i++) {
102
                    int counter = 1;
-
 
103
                    boolean captchaNotBroken = true;
91
                    String imei = remainingImeis.get(i);
104
                    do {
92
                    try {
105
                        String fileName = "/tmp/oppo-simple-i-" + counter + " " + System.currentTimeMillis() + ".png";
-
 
106
                        try {
-
 
107
                            System.out.println("Starting Do");
93
                        System.out.println("Starting fresh with new IMEI " + imei);
108
                            wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.id("dx_captcha_basic_wrapper_1")));
-
 
109
                            wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.id("dx_captcha_basic_slider_1")));
-
 
110
                            slideButton = driver.findElement(By.id("dx_captcha_basic_slider_1"));
-
 
111
                            //System.out.println("Slide button - " + slideButton);
-
 
112
                            System.out.println("Margin Left before- " + slideButton.getCssValue("margin-left"));
94
                        if (driver.findElement(By.className("el-input__inner")).getAttribute("value").length() > 0) {
113
                            actionProvider.moveToElement(slideButton).clickAndHold()
-
 
114
                                    .moveByOffset(1, 0)//.pause(2000)
-
 
115
                                    .perform();
-
 
116
 
-
 
117
                            //actionProvider.moveByOffset(-1,0).pause(2000).perform();
-
 
118
                            System.out.println("Margin Left after - " + slideButton.getCssValue("margin-left"));
95
                            System.out.println("Darn.. leaving" + driver.findElement(By.className("el-input__inner")).getAttribute("value"));
119
                            //Thread.sleep(5000);
-
 
120
                        } catch (Exception e) {
-
 
121
                            System.out.println(e.getMessage());
-
 
122
                            driver.get("https://support.oppo.com/in/warranty-check/");
96
                            driver.get("https://support.oppo.com/in/warranty-check/");
123
                            break;
97
                            break;
124
                        }
98
                        }
-
 
99
                        driver.findElement(By.className("el-input__inner")).sendKeys(imei);
-
 
100
 
-
 
101
                        //driver.findElement(By.className("el-button--primary")).click();
-
 
102
                        driver.findElement(By.cssSelector("div.searchBtn")).click();
-
 
103
 
-
 
104
                        // Perform click-and-hold action on the element
-
 
105
                        int counter = 1;
-
 
106
                        boolean captchaNotBroken = true;
-
 
107
                        do {
-
 
108
                            String fileName = "/tmp/oppo-simple-i-" + counter + " " + System.currentTimeMillis() + ".png";
-
 
109
                            try {
-
 
110
                                System.out.println("Starting Do");
-
 
111
                                wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.id("dx_captcha_basic_wrapper_1")));
-
 
112
                                wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.id("dx_captcha_basic_slider_1")));
-
 
113
                                slideButton = driver.findElement(By.id("dx_captcha_basic_slider_1"));
-
 
114
                                //System.out.println("Slide button - " + slideButton);
-
 
115
                                System.out.println("Margin Left before- " + slideButton.getCssValue("margin-left"));
-
 
116
                                actionProvider.moveToElement(slideButton).clickAndHold()
-
 
117
                                        .moveByOffset(1, 0)//.pause(2000)
-
 
118
                                        .perform();
-
 
119
 
-
 
120
                                //actionProvider.moveByOffset(-1,0).pause(2000).perform();
-
 
121
                                System.out.println("Margin Left after - " + slideButton.getCssValue("margin-left"));
-
 
122
                                //Thread.sleep(5000);
-
 
123
                            } catch (Exception e) {
-
 
124
                                System.out.println(e.getMessage());
-
 
125
                                driver.get("https://support.oppo.com/in/warranty-check/");
-
 
126
                                break;
-
 
127
                            }
125
                        double[] circles = getMatCircles2(driver, fileName);
128
                            double[] circles = getMatCircles2(driver, fileName);
126
                        if (circles != null) {
129
                            if (circles != null) {
127
                            double firstCircleX = circles[0];
130
                                double firstCircleX = circles[0];
128
                            double secondCircleX = circles[1];
131
                                double secondCircleX = circles[1];
129
                            double distance = Math.abs(firstCircleX - secondCircleX);
132
                                double distance = Math.abs(firstCircleX - secondCircleX);
130
                            System.out.println("Distance is " + distance);
133
                                System.out.println("Distance is " + distance);
131
 
134
 
132
                            System.out.println("Starting Checkdistance method");
135
                                System.out.println("Starting Checkdistance method");
133
                            int moveByOffset = checkDistance(driver, wait10Sec, actionProvider, distance, slideButton);
136
                                int moveByOffset = checkDistance(driver, wait10Sec, actionProvider, distance, slideButton);
134
                            if (moveByOffset > 0) {
137
                                if (moveByOffset > 0) {
135
                                System.out.println("Margin Offset - " + slideButton.getCssValue("margin-left"));
138
                                    System.out.println("Margin Offset - " + slideButton.getCssValue("margin-left"));
136
                                System.out.println("Ending Checkdistance method " + moveByOffset);
139
                                    System.out.println("Ending Checkdistance method " + moveByOffset);
137
                                actionProvider.moveByOffset(moveByOffset, 0).release().perform();
140
                                    actionProvider.moveByOffset(moveByOffset, 0).release().perform();
138
                                System.out.println("Move click performed");
141
                                    System.out.println("Move click performed");
139
                                try {
-
 
140
                                    try {
142
                                    try {
-
 
143
                                        try {
141
                                        wait5Sec.until(ExpectedConditions.visibilityOfElementLocated(By.className("dx_captcha_basic_bar-inform")));
144
                                            wait5Sec.until(ExpectedConditions.visibilityOfElementLocated(By.className("dx_captcha_basic_bar-inform")));
142
                                        System.out.println("Failed = " + counter);
145
                                            System.out.println("Failed = " + counter);
143
                                    } catch (Exception notFailedException) {
146
                                        } catch (Exception notFailedException) {
144
                                        System.out.println("Success  at attempt " + counter);
147
                                            System.out.println("Success  at attempt " + counter);
145
                                        //List<WebElement> warrantyDateElements = driver.findElements(By.className("warranty-service--result_info__dateValue"));
148
                                            //List<WebElement> warrantyDateElements = driver.findElements(By.className("warranty-service--result_info__dateValue"));
146
                                        WebElement activationTimeElement = driver.findElement(By.xpath("//*[contains(text(), 'UTC+5.5')  or contains(text(), 'Non-Activate')]"));
149
                                            WebElement activationTimeElement = driver.findElement(By.xpath("//*[contains(text(), 'UTC+5.5')  or contains(text(), 'Non-Activate')]"));
147
//TODO: IF not found activationElement
150
                                            //TODO: IF not found activationElement
148
                                        if (activationTimeElement != null) {
151
                                            if (activationTimeElement != null) {
149
                                            String activationDateString = activationTimeElement.getText().trim();
152
                                                String activationDateString = activationTimeElement.getText().trim();
150
                                            System.out.println("Date --  " + activationDateString);
-
 
151
                                            try {
-
 
152
                                                dateMap.put(imei, LocalDate.parse(activationDateString.split(" ")[0], DateTimeFormatter.ofPattern("dd/MM/yyyy")));
-
 
153
                                                System.out.println("Date --  " + activationDateString);
153
                                                System.out.println("Date --  " + activationDateString);
-
 
154
                                                try {
-
 
155
                                                    dateMap.put(imei, LocalDate.parse(activationDateString.split(" ")[0], DateTimeFormatter.ofPattern("dd/MM/yyyy")));
-
 
156
                                                    System.out.println("Date --  " + activationDateString);
154
                                            } catch (Exception e) {
157
                                                } catch (Exception e) {
-
 
158
                                                    dateMap.put(imei, null);
-
 
159
                                                }
-
 
160
                                            } else {
-
 
161
                                                System.out.println("Could not capture date");
155
                                                dateMap.put(imei, null);
162
                                                dateMap.put(imei, null);
156
                                            }
163
                                            }
157
                                        } else {
-
 
158
                                            System.out.println("Could not capture date");
164
                                            if (dateMap.size() == imeis.size()) {
159
                                            dateMap.put(imei, null);
165
                                                return dateMap;
160
                                        }
166
                                            }
161
                                        if(dateMap.size()==imeis.size()) {
167
                                            WebElement checkAgainButton = driver.findElement(By.className("c-btn"));
162
                                            driver.close();
168
                                            captchaNotBroken = false;
163
                                            driver.quit();
169
                                            checkAgainButton.click();
164
                                            return dateMap;
170
                                            break;
165
                                        }
171
                                        }
166
                                        WebElement checkAgainButton = driver.findElement(By.className("c-btn"));
-
 
167
                                        captchaNotBroken = false;
172
                                    } catch (Throwable e) {
168
                                        checkAgainButton.click();
173
                                        System.out.println("Failed = " + counter + " ----------" + e.getMessage());
169
                                        break;
-
 
170
                                    }
174
                                    }
171
                                } catch (Throwable e) {
-
 
172
                                    System.out.println("Failed = " + counter + " ----------" + e.getMessage());
-
 
173
                                }
175
                                }
174
                            }
176
                            }
175
                        }
-
 
176
                        System.out.println("After circles conditions");
177
                            System.out.println("After circles conditions");
177
                        wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.id("dx_captcha_basic_wrapper_1")));
178
                            wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.id("dx_captcha_basic_wrapper_1")));
178
                        wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.id("dx_captcha_basic_slider_1")));
179
                            wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.id("dx_captcha_basic_slider_1")));
179
                        slideButton = driver.findElement(By.id("dx_captcha_basic_slider_1"));
180
                            slideButton = driver.findElement(By.id("dx_captcha_basic_slider_1"));
180
                        actionProvider.moveToElement(slideButton).doubleClick().perform();
181
                            actionProvider.moveToElement(slideButton).doubleClick().perform();
181
                        System.out.println("Ending Do" + counter++);
182
                            System.out.println("Ending Do" + counter++);
182
 
183
 
183
                    } while (captchaNotBroken && counter <= 20);
184
                        } while (captchaNotBroken && counter <= 20);
184
                    System.out.println("Counter reached upto 20");
185
                        System.out.println("Counter reached upto 20");
-
 
186
                    } catch (Exception e) {
-
 
187
                        System.out.println("Caught unknown - for imei " + imei);
-
 
188
                        e.printStackTrace();
-
 
189
                    }
-
 
190
                    driver.get("https://support.oppo.com/in/warranty-check/");
-
 
191
                }
-
 
192
                mainCount++;
-
 
193
                remainingImeis = imeis.stream().filter(x -> !dateMap.containsKey(x)).collect(Collectors.toList());
-
 
194
            } while (mainCount < 5);
-
 
195
        } finally {
-
 
196
            if (driver != null) {
-
 
197
                try {
-
 
198
                    driver.quit(); //Ensures cleanup even on return or error
185
                } catch (Exception e) {
199
                } catch (Exception e) {
186
                    System.out.println("Caught unknown - for imei " + imei);
200
                    System.out.println("Error closing driver: " + e.getMessage());
187
                    e.printStackTrace();
-
 
188
                }
201
                }
189
                driver.get("https://support.oppo.com/in/warranty-check/");
-
 
190
            }
202
            }
191
            mainCount++;
-
 
192
            remainingImeis = imeis.stream().filter(x -> !dateMap.containsKey(x)).collect(Collectors.toList());
-
 
193
        } while (mainCount < 5);
-
 
194
        driver.close();
-
 
195
        driver.quit();
203
        }
-
 
204
 
196
        return dateMap;
205
        return dateMap;
197
    }
206
    }
198
 
207
 
-
 
208
 
199
    private int checkDistance(WebDriver driver, WebDriverWait wait3Sec, Actions actionProvider, double distance, WebElement slideButton) throws Exception {
209
    private int checkDistance(WebDriver driver, WebDriverWait wait3Sec, Actions actionProvider, double distance, WebElement slideButton) throws Exception {
200
 
210
 
201
        //actionProvider.moveToElement(slideButton).clickAndHold().moveByOffset(1, 0).perform();
211
        //actionProvider.moveToElement(slideButton).clickAndHold().moveByOffset(1, 0).perform();
202
        actionProvider.moveByOffset(15, 0).perform();
212
        actionProvider.moveByOffset(15, 0).perform();
203
        String fileName2 = "/tmp/" + Thread.currentThread().getName() + "-moved.png";
213
        String fileName2 = "/tmp/" + Thread.currentThread().getName() + "-moved.png";