JAVA-21486 Split or move selenium-junit-testng module (moved-4) (#14366)

Co-authored-by: timis1 <noreplay@yahoo.com>
This commit is contained in:
timis1
2023-07-09 14:56:19 +03:00
committed by GitHub
parent 92c575498c
commit cff81e0975
38 changed files with 39 additions and 32 deletions
@@ -0,0 +1,83 @@
package com.baeldung.selenium.setup;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
final class AutomatedSetupLiveTest {
private static final String TITLE_XPATH = "//a[@href='/']";
private static final String URL = "https://www.baeldung.com";
private WebDriver driver;
private void setupChromeDriver() {
WebDriverManager.chromedriver()
.setup();
driver = new ChromeDriver();
options();
}
private void setupGeckoDriver() {
WebDriverManager.firefoxdriver()
.setup();
driver = new FirefoxDriver();
options();
}
private void setupEdgeDriver() {
WebDriverManager.edgedriver()
.setup();
driver = new EdgeDriver();
options();
}
private void options() {
driver.manage()
.window()
.maximize();
}
@Test
void givenChromeDriver_whenNavigateToBaeldung_thenFindTitleIsSuccessful() {
setupChromeDriver();
driver.get(URL);
final WebElement title = driver.findElement(By.xpath(TITLE_XPATH));
assertEquals("Baeldung", title.getAttribute("title"));
}
@Test
void givenGeckoDriver_whenNavigateToBaeldung_thenFindTitleIsSuccessful() {
setupGeckoDriver();
driver.get(URL);
final WebElement title = driver.findElement(By.xpath(TITLE_XPATH));
assertEquals("Baeldung", title.getAttribute("title"));
}
@Test
void givenEdgeDriver_whenNavigateToBaeldung_thenFindTitleIsSuccessful() {
setupEdgeDriver();
driver.get(URL);
final WebElement title = driver.findElement(By.xpath(TITLE_XPATH));
assertEquals("Baeldung", title.getAttribute("title"));
}
@AfterEach
void teardown() {
if (driver != null) {
driver.quit();
}
}
}
@@ -0,0 +1,35 @@
package com.baeldung.selenium.setup;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
final class InvalidSetupLiveTest {
@BeforeAll
static void setup() {
// Make sure the properties are cleared before the tests.
System.setProperty("webdriver.chrome.driver", "");
System.setProperty("webdriver.gecko.driver","");
System.setProperty("webdriver.edge.driver","");
}
@Test
void givenInvalidChromeSetup_whenInit_thenFail() {
assertThrowsExactly(IllegalStateException.class, ChromeDriver::new);
}
@Test
void givenInvalidFirefoxSetup_whenInit_thenFail() {
assertThrowsExactly(IllegalStateException.class, FirefoxDriver::new);
}
@Test
void givenInvalidEdgeSetup_whenInit_thenFail() {
assertThrowsExactly(IllegalStateException.class, EdgeDriver::new);
}
}
@@ -0,0 +1,78 @@
package com.baeldung.selenium.setup;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
final class ManualSetupLiveTest {
private static final String TITLE_XPATH = "//a[@href='/']";
private static final String URL = "https://www.baeldung.com";
private WebDriver driver;
private void setupChromeDriver() {
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
driver = new ChromeDriver();
options();
}
private void setupGeckoDriver() {
System.setProperty("webdriver.gecko.driver", "src/test/resources/geckodriver.exe");
driver = new FirefoxDriver();
options();
}
private void setupEdgeDriver() {
System.setProperty("webdriver.edge.driver", "src/test/resources/msedgedriver.exe");
driver = new EdgeDriver();
options();
}
private void options() {
driver.manage()
.window()
.maximize();
}
@Test
void givenChromeDriver_whenNavigateToBaeldung_thenFindTitleIsSuccessful() {
setupChromeDriver();
driver.get(URL);
final WebElement title = driver.findElement(By.xpath(TITLE_XPATH));
assertEquals("Baeldung", title.getAttribute("title"));
}
@Test
void givenEdgeDriver_whenNavigateToBaeldung_thenFindTitleIsSuccessful() {
setupEdgeDriver();
driver.get(URL);
final WebElement title = driver.findElement(By.xpath(TITLE_XPATH));
assertEquals("Baeldung", title.getAttribute("title"));
}
@Test
void givenGeckoDriver_whenNavigateToBaeldung_thenFindTitleIsSuccessful() {
setupGeckoDriver();
driver.get(URL);
final WebElement title = driver.findElement(By.xpath(TITLE_XPATH));
assertEquals("Baeldung", title.getAttribute("title"));
}
@AfterEach
void teardown() {
if (driver != null) {
driver.quit();
}
}
}
@@ -0,0 +1,80 @@
package com.baeldung.selenium.wait;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.time.Duration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotInteractableException;
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 io.github.bonigarcia.wdm.WebDriverManager;
final class ExplicitWaitLiveTest {
private static WebDriver driver;
private static WebDriverWait wait;
private static final int TIMEOUT = 10;
private static final By LOCATOR_ABOUT = By.xpath("//a[starts-with(., 'About')]");
private static final By LOCATOR_ABOUT_BAELDUNG = By.xpath("//h3[normalize-space()='About Baeldung']");
private static final By LOCATOR_ABOUT_HEADER = By.xpath("//h1");
private static void setupChromeDriver() {
WebDriverManager.chromedriver().setup();
final ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
driver = new ChromeDriver(options);
options();
}
private static void options() {
driver.manage().window().maximize();
}
@BeforeEach
public void init() {
setupChromeDriver();
wait = new WebDriverWait(driver, Duration.ofSeconds(TIMEOUT));
}
@Test
void givenPage_whenNavigatingWithoutExplicitWait_thenElementNotInteractable() {
driver.navigate().to("https://www.baeldung.com/");
driver.findElement(LOCATOR_ABOUT).click();
assertThrows(ElementNotInteractableException.class, () -> driver.findElement(LOCATOR_ABOUT_BAELDUNG).click());
}
@Test
void givenPage_whenNavigatingWithExplicitWait_thenOK() {
final String expected = "About Baeldung";
driver.navigate().to("https://www.baeldung.com/");
driver.findElement(LOCATOR_ABOUT).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(LOCATOR_ABOUT_BAELDUNG));
driver.findElement(LOCATOR_ABOUT_BAELDUNG).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(LOCATOR_ABOUT_HEADER));
final String actual = driver.findElement(LOCATOR_ABOUT_HEADER).getText();
assertEquals(expected, actual);
}
@AfterEach
void teardown() {
if (driver != null) {
driver.quit();
driver = null;
}
}
}
@@ -0,0 +1,84 @@
package com.baeldung.selenium.wait;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.time.Duration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotInteractableException;
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.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import io.github.bonigarcia.wdm.WebDriverManager;
final class FluentWaitLiveTest {
private static WebDriver driver;
private static Wait<WebDriver> wait;
private static final int TIMEOUT = 10;
private static final int POLL_FREQUENCY = 250;
private static final By LOCATOR_ABOUT = By.xpath("//a[starts-with(., 'About')]");
private static final By LOCATOR_ABOUT_BAELDUNG = By.xpath("//h3[normalize-space()='About Baeldung']");
private static final By LOCATOR_ABOUT_HEADER = By.xpath("//h1");
private static void setupChromeDriver() {
WebDriverManager.chromedriver().setup();
final ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
driver = new ChromeDriver(options);
options();
}
private static void options() {
driver.manage().window().maximize();
}
@BeforeEach
public void init() {
setupChromeDriver();
wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(TIMEOUT))
.pollingEvery(Duration.ofMillis(POLL_FREQUENCY));
}
@Test
void givenPage_whenNavigatingWithoutFluentWait_thenElementNotInteractable() {
driver.navigate().to("https://www.baeldung.com/");
driver.findElement(LOCATOR_ABOUT).click();
assertThrows(ElementNotInteractableException.class, () -> driver.findElement(LOCATOR_ABOUT_BAELDUNG).click());
}
@Test
void givenPage_whenNavigatingWithFluentWait_thenOK() {
final String expected = "About Baeldung";
driver.navigate().to("https://www.baeldung.com/");
driver.findElement(LOCATOR_ABOUT).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(LOCATOR_ABOUT_BAELDUNG));
driver.findElement(LOCATOR_ABOUT_BAELDUNG).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(LOCATOR_ABOUT_HEADER));
final String actual = driver.findElement(LOCATOR_ABOUT_HEADER).getText();
assertEquals(expected, actual);
}
@AfterEach
void teardown() {
if (driver != null) {
driver.quit();
driver = null;
}
}
}
@@ -0,0 +1,63 @@
package com.baeldung.selenium.wait;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.time.Duration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import io.github.bonigarcia.wdm.WebDriverManager;
final class ImplicitWaitLiveTest {
private static WebDriver driver;
private static final int TIMEOUT = 10;
private static final By LOCATOR_ABOUT = By.xpath("//a[starts-with(., 'About')]");
private static final By LOCATOR_ABOUT_BAELDUNG = By.xpath("//h3[normalize-space()='About Baeldung']");
private static final By LOCATOR_ABOUT_HEADER = By.xpath("//h1");
private static void setupChromeDriver() {
WebDriverManager.chromedriver().setup();
final ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
driver = new ChromeDriver(options);
options();
}
private static void options() {
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(TIMEOUT));
}
@BeforeEach
public void init() {
setupChromeDriver();
}
@Test
void givenPage_whenNavigatingWithImplicitWait_ThenOK() {
final String expected = "About Baeldung";
driver.navigate().to("https://www.baeldung.com/");
driver.findElement(LOCATOR_ABOUT).click();
driver.findElement(LOCATOR_ABOUT_BAELDUNG).click();
final String actual = driver.findElement(LOCATOR_ABOUT_HEADER).getText();
assertEquals(expected, actual);
}
@AfterEach
void teardown() {
if (driver != null) {
driver.quit();
driver = null;
}
}
}