-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseTest.java
More file actions
173 lines (129 loc) · 5.24 KB
/
BaseTest.java
File metadata and controls
173 lines (129 loc) · 5.24 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package saucedemo.base;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.stream.IntStream;
public class BaseTest{
public WebDriver driver;
private static final Properties PROPERTIES = new Properties();
public static List<Item> productsOrdered = new ArrayList<>();
protected static Integer[] randomNumbers;
protected static int randomNumbersIndex = 0;
@BeforeSuite
public void readPropertiesFile() {
try {
FileInputStream input = new FileInputStream("src/test/java/saucedemo/base/resourcesSaucedemo.properties");
PROPERTIES.load(input);
} catch (IOException e) {
e.printStackTrace();
}
}
@BeforeMethod
public void setup() {
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("--no-sandbox"); // Bypass OS security model
options.addArguments("--incognito"); // Runs a new, clean instance of the browser
options.addArguments("--disable-dev-shm-usage"); // Overcome limited resource problems in Docker
options.addArguments("--headless"); // The browser is not displayed
options.addArguments("--disable-gpu"); // Disables the GPU, used when running headless
options.addArguments("--start-maximized"); // Runs on the entire screen
options.addArguments("--disable-features=PasswordCheck"); // No longer displays a pop-up mentioning that the "passwords are exposed in a data breach"
driver = new ChromeDriver(options);
driver.navigate().to(PROPERTIES.getProperty("url.base"));
BasePageObject.setDriver(driver);
waitImplicit(1);
setUpRandomNumbersArray();
}
public static String getUsernameRegular(){
return PROPERTIES.getProperty("username.standard");
}
public static String getUsernameLocked(){
return PROPERTIES.getProperty("username.locked");
}
public static String getUsernameProblem(){
return PROPERTIES.getProperty("username.problem");
}
public static String getUsernamePerformanceIssues(){
return PROPERTIES.getProperty("username.performance");
}
public static String getUsernameNotRegistered(){
return PROPERTIES.getProperty("username.unavailable");
}
public static String getUsernameError(){
return PROPERTIES.getProperty("username.error");
}
public static String getPassword(){
return PROPERTIES.getProperty("password");
}
public static String getURLBase(){
return PROPERTIES.getProperty("url.base");
}
public static String getURLCart(){
return (PROPERTIES.getProperty("url.base") + PROPERTIES.getProperty("url.cart"));
}
public static String getURLInventory(){
return (PROPERTIES.getProperty("url.base") + PROPERTIES.getProperty("url.inventory"));
}
public static String getURLCheckoutStepOne(){
return (PROPERTIES.getProperty("url.base") + PROPERTIES.getProperty("url.checkout.one"));
}
public static String getURLCheckoutStepTwo(){
return (PROPERTIES.getProperty("url.base") + PROPERTIES.getProperty("url.checkout.two"));
}
public static String getURLCheckoutSuccess(){
return (PROPERTIES.getProperty("url.base") + PROPERTIES.getProperty("/checkout-complete.html"));
}
public static int getTotalNumberOfDisplayedProducts(){
String count = PROPERTIES.getProperty("products.count");
return Integer.parseInt(count);
}
/**
* @return a random unique value
*/
public static int randomNumber0ToTotalAvailableProducts(){
if (randomNumbersIndex >= getTotalNumberOfDisplayedProducts()) {
System.out.println("All random numbers have been used");
return -1;
}
else {
return randomNumbers[randomNumbersIndex++];
}
}
/**
* This method prepares a unique and random array at each test run.
* It will return -1 after the max number of entries.
*/
private void setUpRandomNumbersArray(){
randomNumbers = new Integer[getTotalNumberOfDisplayedProducts()];
IntStream.range(0, getTotalNumberOfDisplayedProducts()).forEach(i -> randomNumbers[i] = i);
List<Integer> intList = Arrays.asList(randomNumbers);
Collections.shuffle(intList);
intList.toArray(randomNumbers);
System.out.println("[DEBUG] The current random array is: " + Arrays.toString(randomNumbers));
}
public static void waitImplicit(long seconds){
try{
Thread.sleep(seconds * 1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
@AfterMethod
public void tearDown() {
randomNumbersIndex = 0;
waitImplicit(1);
driver.quit();
}
}