Skip to content

Commit

Permalink
Feature/support logging of element property getters (#103)
Browse files Browse the repository at this point in the history
* Add DEBUG logging of capabilities, start arguments and options
Disable logging of setScriptTimeout if the value wasn't changed
Support logging of ElementStateProvider methods

* Reworked BrowserFactory, enhance retrying/logging of driver start
Add logging of current URL

* Renamed local variables in DriverSettings to fix sonar code smell

* Add logging of Browser's waitForPageToLoad()

* Add logging of Browser's handle alert methods

* Enhanced logging for CheckBox and RadioButton elements

* Added logging of element property getters
Enhanced logging for ComboBox methods

* Add logging of JsActions getters: isElementOnScreen and getXPath
Removed redundant localization values unused in the current lib

* Add retry for driver creation in case of timeout exception

* Updated access modificator of getDriver to protected in LocalBrowserFactory
Refactored DriverSettings getMapOrEmpty

* Add retry to custom BrowserFactory in tests

* Corrected values in localization and settings files

* Deprecated Form's isDisplayed() method, add state() instead
  • Loading branch information
mialeska authored Jul 6, 2020
1 parent 143fcd3 commit 767a188
Show file tree
Hide file tree
Showing 36 changed files with 386 additions and 266 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.aquality-automation</groupId>
<artifactId>aquality-selenium</artifactId>
<version>2.4.0</version>
<version>2.5.0</version>
<packaging>jar</packaging>
<name>Aquality Selenium</name>
<description>Library around Selenium WebDriver</description>
Expand Down Expand Up @@ -73,7 +73,7 @@
<dependency>
<groupId>com.github.aquality-automation</groupId>
<artifactId>aquality-selenium-core</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import aquality.selenium.configuration.IBrowserProfile;
import aquality.selenium.configuration.IConfiguration;
import aquality.selenium.configuration.ITimeoutConfiguration;
import aquality.selenium.core.localization.ILocalizedLogger;
import aquality.selenium.core.logging.Logger;
import aquality.selenium.core.utilities.IActionRetrier;
import aquality.selenium.core.waitings.IConditionalWait;
import aquality.selenium.elements.interfaces.IElementFactory;
import com.google.inject.Injector;
Expand Down Expand Up @@ -66,7 +68,8 @@ private static Injector getServiceProvider() {
*/
public static void setDefaultBrowserFactory() {
IBrowserFactory browserFactory = getBrowserProfile().isRemote()
? new RemoteBrowserFactory() : new LocalBrowserFactory();
? new RemoteBrowserFactory(get(IActionRetrier.class), getBrowserProfile(), get(ITimeoutConfiguration.class), getLocalizedLogger())
: new LocalBrowserFactory(get(IActionRetrier.class), getBrowserProfile(), getLocalizedLogger());
setBrowserFactory(browserFactory);
}

Expand Down
11 changes: 8 additions & 3 deletions src/main/java/aquality/selenium/browser/Browser.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ public void maximize() {
*/
public String getCurrentUrl() {
localizedLogger.info("loc.browser.getUrl");
return getDriver().getCurrentUrl();
String value = getDriver().getCurrentUrl();
localizedLogger.info("loc.browser.url.value", value);
return value;
}

/**
Expand Down Expand Up @@ -147,8 +149,8 @@ public IBrowserTabNavigation tabs() {
* @param timeout seconds to wait
*/
public void setPageLoadTimeout(Duration timeout) {
localizedLogger.debug("loc.browser.page.load.timeout", timeout.getSeconds());
if (!getBrowserName().equals(BrowserName.SAFARI)) {
localizedLogger.debug("loc.browser.page.load.timeout", timeout.getSeconds());
getDriver().manage().timeouts().pageLoadTimeout(timeout.getSeconds(), TimeUnit.SECONDS);
}
}
Expand All @@ -160,8 +162,8 @@ public void setPageLoadTimeout(Duration timeout) {
* @param timeout duration of time to wait
*/
public void setImplicitWaitTimeout(Duration timeout) {
localizedLogger.debug("loc.browser.implicit.timeout", timeout.getSeconds());
if (!timeout.equals(getImplicitWaitTimeout())) {
localizedLogger.debug("loc.browser.implicit.timeout", timeout.getSeconds());
getDriver().manage().timeouts().implicitlyWait(timeout.getSeconds(), TimeUnit.SECONDS);
implicitTimeout = timeout;
}
Expand All @@ -184,6 +186,7 @@ public void setScriptTimeout(Duration timeout) {
* Use setPageLoadTimeout to configure your own timeout from code if it is necessary
*/
public void waitForPageToLoad() {
localizedLogger.info("loc.browser.page.wait");
ExpectedCondition<Boolean> condition = d -> {
Object result = executeScript(JavaScript.IS_PAGE_LOADED.getScript());
return result instanceof Boolean && (Boolean) result;
Expand Down Expand Up @@ -305,9 +308,11 @@ public void handleAlert(AlertActions alertAction) {
* @param text message to send
*/
public void handlePromptAlert(AlertActions alertAction, String text) {
localizedLogger.info(String.format("loc.browser.alert.%s", alertAction.name().toLowerCase()));
try {
Alert alert = getDriver().switchTo().alert();
if (text != null && !text.isEmpty()) {
localizedLogger.info("loc.send.text", text);
getDriver().switchTo().alert().sendKeys(text);
}
if (alertAction.equals(AlertActions.ACCEPT)) {
Expand Down
58 changes: 29 additions & 29 deletions src/main/java/aquality/selenium/browser/BrowserFactory.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
package aquality.selenium.browser;

import aquality.selenium.core.localization.ILocalizationManager;
import aquality.selenium.core.logging.Logger;
import aquality.selenium.configuration.IBrowserProfile;
import aquality.selenium.core.localization.ILocalizedLogger;
import aquality.selenium.core.utilities.IActionRetrier;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.CommandExecutor;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.UnreachableBrowserException;

import java.util.Collections;
import java.util.Arrays;

interface BrowserFactory extends IBrowserFactory {
public abstract class BrowserFactory implements IBrowserFactory {

default IllegalArgumentException getLoggedWrongBrowserNameException() {
String message = AqualityServices.get(ILocalizationManager.class).getLocalizedMessage("loc.browser.name.wrong");
IllegalArgumentException exception = new IllegalArgumentException(message);
Logger.getInstance().fatal(message, exception);
return exception;
}
private final IActionRetrier actionRetrier;
private final IBrowserProfile browserProfile;
private final ILocalizedLogger localizedLogger;

default void logBrowserIsReady(BrowserName browserName) {
AqualityServices.getLocalizedLogger().info("loc.browser.ready", browserName.toString());
}
protected BrowserFactory(IActionRetrier actionRetrier, IBrowserProfile browserProfile, ILocalizedLogger localizedLogger) {

default <T extends RemoteWebDriver> T getDriver(Class<T> driverClass, Capabilities capabilities) {
return getDriver(driverClass, null, capabilities);
this.actionRetrier = actionRetrier;
this.browserProfile = browserProfile;
this.localizedLogger = localizedLogger;
}

default <T extends RemoteWebDriver> T getDriver(Class<T> driverClass, CommandExecutor commandExecutor, Capabilities capabilities) {
return AqualityServices.get(IActionRetrier.class).doWithRetry(() -> {
try {
if (commandExecutor != null) {
return driverClass.getDeclaredConstructor(CommandExecutor.class, Capabilities.class).newInstance(commandExecutor, capabilities);
}

return driverClass.getDeclaredConstructor(Capabilities.class).newInstance(capabilities);
} catch (ReflectiveOperationException e) {
throw new UnsupportedOperationException(String.format("Cannot instantiate driver with type '%1$s'.", driverClass), e);
}
}, Collections.emptyList());
protected abstract RemoteWebDriver getDriver();

@Override
public Browser getBrowser() {
RemoteWebDriver driver = actionRetrier.doWithRetry(
this::getDriver,
Arrays.asList(
SessionNotCreatedException.class,
UnreachableBrowserException.class,
WebDriverException.class,
TimeoutException.class));
Browser browser = new Browser(driver);
localizedLogger.info("loc.browser.ready", browserProfile.getBrowserName().toString());
return browser;
}
}
24 changes: 17 additions & 7 deletions src/main/java/aquality/selenium/browser/LocalBrowserFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@

import aquality.selenium.configuration.IBrowserProfile;
import aquality.selenium.configuration.driversettings.IDriverSettings;
import aquality.selenium.core.localization.ILocalizedLogger;
import aquality.selenium.core.utilities.IActionRetrier;
import io.github.bonigarcia.wdm.Architecture;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.safari.SafariDriver;

public class LocalBrowserFactory implements BrowserFactory {
public class LocalBrowserFactory extends BrowserFactory {

private final IBrowserProfile browserProfile;

public LocalBrowserFactory() {
this.browserProfile = AqualityServices.getBrowserProfile();
public LocalBrowserFactory(IActionRetrier actionRetrier, IBrowserProfile browserProfile, ILocalizedLogger localizedLogger) {
super(actionRetrier, browserProfile, localizedLogger);
this.browserProfile = browserProfile;
}

@Override
public Browser getBrowser() {
protected RemoteWebDriver getDriver() {
BrowserName browserName = browserProfile.getBrowserName();
RemoteWebDriver driver;
IDriverSettings driverSettings = browserProfile.getDriverSettings();
Expand All @@ -47,10 +51,16 @@ public Browser getBrowser() {
driver = getDriver(SafariDriver.class, driverSettings.getCapabilities());
break;
default:
throw getLoggedWrongBrowserNameException();
throw new IllegalArgumentException(String.format("Browser [%s] is not supported.", browserName));
}
logBrowserIsReady(browserName);
return driver;
}

return new Browser(driver);
private <T extends RemoteWebDriver> T getDriver(Class<T> driverClass, Capabilities capabilities) {
try {
return driverClass.getDeclaredConstructor(Capabilities.class).newInstance(capabilities);
} catch (ReflectiveOperationException e) {
throw new UnsupportedOperationException(String.format("Cannot instantiate driver with type '%1$s'.", driverClass), e);
}
}
}
42 changes: 24 additions & 18 deletions src/main/java/aquality/selenium/browser/RemoteBrowserFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import aquality.selenium.configuration.IBrowserProfile;
import aquality.selenium.configuration.ITimeoutConfiguration;
import aquality.selenium.configuration.driversettings.IDriverSettings;
import aquality.selenium.core.localization.ILocalizedLogger;
import aquality.selenium.core.utilities.IActionRetrier;
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.CommandExecutor;
import org.openqa.selenium.remote.HttpCommandExecutor;
import org.openqa.selenium.remote.LocalFileDetector;
Expand All @@ -17,37 +19,41 @@
import java.time.Duration;


public class RemoteBrowserFactory implements BrowserFactory {
public class RemoteBrowserFactory extends BrowserFactory {

private final IBrowserProfile browserProfile;
private final ITimeoutConfiguration timeoutConfiguration;
private final ILocalizedLogger localizedLogger;

public RemoteBrowserFactory() {
browserProfile = AqualityServices.getBrowserProfile();
timeoutConfiguration = AqualityServices.get(ITimeoutConfiguration.class);
public RemoteBrowserFactory(IActionRetrier actionRetrier, IBrowserProfile browserProfile,
ITimeoutConfiguration timeoutConfiguration, ILocalizedLogger localizedLogger) {
super(actionRetrier, browserProfile, localizedLogger);
this.browserProfile = browserProfile;
this.timeoutConfiguration = timeoutConfiguration;
this.localizedLogger = localizedLogger;
}

@Override
public Browser getBrowser() {
BrowserName browserName = browserProfile.getBrowserName();
IDriverSettings driverSettings = browserProfile.getDriverSettings();
logBrowserIsReady(browserName);
RemoteWebDriver driver = createRemoteDriver(driverSettings.getCapabilities());
return new Browser(driver);
}

private RemoteWebDriver createRemoteDriver(Capabilities capabilities) {
AqualityServices.getLocalizedLogger().info("loc.browser.grid");
protected RemoteWebDriver getDriver() {
Capabilities capabilities = browserProfile.getDriverSettings().getCapabilities();
localizedLogger.info("loc.browser.grid");

ClientFactory clientFactory = new ClientFactory();
CommandExecutor commandExecutor = new HttpCommandExecutor(
ImmutableMap.of(),
browserProfile.getRemoteConnectionUrl(),
clientFactory);

RemoteWebDriver driver = getDriver(RemoteWebDriver.class, commandExecutor, capabilities);
driver.setFileDetector(new LocalFileDetector());
return driver;
try {
RemoteWebDriver driver = new RemoteWebDriver(commandExecutor, capabilities);
driver.setFileDetector(new LocalFileDetector());
return driver;
}
catch (WebDriverException e) {
localizedLogger.fatal("loc.browser.grid.fail", e);
throw e;
}

}

class ClientFactory implements Factory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ private void setChromePrefs(ChromeOptions options){
}

private void setChromeArgs(ChromeOptions options) {
logStartArguments();
for (String arg : getBrowserStartArguments()) {
options.addArguments(arg);
}
Expand Down
Loading

0 comments on commit 767a188

Please sign in to comment.