| 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;
|
|
|
8 |
import org.opencv.imgcodecs.Imgcodecs;
|
|
|
9 |
import org.opencv.imgproc.Imgproc;
|
|
|
10 |
import org.openqa.selenium.*;
|
|
|
11 |
import org.openqa.selenium.chrome.ChromeDriver;
|
|
|
12 |
import org.openqa.selenium.chrome.ChromeOptions;
|
|
|
13 |
import org.openqa.selenium.interactions.Actions;
|
|
|
14 |
import org.openqa.selenium.support.ui.ExpectedConditions;
|
|
|
15 |
import org.openqa.selenium.support.ui.WebDriverWait;
|
| 30373 |
amit.gupta |
16 |
import org.springframework.stereotype.Component;
|
| 30209 |
amit.gupta |
17 |
|
|
|
18 |
import javax.imageio.ImageIO;
|
|
|
19 |
import java.awt.image.BufferedImage;
|
|
|
20 |
import java.io.File;
|
|
|
21 |
import java.time.LocalDate;
|
|
|
22 |
import java.time.format.DateTimeFormatter;
|
|
|
23 |
import java.util.HashMap;
|
|
|
24 |
import java.util.List;
|
|
|
25 |
import java.util.Map;
|
|
|
26 |
import java.util.concurrent.TimeUnit;
|
|
|
27 |
import java.util.stream.Collectors;
|
|
|
28 |
|
| 30373 |
amit.gupta |
29 |
@Component
|
|
|
30 |
public class CheckOppoWarrantyTask {
|
| 30209 |
amit.gupta |
31 |
|
|
|
32 |
|
|
|
33 |
private static final Logger LOGGER = LogManager.getLogger(CheckOppoWarrantyTask.class);
|
|
|
34 |
|
| 30373 |
amit.gupta |
35 |
public Map<String, LocalDate> checkWarranty(List<String> imeis) {
|
| 30209 |
amit.gupta |
36 |
String name = Thread.currentThread().getName();
|
|
|
37 |
Map<String, LocalDate> dateMap = new HashMap<>();
|
|
|
38 |
OpenCV.loadShared();
|
|
|
39 |
LOGGER.info("Initiating webdriver...");
|
|
|
40 |
System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
|
|
|
41 |
|
|
|
42 |
Map<String, Object> prefsMap = new HashMap<>();
|
|
|
43 |
prefsMap.put("profile.default_content_settings.popups", 0);
|
|
|
44 |
prefsMap.put("download.prompt", false);
|
|
|
45 |
prefsMap.put("download.directory_upgrade", true);
|
|
|
46 |
|
|
|
47 |
ChromeOptions options = new ChromeOptions();
|
|
|
48 |
options.setExperimentalOption("prefs", prefsMap);
|
| 30354 |
amit.gupta |
49 |
//options.setExperimentalOption("detach", true);
|
|
|
50 |
options.addArguments("--headless");
|
| 30209 |
amit.gupta |
51 |
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\"");
|
|
|
52 |
|
|
|
53 |
options.addArguments("--no-sandbox");
|
|
|
54 |
options.addArguments("start-maximized");
|
|
|
55 |
options.addArguments("disable-infobars");
|
|
|
56 |
options.addArguments("--disable-extensions");
|
|
|
57 |
options.addArguments("--test-type");
|
|
|
58 |
|
|
|
59 |
WebDriver driver = new ChromeDriver(options);
|
|
|
60 |
driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
|
|
|
61 |
driver.manage().window().setSize(new Dimension(1600, 900));
|
|
|
62 |
driver.manage().window().maximize();
|
|
|
63 |
// Deleting all the cookies
|
|
|
64 |
driver.manage().deleteAllCookies();
|
|
|
65 |
// Specifiying pageLoadTimeout and Implicit wait
|
| 30352 |
amit.gupta |
66 |
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
|
|
|
67 |
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
|
| 30209 |
amit.gupta |
68 |
|
|
|
69 |
driver.get("https://support.oppo.com/in/warranty-check/");
|
|
|
70 |
int mainCount = 0;
|
|
|
71 |
List<String> remainingImeis = imeis;
|
| 30352 |
amit.gupta |
72 |
WebDriverWait wait10Sec = new WebDriverWait(driver, 10);
|
|
|
73 |
WebDriverWait wait5Sec = new WebDriverWait(driver, 5);
|
|
|
74 |
WebElement slideButton;
|
|
|
75 |
Actions actionProvider = new Actions(driver);
|
| 30209 |
amit.gupta |
76 |
do {
|
|
|
77 |
for (int i = 0; i < remainingImeis.size(); i++) {
|
|
|
78 |
try {
|
|
|
79 |
String imei = remainingImeis.get(i);
|
|
|
80 |
System.out.println("Starting fresh with new IMEI " + imei);
|
|
|
81 |
driver.findElement(By.className("el-input__inner")).sendKeys(imei);
|
|
|
82 |
driver.findElement(By.className("el-button--primary")).click();
|
|
|
83 |
|
|
|
84 |
// Perform click-and-hold action on the element
|
|
|
85 |
int counter = 1;
|
|
|
86 |
String fileName = "/tmp/" + name + "-simple.png";
|
| 30352 |
amit.gupta |
87 |
boolean captchaBroken = false;
|
| 30209 |
amit.gupta |
88 |
do {
|
| 30359 |
amit.gupta |
89 |
try {
|
| 30360 |
amit.gupta |
90 |
System.out.println("Starting Do");
|
|
|
91 |
wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.id("dx_captcha_basic_wrapper_1")));
|
|
|
92 |
wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.id("dx_captcha_basic_slider_1")));
|
|
|
93 |
slideButton = driver.findElement(By.id("dx_captcha_basic_slider_1"));
|
| 30359 |
amit.gupta |
94 |
actionProvider.moveToElement(slideButton).clickAndHold().moveByOffset(10, 0).perform();
|
|
|
95 |
} catch (StaleElementReferenceException staleElementReferenceException) {
|
|
|
96 |
System.out.println("Found stale element for " + imei + ", lets move ahead with fresh imei");
|
|
|
97 |
driver.get("https://support.oppo.com/in/warranty-check/");
|
| 30360 |
amit.gupta |
98 |
break;
|
| 30359 |
amit.gupta |
99 |
}
|
| 30209 |
amit.gupta |
100 |
Mat circles = getMatCircles(driver, fileName);
|
|
|
101 |
if (circles.cols() == 2) {
|
|
|
102 |
double firstCircleX = circles.get(0, 0)[0];
|
|
|
103 |
double secondCircleX = circles.get(0, 1)[0];
|
|
|
104 |
double distance = Math.abs(firstCircleX - secondCircleX);
|
|
|
105 |
|
|
|
106 |
System.out.println("Starting Checkdistance method");
|
| 30352 |
amit.gupta |
107 |
int moveBy = checkDistance(driver, wait10Sec, actionProvider, distance);
|
| 30209 |
amit.gupta |
108 |
if (moveBy > 0) {
|
|
|
109 |
System.out.println("Ending Checkdistance method " + moveBy);
|
|
|
110 |
actionProvider.moveByOffset(moveBy, 0).release().perform();
|
|
|
111 |
System.out.println("Move click performed");
|
|
|
112 |
try {
|
|
|
113 |
try {
|
| 30352 |
amit.gupta |
114 |
wait5Sec.until(ExpectedConditions.visibilityOfElementLocated(By.className("dx_captcha_basic_bar-inform")));
|
| 30209 |
amit.gupta |
115 |
System.out.println("Failed = " + counter);
|
|
|
116 |
} catch (Exception notFailedException) {
|
|
|
117 |
System.out.println("Success at attempt " + counter);
|
| 30359 |
amit.gupta |
118 |
List<WebElement> warrantyDateElements = driver.findElements(By.className("warranty-service--result_info__dateValue"));
|
|
|
119 |
if (warrantyDateElements.size() > 0) {
|
|
|
120 |
String warrantyDate = warrantyDateElements.get(0).getText();
|
| 30209 |
amit.gupta |
121 |
System.out.println("Date -- " + warrantyDate);
|
|
|
122 |
dateMap.put(imei, LocalDate.parse(warrantyDate, DateTimeFormatter.ofPattern("yyyy/MM/dd")).minusYears(1));
|
| 30359 |
amit.gupta |
123 |
} else {
|
| 30209 |
amit.gupta |
124 |
System.out.println("Could not capture date");
|
|
|
125 |
dateMap.put(imei, null);
|
|
|
126 |
}
|
|
|
127 |
WebElement checkAgainButton = driver.findElement(By.className("van-button--normal"));
|
| 30352 |
amit.gupta |
128 |
captchaBroken = true;
|
| 30209 |
amit.gupta |
129 |
checkAgainButton.click();
|
|
|
130 |
break;
|
|
|
131 |
}
|
|
|
132 |
} catch (Throwable e) {
|
|
|
133 |
System.out.println("Failed = " + counter + " ----------" + e.getMessage());
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
System.out.println("After circles conditions");
|
|
|
138 |
wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.id("dx_captcha_basic_wrapper_1")));
|
|
|
139 |
wait10Sec.until(ExpectedConditions.visibilityOfElementLocated(By.id("dx_captcha_basic_slider_1")));
|
|
|
140 |
slideButton = driver.findElement(By.id("dx_captcha_basic_slider_1"));
|
|
|
141 |
actionProvider.moveToElement(slideButton).doubleClick().perform();
|
| 30352 |
amit.gupta |
142 |
System.out.println("Ending Do" + counter++);
|
|
|
143 |
|
|
|
144 |
} while (!captchaBroken);
|
| 30209 |
amit.gupta |
145 |
} catch (Exception e) {
|
|
|
146 |
e.printStackTrace();
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
mainCount++;
|
|
|
150 |
remainingImeis = imeis.stream().filter(x -> dateMap.containsKey(x)).collect(Collectors.toList());
|
| 30359 |
amit.gupta |
151 |
} while (mainCount < 10 && dateMap.size() != imeis.size());
|
| 30209 |
amit.gupta |
152 |
driver.close();
|
|
|
153 |
driver.quit();
|
|
|
154 |
return dateMap;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
private int checkDistance(WebDriver driver, WebDriverWait wait3Sec, Actions actionProvider, double distance) throws Exception {
|
|
|
158 |
|
|
|
159 |
//actionProvider.moveToElement(slideButton).clickAndHold().moveByOffset(1, 0).perform();
|
|
|
160 |
actionProvider.moveByOffset(15, 0).perform();//
|
|
|
161 |
String fileName2 = "/tmp/" + Thread.currentThread().getName() + "-moved.png";
|
|
|
162 |
Mat movedCircles = getMatCircles(driver, fileName2);
|
|
|
163 |
if (movedCircles.cols() != 2) return 0;
|
|
|
164 |
double movedFirst = movedCircles.get(0, 0)[0];
|
|
|
165 |
double movedSecond = movedCircles.get(0, 1)[0];
|
|
|
166 |
//System.out.println("Test Offset Move " + testMove);
|
|
|
167 |
System.out.println("Found Moved " + movedFirst + " ," + movedSecond);
|
|
|
168 |
double movedDistance = Math.abs(movedFirst - movedSecond);
|
|
|
169 |
double moved = distance - movedDistance;
|
|
|
170 |
/*System.out.println("Original distance " + distance);
|
|
|
171 |
System.out.println("Moved Distance " + movedDistance);
|
|
|
172 |
System.out.println("Moved " + moved);*/
|
|
|
173 |
if (moved == 0) return 0;
|
|
|
174 |
int moveBy = (int) ((movedDistance) * (15 / moved));
|
|
|
175 |
//int moveBy = (int) distance - 21;
|
|
|
176 |
System.out.println("Move by = " + moveBy);
|
|
|
177 |
return moveBy;
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
private Mat getMatCircles(WebDriver driver, String fileName) throws Exception {
|
|
|
181 |
|
|
|
182 |
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
|
|
|
183 |
BufferedImage fullImg = ImageIO.read(screenshot);
|
|
|
184 |
|
|
|
185 |
// Get the location of element on the page
|
|
|
186 |
WebElement imageElement = driver.findElement(By.id("dx_captcha_basic_bg_1"));
|
|
|
187 |
Point point = imageElement.getLocation();
|
|
|
188 |
|
|
|
189 |
// Get width and height of the element
|
|
|
190 |
int eleWidth = imageElement.getSize().getWidth();
|
|
|
191 |
int eleHeight = imageElement.getSize().getHeight();
|
|
|
192 |
|
|
|
193 |
// Crop the entire page screenshot to get only element screenshot
|
|
|
194 |
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(),
|
|
|
195 |
eleWidth, eleHeight);
|
|
|
196 |
ImageIO.write(eleScreenshot, "png", screenshot);
|
|
|
197 |
|
|
|
198 |
// Copy the element screenshot to disk
|
|
|
199 |
File screenshotLocation = new File(fileName);
|
|
|
200 |
FileUtils.copyFile(screenshot, screenshotLocation);
|
|
|
201 |
|
|
|
202 |
Imgcodecs imageCodecs = new Imgcodecs();
|
|
|
203 |
Mat grayMatrix = imageCodecs.imread(fileName, Imgcodecs.IMREAD_COLOR);
|
|
|
204 |
Imgproc.cvtColor(grayMatrix, grayMatrix, Imgproc.COLOR_BGR2GRAY);
|
|
|
205 |
Mat medianBlur = new Mat();
|
|
|
206 |
Imgproc.medianBlur(grayMatrix, medianBlur, 5);
|
|
|
207 |
Mat circles = new Mat();
|
|
|
208 |
Imgproc.HoughCircles(medianBlur, circles, Imgproc.HOUGH_GRADIENT, 1, 15, 50, 20, 17, 25);
|
|
|
209 |
return circles;
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
}
|