Skip to content

Commit c63bfd8

Browse files
knyshpaveliam
authored andcommitted
Fix scrolling
* #126 fixed scrolling tests * #126 fixed scrollToTheCenter test * #126 resolve parameters in executeScript method * #126 added remarks to ScrollBy
1 parent 5d7b51f commit c63bfd8

File tree

9 files changed

+113
-27
lines changed

9 files changed

+113
-27
lines changed

Aquality.Selenium/src/Aquality.Selenium/Elements/Actions/JsActions.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Drawing;
45
using System.Linq;
@@ -72,8 +73,9 @@ public void ScrollIntoView()
7273
/// <summary>
7374
/// Scrolling element by coordinates.
7475
/// </summary>
76+
/// <remarks>Element have to contains inner scroll bar.</remarks>
7577
/// <param name="x">Horizontal coordinate</param>
76-
/// <param name="y">Verticale coordinate</param>
78+
/// <param name="y">Vertical coordinate</param>
7779
public void ScrollBy(int x, int y)
7880
{
7981
LogElementAction("loc.scrolling.js");
@@ -82,6 +84,7 @@ public void ScrollBy(int x, int y)
8284

8385
/// <summary>
8486
/// Scrolling to the center of element.
87+
/// Upper bound of element will be in the center of the page after scrolling
8588
/// </summary>
8689
public void ScrollToTheCenter()
8790
{
@@ -159,17 +162,24 @@ public Point GetViewPortCoordinates()
159162

160163
protected T ExecuteScript<T>(JavaScript scriptName, params object[] arguments)
161164
{
162-
return Browser.ExecuteScript<T>(scriptName, element.GetElement(), arguments);
165+
return Browser.ExecuteScript<T>(scriptName, ResolveArguments(arguments));
163166
}
164167

165168
protected void ExecuteScript(JavaScript scriptName, params object[] arguments)
166169
{
167-
Browser.ExecuteScript(scriptName, element.GetElement(), arguments);
170+
Browser.ExecuteScript(scriptName, ResolveArguments(arguments));
168171
}
169172

170173
protected internal void LogElementAction(string messageKey, params object[] args)
171174
{
172175
Logger.InfoLocElementAction(elementType, element.Name, messageKey, args);
173176
}
177+
178+
private object[] ResolveArguments(params object[] arguments)
179+
{
180+
var args = new ArrayList { element.GetElement() };
181+
args.AddRange(arguments);
182+
return args.ToArray();
183+
}
174184
}
175185
}

Aquality.Selenium/tests/Aquality.Selenium.Tests/Aquality.Selenium.Tests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@
88

99
<ItemGroup>
1010
<None Remove="Resources\GetCurrentUrl.js" />
11+
<None Remove="Resources\GetElementYCoordinate.js" />
12+
<None Remove="Resources\GetScrollCoordinates.js" />
13+
<None Remove="Resources\GetWindowSize.js" />
1114
<None Remove="Resources\OpenUrlInNewWindow.js" />
1215
<None Remove="Resources\TestJavaScript.js" />
1316
</ItemGroup>
1417

1518
<ItemGroup>
19+
<EmbeddedResource Include="Resources\GetWindowSize.js" />
20+
<EmbeddedResource Include="Resources\GetElementYCoordinate.js" />
1621
<EmbeddedResource Include="Resources\OpenUrlInNewWindow.js" />
1722
<EmbeddedResource Include="Resources\GetCurrentUrl.js" />
23+
<EmbeddedResource Include="Resources\GetScrollCoordinates.js" />
1824
<EmbeddedResource Include="Resources\TestJavaScript.js" />
1925
</ItemGroup>
2026

Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/Actions/JsActionsTests.cs

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing;
4+
using System.Linq;
15
using Aquality.Selenium.Browsers;
26
using Aquality.Selenium.Elements;
37
using Aquality.Selenium.Tests.Integration.TestApp;
@@ -22,7 +26,7 @@ public void Should_BePossibleTo_Click()
2226

