Selenium error (#12924)

* BAEL-5010 added manual and automated selenium setup tests

* BAEL-5010 added manual and automated selenium setup tests

Co-authored-by: aschoeng <andreas.schoengruber@mic-cust.com>
This commit is contained in:
AndiCover 2022-11-09 05:01:14 +01:00 committed by GitHub
parent d0c3a7dfd6
commit 50f2c39544
4 changed files with 203 additions and 1 deletions

View File

@ -35,6 +35,11 @@
<artifactId>ashot</artifactId>
<version>${ashot.version}</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>${webdrivermanager.version}</version>
</dependency>
</dependencies>
<build>
@ -52,8 +57,9 @@
<properties>
<testng.version>6.10</testng.version>
<selenium-java.version>3.4.0</selenium-java.version>
<selenium-java.version>3.141.59</selenium-java.version>
<ashot.version>1.5.4</ashot.version>
<webdrivermanager.version>5.3.0</webdrivermanager.version>
</properties>
</project>

View File

@ -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();
}
}
}

View File

@ -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.clearProperty("webdriver.chrome.driver");
System.clearProperty("webdriver.gecko.driver");
System.clearProperty("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);
}
}

View File

@ -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();
}
}
}