Skip to content

Commit 767a188

Browse files
authored
Feature/support logging of element property getters (#103)
* 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
1 parent 143fcd3 commit 767a188

36 files changed

+386
-266
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.aquality-automation</groupId>
88
<artifactId>aquality-selenium</artifactId>
9-
<version>2.4.0</version>
9+
<version>2.5.0</version>
1010
<packaging>jar</packaging>
1111
<name>Aquality Selenium</name>
1212
<description>Library around Selenium WebDriver</description>
@@ -73,7 +73,7 @@
7373
<dependency>
7474
<groupId>com.github.aquality-automation</groupId>
7575
<artifactId>aquality-selenium-core</artifactId>
76-
<version>1.1.0</version>
76+
<version>1.2.0</version>
7777
</dependency>
7878

7979
<dependency>

src/main/java/aquality/selenium/browser/AqualityServices.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import aquality.selenium.configuration.IBrowserProfile;
44
import aquality.selenium.configuration.IConfiguration;
5+
import aquality.selenium.configuration.ITimeoutConfiguration;
56
import aquality.selenium.core.localization.ILocalizedLogger;
67
import aquality.selenium.core.logging.Logger;
8+
import aquality.selenium.core.utilities.IActionRetrier;
79
import aquality.selenium.core.waitings.IConditionalWait;
810
import aquality.selenium.elements.interfaces.IElementFactory;
911
import com.google.inject.Injector;
@@ -66,7 +68,8 @@ private static Injector getServiceProvider() {
6668
*/
6769
public static void setDefaultBrowserFactory() {
6870
IBrowserFactory browserFactory = getBrowserProfile().isRemote()
69-
? new RemoteBrowserFactory() : new LocalBrowserFactory();
71+
? new RemoteBrowserFactory(get(IActionRetrier.class), getBrowserProfile(), get(ITimeoutConfiguration.class), getLocalizedLogger())
72+
: new LocalBrowserFactory(get(IActionRetrier.class), getBrowserProfile(), getLocalizedLogger());
7073
setBrowserFactory(browserFactory);
7174
}
7275

src/main/java/aquality/selenium/browser/Browser.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ public void maximize() {
109109
*/
110110
public String getCurrentUrl() {
111111
localizedLogger.info("loc.browser.getUrl");
112-
return getDriver().getCurrentUrl();
112+
String value = getDriver().getCurrentUrl();
113+
localizedLogger.info("loc.browser.url.value", value);
114+
return value;
113115
}
114116

115117
/**
@@ -147,8 +149,8 @@ public IBrowserTabNavigation tabs() {
147149
* @param timeout seconds to wait
148150
*/
149151
public void setPageLoadTimeout(Duration timeout) {
150-
localizedLogger.debug("loc.browser.page.load.timeout", timeout.getSeconds());
151152
if (!getBrowserName().equals(BrowserName.SAFARI)) {
153+
localizedLogger.debug("loc.browser.page.load.timeout", timeout.getSeconds());
152154
getDriver().manage().timeouts().pageLoadTimeout(timeout.getSeconds(), TimeUnit.SECONDS);
153155
}
154156
}
@@ -160,8 +162,8 @@ public void setPageLoadTimeout(Duration timeout) {
160162
* @param timeout duration of time to wait
161163
*/
162164
public void setImplicitWaitTimeout(Duration timeout) {
163-
localizedLogger.debug("loc.browser.implicit.timeout", timeout.getSeconds());
164165
if (!timeout.equals(getImplicitWaitTimeout())) {
166+
localizedLogger.debug("loc.browser.implicit.timeout", timeout.getSeconds());
165167
getDriver().manage().timeouts().implicitlyWait(timeout.getSeconds(), TimeUnit.SECONDS);
166168
implicitTimeout = timeout;
167169
}
@@ -184,6 +186,7 @@ public void setScriptTimeout(Duration timeout) {
184186
* Use setPageLoadTimeout to configure your own timeout from code if it is necessary
185187
*/
186188
public void waitForPageToLoad() {
189+
localizedLogger.info("loc.browser.page.wait");
187190
ExpectedCondition<Boolean> condition = d -> {
188191
Object result = executeScript(JavaScript.IS_PAGE_LOADED.getScript());
189192
return result instanceof Boolean && (Boolean) result;
@@ -305,9 +308,11 @@ public void handleAlert(AlertActions alertAction) {
305308
* @param text message to send
306309
*/
307310
public void handlePromptAlert(AlertActions alertAction, String text) {
311+
localizedLogger.info(String.format("loc.browser.alert.%s", alertAction.name().toLowerCase()));
308312
try {
309313
Alert alert = getDriver().switchTo().alert();
310314
if (text != null && !text.isEmpty()) {
315+
localizedLogger.info("loc.send.text", text);
311316
getDriver().switchTo().alert().sendKeys(text);
312317
}
313318
if (alertAction.equals(AlertActions.ACCEPT)) {
Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
package aquality.selenium.browser;
22

3-
import aquality.selenium.core.localization.ILocalizationManager;
4-
import aquality.selenium.core.logging.Logger;
3+
import aquality.selenium.configuration.IBrowserProfile;
4+
import aquality.selenium.core.localization.ILocalizedLogger;
55
import aquality.selenium.core.utilities.IActionRetrier;
6-
import org.openqa.selenium.Capabilities;
7-
import org.openqa.selenium.remote.CommandExecutor;
6+
import org.openqa.selenium.SessionNotCreatedException;
7+
import org.openqa.selenium.TimeoutException;
8+
import org.openqa.selenium.WebDriverException;
89
import org.openqa.selenium.remote.RemoteWebDriver;
10+
import org.openqa.selenium.remote.UnreachableBrowserException;
911

10-
import java.util.Collections;
12+
import java.util.Arrays;
1113

12-
interface BrowserFactory extends IBrowserFactory {
14+
public abstract class BrowserFactory implements IBrowserFactory {
1315

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

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

25-
default <T extends RemoteWebDriver> T getDriver(Class<T> driverClass, Capabilities capabilities) {
26-
return getDriver(driverClass, null, capabilities);
22+
this.actionRetrier = actionRetrier;
23+
this.browserProfile = browserProfile;
24+
this.localizedLogger = localizedLogger;
2725
}
2826

29-
default <T extends RemoteWebDriver> T getDriver(Class<T> driverClass, CommandExecutor commandExecutor, Capabilities capabilities) {
30-
return AqualityServices.get(IActionRetrier.class).doWithRetry(() -> {
31-
try {
32-
if (commandExecutor != null) {
33-
return driverClass.getDeclaredConstructor(CommandExecutor.class, Capabilities.class).newInstance(commandExecutor, capabilities);
34-
}
35-
36-
return driverClass.getDeclaredConstructor(Capabilities.class).newInstance(capabilities);
37-
} catch (ReflectiveOperationException e) {
38-
throw new UnsupportedOperationException(String.format("Cannot instantiate driver with type '%1$s'.", driverClass), e);
39-
}
40-
}, Collections.emptyList());
27+
protected abstract RemoteWebDriver getDriver();
28+
29+
@Override
30+
public Browser getBrowser() {
31+
RemoteWebDriver driver = actionRetrier.doWithRetry(
32+
this::getDriver,
33+
Arrays.asList(
34+
SessionNotCreatedException.class,
35+
UnreachableBrowserException.class,
36+
WebDriverException.class,
37+
TimeoutException.class));
38+
Browser browser = new Browser(driver);
39+
localizedLogger.info("loc.browser.ready", browserProfile.getBrowserName().toString());
40+
return browser;
4141
}
4242
}

src/main/java/aquality/selenium/browser/LocalBrowserFactory.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@
22

33
import aquality.selenium.configuration.IBrowserProfile;
44
import aquality.selenium.configuration.driversettings.IDriverSettings;
5+
import aquality.selenium.core.localization.ILocalizedLogger;
6+
import aquality.selenium.core.utilities.IActionRetrier;
57
import io.github.bonigarcia.wdm.Architecture;
68
import io.github.bonigarcia.wdm.WebDriverManager;
9+
import org.openqa.selenium.Capabilities;
710
import org.openqa.selenium.chrome.ChromeDriver;
811
import org.openqa.selenium.edge.EdgeDriver;
912
import org.openqa.selenium.firefox.FirefoxDriver;
1013
import org.openqa.selenium.ie.InternetExplorerDriver;
1114
import org.openqa.selenium.remote.RemoteWebDriver;
1215
import org.openqa.selenium.safari.SafariDriver;
1316

14-
public class LocalBrowserFactory implements BrowserFactory {
17+
public class LocalBrowserFactory extends BrowserFactory {
1518

1619
private final IBrowserProfile browserProfile;
1720

18-
public LocalBrowserFactory() {
19-
this.browserProfile = AqualityServices.getBrowserProfile();
21+
public LocalBrowserFactory(IActionRetrier actionRetrier, IBrowserProfile browserProfile, ILocalizedLogger localizedLogger) {
22+
super(actionRetrier, browserProfile, localizedLogger);
23+
this.browserProfile = browserProfile;
2024
}
2125

2226
@Override
23-
public Browser getBrowser() {
27+
protected RemoteWebDriver getDriver() {
2428
BrowserName browserName = browserProfile.getBrowserName();
2529
RemoteWebDriver driver;
2630
IDriverSettings driverSettings = browserProfile.getDriverSettings();
@@ -47,10 +51,16 @@ public Browser getBrowser() {
4751
driver = getDriver(SafariDriver.class, driverSettings.getCapabilities());
4852
break;
4953
default:
50-
throw getLoggedWrongBrowserNameException();
54+
throw new IllegalArgumentException(String.format("Browser [%s] is not supported.", browserName));
5155
}
52-
logBrowserIsReady(browserName);
56+
return driver;
57+
}
5358

54-
return new Browser(driver);
59+
private <T extends RemoteWebDriver> T getDriver(Class<T> driverClass, Capabilities capabilities) {
60+
try {
61+
return driverClass.getDeclaredConstructor(Capabilities.class).newInstance(capabilities);
62+
} catch (ReflectiveOperationException e) {
63+
throw new UnsupportedOperationException(String.format("Cannot instantiate driver with type '%1$s'.", driverClass), e);
64+
}
5565
}
5666
}

src/main/java/aquality/selenium/browser/RemoteBrowserFactory.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import aquality.selenium.configuration.IBrowserProfile;
44
import aquality.selenium.configuration.ITimeoutConfiguration;
5-
import aquality.selenium.configuration.driversettings.IDriverSettings;
5+
import aquality.selenium.core.localization.ILocalizedLogger;
6+
import aquality.selenium.core.utilities.IActionRetrier;
67
import com.google.common.collect.ImmutableMap;
78
import org.openqa.selenium.Capabilities;
9+
import org.openqa.selenium.WebDriverException;
810
import org.openqa.selenium.remote.CommandExecutor;
911
import org.openqa.selenium.remote.HttpCommandExecutor;
1012
import org.openqa.selenium.remote.LocalFileDetector;
@@ -17,37 +19,41 @@
1719
import java.time.Duration;
1820

1921

20-
public class RemoteBrowserFactory implements BrowserFactory {
22+
public class RemoteBrowserFactory extends BrowserFactory {
2123

2224
private final IBrowserProfile browserProfile;
2325
private final ITimeoutConfiguration timeoutConfiguration;
26+
private final ILocalizedLogger localizedLogger;
2427

25-
public RemoteBrowserFactory() {
26-
browserProfile = AqualityServices.getBrowserProfile();
27-
timeoutConfiguration = AqualityServices.get(ITimeoutConfiguration.class);
28+
public RemoteBrowserFactory(IActionRetrier actionRetrier, IBrowserProfile browserProfile,
29+
ITimeoutConfiguration timeoutConfiguration, ILocalizedLogger localizedLogger) {
30+
super(actionRetrier, browserProfile, localizedLogger);
31+
this.browserProfile = browserProfile;
32+
this.timeoutConfiguration = timeoutConfiguration;
33+
this.localizedLogger = localizedLogger;
2834
}
2935

3036
@Override
31-
public Browser getBrowser() {
32-
BrowserName browserName = browserProfile.getBrowserName();
33-
IDriverSettings driverSettings = browserProfile.getDriverSettings();
34-
logBrowserIsReady(browserName);
35-
RemoteWebDriver driver = createRemoteDriver(driverSettings.getCapabilities());
36-
return new Browser(driver);
37-
}
38-
39-
private RemoteWebDriver createRemoteDriver(Capabilities capabilities) {
40-
AqualityServices.getLocalizedLogger().info("loc.browser.grid");
37+
protected RemoteWebDriver getDriver() {
38+
Capabilities capabilities = browserProfile.getDriverSettings().getCapabilities();
39+
localizedLogger.info("loc.browser.grid");
4140

4241
ClientFactory clientFactory = new ClientFactory();
4342
CommandExecutor commandExecutor = new HttpCommandExecutor(
4443
ImmutableMap.of(),
4544
browserProfile.getRemoteConnectionUrl(),
4645
clientFactory);
4746

48-
RemoteWebDriver driver = getDriver(RemoteWebDriver.class, commandExecutor, capabilities);
49-
driver.setFileDetector(new LocalFileDetector());
50-
return driver;
47+
try {
48+
RemoteWebDriver driver = new RemoteWebDriver(commandExecutor, capabilities);
49+
driver.setFileDetector(new LocalFileDetector());
50+
return driver;
51+
}
52+
catch (WebDriverException e) {
53+
localizedLogger.fatal("loc.browser.grid.fail", e);
54+
throw e;
55+
}
56+
5157
}
5258

5359
class ClientFactory implements Factory {

src/main/java/aquality/selenium/configuration/driversettings/ChromeSettings.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ private void setChromePrefs(ChromeOptions options){
3737
}
3838

3939
private void setChromeArgs(ChromeOptions options) {
40-
logStartArguments();
4140
for (String arg : getBrowserStartArguments()) {
4241
options.addArguments(arg);
4342
}

0 commit comments

Comments
 (0)