-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleTest.java
More file actions
141 lines (115 loc) · 5.62 KB
/
SimpleTest.java
File metadata and controls
141 lines (115 loc) · 5.62 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
package com.example;
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.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.net.URL;
import java.time.Duration;
public class SimpleTest {
private WebDriver driver;
private static final String HUB_URL = "http://localhost:4444/wd/hub";
@BeforeMethod
public void setUp() throws Exception {
try {
System.out.println("Setting up Chrome options...");
// Try a simpler approach first - just basic capabilities
DesiredCapabilities caps = new DesiredCapabilities();
caps.setBrowserName("chrome");
// Add Chrome options as a map
ChromeOptions chromeOptions = new ChromeOptions();
// Use timestamp for unique user data directory
String uniqueUserDataDir = "/tmp/chrome-user-data-" + System.currentTimeMillis();
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("Chrome options configured. User data dir: " + uniqueUserDataDir);
// Merge the chrome options with capabilities
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() {
// Visit BStackDemo
driver.get("https://bstackdemo.com/");
// Get name of product to add to cart (first product)
WebElement productNameElem = driver.findElement(By.cssSelector("#\\33 > p"));
String productToAdd = productNameElem.getText();
// Click on add to cart
WebElement addToCartBtn = driver.findElement(By.cssSelector("#\\33 > .shelf-item__buy-btn"));
addToCartBtn.click();
// Get name of item in cart
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();
// Check if product in cart is same as one added
Assert.assertEquals(productInCart, productToAdd);
System.out.println("Test passed: Add to cart works!");
}
@Test
public void testCheckoutFlowBStackDemo() {
// Visit BStackDemo
driver.get("https://bstackdemo.com/");
// Sign in
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();
// Wait for login
try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
// Click on buy item
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();
// Add address details
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");
// Checkout
driver.findElement(By.id("checkout-shipping-continue")).click();
driver.findElement(By.xpath("//*[text()='Continue']")).click();
driver.findElement(By.xpath("//*[text()='Orders']")).click();
System.out.println("Test passed: Checkout flow works!");
}
@AfterMethod
public void tearDown() {
if (driver != null) {
driver.quit();
System.out.println("Browser closed");
}
}
}