-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaiting
122 lines (88 loc) · 4.11 KB
/
waiting
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
//Waiting examples
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class WaitForXHRRequests {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
driver.get("https://your-react-page.com");
// Wait for all XHR requests to complete
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(driver1 -> (Boolean) js.executeScript(
"return window.performance.getEntriesByType('resource')" +
".filter(e => e.initiatorType === 'xmlhttprequest').length === 0"));
System.out.println("All XHR requests have completed. The page is likely loaded!");
driver.quit();
}
}
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class WaitForReactReady {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
driver.get("https://your-react-page.com");
// Wait for React to be fully loaded by checking some global React-ready variable
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(driver1 -> (Boolean) js.executeScript("return window.__REACT_DEVTOOLS_GLOBAL_HOOK__ != null"));
System.out.println("React is ready.");
driver.quit();
}
}
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class WaitForDOMReady {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
driver.get("https://your-react-page.com");
// Wait for the document to reach the readyState of "complete"
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
wait.until(driver1 -> js.executeScript("return document.readyState").equals("complete"));
System.out.println("DOM is ready, though React may still be rendering.");
driver.quit();
}
}
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class WaitForSpinnerToDisappear {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://your-react-page.com");
// Wait for a spinner or loader to disappear (by class name, for example)
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("spinner-class")));
System.out.println("Loading spinner is gone, page loaded!");
driver.quit();
}
}
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class WaitForCustomReactFlag {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
driver.get("https://your-react-page.com");
// Custom waiting logic for some global React-ready flag or state
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(driver1 -> (Boolean) js.executeScript("return window.someGlobalReactFlag == true"));
System.out.println("React has finished rendering!");
driver.quit();
}
}