Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added NeoWaitTime #296

Open
wants to merge 24 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.GeckoDriverService;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerDriverService;
import org.openqa.selenium.ie.InternetExplorerOptions;
import org.openqa.selenium.os.ExecutableFinder;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.HttpCommandExecutor;
import org.openqa.selenium.remote.RemoteWebDriver;
Expand Down Expand Up @@ -181,15 +178,15 @@ private static FirefoxBinary createFirefoxBinary(final String pathToBrowser)
* if <a href="https://github.com/Xceptance/neodymium-library/wiki/Selenium-grid">Selenium grid</a> is
* used and the given grid URL is invalid
*/
public static WebDriverStateContainer createWebDriverStateContainer(final BrowserConfiguration config, final Object testClassInstance)
public static WebDriverStateContainer createWebDriverStateContainer(final BrowserConfiguration config, Object testClassInstance)
throws MalformedURLException
{
final MutableCapabilities capabilities = config.getCapabilities();
final WebDriverStateContainer wDSC = new WebDriverStateContainer();
SelenideProxyServer selenideProxyServer = null;
if (Neodymium.configuration().useLocalProxy())
{
final BrowserUpProxy proxy = setupEmbeddedProxy();
BrowserUpProxy proxy = setupEmbeddedProxy();

// set the Proxy for later usage
wDSC.setProxy(proxy);
Expand All @@ -206,17 +203,16 @@ else if (Neodymium.configuration().useProxy())
{
if (Neodymium.configuration().enableSelenideProxy())
{
final SelenideProxyServerFactory selenideProxyServerFactory = Plugins.inject(SelenideProxyServerFactory.class);
SelenideProxyServerFactory selenideProxyServerFactory = Plugins.inject(SelenideProxyServerFactory.class);
selenideProxyServer = selenideProxyServerFactory.create(new SelenideConfig(),
(Proxy) capabilities.getCapability(CapabilityType.PROXY));
final var proxy = selenideProxyServer.getSeleniumProxy();
var proxy = selenideProxyServer.getSeleniumProxy();
capabilities.setCapability(CapabilityType.PROXY, proxy);
}
final String browserName = capabilities.getBrowserName();
if (chromeBrowsers.contains(browserName))
{
final ChromeOptions options = new ChromeOptions();
final String driverInPathPath = new ExecutableFinder().find("chromedriver");
final ChromeOptions options = (ChromeOptions) capabilities;

// do we have a custom path?
final String pathToBrowser = Neodymium.configuration().getChromeBrowserPath();
Expand All @@ -238,10 +234,9 @@ else if (Neodymium.configuration().useProxy())
options.setExperimentalOption("prefs", config.getPreferences());
}

if ((config.getPreferences() != null && !config.getPreferences().isEmpty()) ||
StringUtils.isNotBlank(config.getDownloadDirectory()))
if ((config.getPreferences() != null && !config.getPreferences().isEmpty()) || StringUtils.isNotBlank(config.getDownloadDirectory()))
{
final HashMap<String, Object> prefs = new HashMap<>();
HashMap<String, Object> prefs = new HashMap<>();

// if we have configured prefs, we need to add all to the experimental options
if (config.getPreferences() != null && !config.getPreferences().isEmpty())
Expand All @@ -257,14 +252,12 @@ else if (Neodymium.configuration().useProxy())

options.setExperimentalOption("prefs", prefs);
}
wDSC.setWebDriver(new ChromeDriver(new ChromeDriverService.Builder()
.usingDriverExecutable(new File(driverInPathPath))
.build(), options.merge(capabilities)));

wDSC.setWebDriver(new ChromeDriver(options));
}
else if (firefoxBrowsers.contains(browserName))
{
final FirefoxOptions options = new FirefoxOptions();
final String driverInPathPath = new ExecutableFinder().find("geckodriver");
final FirefoxOptions options = new FirefoxOptions().merge(capabilities);
options.setBinary(createFirefoxBinary(Neodymium.configuration().getFirefoxBrowserPath()));
if (config.isHeadless())
{
Expand All @@ -274,10 +267,9 @@ else if (firefoxBrowsers.contains(browserName))
{
options.addArguments(config.getArguments());
}
if ((config.getPreferences() != null && !config.getPreferences().isEmpty()) ||
StringUtils.isNotBlank(config.getDownloadDirectory()))
if ((config.getPreferences() != null && !config.getPreferences().isEmpty()) || StringUtils.isNotBlank(config.getDownloadDirectory()))
{
final FirefoxProfile profile = new FirefoxProfile();
FirefoxProfile profile = new FirefoxProfile();

// if we have configured prefs, we need to add all to the experimental options
if (config.getPreferences() != null && !config.getPreferences().isEmpty())
Expand Down Expand Up @@ -310,52 +302,45 @@ else if (StringUtils.isNumeric(val.toString()))
options.setProfile(profile);
}

wDSC.setWebDriver(new FirefoxDriver(new GeckoDriverService.Builder().withAllowHosts("localhost")
.usingDriverExecutable(new File(driverInPathPath))
.build(), options.merge(capabilities)));
wDSC.setWebDriver(new FirefoxDriver(new GeckoDriverService.Builder().withAllowHosts("localhost").build(), options));
}
else if (internetExplorerBrowsers.contains(browserName))
{
final InternetExplorerOptions options = new InternetExplorerOptions();
final String driverInPathPath = new ExecutableFinder().find("IEDriverServer");
final InternetExplorerOptions options = new InternetExplorerOptions().merge(capabilities);
if (config.getArguments() != null && config.getArguments().size() > 0)
{
for (final String argument : config.getArguments())
for (String argument : config.getArguments())
{
options.addCommandSwitches(argument);
}
}
wDSC.setWebDriver(new InternetExplorerDriver(new InternetExplorerDriverService.Builder()
.usingDriverExecutable(new File(driverInPathPath))
.build(), options.merge(capabilities)));
wDSC.setWebDriver(new InternetExplorerDriver(options));
}
else if (safariBrowsers.contains(browserName))
{
// safari driver is not expected to be in PATH, it will be looked in
// /usr/bin/safaridriver and /Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver
final SafariOptions options = new SafariOptions();
final SafariOptions options = (SafariOptions) capabilities;
wDSC.setWebDriver(new SafariDriver(options));
}
else
{
wDSC.setWebDriver(new RemoteWebDriver(capabilities.merge(capabilities)));
wDSC.setWebDriver(new RemoteWebDriver(capabilities));
}

}
else
{
// establish connection to target website
final TestEnvironment testEnvironmentProperties = MultibrowserConfiguration.getInstance().getTestEnvironment(testEnvironment);
TestEnvironment testEnvironmentProperties = MultibrowserConfiguration.getInstance().getTestEnvironment(testEnvironment);
if (testEnvironmentProperties == null)
{
throw new IllegalArgumentException("No properties found for test environment: \"" + testEnvironment + "\"");
}
final String testEnvironmentUrl = testEnvironmentProperties.getUrl();
String testEnvironmentUrl = testEnvironmentProperties.getUrl();
ClientConfig configClient = ClientConfig.defaultConfig();
configClient = configClient.baseUrl(new URL(testEnvironmentUrl));
config.getGridProperties().put("userName", testEnvironmentProperties.getUsername());
config.getGridProperties().put("accessKey", testEnvironmentProperties.getPassword());
final String buildId = StringUtils.isBlank(System.getenv("BUILD_NUMBER")) ? "local run" : System.getenv("BUILD_NUMBER");
String buildId = StringUtils.isBlank(System.getenv("BUILD_NUMBER")) ? "local run" : System.getenv("BUILD_NUMBER");
config.getGridProperties().put("sessionName", testClassInstance.getClass().toString());
config.getGridProperties().put("buildName", "Test Automation");
config.getGridProperties().put("buildIdentifier", buildId);
Expand All @@ -369,10 +354,10 @@ else if (testEnvironmentUrl.contains("saucelabs"))
}
else
{
final String optionsTag = testEnvironmentProperties.getOptionsTag();
String optionsTag = testEnvironmentProperties.getOptionsTag();
if (StringUtils.isBlank(optionsTag))
{
for (final String key : config.getGridProperties().keySet())
for (String key : config.getGridProperties().keySet())
{
capabilities.setCapability(key, config.getGridProperties().get(key));
}
Expand All @@ -384,7 +369,7 @@ else if (testEnvironmentUrl.contains("saucelabs"))
}
wDSC.setWebDriver(new RemoteWebDriver(new HttpCommandExecutor(new HashMap<>(), configClient, new NeodymiumProxyHttpClientFactory(testEnvironmentProperties)), capabilities));
}
final WebDriver decoratedDriver = new EventFiringDecorator<WebDriver>(new NeodymiumWebDriverListener()).decorate(wDSC.getWebDriver());
WebDriver decoratedDriver = new EventFiringDecorator<WebDriver>(new NeodymiumWebDriverListener()).decorate(wDSC.getWebDriver());
wDSC.setDecoratedWebDriver(decoratedDriver);
WebDriverRunner.setWebDriver(decoratedDriver, selenideProxyServer);
return wDSC;
Expand All @@ -398,8 +383,7 @@ private static BrowserUpProxy setupEmbeddedProxy()
if (Neodymium.configuration().useLocalProxyWithSelfSignedCertificate())
{
final CertificateAndKeySource rootCertificateSource = createLocalProxyRootCertSource();
final ImpersonatingMitmManager mitmManager = ImpersonatingMitmManager.builder().rootCertificateSource(rootCertificateSource)
.build();
final ImpersonatingMitmManager mitmManager = ImpersonatingMitmManager.builder().rootCertificateSource(rootCertificateSource).build();
proxy.setMitmManager(mitmManager);
}
else
Expand Down Expand Up @@ -467,10 +451,8 @@ public static Proxy createProxyCapabilities()
final Proxy webdriverProxy = new Proxy();
webdriverProxy.setHttpProxy(proxyHost);
webdriverProxy.setSslProxy(proxyHost);
if (!StringUtils.isAllEmpty(Neodymium.configuration().getProxySocketUsername(),
Neodymium.configuration().getProxySocketPassword())
||
Neodymium.configuration().getProxySocketVersion() != null)
if (!StringUtils.isAllEmpty(Neodymium.configuration().getProxySocketUsername(), Neodymium.configuration().getProxySocketPassword())
|| Neodymium.configuration().getProxySocketVersion() != null)
{
webdriverProxy.setSocksProxy(proxyHost);
if (StringUtils.isNoneEmpty(Neodymium.configuration().getProxySocketUsername(),
Expand Down Expand Up @@ -501,12 +483,11 @@ private static String popularContentTypes()
{
try
{
final List<String> popularContentTypes = IOUtils.readLines(BrowserConfigurationMapper.class.getResourceAsStream("/content-types.properties"),
UTF_8);
List<String> popularContentTypes = IOUtils.readLines(BrowserConfigurationMapper.class.getResourceAsStream("/content-types.properties"), UTF_8);
popularContentTypes.add("application/x-download");
return String.join(";", popularContentTypes);
}
catch (final Exception e)
catch (Exception e)
{
return "text/plain;text/csv;application/zip;application/pdf;application/octet-stream;" +
"application/msword;application/vnd.ms-excel;text/css;text/html";
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/com/xceptance/neodymium/util/NeoWaitTime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.xceptance.neodymium.util;

import java.util.Map;

import com.codeborne.selenide.Selenide;

public class NeoWaitTime
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some reconsideration on how to make this functionality as easy to access as possible (and easy to find without reading the whole documentation), Let's change the approach a bit:

  • we do not provide a seperate class for this
  • we add the wait methods (like waitStandardWaitTime()) directly to the Neodymium Class so that a user can call Neodymium.waitStandardWaitTime()
  • we add getter Methods to our config class to get each value as a java.time.Duration object (which is needed for all of Selenides should... methods).

I'll update the ticket for clarification as well.

{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The simple Selenide.Wait() case is much less common than the $("something").shouldBe(visible, Duration.ofMillis(1000)); case. So for easy access to our property we should offer a java.time.Duration version of each of our waiting times.

private final int standardWait, shortWait, doubleWait, longWait;

private final Map<String, String> customWaitTimeMap;

private static NeoWaitTime INSTANCE;

private NeoWaitTime() {
this.standardWait = Neodymium.configuration().getStandardWaitTime();
this.shortWait = Neodymium.configuration().getShortWaitTime();
this.doubleWait = Neodymium.configuration().getDoubleWaitTime();
this.longWait = Neodymium.configuration().getLongWaitTime();
this.customWaitTimeMap = PropertiesUtil.getPropertiesMapForCustomIdentifier("neodymium.waitTime.custom");
}

public static void waitStandardWaitTime()
{
if (INSTANCE == null)
{
INSTANCE = new NeoWaitTime();
}
Selenide.sleep(INSTANCE.standardWait);
}

public static void waitShortWaitTime()
{
if (INSTANCE == null)
{
INSTANCE = new NeoWaitTime();
}
Selenide.sleep(INSTANCE.shortWait);
}

public static void waitDoubleWaitTime()
{
if (INSTANCE == null)
{
INSTANCE = new NeoWaitTime();
}
Selenide.sleep(INSTANCE.doubleWait);
}

public static void waitLongWaitTime()
{
if (INSTANCE == null)
{
INSTANCE = new NeoWaitTime();
}
Selenide.sleep(INSTANCE.longWait);
}

public static void waitCustomWaitTime(String key)
{
if (INSTANCE == null)
{
INSTANCE = new NeoWaitTime();
}
Selenide.sleep(Integer.valueOf(INSTANCE.customWaitTimeMap.get(key)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a proper handling if the key given is not present and produce a proper error message, otherwise a not very communicative NullPointerException will be thrown

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,20 @@ public interface NeodymiumConfiguration extends Mutable
@Key("neodymium.logNeoVersion")
@DefaultValue("true")
public boolean logNeoVersion();

@Key("neodymium.waitTime.standard")
@DefaultValue("100")
public int getStandardWaitTime();

@Key("neodymium.waitTime.short")
@DefaultValue("50")
public int getShortWaitTime();

@Key("neodymium.waitTime.double")
@DefaultValue("200")
public int getDoubleWaitTime();

@Key("neodymium.waitTime.long")
@DefaultValue("500")
public int getLongWaitTime();
}
Loading