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
+10
View File
@@ -0,0 +1,10 @@
### Relevant Articles:
- [Running Selenium Scripts with JMeter](https://www.baeldung.com/selenium-jmeter)
- [Fixing Selenium WebDriver Executable Path Error](https://www.baeldung.com/java-selenium-webdriver-path-error)
- [Implicit Wait vs Explicit Wait in Selenium Webdriver](https://www.baeldung.com/selenium-implicit-explicit-wait)
#### Notes:
- to run the live tests for the article *Fixing Selenium WebDriver Executable Path Error*, follow the manual setup described
[Fixing Selenium WebDriver Executable Path Error](https://www.baeldung.com/java-selenium-webdriver-path-error#manual-setup); download the 3
drivers mentioned and place them in the src/test/resources directory
+140
View File
@@ -0,0 +1,140 @@
+<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.4.1">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
<stringProp name="TestPlan.comments"></stringProp>
<boolProp name="TestPlan.functional_mode">false</boolProp>
<boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp>
<boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments"/>
</elementProp>
<stringProp name="TestPlan.user_define_classpath"></stringProp>
</TestPlan>
<hashTree>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">1</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">1</stringProp>
<stringProp name="ThreadGroup.ramp_time">1</stringProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration"></stringProp>
<stringProp name="ThreadGroup.delay"></stringProp>
<boolProp name="ThreadGroup.same_user_on_next_iteration">true</boolProp>
</ThreadGroup>
<hashTree>
<com.googlecode.jmeter.plugins.webdriver.config.ChromeDriverConfig guiclass="com.googlecode.jmeter.plugins.webdriver.config.gui.ChromeDriverConfigGui" testclass="com.googlecode.jmeter.plugins.webdriver.config.ChromeDriverConfig" testname="jp@gc - Chrome Driver Config" enabled="true">
<stringProp name="WebDriverConfig.proxy_type">SYSTEM</stringProp>
<stringProp name="WebDriverConfig.proxy_pac_url"></stringProp>
<stringProp name="WebDriverConfig.http_host"></stringProp>
<intProp name="WebDriverConfig.http_port">8080</intProp>
<boolProp name="WebDriverConfig.use_http_for_all_protocols">true</boolProp>
<stringProp name="WebDriverConfig.https_host"></stringProp>
<intProp name="WebDriverConfig.https_port">8080</intProp>
<stringProp name="WebDriverConfig.ftp_host"></stringProp>
<intProp name="WebDriverConfig.ftp_port">8080</intProp>
<stringProp name="WebDriverConfig.socks_host"></stringProp>
<intProp name="WebDriverConfig.socks_port">8080</intProp>
<stringProp name="WebDriverConfig.no_proxy">localhost</stringProp>
<boolProp name="WebDriverConfig.maximize_browser">true</boolProp>
<boolProp name="WebDriverConfig.reset_per_iteration">false</boolProp>
<boolProp name="WebDriverConfig.dev_mode">false</boolProp>
<stringProp name="ChromeDriverConfig.chromedriver_path">C:\Users\Dell\Downloads\chromedriver.exe</stringProp>
<boolProp name="ChromeDriverConfig.android_enabled">false</boolProp>
<boolProp name="ChromeDriverConfig.headless_enabled">false</boolProp>
<boolProp name="ChromeDriverConfig.insecurecerts_enabled">false</boolProp>
<boolProp name="ChromeDriverConfig.incognito_enabled">false</boolProp>
<boolProp name="ChromeDriverConfig.no_sandbox_enabled">false</boolProp>
</com.googlecode.jmeter.plugins.webdriver.config.ChromeDriverConfig>
<hashTree/>
<com.googlecode.jmeter.plugins.webdriver.sampler.WebDriverSampler guiclass="com.googlecode.jmeter.plugins.webdriver.sampler.gui.WebDriverSamplerGui" testclass="com.googlecode.jmeter.plugins.webdriver.sampler.WebDriverSampler" testname="jp@gc - WebDriver Sampler" enabled="true">
<stringProp name="WebDriverSampler.script">WDS.sampleResult.sampleStart()
WDS.browser.get(&apos;http://baeldung.com&apos;)
WDS.sampleResult.sampleEnd()
WDS.log.info(&quot;successfully navigated to http://baeldung.com/&quot;);
</stringProp>
<stringProp name="WebDriverSampler.parameters"></stringProp>
<stringProp name="WebDriverSampler.language">javascript</stringProp>
</com.googlecode.jmeter.plugins.webdriver.sampler.WebDriverSampler>
<hashTree/>
<ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="View Results Tree" enabled="true">
<boolProp name="ResultCollector.error_logging">false</boolProp>
<objProp>
<name>saveConfig</name>
<value class="SampleSaveConfiguration">
<time>true</time>
<latency>true</latency>
<timestamp>true</timestamp>
<success>true</success>
<label>true</label>
<code>true</code>
<message>true</message>
<threadName>true</threadName>
<dataType>true</dataType>
<encoding>false</encoding>
<assertions>true</assertions>
<subresults>true</subresults>
<responseData>false</responseData>
<samplerData>false</samplerData>
<xml>false</xml>
<fieldNames>true</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
<sentBytes>true</sentBytes>
<url>true</url>
<threadCounts>true</threadCounts>
<idleTime>true</idleTime>
<connectTime>true</connectTime>
</value>
</objProp>
<stringProp name="filename"></stringProp>
</ResultCollector>
<hashTree/>
<ResultCollector guiclass="TableVisualizer" testclass="ResultCollector" testname="View Results in Table" enabled="true">
<boolProp name="ResultCollector.error_logging">false</boolProp>
<objProp>
<name>saveConfig</name>
<value class="SampleSaveConfiguration">
<time>true</time>
<latency>true</latency>
<timestamp>true</timestamp>
<success>true</success>
<label>true</label>
<code>true</code>
<message>true</message>
<threadName>true</threadName>
<dataType>true</dataType>
<encoding>false</encoding>
<assertions>true</assertions>
<subresults>true</subresults>
<responseData>false</responseData>
<samplerData>false</samplerData>
<xml>false</xml>
<fieldNames>true</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
<sentBytes>true</sentBytes>
<url>true</url>
<threadCounts>true</threadCounts>
<idleTime>true</idleTime>
<connectTime>true</connectTime>
</value>
</objProp>
<stringProp name="filename"></stringProp>
</ResultCollector>
<hashTree/>
</hashTree>
</hashTree>
</hashTree>
</jmeterTestPlan>
+59
View File
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>selenium-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>selenium-2</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>testing-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium-java.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>${webdrivermanager.version}</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<testng.version>6.10</testng.version>
<selenium-java.version>4.8.3</selenium-java.version>
<webdrivermanager.version>5.3.2</webdrivermanager.version>
</properties>
</project>
@@ -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;
}
}
}