Merge branch 'master' of https://github.com/eugenp/tutorials into
task/JAVA-13721 # Conflicts: # testing-modules/mockito-simple/pom.xml
This commit is contained in:
@@ -8,3 +8,4 @@
|
||||
- [Parallel Test Execution for JUnit 5](https://www.baeldung.com/junit-5-parallel-tests)
|
||||
- [JUnit – Testing Methods That Call System.exit()](https://www.baeldung.com/junit-system-exit)
|
||||
- [Single Assert Call for Multiple Properties in Java Unit Testing](https://www.baeldung.com/java-testing-single-assert-multiple-properties)
|
||||
- [Creating a Test Suite With JUnit](https://www.baeldung.com/java-junit-test-suite)
|
||||
|
||||
+12
@@ -3,8 +3,10 @@ package com.baeldung.mockito.mockfinal;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.withSettings;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.MockMakers;
|
||||
|
||||
import com.baeldung.mockito.FinalList;
|
||||
import com.baeldung.mockito.MyList;
|
||||
@@ -28,4 +30,14 @@ class MockFinalsUnitTest {
|
||||
|
||||
assertThat(mock.size()).isNotEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMockFinalMethodMockWorks_withInlineMockMaker() {
|
||||
MyList myList = new MyList();
|
||||
|
||||
MyList mock = mock(MyList.class, withSettings().mockMaker(MockMakers.INLINE));
|
||||
when(mock.finalMethod()).thenReturn(1);
|
||||
|
||||
assertThat(mock.finalMethod()).isNotEqualTo(myList.finalMethod());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
<module>rest-assured</module>
|
||||
<module>rest-testing</module>
|
||||
<module>selenium-junit-testng</module>
|
||||
<module>selenium-webdriver</module>
|
||||
<module>spring-mockito</module>
|
||||
<module>spring-testing-2</module>
|
||||
<module>spring-testing</module>
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
- [Fixing Selenium WebDriver Executable Path Error](https://www.baeldung.com/java-selenium-webdriver-path-error)
|
||||
- [Handle Browser Tabs With Selenium](https://www.baeldung.com/java-handle-browser-tabs-selenium)
|
||||
- [Implicit Wait vs Explicit Wait in Selenium Webdriver](https://www.baeldung.com/selenium-implicit-explicit-wait)
|
||||
- [StaleElementReferenceException in Selenium](https://www.baeldung.com/selenium-staleelementreferenceexception)
|
||||
- [Retrieve the Value of an HTML Input in Selenium WebDriver](https://www.baeldung.com/java-selenium-html-input-value)
|
||||
|
||||
#### Notes:
|
||||
- to run the live tests for the article *Fixing Selenium WebDriver Executable Path Error*, follow the manual setup described
|
||||
|
||||
@@ -57,9 +57,9 @@
|
||||
|
||||
<properties>
|
||||
<testng.version>6.10</testng.version>
|
||||
<selenium-java.version>4.6.0</selenium-java.version>
|
||||
<selenium-java.version>4.8.3</selenium-java.version>
|
||||
<ashot.version>1.5.4</ashot.version>
|
||||
<webdrivermanager.version>5.3.0</webdrivermanager.version>
|
||||
<webdrivermanager.version>5.3.2</webdrivermanager.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package com.baeldung.selenium.stale;
|
||||
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class RobustWebDriver implements WebDriver {
|
||||
|
||||
private final WebDriver originalWebDriver;
|
||||
|
||||
public RobustWebDriver(WebDriver webDriver) {
|
||||
this.originalWebDriver = webDriver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void get(String url) {
|
||||
this.originalWebDriver.get(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCurrentUrl() {
|
||||
return this.originalWebDriver.getCurrentUrl();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTitle() {
|
||||
return this.originalWebDriver.getTitle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<WebElement> findElements(By by) {
|
||||
return this.originalWebDriver.findElements(by)
|
||||
.stream().map(e -> new RobustWebElement(e, by, this))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebElement findElement(By by) {
|
||||
return new RobustWebElement(this.originalWebDriver.findElement(by), by, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPageSource() {
|
||||
return this.originalWebDriver.getPageSource();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
this.originalWebDriver.close();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void quit() {
|
||||
this.originalWebDriver.quit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getWindowHandles() {
|
||||
return this.originalWebDriver.getWindowHandles();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getWindowHandle() {
|
||||
return this.originalWebDriver.getWindowHandle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TargetLocator switchTo() {
|
||||
return this.originalWebDriver.switchTo();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Navigation navigate() {
|
||||
return this.originalWebDriver.navigate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Options manage() {
|
||||
return this.originalWebDriver.manage();
|
||||
}
|
||||
}
|
||||
+176
@@ -0,0 +1,176 @@
|
||||
package com.baeldung.selenium.stale;
|
||||
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.Dimension;
|
||||
import org.openqa.selenium.OutputType;
|
||||
import org.openqa.selenium.Point;
|
||||
import org.openqa.selenium.Rectangle;
|
||||
import org.openqa.selenium.StaleElementReferenceException;
|
||||
import org.openqa.selenium.WebDriverException;
|
||||
import org.openqa.selenium.WebElement;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class RobustWebElement implements WebElement {
|
||||
|
||||
private WebElement originalElement;
|
||||
private final RobustWebDriver driver;
|
||||
private final By by;
|
||||
|
||||
private static final int MAX_RETRIES = 10;
|
||||
private static final String SERE = "Element is no longer attached to the DOM";
|
||||
|
||||
public RobustWebElement(WebElement element, By by, RobustWebDriver driver) {
|
||||
this.originalElement = element;
|
||||
this.by = by;
|
||||
this.driver = driver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void click() {
|
||||
executeMethodWithRetries(WebElement::click);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void submit() {
|
||||
executeMethodWithRetries(WebElement::submit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendKeys(CharSequence... keysToSend) {
|
||||
executeMethodWithRetriesVoid(WebElement::sendKeys, keysToSend);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
executeMethodWithRetries(WebElement::clear);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTagName() {
|
||||
return executeMethodWithRetries(WebElement::getTagName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAttribute(String name) {
|
||||
return executeMethodWithRetries(WebElement::getAttribute, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSelected() {
|
||||
return executeMethodWithRetries(WebElement::isSelected);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return executeMethodWithRetries(WebElement::isEnabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getText() {
|
||||
return executeMethodWithRetries(WebElement::getText);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<WebElement> findElements(By by) {
|
||||
return executeMethodWithRetries(WebElement::findElements, by);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebElement findElement(By by) {
|
||||
return executeMethodWithRetries(WebElement::findElement, by);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDisplayed() {
|
||||
return executeMethodWithRetries(WebElement::isDisplayed);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Point getLocation() {
|
||||
return executeMethodWithRetries(WebElement::getLocation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dimension getSize() {
|
||||
return executeMethodWithRetries(WebElement::getSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rectangle getRect() {
|
||||
return executeMethodWithRetries(WebElement::getRect);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCssValue(String propertyName) {
|
||||
return executeMethodWithRetries(WebElement::getCssValue, propertyName);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public <X> X getScreenshotAs(OutputType<X> target) throws WebDriverException {
|
||||
return executeMethodWithRetries(WebElement::getScreenshotAs, target);
|
||||
}
|
||||
|
||||
private void executeMethodWithRetries(Consumer<WebElement> method) {
|
||||
int retries = 0;
|
||||
while (retries < MAX_RETRIES) {
|
||||
try {
|
||||
WebElementUtils.callMethod(originalElement, method);
|
||||
return;
|
||||
} catch (StaleElementReferenceException ex) {
|
||||
refreshElement();
|
||||
}
|
||||
retries++;
|
||||
}
|
||||
throw new StaleElementReferenceException(SERE);
|
||||
}
|
||||
|
||||
private <T> T executeMethodWithRetries(Function<WebElement, T> method) {
|
||||
int retries = 0;
|
||||
while (retries < MAX_RETRIES) {
|
||||
try {
|
||||
return WebElementUtils.callMethodWithReturn(originalElement, method);
|
||||
} catch (StaleElementReferenceException ex) {
|
||||
refreshElement();
|
||||
}
|
||||
retries++;
|
||||
}
|
||||
throw new StaleElementReferenceException(SERE);
|
||||
}
|
||||
|
||||
private <U> void executeMethodWithRetriesVoid(BiConsumer<WebElement, U> method, U parameter) {
|
||||
int retries = 0;
|
||||
while (retries < MAX_RETRIES) {
|
||||
try {
|
||||
WebElementUtils.callMethod(originalElement, method, parameter);
|
||||
return;
|
||||
} catch (StaleElementReferenceException ex) {
|
||||
refreshElement();
|
||||
}
|
||||
retries++;
|
||||
}
|
||||
throw new StaleElementReferenceException(SERE);
|
||||
}
|
||||
|
||||
private <T, U> T executeMethodWithRetries(BiFunction<WebElement, U, T> method, U parameter) {
|
||||
int retries = 0;
|
||||
while (retries < MAX_RETRIES) {
|
||||
try {
|
||||
return WebElementUtils.callMethodWithReturn(originalElement, method, parameter);
|
||||
} catch (StaleElementReferenceException ex) {
|
||||
refreshElement();
|
||||
}
|
||||
retries++;
|
||||
}
|
||||
throw new StaleElementReferenceException(SERE);
|
||||
}
|
||||
|
||||
private void refreshElement() {
|
||||
this.originalElement = driver.findElement(by);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.selenium.stale;
|
||||
|
||||
import org.openqa.selenium.WebElement;
|
||||
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class WebElementUtils {
|
||||
|
||||
private WebElementUtils(){
|
||||
}
|
||||
|
||||
public static void callMethod(WebElement element, Consumer<WebElement> method) {
|
||||
method.accept(element);
|
||||
}
|
||||
|
||||
public static <U> void callMethod(WebElement element, BiConsumer<WebElement, U> method, U parameter) {
|
||||
method.accept(element, parameter);
|
||||
}
|
||||
|
||||
public static <T> T callMethodWithReturn(WebElement element, Function<WebElement, T> method) {
|
||||
return method.apply(element);
|
||||
}
|
||||
|
||||
public static <T, U> T callMethodWithReturn(WebElement element, BiFunction<WebElement, U, T> method, U parameter) {
|
||||
return method.apply(element, parameter);
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.selenium.stale;
|
||||
|
||||
import io.github.bonigarcia.wdm.WebDriverManager;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.chrome.ChromeDriver;
|
||||
import org.openqa.selenium.chrome.ChromeOptions;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
final class RobustWebElementLiveTest {
|
||||
|
||||
private static RobustWebDriver driver;
|
||||
private static final int TIMEOUT = 10;
|
||||
|
||||
private static final By LOCATOR_REFRESH = By.xpath("//a[.='click here']");
|
||||
private static final By LOCATOR_DYNAMIC_CONTENT = By.xpath(
|
||||
"(//div[@id='content']//div[@class='large-10 columns'])[1]");
|
||||
|
||||
private static void setupChromeDriver() {
|
||||
WebDriverManager.chromedriver().setup();
|
||||
final ChromeOptions options = new ChromeOptions();
|
||||
options.addArguments("--remote-allow-origins=*");
|
||||
driver = new RobustWebDriver(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 givenDynamicPage_whenRefreshingAndAccessingSavedElement_thenOK() {
|
||||
driver.navigate().to("https://the-internet.herokuapp.com/dynamic_content?with_content=static");
|
||||
final WebElement element = driver.findElement(LOCATOR_DYNAMIC_CONTENT);
|
||||
|
||||
driver.findElement(LOCATOR_REFRESH).click();
|
||||
Assertions.assertDoesNotThrow(element::getText);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void teardown() {
|
||||
if (driver != null) {
|
||||
driver.quit();
|
||||
driver = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
package com.baeldung.selenium.stale;
|
||||
|
||||
import io.github.bonigarcia.wdm.WebDriverManager;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.StaleElementReferenceException;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.chrome.ChromeDriver;
|
||||
import org.openqa.selenium.chrome.ChromeOptions;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
final class StaleElementReferenceLiveTest {
|
||||
|
||||
private static WebDriver driver;
|
||||
private static final int TIMEOUT = 10;
|
||||
|
||||
private static final By LOCATOR_REFRESH = By.xpath("//a[.='click here']");
|
||||
private static final By LOCATOR_DYNAMIC_CONTENT = By.xpath(
|
||||
"(//div[@id='content']//div[@class='large-10 columns'])[1]");
|
||||
|
||||
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 givenDynamicPage_whenRefreshingAndAccessingSavedElement_thenSERE() {
|
||||
driver.navigate().to("https://the-internet.herokuapp.com/dynamic_content?with_content=static");
|
||||
final WebElement element = driver.findElement(LOCATOR_DYNAMIC_CONTENT);
|
||||
|
||||
driver.findElement(LOCATOR_REFRESH).click();
|
||||
Assertions.assertThrows(StaleElementReferenceException.class, element::getText);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDynamicPage_whenRefreshingAndAccessingSavedElement_thenHandleSERE() {
|
||||
driver.navigate().to("https://the-internet.herokuapp.com/dynamic_content?with_content=static");
|
||||
final WebElement element = driver.findElement(LOCATOR_DYNAMIC_CONTENT);
|
||||
|
||||
if (!retryingFindClick(LOCATOR_REFRESH)) {
|
||||
Assertions.fail("Element is still stale after 5 attempts");
|
||||
}
|
||||
Assertions.assertDoesNotThrow(() -> retryingFindGetText(LOCATOR_DYNAMIC_CONTENT));
|
||||
}
|
||||
|
||||
private boolean retryingFindClick(By locator) {
|
||||
boolean result = false;
|
||||
int attempts = 0;
|
||||
while (attempts < 5) {
|
||||
try {
|
||||
driver.findElement(locator).click();
|
||||
result = true;
|
||||
break;
|
||||
} catch (StaleElementReferenceException ex) {
|
||||
System.out.println(ex.getMessage());
|
||||
}
|
||||
attempts++;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private String retryingFindGetText(By locator) {
|
||||
String result = null;
|
||||
int attempts = 0;
|
||||
while (attempts < 5) {
|
||||
try {
|
||||
result = driver.findElement(locator).getText();
|
||||
break;
|
||||
} catch (StaleElementReferenceException ex) {
|
||||
System.out.println(ex.getMessage());
|
||||
}
|
||||
attempts++;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void teardown() {
|
||||
if (driver != null) {
|
||||
driver.quit();
|
||||
driver = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.selenium.tabs;
|
||||
|
||||
import io.github.bonigarcia.wdm.WebDriverManager;
|
||||
import org.openqa.selenium.JavascriptExecutor;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WindowType;
|
||||
import org.openqa.selenium.chrome.ChromeDriver;
|
||||
import org.openqa.selenium.support.ui.ExpectedConditions;
|
||||
import org.openqa.selenium.support.ui.WebDriverWait;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
public class SeleniumOpenNewTabLiveTest {
|
||||
|
||||
private WebDriver driver;
|
||||
private static final int TIMEOUT = 10;
|
||||
private static final int EXPECTED_TABS_COUNT = 2;
|
||||
|
||||
@BeforeMethod
|
||||
public void initDriver() {
|
||||
WebDriverManager.chromedriver().setup();
|
||||
driver = new ChromeDriver();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUseTabsApiOpenWindow_thenNewTabOpened() {
|
||||
driver.switchTo().newWindow(WindowType.TAB);
|
||||
waitTabsCount(EXPECTED_TABS_COUNT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenExecuteOpenWindowJsScript_thenNewTabOpened() {
|
||||
((JavascriptExecutor) driver).executeScript("window.open()");
|
||||
waitTabsCount(EXPECTED_TABS_COUNT);
|
||||
}
|
||||
|
||||
|
||||
@AfterMethod
|
||||
public void closeBrowser() {
|
||||
driver.quit();
|
||||
}
|
||||
|
||||
|
||||
private void waitTabsCount(int tabsCount) {
|
||||
new WebDriverWait(driver, Duration.ofSeconds(TIMEOUT))
|
||||
.withMessage("Tabs count should be: " + tabsCount)
|
||||
.until(ExpectedConditions.numberOfWindowsToBe(tabsCount));
|
||||
}
|
||||
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.selenium.webdriver;
|
||||
|
||||
import org.junit.Assert;
|
||||
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.Keys;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.chrome.ChromeDriver;
|
||||
import io.github.bonigarcia.wdm.WebDriverManager;
|
||||
|
||||
public class SeleniumWebDriverUnitTest {
|
||||
|
||||
private WebDriver driver;
|
||||
|
||||
private static final String URL = "https://duckduckgo.com/";
|
||||
private static final String INPUT_ID = "search_form_input_homepage";
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
WebDriverManager.chromedriver().setup();
|
||||
driver = new ChromeDriver();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
driver.quit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDuckDuckGoHomePage_whenInputHelloWorld_thenInputValueIsHelloWorld() {
|
||||
driver.get(URL);
|
||||
WebElement inputElement = driver.findElement(By.id(INPUT_ID));
|
||||
inputElement.sendKeys(Keys.chord(Keys.CONTROL, "a"), Keys.DELETE);
|
||||
inputElement.sendKeys("Hello World!");
|
||||
|
||||
String inputValue = inputElement.getAttribute("value");
|
||||
Assert.assertEquals("Hello World!", inputValue);
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
@@ -0,0 +1,2 @@
|
||||
### Relevant Articles:
|
||||
- [Uploading File Using Selenium Webdriver in Java](https://www.baeldung.com/java-selenium-upload-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-webdriver</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>selenium-webdriver</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>
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.selenium.webdriver.fileupload;
|
||||
|
||||
import org.junit.Assert;
|
||||
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.WebElement;
|
||||
|
||||
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||
|
||||
import io.github.bonigarcia.wdm.WebDriverManager;
|
||||
|
||||
public class FileUploadWebDriverUnitTest {
|
||||
|
||||
private WebDriver driver;
|
||||
|
||||
private static final String URL = "http://www.csm-testcenter.org/test?do=show&subdo=common&test=file_upload";
|
||||
private static final String INPUT_NAME = "file_upload";
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
WebDriverManager.firefoxdriver()
|
||||
.setup();
|
||||
driver = new FirefoxDriver();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
driver.quit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFileUploadPage_whenInputFilePath_thenFileUploadEndsWithFilename() {
|
||||
driver.get(URL);
|
||||
|
||||
String filePath = System.getProperty("user.dir") + "/1688web.png";
|
||||
WebElement inputElement = driver.findElement(By.name(INPUT_NAME));
|
||||
WebElement submitButton = driver.findElement(By.name("http_submit"));
|
||||
|
||||
inputElement.sendKeys(filePath);
|
||||
|
||||
String actualFilePath = inputElement.getAttribute("value");
|
||||
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
|
||||
|
||||
submitButton.click();
|
||||
|
||||
Assert.assertTrue(actualFilePath.endsWith(fileName));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,3 +4,4 @@
|
||||
- [Concurrent Test Execution in Spring 5](https://www.baeldung.com/spring-5-concurrent-tests)
|
||||
- [Spring 5 Testing with @EnabledIf Annotation](https://www.baeldung.com/spring-5-enabledif)
|
||||
- [The Spring TestExecutionListener](https://www.baeldung.com/spring-testexecutionlistener)
|
||||
- [Execute Tests Based on Active Profile With JUnit 5](https://www.baeldung.com/spring-boot-junit-5-testing-active-profile)
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.activeprofile;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ActiveProfileApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ActiveProfileApplication.class);
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.activeprofile;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest(classes = ActiveProfileApplication.class)
|
||||
public class DevActiveProfileUnitTest {
|
||||
|
||||
@Value("${profile.property.value}")
|
||||
private String propertyString;
|
||||
|
||||
@Test
|
||||
void whenDevIsActive_thenValueShouldBeKeptFromApplicationYaml() {
|
||||
Assertions.assertEquals("This the the application.yaml file", propertyString);
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.activeprofile;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit.jupiter.EnabledIf;
|
||||
|
||||
@SpringBootTest(classes = ActiveProfileApplication.class)
|
||||
@EnabledIf(value = "#{{'test', 'prod'}.contains(environment.getActiveProfiles()[0])}", loadContext = true)
|
||||
@ActiveProfiles(value = "test")
|
||||
public class MultipleActiveProfileUnitTest {
|
||||
@Value("${profile.property.value}")
|
||||
private String propertyString;
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
@Test
|
||||
void whenDevIsActive_thenValueShouldBeKeptFromDedicatedApplicationYaml() {
|
||||
String currentProfile = env.getActiveProfiles()[0];
|
||||
Assertions.assertEquals(String.format("This the the application-%s.yaml file", currentProfile), propertyString);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.activeprofile;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit.jupiter.EnabledIf;
|
||||
|
||||
@SpringBootTest(classes = ActiveProfileApplication.class)
|
||||
@EnabledIf(value = "#{environment.getActiveProfiles()[0] == 'prod'}", loadContext = true)
|
||||
@ActiveProfiles(value = "prod")
|
||||
public class ProdActiveProfileUnitTest {
|
||||
|
||||
@Value("${profile.property.value}")
|
||||
private String propertyString;
|
||||
|
||||
@Test
|
||||
void whenProdIsActive_thenValueShouldBeKeptFromApplicationProdYaml() {
|
||||
Assertions.assertEquals("This the the application-prod.yaml file", propertyString);
|
||||
}
|
||||
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.activeprofile;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
@SpringBootTest(classes = ActiveProfileApplication.class)
|
||||
@ActiveProfiles(value = "test")
|
||||
public class TestActiveProfileUnitTest {
|
||||
|
||||
@Value("${profile.property.value}")
|
||||
private String propertyString;
|
||||
|
||||
@Test
|
||||
void whenTestIsActive_thenValueShouldBeKeptFromApplicationTestYaml() {
|
||||
Assertions.assertEquals("This the the application-test.yaml file", propertyString);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
profile:
|
||||
property:
|
||||
value: This the the application-prod.yaml file
|
||||
@@ -0,0 +1,3 @@
|
||||
profile:
|
||||
property:
|
||||
value: This the the application-test.yaml file
|
||||
@@ -0,0 +1,6 @@
|
||||
spring:
|
||||
profiles:
|
||||
active: dev
|
||||
profile:
|
||||
property:
|
||||
value: This the the application.yaml file
|
||||
Reference in New Issue
Block a user