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,70 @@
package com.baeldung.selenium;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import com.baeldung.selenium.config.SeleniumConfig;
public class SeleniumExample {
private SeleniumConfig config;
private String url = "http://www.baeldung.com/";
public SeleniumExample() {
config = new SeleniumConfig();
config.getDriver()
.get(url);
}
public void closeWindow() {
this.config.getDriver()
.close();
}
public String getTitle() {
return this.config.getDriver()
.getTitle();
}
public void getAboutBaeldungPage() {
closeOverlay();
clickAboutLink();
clickAboutUsLink();
}
private void closeOverlay() {
List<WebElement> webElementList = this.config.getDriver()
.findElements(By.tagName("a"));
if (webElementList != null) {
webElementList.stream()
.filter(webElement -> "Close".equalsIgnoreCase(webElement.getAttribute("title")))
.filter(WebElement::isDisplayed)
.findAny()
.ifPresent(WebElement::click);
}
}
private void clickAboutLink() {
Actions actions = new Actions(config.getDriver());
WebElement aboutElement = this.config.getDriver()
.findElement(By.id("menu-item-6138"));
actions.moveToElement(aboutElement).perform();
}
private void clickAboutUsLink() {
WebElement element = this.config.getDriver()
.findElement(By.partialLinkText("About Baeldung."));
element.click();
}
public boolean isAuthorInformationAvailable() {
return this.config.getDriver()
.getPageSource()
.contains("Hey ! I'm Eugen");
}
}
@@ -0,0 +1,51 @@
package com.baeldung.selenium.config;
import java.io.File;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SeleniumConfig {
private WebDriver driver;
public SeleniumConfig() {
driver = new FirefoxDriver();
driver.manage()
.timeouts()
.implicitlyWait(Duration.ofSeconds(5));
}
static {
System.setProperty("webdriver.gecko.driver", findFile("geckodriver.mac"));
}
private static String findFile(String filename) {
String[] paths = { "", "bin/", "target/classes" }; // if you have chromedriver somewhere else on the path, then put it here.
for (String path : paths) {
if (new File(path + filename).exists())
return path + filename;
}
return "";
}
public void close() {
driver.close();
}
public void navigateTo(String url) {
driver.navigate()
.to(url);
}
public void clickElement(WebElement element) {
element.click();
}
public WebDriver getDriver() {
return driver;
}
}
@@ -0,0 +1,23 @@
package com.baeldung.selenium.models;
import com.baeldung.selenium.config.SeleniumConfig;
import com.baeldung.selenium.pages.BaeldungAboutPage;
import org.openqa.selenium.support.PageFactory;
public class BaeldungAbout {
private SeleniumConfig config;
public BaeldungAbout(SeleniumConfig config) {
this.config = config;
PageFactory.initElements(config.getDriver(), BaeldungAboutPage.class);
}
public void navigateTo() {
config.navigateTo("http://www.baeldung.com/about/");
}
public String getPageTitle() {
return BaeldungAboutPage.title.getText();
}
}
@@ -0,0 +1,10 @@
package com.baeldung.selenium.pages;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class BaeldungAboutPage {
@FindBy(css = ".page-header > h1")
public static WebElement title;
}
@@ -0,0 +1,37 @@
package com.baeldung.selenium.pages;
import com.baeldung.selenium.config.SeleniumConfig;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class BaeldungHomePage {
private SeleniumConfig config;
@FindBy(css = ".nav--logo_mobile")
private WebElement title;
@FindBy(css = ".header--menu")
private WebElement startHere;
public BaeldungHomePage(SeleniumConfig config) {
this.config = config;
PageFactory.initElements(this.config.getDriver(), this);
}
public void navigate() {
this.config.navigateTo("http://www.baeldung.com/");
}
public String getPageTitle() {
return title.getAttribute("title");
}
public StartHerePage clickOnStartHere() {
config.clickElement(startHere);
StartHerePage startHerePage = new StartHerePage(config);
PageFactory.initElements(config.getDriver(), startHerePage);
return startHerePage;
}
}
@@ -0,0 +1,21 @@
package com.baeldung.selenium.pages;
import com.baeldung.selenium.config.SeleniumConfig;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class StartHerePage {
private SeleniumConfig config;
@FindBy(css = ".page-title")
private WebElement title;
public StartHerePage(SeleniumConfig config) {
this.config = config;
}
public String getPageTitle() {
return title.getText();
}
}
@@ -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();
}
}
@@ -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);
}
}
@@ -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);
}
}
@@ -0,0 +1,76 @@
package com.baeldung.selenium.tabs;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import javax.annotation.Nonnull;
import java.util.Optional;
import java.util.Set;
/**
* Helper class for browser tab handling.
*/
public class TabHelper {
private final WebDriver driver;
public TabHelper(@Nonnull final WebDriver driver) {
this.driver = driver;
}
/**
* Switch to the given browser tab.
*
* @param destinationWindowHandle the window handle of the destination tab.
* @return the window handle of the current tab before switching.
*/
public String switchToTab(@Nonnull final String destinationWindowHandle) {
final String currentWindowHandle = driver.getWindowHandle();
driver.switchTo().window(destinationWindowHandle);
return currentWindowHandle;
}
/**
* Close all browser tabs except the given one.
*
* @param windowHandle the window handle of the tab that should stay open.
*/
public void closeAllTabsExcept(@Nonnull final String windowHandle) {
for (final String handle : driver.getWindowHandles()) {
if (!handle.equals(windowHandle)) {
driver.switchTo().window(handle);
driver.close();
}
}
driver.switchTo().window(windowHandle);
}
/**
* Close all browser tabs except the current active one.
*/
public void closeAllTabsExceptCurrent() {
final String currentWindow = driver.getWindowHandle();
closeAllTabsExcept(currentWindow);
}
/**
* Open the given link and switch to the new opened tab.
* If the link is not opened in a new tab then no switch will be performed.
*
* @param link By of the link to open.
* @return the window handle of the tab before switching.
*/
public String openLinkAndSwitchToNewTab(@Nonnull final By link) {
final String windowHandle = driver.getWindowHandle();
final Set<String> windowHandlesBefore = driver.getWindowHandles();
driver.findElement(link).click();
final Set<String> windowHandlesAfter = driver.getWindowHandles();
windowHandlesAfter.removeAll(windowHandlesBefore);
final Optional<String> newWindowHandle = windowHandlesAfter.stream().findFirst();
newWindowHandle.ifPresent(s -> driver.switchTo().window(s));
return windowHandle;
}
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>