-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathTest1.java
More file actions
226 lines (182 loc) · 9.09 KB
/
Copy pathTest1.java
File metadata and controls
226 lines (182 loc) · 9.09 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.HashMap;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.MediaEntityBuilder;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
import com.aventstack.extentreports.reporter.JsonFormatter;
public class Test1 {
RemoteWebDriver driver = null;
public static String status = "passed";
public static String username = System.getenv("LT_USERNAME");
public static String access_key = System.getenv("LT_ACCESS_KEY");
ExtentSparkReporter spark = new ExtentSparkReporter("target/surefire-reports/html/extentReport.html");
JsonFormatter json = new JsonFormatter("target/surefire-reports/json/Extent_Report.json");
ExtentReports extent = new ExtentReports();
// String testURL = "https://todomvc.com/examples/react/#/";
String testURL = "https://ltqa-frontend.lambdatestinternal.com/sample-todo-app/";
String testURLTitle = "Sample page - lambdatest.com";
@BeforeMethod
@Parameters(value = { "browser", "version", "platform", "resolution" })
public void testSetUp(String browser, String version, String platform, String resolution) throws Exception {
String platformName = System.getenv("HYPEREXECUTE_PLATFORM") != null ? System.getenv("HYPEREXECUTE_PLATFORM")
: platform;
// LambdaTest specific options using W3C protocol (LT:Options)
HashMap<String, Object> ltOptions = new HashMap<>();
ltOptions.put("build", "[HyperExecute - 1] Demonstration of the TestNG Framework");
ltOptions.put("name", "[HyperExecute - 1] Demonstration of the TestNG Framework");
ltOptions.put("platformName", platformName);
ltOptions.put("tunnel", false);
ltOptions.put("network", true);
ltOptions.put("console", true);
ltOptions.put("visual", true);
ltOptions.put("selenium_version", "4.24.0");
ltOptions.put("w3c", true);
// Accessibility options
ltOptions.put("accessibility", true);
ltOptions.put("accessibility.wcagVersion", "wcag21a");
ltOptions.put("accessibility.bestPractice", false);
ltOptions.put("accessibility.needsReview", true);
// Use browser-specific Options class for W3C compliance
MutableCapabilities browserOptions;
switch (browser.toLowerCase()) {
case "chrome":
browserOptions = new ChromeOptions();
break;
case "microsoftedge":
case "edge":
browserOptions = new EdgeOptions();
break;
case "firefox":
browserOptions = new FirefoxOptions();
break;
default:
browserOptions = new ChromeOptions();
}
browserOptions.setCapability("browserVersion", version);
browserOptions.setCapability("platformName", platformName);
browserOptions.setCapability("LT:Options", ltOptions);
try {
driver = new RemoteWebDriver(
new URL("https://" + username + ":" + access_key + "@hub.lambdatest.com/wd/hub"), browserOptions);
} catch (MalformedURLException e) {
System.out.println("Invalid grid URL");
}
System.out.println("Started session");
}
@Test(description = "To Do App on React App")
public void test1_element_addition_1() throws InterruptedException {
extent.attachReporter(json, spark);
ExtentTest test1 = extent.createTest("demo application test 1-1", "To Do App test 1");
driver.get(testURL);
Thread.sleep(5000);
test1.log(Status.PASS, "URL is opened");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
test1.log(Status.PASS, "Wait created");
By textField = By.id("sampletodotext");
WebElement addText = driver.findElement(textField);
int item_count = 5;
for (int i = 1; i <= item_count; i++) {
addText.click();
addText.sendKeys("Adding a new item " + i + Keys.ENTER);
test1.log(Status.PASS, "New item No. " + i + " is added");
Thread.sleep(2000);
}
WebElement temp_element;
int totalCount = item_count + 5;
int remaining = totalCount - 1;
for (int i = 1; i < totalCount; i++, remaining--) {
String xpath = "(//input[@type='checkbox'])[" + i + "]";
driver.findElement(By.xpath(xpath)).click();
Thread.sleep(500);
test1.log(Status.PASS, "Item No. " + i + " marked completed");
By remainingItem = By.cssSelector("[data-testid='remaining-count']");
String actualText = driver.findElement(remainingItem).getText();
String expectedText = remaining + " of " + totalCount + " remaining";
if (!actualText.contains(expectedText)) {
test1.log(Status.FAIL, "Wrong Text Description");
System.out.println("unmatched at " + expectedText + " " + actualText);
status = "failed";
}
Thread.sleep(500);
String base64Screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
test1.log(Status.PASS, "Item No. " + i + " completed",
MediaEntityBuilder.createScreenCaptureFromBase64String(base64Screenshot, "sp-test").build());
}
extent.flush();
/* Once you are outside this code, the list would be empty */
}
@Test(description = "To Do App on React App")
public void test1_element_addition_2() throws InterruptedException, IOException {
ExtentTest test2 = extent.createTest("demo application test 1-2", "To Do App test 2");
driver.get(testURL);
Thread.sleep(5000);
test2.log(Status.PASS, "URL is opened");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
test2.log(Status.PASS, "Wait created");
By textField = By.id("sampletodotext");
WebElement addText = driver.findElement(textField);
int item_count = 5;
for (int i = 1; i <= item_count; i++) {
addText.click();
addText.sendKeys("Adding a new item " + i + Keys.ENTER);
test2.log(Status.PASS, "New item No. " + i + " is added");
Thread.sleep(2000);
}
WebElement temp_element;
int totalCount = item_count + 5;
int remaining = totalCount - 1;
for (int i = 1; i < totalCount; i++, remaining--) {
String xpath = "(//input[@type='checkbox'])[" + i + "]";
driver.findElement(By.xpath(xpath)).click();
Thread.sleep(500);
test2.log(Status.PASS, "Item No. " + i + " marked completed");
By remainingItem = By.cssSelector("[data-testid='remaining-count']");
String actualText = driver.findElement(remainingItem).getText();
String expectedText = remaining + " of " + totalCount + " remaining";
if (!actualText.contains(expectedText)) {
test2.log(Status.FAIL, "Wrong Text Description");
System.out.println("unmatched at " + expectedText + " " + actualText);
status = "failed";
}
Thread.sleep(500);
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
File destFile = new File("target/surefire-reports/json/screenshot.png");
// Copy the screenshot to destination
FileUtils.copyFile(srcFile, destFile);
test2.log(Status.PASS, "Item No. " + i + " completed",
MediaEntityBuilder.createScreenCaptureFromPath(destFile.getAbsolutePath(), "sp-test").build());
}
extent.flush();
/* Once you are outside this code, the list would be empty */
}
@AfterMethod
public void tearDown() {
if (driver != null) {
((JavascriptExecutor) driver).executeScript("lambda-status=" + status);
driver.quit();
}
}
}