Repository navigation
Expand file tree
/
Copy pathPageObject.java
More file actions
71 lines (63 loc) · 2.54 KB
/
Copy pathPageObject.java
File metadata and controls
71 lines (63 loc) · 2.54 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
package br.com.alura.leilao;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class PageObject {
protected WebDriver browser;
public PageObject(WebDriver browser) {
if (browser == null) {
ChromeOptions options = new ChromeOptions();
boolean isCi = "true".equalsIgnoreCase(System.getenv("CI"));
String headlessProp = System.getProperty("headless");
boolean isHeadless = headlessProp != null
? Boolean.parseBoolean(headlessProp)
: isCi && System.getenv("DISPLAY") == null;
if (isHeadless) {
options.addArguments("--headless=new", "--disable-extensions");
}
if (isHeadless || isCi) {
options.addArguments(
"--no-sandbox",
"--disable-dev-shm-usage",
"--disable-gpu",
"--remote-allow-origins=*",
"--window-size=1920,1080"
);
}
String chromeBinary = System.getProperty("webdriver.chrome.binary");
if (chromeBinary != null && !chromeBinary.trim().isEmpty()) {
options.setBinary(chromeBinary);
}
this.browser = new ChromeDriver(options);
} else {
this.browser = browser;
}
this.browser.manage().timeouts()
.implicitlyWait(Duration.ofSeconds(5))
.pageLoadTimeout(Duration.ofSeconds(10));
}
/**
* Cria uma instancia padrao de espera explicita para interacoes de UI.
*/
protected WebDriverWait esperaPadrao() {
return new WebDriverWait(this.browser, Duration.ofSeconds(5));
}
/**
* Aguarda a navegacao sair da URL informada e concluir o carregamento do novo documento.
*/
protected void aguardarSaidaDaUrl(String urlAnterior) {
WebDriverWait wait = esperaPadrao();
wait.until(ExpectedConditions.not(ExpectedConditions.urlToBe(urlAnterior)));
wait.until(driver -> "complete".equals(
((JavascriptExecutor) driver).executeScript("return document.readyState")));
}
public void fechar() {
if (this.browser != null) {
this.browser.quit();
}
}
}