-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleTest.java
More file actions
114 lines (95 loc) · 4.55 KB
/
SimpleTest.java
File metadata and controls
114 lines (95 loc) · 4.55 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
package com.example;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.URL;
import java.time.Duration;
import static org.junit.Assert.assertEquals;
public class SimpleTest {
private WebDriver driver;
private static final String HUB_URL = "http://localhost:4444/wd/hub";
@Before
public void setUp() throws Exception {
try {
System.out.println("Setting up Chrome options...");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments(
"--headless",
"--no-first-run",
"--no-default-browser-check",
"--disable-extensions",
"--disable-default-apps",
"--disable-gpu",
"--disable-dev-shm-usage",
"--disable-software-rasterizer",
"--no-sandbox",
"--disable-background-timer-throttling",
"--disable-backgrounding-occluded-windows",
"--disable-renderer-backgrounding",
"--disable-features=TranslateUI",
"--disable-ipc-flooding-protection",
"--disable-web-security",
"--disable-features=VizDisplayCompositor",
"--disable-logging",
"--silent"
);
System.out.println("Connecting to Selenium Grid at: " + HUB_URL);
driver = new RemoteWebDriver(new URL(HUB_URL), chromeOptions);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().window().maximize();
System.out.println("Successfully connected to Selenium Grid!");
} catch (Exception e) {
System.err.println("Error during setup: " + e.getMessage());
e.printStackTrace();
throw e;
}
}
@Test
public void testAddToCartBStackDemo() {
driver.get("https://bstackdemo.com/");
WebElement productNameElem = driver.findElement(By.cssSelector("#\\33 > p"));
String productToAdd = productNameElem.getText();
WebElement addToCartBtn = driver.findElement(By.cssSelector("#\\33 > .shelf-item__buy-btn"));
addToCartBtn.click();
WebElement productInCartElem = driver.findElement(By.cssSelector("#__next > div > div > div.float-cart.float-cart--open > div.float-cart__content > div.float-cart__shelf-container > div > div.shelf-item__details > p.title"));
String productInCart = productInCartElem.getText();
assertEquals(productToAdd, productInCart);
System.out.println("Test passed: Add to cart works!");
}
@Test
public void testCheckoutFlowBStackDemo() {
driver.get("https://bstackdemo.com/");
driver.findElement(By.id("signin")).click();
driver.findElement(By.cssSelector("#username svg")).click();
driver.findElement(By.id("react-select-2-option-0-0")).click();
driver.findElement(By.cssSelector("#password svg")).click();
driver.findElement(By.id("react-select-3-option-0-0")).click();
driver.findElement(By.id("login-btn")).click();
try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
driver.findElement(By.cssSelector("#\\31 > .shelf-item__buy-btn")).click();
driver.findElement(By.cssSelector("div.float-cart__close-btn")).click();
driver.findElement(By.cssSelector("#\\32 > .shelf-item__buy-btn")).click();
driver.findElement(By.cssSelector(".buy-btn")).click();
driver.findElement(By.id("firstNameInput")).sendKeys("first");
driver.findElement(By.id("lastNameInput")).sendKeys("last");
driver.findElement(By.id("addressLine1Input")).sendKeys("address");
driver.findElement(By.id("provinceInput")).sendKeys("province");
driver.findElement(By.id("postCodeInput")).sendKeys("pincode");
driver.findElement(By.id("checkout-shipping-continue")).click();
String checkoutMessage = driver.findElement(By.id("confirmation-message")).getText();
assertEquals("Your Order has been successfully placed.", checkoutMessage);
System.out.println("Test passed: Checkout flow works!");
}
@After
public void tearDown() {
if (driver != null) {
driver.quit();
System.out.println("Browser closed");
}
}
}