Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
Migrate to SeleniumHQ 3 #11
Browse files Browse the repository at this point in the history
- Add ScrollIntoViewIfNotVisible
  • Loading branch information
cezarypiatek committed Apr 24, 2017
1 parent 832494e commit 98605c3
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions Src/MvcPages/SeleniumUtils/SeleniumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,18 @@ internal static bool IsElementClickable(this RemoteWebDriver driver, IWebElement
", element);
}

internal static void ScrollIntoView(this RemoteWebDriver driver, IWebElement element)
internal static void ScrollIntoViewIfNotVisible(this RemoteWebDriver driver, IWebElement element)
{
driver.ExecuteScript(@"
if(typeof arguments[0].scrollIntoView == 'function'){
arguments[0].scrollIntoView();
}
(function(element){
if(typeof element.scrollIntoView == 'function'){
var rec = element.getBoundingClientRect();
var elementAtCenter = document.elementFromPoint(rec.left+rec.width/2, rec.top+rec.height/2);
if(element != elementAtCenter && element.contains(elementAtCenter) == false)
{
element.scrollIntoView();
}
}})(arguments[0]);
", element);
}

Expand Down Expand Up @@ -276,6 +282,7 @@ public static void ClickOn(this RemoteWebDriver driver, IWebElement expectedElem
{
try
{
driver.ScrollIntoViewIfNotVisible(expectedElement);
expectedElement.Click();
}
catch (Exception ex)
Expand All @@ -293,7 +300,7 @@ public static void ClickOn(this RemoteWebDriver driver, IWebElement expectedElem
driver.ScrollToY(expectedElement.Location.Y + expectedElement.Size.Height);
Thread.Sleep(500);
}
driver.ScrollIntoView(expectedElement);
driver.ScrollIntoViewIfNotVisible(expectedElement);
driver.WaitUntil(SearchElementDefaultTimeout, (d) => driver.IsElementClickable(expectedElement));
expectedElement.Click();
if (originalScrollPosition != null)
Expand Down

0 comments on commit 98605c3

Please sign in to comment.