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

#97: Add Configurtion.WindowWidth and Configuration.WindowHeight #98

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
93 changes: 88 additions & 5 deletions NSelene/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public interface _SeleneSettings_
bool? WaitForNoOverlapFoundByJs { get; set; }
Action<object, Func<string>, Action> _HookWaitAction { get; set; }
string BaseUrl { get; set; }
int? WindowWidth { get; set; }
int? WindowHeight { get; set; }
}

/// Configuration is considered as a defined group of all settings
Expand Down Expand Up @@ -174,6 +176,33 @@ string _SeleneSettings_.BaseUrl
set { this._refBaseUrl.Value = value; }
}

private Ref<int?> _refWindowWidth = new Ref<int?>();
int? _SeleneSettings_.WindowWidth
{
get
{
return this._refWindowWidth.Value;
}
set
{
this._refWindowWidth.Value = value;
}
}

private Ref<int?> _refWindowHeight = new Ref<int?>();
int? _SeleneSettings_.WindowHeight
{
get
{
return this._refWindowHeight.Value;
}
set
{
this._refWindowHeight.Value = value;
}
}


private Configuration(
Ref<IWebDriver> refDriver,
Ref<double?> refTimeout,
Expand All @@ -183,7 +212,9 @@ private Configuration(
Ref<bool?> refClickByJs,
Ref<bool?> refWaitForNoOverlapFoundByJs,
Ref<Action<object, Func<string>, Action>> _ref_HookWaitAction,
Ref<string> refBaseUrl)
Ref<string> refBaseUrl,
Ref<int?> refWindowWidth,
Ref<int?> refWindowHeight)
{
_refDriver = refDriver ?? new Ref<IWebDriver>();
_refTimeout = refTimeout ?? new Ref<double?>();
Expand All @@ -194,6 +225,8 @@ private Configuration(
_refWaitForNoOverlapFoundByJs = refWaitForNoOverlapFoundByJs ?? new Ref<bool?>();
this._ref_HookWaitAction = _ref_HookWaitAction ?? new Ref<Action<object, Func<string>, Action>>();
_refBaseUrl = refBaseUrl ?? new Ref<string>();
_refWindowWidth = refWindowWidth ?? new Ref<int?>();
_refWindowHeight = refWindowHeight ?? new Ref<int?>();
}

// TODO: consider making public
Expand All @@ -209,7 +242,9 @@ internal Configuration()
refClickByJs: null,
refWaitForNoOverlapFoundByJs: null,
_ref_HookWaitAction: null,
refBaseUrl: null
refBaseUrl: null,
refWindowWidth: null,
refWindowHeight: null
) {}

public static _SeleneSettings_ _New_(
Expand All @@ -221,7 +256,9 @@ public static _SeleneSettings_ _New_(
bool clickByJs = false,
bool waitForNoOverlapFoundByJs = false,
Action<object, Func<string>, Action> _hookWaitAction = null,
string baseUrl = ""
string baseUrl = "",
int? windowWidth = null,
int? windowHeight = null
)
{
_SeleneSettings_ next = new Configuration();
Expand All @@ -235,6 +272,8 @@ public static _SeleneSettings_ _New_(
next.WaitForNoOverlapFoundByJs = waitForNoOverlapFoundByJs;
next._HookWaitAction = _hookWaitAction;
next.BaseUrl = baseUrl;
next.WindowWidth = windowWidth;
next.WindowHeight = windowHeight;

return next;
}
Expand Down Expand Up @@ -277,6 +316,14 @@ internal static _SeleneSettings_ Shared
refBaseUrl: new Ref<string>(
getter: () => Configuration.BaseUrl,
setter: value => Configuration.BaseUrl = value ?? ""
),
refWindowWidth: new Ref<int?>(
getter: () => Configuration.WindowWidth,
setter: value => Configuration.WindowWidth = value
),
refWindowHeight: new Ref<int?>(
getter: () => Configuration.WindowHeight,
setter: value => Configuration.WindowHeight = value
)
);

Expand All @@ -292,7 +339,9 @@ public static _SeleneSettings_ _With_(
bool? clickByJs = null,
bool? waitForNoOverlapFoundByJs = null,
Action<object, Func<string>, Action> _hookWaitAction = null,
string baseUrl = null
string baseUrl = null,
int? windowWidth = null,
int? windowHeight = null
)
{
_SeleneSettings_ next = new Configuration();
Expand All @@ -306,6 +355,8 @@ public static _SeleneSettings_ _With_(
next.WaitForNoOverlapFoundByJs = waitForNoOverlapFoundByJs;
next._HookWaitAction = _hookWaitAction;
next.BaseUrl = baseUrl;
next.WindowWidth = windowWidth;
next.WindowHeight = windowHeight;

return Configuration.Shared.With(next);
}
Expand Down Expand Up @@ -341,7 +392,13 @@ _SeleneSettings_ overrides
: new Ref<Action<object, Func<string>, Action>>(overrides._HookWaitAction),
refBaseUrl: overrides.BaseUrl == null
? this._refBaseUrl
: new Ref<string>(overrides.BaseUrl)
: new Ref<string>(overrides.BaseUrl),
refWindowWidth: overrides.WindowWidth == null
? this._refWindowWidth
: new Ref<int?>(overrides.WindowWidth),
refWindowHeight: overrides.WindowHeight == null
? this._refWindowWidth
: new Ref<int?>(overrides.WindowHeight)
);
}

