Skip to content

Commit

Permalink
Fix scrolling
Browse files Browse the repository at this point in the history
* #126 fixed scrolling tests

* #126 fixed scrollToTheCenter test

* #126 resolve parameters in executeScript method

* #126 added remarks to ScrollBy
  • Loading branch information
knysh authored and paveliam committed Sep 4, 2019
1 parent 5d7b51f commit c63bfd8
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
Expand Down Expand Up @@ -72,8 +73,9 @@ public void ScrollIntoView()
/// <summary>
/// Scrolling element by coordinates.
/// </summary>
/// <remarks>Element have to contains inner scroll bar.</remarks>
/// <param name="x">Horizontal coordinate</param>
/// <param name="y">Verticale coordinate</param>
/// <param name="y">Vertical coordinate</param>
public void ScrollBy(int x, int y)
{
LogElementAction("loc.scrolling.js");
Expand All @@ -82,6 +84,7 @@ public void ScrollBy(int x, int y)

/// <summary>
/// Scrolling to the center of element.
/// Upper bound of element will be in the center of the page after scrolling
/// </summary>
public void ScrollToTheCenter()
{
Expand Down Expand Up @@ -159,17 +162,24 @@ public Point GetViewPortCoordinates()

protected T ExecuteScript<T>(JavaScript scriptName, params object[] arguments)
{
return Browser.ExecuteScript<T>(scriptName, element.GetElement(), arguments);
return Browser.ExecuteScript<T>(scriptName, ResolveArguments(arguments));
}

protected void ExecuteScript(JavaScript scriptName, params object[] arguments)
{
Browser.ExecuteScript(scriptName, element.GetElement(), arguments);
Browser.ExecuteScript(scriptName, ResolveArguments(arguments));
}

protected internal void LogElementAction(string messageKey, params object[] args)
{
Logger.InfoLocElementAction(elementType, element.Name, messageKey, args);
}

private object[] ResolveArguments(params object[] arguments)
{
var args = new ArrayList { element.GetElement() };
args.AddRange(arguments);
return args.ToArray();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@

<ItemGroup>
<None Remove="Resources\GetCurrentUrl.js" />
<None Remove="Resources\GetElementYCoordinate.js" />
<None Remove="Resources\GetScrollCoordinates.js" />
<None Remove="Resources\GetWindowSize.js" />
<None Remove="Resources\OpenUrlInNewWindow.js" />
<None Remove="Resources\TestJavaScript.js" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\GetWindowSize.js" />
<EmbeddedResource Include="Resources\GetElementYCoordinate.js" />
<EmbeddedResource Include="Resources\OpenUrlInNewWindow.js" />
<EmbeddedResource Include="Resources\GetCurrentUrl.js" />
<EmbeddedResource Include="Resources\GetScrollCoordinates.js" />
<EmbeddedResource Include="Resources\TestJavaScript.js" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using Aquality.Selenium.Browsers;
using Aquality.Selenium.Elements;
using Aquality.Selenium.Tests.Integration.TestApp;
Expand All @@ -22,7 +26,7 @@ public void Should_BePossibleTo_Click()

[Test]
public void Should_BePossibleTo_ClickAndWait()
{
{
var welcomeForm = new WelcomeForm();
welcomeForm.Open();
welcomeForm.GetExampleLink(AvailableExample.Dropdown).JsActions.ClickAndWait();
Expand Down Expand Up @@ -75,7 +79,7 @@ public void Should_BePossibleTo_CheckIsElementOnScreen()
hoversForm.Open();
Assert.Multiple(() =>
{
Assert.IsFalse(hoversForm.GetHiddenElement(HoverExample.First, ElementState.ExistsInAnyState).JsActions.IsElementOnScreen(),
Assert.IsFalse(hoversForm.GetHiddenElement(HoverExample.First, ElementState.ExistsInAnyState).JsActions.IsElementOnScreen(),
$"Hidden element for {HoverExample.First} should be invisible.");
Assert.IsTrue(hoversForm.GetExample(HoverExample.First).JsActions.IsElementOnScreen(),
$"Element for {HoverExample.First} should be visible.");
Expand All @@ -87,7 +91,7 @@ public void Should_BePossibleTo_SetValue()
{
var keyPressesForm = new KeyPressesForm();
keyPressesForm.Open();
var text = "text";
const string text = "text";
keyPressesForm.InputTextBox.JsActions.SetValue(text);
var actualText = keyPressesForm.InputTextBox.Value;
Assert.AreEqual(text, actualText, $"Text should be '{text}' after setting value via JS");
Expand All @@ -108,7 +112,7 @@ public void Should_BePossibleTo_GetXPathLocator()
var welcomeForm = new WelcomeForm();
welcomeForm.Open();
var actualLocator = welcomeForm.SubTitleLabel.JsActions.GetXPath();
var expectedLocator = "/html/body/DIV[2]/DIV[1]/H2[1]";
const string expectedLocator = "/html/body/DIV[2]/DIV[1]/H2[1]";
Assert.AreEqual(expectedLocator, actualLocator, $"Locator of sub title should be {expectedLocator}");
}

Expand All @@ -126,48 +130,59 @@ public void Should_BePossibleTo_ScrollIntoView()
{
var infiniteScrollForm = new InfiniteScrollForm();
infiniteScrollForm.Open();
infiniteScrollForm.WaitForPageToLoad();
var defaultCount = infiniteScrollForm.ExampleLabels.Count;
infiniteScrollForm.LastExampleLabel.JsActions.ScrollIntoView();
Assert.DoesNotThrow(
() => ConditionalWait.WaitForTrue(() =>
{
infiniteScrollForm.LastExampleLabel.JsActions.ScrollIntoView();
return infiniteScrollForm.ExampleLabels.Count > defaultCount;
}),
"Some examples should be added after scroll");
}), "Some examples should be added after scroll");
}

[Ignore("Need to fix on Azure")]
[Test]
public void Should_BePossibleTo_ScrollBy()
{
var infiniteScrollForm = new InfiniteScrollForm();
infiniteScrollForm.Open();
var defaultCount = infiniteScrollForm.ExampleLabels.Count;
Assert.DoesNotThrow(
() => ConditionalWait.WaitForTrue(() =>
{
infiniteScrollForm.LastExampleLabel.JsActions.ScrollBy(100000, 100000);
return infiniteScrollForm.ExampleLabels.Count > defaultCount;
}),
"Some examples should be added after scroll");
var point = new Point(50, 40);
var homeDemoSiteForm = new HomeDemoSiteForm();
homeDemoSiteForm.Open();
homeDemoSiteForm.FirstScrollableExample.JsActions.ScrollBy(point.X, point.Y);
var currentCoordinates = BrowserManager.Browser
.ExecuteScriptFromFile<IList<object>>("Resources.GetScrollCoordinates.js",
homeDemoSiteForm.FirstScrollableExample.GetElement()).Select(item => int.Parse(item.ToString()))
.ToList();
var actualPoint = new Point(currentCoordinates[0], currentCoordinates[1]);
Assert.AreEqual(point, actualPoint, $"Current coordinates should be '{point}'");
}

[Ignore("Need to fix on Azure")]
[Test]
public void Should_BePossibleTo_ScrollToTheCenter()
{
const int accuracy = 1;
var welcomeForm = new WelcomeForm();
welcomeForm.Open();
welcomeForm.GetExampleLink(AvailableExample.Dropdown).JsActions.ScrollToTheCenter();

var windowSize = BrowserManager.Browser.ExecuteScriptFromFile<object>("Resources.GetWindowSize.js").ToString();
var currentY = BrowserManager.Browser.ExecuteScriptFromFile<object>("Resources.GetElementYCoordinate.js",
welcomeForm.GetExampleLink(AvailableExample.Dropdown).GetElement()).ToString();
var coordinateRelatingWindowCenter = double.Parse(windowSize) / 2 - double.Parse(currentY);
Assert.LessOrEqual(Math.Abs(coordinateRelatingWindowCenter), accuracy, "Upper bound of element should be in the center of the page");
}

[Test]
public void Should_BePossibleTo_ScrollToTheCenter_CheckUI()
{
var infiniteScrollForm = new InfiniteScrollForm();
infiniteScrollForm.Open();
var defaultCount = infiniteScrollForm.ExampleLabels.Count;

infiniteScrollForm.WaitForPageToLoad();
var defaultCount = infiniteScrollForm.ExampleLabels.Count;
Assert.DoesNotThrow(
() => ConditionalWait.WaitForTrue(() =>
{
infiniteScrollForm.LastExampleLabel.JsActions.ScrollToTheCenter();
infiniteScrollForm.Footer.JsActions.ScrollToTheCenter();
return infiniteScrollForm.ExampleLabels.Count > defaultCount;
}),
"Some examples should be added after scroll");
}), "Some examples should be added after scroll");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Aquality.Selenium.Elements.Interfaces;
using OpenQA.Selenium;

namespace Aquality.Selenium.Tests.Integration.TestApp.TheInternet.Forms
{
internal class HomeDemoSiteForm : TheDemoSiteForm
{
public HomeDemoSiteForm() : base(By.XPath("//strong[contains(.,'1. Home ')]"), "Home")
{
}

public ILabel FirstScrollableExample => ElementFactory.GetLabel(
By.XPath("//div[@align='center']//tr[.//strong[contains(.,'index.php')]]//div[@align='left']"), "First example");

protected override string UrlPart => string.Empty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Aquality.Selenium.Browsers;
using Aquality.Selenium.Forms;
using OpenQA.Selenium;

namespace Aquality.Selenium.Tests.Integration.TestApp.TheInternet.Forms
{
internal abstract class TheDemoSiteForm : Form
{
private const string BaseUrl = "http://thedemosite.co.uk/";

protected TheDemoSiteForm(By locator, string name) : base(locator, name)
{
}

protected abstract string UrlPart { get; }

public string Url => BaseUrl + UrlPart;

public void Open()
{
BrowserManager.Browser.GoTo(Url);
BrowserManager.Browser.WaitForPageToLoad();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using Aquality.Selenium.Configurations;
using Aquality.Selenium.Elements.Interfaces;
using Aquality.Selenium.Waitings;
using OpenQA.Selenium;

namespace Aquality.Selenium.Tests.Integration.TestApp.TheInternet.Forms
Expand All @@ -16,5 +18,13 @@ public InfiniteScrollForm() : base(By.XPath("//div[@id='content' and .//h3[conta
public IList<ILabel> ExampleLabels => ElementFactory.FindElements(By.XPath("//div[contains(@class,'jscroll-added')]"), ElementFactory.GetLabel);

public ILabel LastExampleLabel => ExampleLabels.Last();

public ILabel Footer => ElementFactory.GetLabel(By.Id("page-footer"), "Footer");

public void WaitForPageToLoad()
{
var examplesCount = ExampleLabels.Count;
ConditionalWait.WaitFor(() => examplesCount != ExampleLabels.Count, Configuration.Instance.TimeoutConfiguration.Script);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return arguments[0].getBoundingClientRect().top;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return [arguments[0].scrollLeft, arguments[0].scrollTop];
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return Math.max(document.documentElement.clientHeight, window.innerHeight || 0);

0 comments on commit c63bfd8

Please sign in to comment.