forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmazonDataSearch.java
More file actions
107 lines (89 loc) · 2.89 KB
/
Copy pathAmazonDataSearch.java
File metadata and controls
107 lines (89 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import Utilities.ExcelUtils;
public class AmazonDataSearch {
static WebDriver driver;
WebElement results;
WebElement search;
@BeforeClass
public static void setUp() {
// System.setProperty("webdriver.chrome.driver"," ")
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.amazon.com");
}
@Test
public void searchTest() {
ExcelUtils.openExcelFile("./src/test/resources/ExcelData/amazon.xlsx", "sheet1");
int rowsCount = ExcelUtils.getUsedRowsCount();
for (int rowNum = 1; rowNum < rowsCount; rowNum++) {
String searchItem = ExcelUtils.getCellData(rowNum, 1);
searchFor(searchItem);
String resultText = getSearchResult();
int resultCount = cleanupSearchResultsCount(resultText);
ExcelUtils.setCellData(String.valueOf(resultCount), rowNum, 2);
System.out.println("Search count is : " + resultCount);
if (resultCount > 0) {
System.out.println("Search Test pass");
ExcelUtils.setCellData("pass", rowNum, 3);
} else {
System.out.println("Search Test fail");
ExcelUtils.setCellData("fail", rowNum, 3);
}
String now = LocalDateTime.now().toString();
ExcelUtils.setCellData(now, rowNum, 4);
}
// driver.findElement(By.id("twotabsearchtextbox")).sendKeys("Cucumber
// book"+Keys.ENTER);
// String results=driver.findElement(By.id("s-result-count")).getText();
// System.out.println(results);
// int numberOfResults=cleanupSearchResultsCount(results);
// if(numberOfResults > 0){
// System.out.println("PASS");
// }else{
// System.out.println("FAIL");
// }
}
public String getSearchResult() {
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
try {
results = driver.findElement(By.id("s-result-count"));
} catch (Exception e) {
return "0 result";
}
return results.getText();
}
public void searchFor(String item) {
search = driver.findElement(By.id("twotabsearchtextbox"));
search.clear();
search.sendKeys(item + Keys.ENTER);
}
public int cleanupSearchResultsCount(String searchResult) {
int resultsCount;
String longOne = searchResult;
String[] resultArr = searchResult.split(" ");
if (longOne.contains("of")) {
resultsCount = Integer.parseInt(resultArr[2].replace(",", ""));
} else {
resultsCount = Integer.parseInt(resultArr[0]);
}
return resultsCount;
}
@AfterClass
public static void tearDown() {
driver.quit();
}
}