2327
[Test]
2428
public void Should_BePossibleTo_ClickAndWait()
25-
{
29+
{
2630
var welcomeForm = new WelcomeForm();
2731
welcomeForm.Open();
2832
welcomeForm.GetExampleLink(AvailableExample.Dropdown).JsActions.ClickAndWait();
@@ -75,7 +79,7 @@ public void Should_BePossibleTo_CheckIsElementOnScreen()
7579
hoversForm.Open();
7680
Assert.Multiple(() =>
7781
{
78-
Assert.IsFalse(hoversForm.GetHiddenElement(HoverExample.First, ElementState.ExistsInAnyState).JsActions.IsElementOnScreen(),
82+
Assert.IsFalse(hoversForm.GetHiddenElement(HoverExample.First, ElementState.ExistsInAnyState).JsActions.IsElementOnScreen(),
7983
$"Hidden element for {HoverExample.First} should be invisible.");
8084
Assert.IsTrue(hoversForm.GetExample(HoverExample.First).JsActions.IsElementOnScreen(),
8185
$"Element for {HoverExample.First} should be visible.");
@@ -87,7 +91,7 @@ public void Should_BePossibleTo_SetValue()
8791
{
8892
var keyPressesForm = new KeyPressesForm();
8993
keyPressesForm.Open();
90-
var text = "text";
94+
const string text = "text";
9195
keyPressesForm.InputTextBox.JsActions.SetValue(text);
9296
var actualText = keyPressesForm.InputTextBox.Value;
9397
Assert.AreEqual(text, actualText, $"Text should be '{text}' after setting value via JS");
@@ -108,7 +112,7 @@ public void Should_BePossibleTo_GetXPathLocator()
108112
var welcomeForm = new WelcomeForm();
109113
welcomeForm.Open();
110114
var actualLocator = welcomeForm.SubTitleLabel.JsActions.GetXPath();
111-
var expectedLocator = "/html/body/DIV[2]/DIV[1]/H2[1]";
115+
const string expectedLocator = "/html/body/DIV[2]/DIV[1]/H2[1]";
112116
Assert.AreEqual(expectedLocator, actualLocator, $"Locator of sub title should be {expectedLocator}");
113117
}
114118

@@ -126,48 +130,59 @@ public void Should_BePossibleTo_ScrollIntoView()
126130
{
127131
var infiniteScrollForm = new InfiniteScrollForm();
128132
infiniteScrollForm.Open();
133+
infiniteScrollForm.WaitForPageToLoad();
129134
var defaultCount = infiniteScrollForm.ExampleLabels.Count;
130-
infiniteScrollForm.LastExampleLabel.JsActions.ScrollIntoView();
131135
Assert.DoesNotThrow(
132136
() => ConditionalWait.WaitForTrue(() =>
133137
{
134138
infiniteScrollForm.LastExampleLabel.JsActions.ScrollIntoView();
135139
return infiniteScrollForm.ExampleLabels.Count > defaultCount;
136-
}),
137-
"Some examples should be added after scroll");
140+
}), "Some examples should be added after scroll");
138141
}
139142

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

156-
[Ignore("Need to fix on Azure")]
157158
[Test]
158159
public void Should_BePossibleTo_ScrollToTheCenter()
160+
{
161+
const int accuracy = 1;
162+
var welcomeForm = new WelcomeForm();
163+
welcomeForm.Open();
164+
welcomeForm.GetExampleLink(AvailableExample.Dropdown).JsActions.ScrollToTheCenter();
165+
166+
var windowSize = BrowserManager.Browser.ExecuteScriptFromFile<object>("Resources.GetWindowSize.js").ToString();
167+
var currentY = BrowserManager.Browser.ExecuteScriptFromFile<object>("Resources.GetElementYCoordinate.js",
168+
welcomeForm.GetExampleLink(AvailableExample.Dropdown).GetElement()).ToString();
169+
var coordinateRelatingWindowCenter = double.Parse(windowSize) / 2 - double.Parse(currentY);
170+
Assert.LessOrEqual(Math.Abs(coordinateRelatingWindowCenter), accuracy, "Upper bound of element should be in the center of the page");
171+
}
172+
173+
[Test]
174+
public void Should_BePossibleTo_ScrollToTheCenter_CheckUI()
159175
{
160176
var infiniteScrollForm = new InfiniteScrollForm();
161177
infiniteScrollForm.Open();
162-
var defaultCount = infiniteScrollForm.ExampleLabels.Count;
163-
178+
infiniteScrollForm.WaitForPageToLoad();
179+
var defaultCount = infiniteScrollForm.ExampleLabels.Count;
164180
Assert.DoesNotThrow(
165181
() => ConditionalWait.WaitForTrue(() =>
166182
{
167-
infiniteScrollForm.LastExampleLabel.JsActions.ScrollToTheCenter();
183+
infiniteScrollForm.Footer.JsActions.ScrollToTheCenter();
168184
return infiniteScrollForm.ExampleLabels.Count > defaultCount;
169-
}),
170-
"Some examples should be added after scroll");
185+
}), "Some examples should be added after scroll");
171186
}
172187
}
173188
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Aquality.Selenium.Elements.Interfaces;
2+
using OpenQA.Selenium;
3+
4+
namespace Aquality.Selenium.Tests.Integration.TestApp.TheInternet.Forms
5+
{
6+
internal class HomeDemoSiteForm : TheDemoSiteForm
7+
{
8+
public HomeDemoSiteForm() : base(By.XPath("//strong[contains(.,'1. Home ')]"), "Home")
9+
{
10+
}
11+
12+
public ILabel FirstScrollableExample => ElementFactory.GetLabel(
13+
By.XPath("//div[@align='center']//tr[.//strong[contains(.,'index.php')]]//div[@align='left']"), "First example");
14+
15+
protected override string UrlPart => string.Empty;
16+
}
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Aquality.Selenium.Browsers;
2+
using Aquality.Selenium.Forms;
3+
using OpenQA.Selenium;
4+
5+
namespace Aquality.Selenium.Tests.Integration.TestApp.TheInternet.Forms
6+
{
7+
internal abstract class TheDemoSiteForm : Form
8+
{
9+
private const string BaseUrl = "http://thedemosite.co.uk/";
10+
11+
protected TheDemoSiteForm(By locator, string name) : base(locator, name)
12+
{
13+
}
14+
15+
protected abstract string UrlPart { get; }
16+
17+
public string Url => BaseUrl + UrlPart;
18+
19+
public void Open()
20+
{
21+
BrowserManager.Browser.GoTo(Url);
22+
BrowserManager.Browser.WaitForPageToLoad();
23+
}
24+
}
25+
}

Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/TestApp/TheInternet/Forms/InfiniteScrollForm.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using Aquality.Selenium.Configurations;
34
using Aquality.Selenium.Elements.Interfaces;
5+
using Aquality.Selenium.Waitings;
46
using OpenQA.Selenium;
57

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

1820
public ILabel LastExampleLabel => ExampleLabels.Last();
21+
22+
public ILabel Footer => ElementFactory.GetLabel(By.Id("page-footer"), "Footer");
23+
24+
public void WaitForPageToLoad()
25+
{
26+
var examplesCount = ExampleLabels.Count;
27+
ConditionalWait.WaitFor(() => examplesCount != ExampleLabels.Count, Configuration.Instance.TimeoutConfiguration.Script);
28+
}
1929
}
2030
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return arguments[0].getBoundingClientRect().top;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return [arguments[0].scrollLeft, arguments[0].scrollTop];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return Math.max(document.documentElement.clientHeight, window.innerHeight || 0);

0 commit comments

Comments
 (0)