From c63bfd8bfb31f3a18fa7deafb580659dc7c47e34 Mon Sep 17 00:00:00 2001 From: knysh Date: Wed, 4 Sep 2019 10:51:53 +0300 Subject: [PATCH] Fix scrolling * #126 fixed scrolling tests * #126 fixed scrollToTheCenter test * #126 resolve parameters in executeScript method * #126 added remarks to ScrollBy --- .../Elements/Actions/JsActions.cs | 16 ++++- .../Aquality.Selenium.Tests.csproj | 6 ++ .../Integration/Actions/JsActionsTests.cs | 63 ++++++++++++------- .../TheDemoSite/Forms/HomeDemoSiteForm.cs | 17 +++++ .../TheDemoSite/Forms/TheDemoSiteForm.cs | 25 ++++++++ .../TheInternet/Forms/InfiniteScrollForm.cs | 10 +++ .../Resources/GetElementYCoordinate.js | 1 + .../Resources/GetScrollCoordinates.js | 1 + .../Resources/GetWindowSize.js | 1 + 9 files changed, 113 insertions(+), 27 deletions(-) create mode 100644 Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/TestApp/TheDemoSite/Forms/HomeDemoSiteForm.cs create mode 100644 Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/TestApp/TheDemoSite/Forms/TheDemoSiteForm.cs create mode 100644 Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/GetElementYCoordinate.js create mode 100644 Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/GetScrollCoordinates.js create mode 100644 Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/GetWindowSize.js diff --git a/Aquality.Selenium/src/Aquality.Selenium/Elements/Actions/JsActions.cs b/Aquality.Selenium/src/Aquality.Selenium/Elements/Actions/JsActions.cs index 0a9f9db3..1f8f18d6 100644 --- a/Aquality.Selenium/src/Aquality.Selenium/Elements/Actions/JsActions.cs +++ b/Aquality.Selenium/src/Aquality.Selenium/Elements/Actions/JsActions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Drawing; using System.Linq; @@ -72,8 +73,9 @@ public void ScrollIntoView() /// /// Scrolling element by coordinates. /// + /// Element have to contains inner scroll bar. /// Horizontal coordinate - /// Verticale coordinate + /// Vertical coordinate public void ScrollBy(int x, int y) { LogElementAction("loc.scrolling.js"); @@ -82,6 +84,7 @@ public void ScrollBy(int x, int y) /// /// Scrolling to the center of element. + /// Upper bound of element will be in the center of the page after scrolling /// public void ScrollToTheCenter() { @@ -159,17 +162,24 @@ public Point GetViewPortCoordinates() protected T ExecuteScript(JavaScript scriptName, params object[] arguments) { - return Browser.ExecuteScript(scriptName, element.GetElement(), arguments); + return Browser.ExecuteScript(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(); + } } } diff --git a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Aquality.Selenium.Tests.csproj b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Aquality.Selenium.Tests.csproj index 321470cf..161bb442 100644 --- a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Aquality.Selenium.Tests.csproj +++ b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Aquality.Selenium.Tests.csproj @@ -8,13 +8,19 @@ + + + + + + diff --git a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/Actions/JsActionsTests.cs b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/Actions/JsActionsTests.cs index 8356e127..59cfb31f 100644 --- a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/Actions/JsActionsTests.cs +++ b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/Actions/JsActionsTests.cs @@ -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; @@ -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(); @@ -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."); @@ -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"); @@ -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}"); } @@ -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>("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("Resources.GetWindowSize.js").ToString(); + var currentY = BrowserManager.Browser.ExecuteScriptFromFile("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"); } } } diff --git a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/TestApp/TheDemoSite/Forms/HomeDemoSiteForm.cs b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/TestApp/TheDemoSite/Forms/HomeDemoSiteForm.cs new file mode 100644 index 00000000..ff3fce8a --- /dev/null +++ b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/TestApp/TheDemoSite/Forms/HomeDemoSiteForm.cs @@ -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; + } +} diff --git a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/TestApp/TheDemoSite/Forms/TheDemoSiteForm.cs b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/TestApp/TheDemoSite/Forms/TheDemoSiteForm.cs new file mode 100644 index 00000000..cf1988bd --- /dev/null +++ b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/TestApp/TheDemoSite/Forms/TheDemoSiteForm.cs @@ -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(); + } + } +} diff --git a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/TestApp/TheInternet/Forms/InfiniteScrollForm.cs b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/TestApp/TheInternet/Forms/InfiniteScrollForm.cs index 91f78025..322a308d 100644 --- a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/TestApp/TheInternet/Forms/InfiniteScrollForm.cs +++ b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/TestApp/TheInternet/Forms/InfiniteScrollForm.cs @@ -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 @@ -16,5 +18,13 @@ public InfiniteScrollForm() : base(By.XPath("//div[@id='content' and .//h3[conta public IList 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); + } } } diff --git a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/GetElementYCoordinate.js b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/GetElementYCoordinate.js new file mode 100644 index 00000000..261a0b36 --- /dev/null +++ b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/GetElementYCoordinate.js @@ -0,0 +1 @@ +return arguments[0].getBoundingClientRect().top; \ No newline at end of file diff --git a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/GetScrollCoordinates.js b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/GetScrollCoordinates.js new file mode 100644 index 00000000..1add7a42 --- /dev/null +++ b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/GetScrollCoordinates.js @@ -0,0 +1 @@ +return [arguments[0].scrollLeft, arguments[0].scrollTop]; \ No newline at end of file diff --git a/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/GetWindowSize.js b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/GetWindowSize.js new file mode 100644 index 00000000..67cf771f --- /dev/null +++ b/Aquality.Selenium/tests/Aquality.Selenium.Tests/Resources/GetWindowSize.js @@ -0,0 +1 @@ +return Math.max(document.documentElement.clientHeight, window.innerHeight || 0); \ No newline at end of file