Expand Down Expand Up @@ -475,6 +532,32 @@ public static string BaseUrl
}
}

private static ThreadLocal<int?> _WindowWidth = new ThreadLocal<int?>();
public static int? WindowWidth
{
get
{
return Configuration._WindowWidth.Value;
}
set
{
Configuration._WindowWidth.Value = value;
}
}

private static ThreadLocal<int?> _WindowHeight = new ThreadLocal<int?>();
public static int? WindowHeight
{
get
{
return Configuration._WindowHeight.Value;
}
set
{
Configuration._WindowHeight.Value = value;
}
}

[Obsolete("Use Configuration.Driver over deprecated Configuration.WebDriver")]
public static IWebDriver WebDriver
{
Expand Down
20 changes: 20 additions & 0 deletions NSelene/Selene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,32 @@ public static void Open(string url)

public static void GoToUrl(string url)
{
ResizeWindow();

var absoluteUrl = Uri.IsWellFormedUriString(url, UriKind.Absolute)
? url
: Configuration.BaseUrl + url;
GetWebDriver().Navigate().GoToUrl(absoluteUrl);
}

private static void ResizeWindow()
{
var width = Configuration.WindowWidth;
var height = Configuration.WindowHeight;
var sizeBefore = GetWebDriver().Manage().Window.Size;

if (width.HasValue || height.HasValue)
{
if (!(width.HasValue && height.HasValue))
{
width = width ?? sizeBefore.Width;
height = height ?? sizeBefore.Height;
}

GetWebDriver().Manage().Window.Size = new System.Drawing.Size(width.Value, height.Value);
}
}

// TODO: consider changing to static property
public static string Url()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using System;
using System.Drawing;
using System.Reflection;
using NSelene;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace NSeleneTests.Integration.SharedDriver
{
[TestFixture]
public class ConfigurationWebDriverWindowSpecs
{
private IWebDriver _driver { get; set; }

private readonly string _emptyPage = new Uri(new Uri(Assembly.GetExecutingAssembly().Location),
"../../../Resources/empty.html").AbsoluteUri;

private const int DefaultSeleniumWidth = 800;
private const int DefaultSeleniumHeight = 600;

[SetUp]
public void SetUp()
{
var options = new ChromeOptions();
options.AddArguments("headless");
_driver = new ChromeDriver(options);
Configuration.Driver = _driver;
}

[TearDown]
public void TearDown()
{
_driver.Quit();
Configuration.Driver = null;
}


[Test]
public void WindowShouldHaveConfiguredSize()
{
ResetSeleneConfiguration();

Configuration.WindowWidth = 888;
Configuration.WindowHeight = 666;
Selene.Open(_emptyPage);

Assert.AreEqual(new Size(888, 666), Configuration.Driver.Manage().Window.Size);
}

[Test]
public void WindowShouldHaveDefaultSizeIfNotConfigured()
{
ResetSeleneConfiguration();

Selene.Open(_emptyPage);

Assert.AreEqual(new Size(DefaultSeleniumWidth, DefaultSeleniumHeight),
Configuration.Driver.Manage().Window.Size);
}


[Test]
public void WindowShouldHaveConfiguredHeightAndDefaultWidth_OnNullInConfigurationWidth()
{
ResetSeleneConfiguration();

Configuration.WindowHeight = 666;
Selene.Open(_emptyPage);

Assert.AreEqual(new Size(DefaultSeleniumWidth, 666), Configuration.Driver.Manage().Window.Size);
}

[Test]
public void WindowShouldHaveConfiguredWidthAndDefaultHeight_OnNullInConfigurationHeight()
{
ResetSeleneConfiguration();

Configuration.WindowWidth = 888;
Selene.Open(_emptyPage);

Assert.AreEqual(new Size(888, DefaultSeleniumHeight), Configuration.Driver.Manage().Window.Size);
}

[TestCase(-888, 666)]
[TestCase(888, -666)]
public void WebDriverArgumentExceptionShouldBeThrown_OnNegativeNumberInConfiguration(int width, int height)
{
ResetSeleneConfiguration();
Configuration.WindowWidth = width;
Configuration.WindowHeight = height;

try
{
Selene.Open(_emptyPage);
throw new Exception("Should fail when window size configs have negative values.");
}

catch (Exception e)
{
Assert.IsInstanceOf<WebDriverArgumentException>(e);
}
}

[Test]
public void WindowSizeShouldRemainUnchangedOnUpdateConfig()
{
ResetSeleneConfiguration();
Configuration.WindowWidth = 888;
Configuration.WindowHeight = 666;
Selene.Open(_emptyPage);

Configuration.WindowWidth = DefaultSeleniumWidth;
Configuration.WindowHeight = DefaultSeleniumHeight;

Assert.AreEqual(new Size(888, 666), Configuration.Driver.Manage().Window.Size);
}


private static void ResetSeleneConfiguration()
{
Configuration.WindowWidth = null;
Configuration.WindowHeight = null;
}
}
}
Loading