Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* #770: Remove unnecessary private methods * #770: Change private method order in FluentDriver * #770: Add javadoc and null check to ImageUtils * #770: Remove getSearch() private method * #770: Extract argument and state validation, and elaborate error messages * #770: Extract await() logic from FluentDriver to FluentDriverWait * #770: Extract retrieveing capabilities logic from FluentDriver to FluentDriverCapabiltiesProvider * #770: Extract screenshot persisting logic from FluentDriver to FluentDriverScreenshotPersister * #770: Extract taking html dump from FluentDriver to FluentDriverHtmlDumper * #770: Deduplicate getting wrapped element * #770: Remove NOPMD comment * #770: Move wrapped element retrieval logic to ElementUtils * #770: Extract timeout configuration logic to FluentDriverTimeoutConfigurer * #770: Rename capabilities provider and update javadoc * #770: Fix test file names in unit test * #770: Add some unit tests to FluentDriver * #770: Remove null check and fix javadoc in ImageUtils * #770: Extract some methods to decrease cognitive complexity * #770: Remove unused import * #770: Update temp file creation logic in unit tests * #770: Fix Java 8 compatibility issue with charset
- Loading branch information
1 parent
f09dfca
commit cbf9d71
Showing
17 changed files
with
784 additions
and
185 deletions.
There are no files selected for viewing
249 changes: 68 additions & 181 deletions
249
fluentlenium-core/src/main/java/org/fluentlenium/core/FluentDriver.java
Large diffs are not rendered by default.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
fluentlenium-core/src/main/java/org/fluentlenium/core/FluentDriverHtmlDumper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.fluentlenium.core; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import org.fluentlenium.configuration.Configuration; | ||
import org.apache.commons.io.FileUtils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.nio.file.Paths; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Takes HTML dump.. | ||
*/ | ||
public class FluentDriverHtmlDumper { | ||
|
||
private final Configuration configuration; | ||
|
||
public FluentDriverHtmlDumper(Configuration configuration) { | ||
this.configuration = requireNonNull(configuration); | ||
} | ||
|
||
/** | ||
* Dumps the HTML provided by the html supplier to a file. | ||
* <p> | ||
* If the configuration is set with an html dump path, the argument file name will be concatenated to that, creating | ||
* the destination file path, otherwise the destination file will be the argument file name. | ||
* <p> | ||
* If an error occurs during taking the HTML dump, the dump file is still created, but it will contain a message | ||
* that HTML dump could not be taken. | ||
* | ||
* @param fileName the file name to dump the HTML to | ||
* @throws RuntimeException when an error occurs during dumping HTML | ||
*/ | ||
public void takeHtmlDump(String fileName, Supplier<String> htmlSupplier) { | ||
File destFile = null; | ||
try { | ||
destFile = getDestinationFile(fileName); | ||
FileUtils.write(destFile, htmlSupplier.get(), "UTF-8"); | ||
} catch (Exception e) { | ||
if (destFile == null) { | ||
destFile = new File(fileName); | ||
} | ||
try (PrintWriter printWriter = new PrintWriter(destFile, "UTF-8")) { | ||
printWriter.write("Can't dump HTML"); | ||
printWriter.println(); | ||
e.printStackTrace(printWriter); | ||
} catch (IOException ioe) { | ||
throw new RuntimeException("Error when dumping HTML", e); //NOPMD PreserveStackTrace | ||
} | ||
} | ||
} | ||
|
||
private File getDestinationFile(String fileName) { | ||
File destFile; | ||
if (configuration.getHtmlDumpPath() == null) { | ||
destFile = new File(fileName); | ||
} else { | ||
destFile = Paths.get(configuration.getHtmlDumpPath(), fileName).toFile(); | ||
} | ||
return destFile; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
fluentlenium-core/src/main/java/org/fluentlenium/core/FluentDriverScreenshotPersister.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.fluentlenium.core; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import org.fluentlenium.configuration.Configuration; | ||
import org.fluentlenium.utils.ImageUtils; | ||
import org.apache.commons.io.FileUtils; | ||
import org.openqa.selenium.OutputType; | ||
import org.openqa.selenium.TakesScreenshot; | ||
import org.openqa.selenium.UnhandledAlertException; | ||
import org.openqa.selenium.WebDriver; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Paths; | ||
|
||
/** | ||
* Persists a screenshot to a target file. | ||
*/ | ||
public class FluentDriverScreenshotPersister { | ||
|
||
private final Configuration configuration; | ||
private final WebDriver driver; | ||
|
||
public FluentDriverScreenshotPersister(Configuration configuration, WebDriver driver) { | ||
this.configuration = requireNonNull(configuration); | ||
this.driver = driver; | ||
} | ||
|
||
/** | ||
* Persists a screenshot to the argument target file using the screenshot path from {@link Configuration}. | ||
* <p> | ||
* If there is no screenshot path set in the configuration, the file will be the argument file name, | ||
* otherwise the argument file name will be concatenated to the screenshot path to create the destination file. | ||
* | ||
* @param fileName the target file to save the screenshot to | ||
* @throws RuntimeException when an error occurs during taking the screenshot | ||
*/ | ||
public void persistScreenshot(String fileName) { | ||
try { | ||
File destFile; | ||
if (configuration.getScreenshotPath() == null) { | ||
destFile = new File(fileName); | ||
} else { | ||
destFile = Paths.get(configuration.getScreenshotPath(), fileName).toFile(); | ||
} | ||
FileUtils.writeByteArrayToFile(destFile, prepareScreenshot()); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Error when taking the screenshot", e); | ||
} | ||
} | ||
|
||
private byte[] prepareScreenshot() { | ||
byte[] screenshot; | ||
try { | ||
screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES); | ||
} catch (UnhandledAlertException uae) { | ||
screenshot = new ImageUtils(driver).handleAlertAndTakeScreenshot(); | ||
} | ||
return screenshot; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
fluentlenium-core/src/main/java/org/fluentlenium/core/FluentDriverTimeoutConfigurer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.fluentlenium.core; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
|
||
import org.fluentlenium.configuration.Configuration; | ||
import org.openqa.selenium.WebDriver; | ||
|
||
/** | ||
* Configures a {@link WebDriver} instance with timeouts from a {@link Configuration}. | ||
*/ | ||
public class FluentDriverTimeoutConfigurer { | ||
|
||
private final Configuration configuration; | ||
private final WebDriver driver; | ||
|
||
public FluentDriverTimeoutConfigurer(Configuration configuration, WebDriver driver) { | ||
this.configuration = requireNonNull(configuration); | ||
this.driver = driver; | ||
} | ||
|
||
/** | ||
* Configures a {@link WebDriver} instance with timeouts from a {@link Configuration}. | ||
* <p> | ||
* Configures the following options: | ||
* <ul> | ||
* <li>page load timeout</li> | ||
* <li>implicitly wait</li> | ||
* <li>script timeout</li> | ||
* </ul> | ||
*/ | ||
public void configureDriver() { | ||
if (driver != null && driver.manage() != null && driver.manage().timeouts() != null) { | ||
configurePageLoadTimeout(); | ||
configureImplicitlyWait(); | ||
configureScriptTimeout(); | ||
} | ||
} | ||
|
||
private void configurePageLoadTimeout() { | ||
if (configuration.getPageLoadTimeout() != null) { | ||
driver.manage().timeouts().pageLoadTimeout(configuration.getPageLoadTimeout(), MILLISECONDS); | ||
} | ||
} | ||
|
||
private void configureImplicitlyWait() { | ||
if (configuration.getImplicitlyWait() != null) { | ||
driver.manage().timeouts().implicitlyWait(configuration.getImplicitlyWait(), MILLISECONDS); | ||
} | ||
} | ||
|
||
private void configureScriptTimeout() { | ||
if (configuration.getScriptTimeout() != null) { | ||
driver.manage().timeouts().setScriptTimeout(configuration.getScriptTimeout(), MILLISECONDS); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
fluentlenium-core/src/main/java/org/fluentlenium/core/FluentDriverWait.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.fluentlenium.core; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import org.fluentlenium.configuration.Configuration; | ||
import org.fluentlenium.core.wait.FluentWait; | ||
|
||
/** | ||
* Creates and configures a {@link FluentWait} from a {@link Configuration} to be used via {@link FluentDriver}. | ||
*/ | ||
public class FluentDriverWait { | ||
|
||
private final Configuration configuration; | ||
|
||
public FluentDriverWait(Configuration configuration) { | ||
this.configuration = requireNonNull(configuration); | ||
} | ||
|
||
/** | ||
* Creates a {@link FluentWait} instance with the argument {@link FluentControl}, | ||
* and configures the FluentWait with the {@code awaitAtMost} and {@code pollingEvery} values from | ||
* a {@link Configuration} if they are set in that configuration. | ||
* | ||
* @return the configured FluentWait | ||
*/ | ||
public FluentWait await(FluentControl control) { | ||
FluentWait fluentWait = new FluentWait(control); | ||
configureWithAwaitAtMost(fluentWait); | ||
configureWithPollingEvery(fluentWait); | ||
return fluentWait; | ||
} | ||
|
||
private void configureWithAwaitAtMost(FluentWait fluentWait) { | ||
Long atMost = configuration.getAwaitAtMost(); | ||
if (atMost != null) { | ||
fluentWait.atMost(atMost); | ||
} | ||
} | ||
|
||
private void configureWithPollingEvery(FluentWait fluentWait) { | ||
Long pollingEvery = configuration.getAwaitPollingEvery(); | ||
if (pollingEvery != null) { | ||
fluentWait.pollingEvery(pollingEvery); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...ium-core/src/main/java/org/fluentlenium/core/FluentDriverWrappedCapabilitiesProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.fluentlenium.core; | ||
|
||
import org.openqa.selenium.Capabilities; | ||
import org.openqa.selenium.HasCapabilities; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.WrapsDriver; | ||
|
||
/** | ||
* Provides wrapped WebDriver capabilities for {@link FluentDriver}. | ||
*/ | ||
public class FluentDriverWrappedCapabilitiesProvider { | ||
|
||
/** | ||
* Goes through all the underlying wrapped drivers ({@link WrapsDriver}) in the argument {@link WebDriver}, | ||
* and returns the {@link Capabilities} of the innermost wrapped driver. | ||
* | ||
* @return capabilities of the innermost wrapped driver. | ||
*/ | ||
public Capabilities getCapabilities(WebDriver driver) { | ||
WebDriver currentDriver = driver; | ||
Capabilities capabilities = capabilitiesOf(currentDriver); | ||
while (currentDriver instanceof WrapsDriver && capabilities == null) { | ||
currentDriver = ((WrapsDriver) currentDriver).getWrappedDriver(); | ||
capabilities = capabilitiesOf(currentDriver); | ||
} | ||
return capabilities; | ||
} | ||
|
||
private Capabilities capabilitiesOf(WebDriver currentDriver) { | ||
return currentDriver instanceof HasCapabilities | ||
? ((HasCapabilities) currentDriver).getCapabilities() | ||
: null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.