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