Created
June 25, 2020 13:50
-
-
Save yashaka/159ed5d39922dfcd7d98b96ab207d8fd to your computer and use it in GitHub Desktop.
Bills.Tests/Core/SeleneElementExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using OpenQA.Selenium; | |
using static NSelene.Selene; | |
using NSelene; | |
using OpenQA.Selenium.Interactions; | |
using System; | |
using NSelene.Conditions; | |
namespace Bills.Tests.Core | |
{ | |
public static class SeleneElementExtensions | |
{ | |
public static bool Matching(this SeleneElement element, Condition<SeleneElement> condition) | |
{ | |
try | |
{ | |
return condition.Apply(element); | |
} | |
catch | |
{ | |
return false; | |
} | |
} | |
public static bool WaitUntil(this SeleneElement element, Condition<SeleneElement> condition) | |
{ | |
try | |
{ | |
element.Should(condition); | |
} | |
catch | |
{ | |
return false; | |
} | |
return true; | |
} | |
public static SeleneElement JsSetValue(this SeleneElement element, string value) | |
{ | |
element.Should(Be.Visible); | |
var webelement = element.ActualWebElement; | |
IJavaScriptExecutor js = (IJavaScriptExecutor) GetWebDriver(); | |
js.ExecuteScript( | |
@"return (function(element, text) { | |
var maxlength = element.getAttribute('maxlength') === null | |
? -1 | |
: parseInt(element.getAttribute('maxlength')); | |
element.value = maxlength === -1 | |
? text | |
: text.length <= maxlength | |
? text | |
: text.substring(0, maxlength); | |
return null; | |
})(arguments[0], arguments[1]);", | |
webelement, value); | |
return element; | |
} | |
public static SeleneElement JsClick(this SeleneElement element) | |
{ | |
element.Should(Be.InDom); | |
var webelement = element.ActualWebElement; | |
IJavaScriptExecutor js = (IJavaScriptExecutor) GetWebDriver(); | |
js.ExecuteScript( | |
@"return (function(element) { | |
element.click(); | |
return null; | |
})(arguments[0]);", webelement); | |
return element; | |
} | |
public static SeleneElement JsScrollIntoView(this SeleneElement element) | |
{ | |
element.Should(Be.InDom); | |
var webelement = element.ActualWebElement; | |
IJavaScriptExecutor js = (IJavaScriptExecutor) GetWebDriver(); | |
js.ExecuteScript( | |
@"return (function(element) { | |
element.scrollIntoView(); | |
return null; | |
})(arguments[0]);", webelement); | |
return element; | |
} | |
public static SeleneElement ActionsMoveToElementThenClick(this SeleneElement element) | |
{ | |
element.Should(Be.InDom); | |
var webelement = element.ActualWebElement; | |
var actions = new Actions(GetWebDriver()); | |
actions.MoveToElement(webelement).Click().Perform(); | |
return element; | |
} | |
public static SeleneElement ClickBelowOnNext(this SeleneElement element, int number = 1) | |
{ | |
element.Should(Be.Visible); | |
var webelement = element.ActualWebElement; | |
var size = webelement.Size; | |
var xOffsetToClick = 0; | |
var yOffsetToClick = size.Height * number; | |
var actions = new Actions(GetWebDriver()); | |
actions.MoveToElement(webelement) | |
.MoveByOffset(xOffsetToClick, yOffsetToClick) | |
.Perform(); | |
actions.Click().Perform(); | |
return element; | |
} | |
public static SeleneElement JsClickBelowOnNext(this SeleneElement element, int number = 1) | |
{ | |
element.Should(Be.Visible); | |
var webelement = element.ActualWebElement; | |
var size = webelement.Size; | |
var location = webelement.Location; | |
var x = location.X + 0; | |
var y = location.Y + size.Height * number; | |
IJavaScriptExecutor js = (IJavaScriptExecutor) GetWebDriver(); | |
js.ExecuteScript( | |
@"return (function(x, y) { | |
document.elementFromPoint(x, y).click(); | |
return null; | |
})(arguments[0], arguments[1]);", | |
x, y); | |
return element; | |
} | |
public static SeleneElement JsClickByCoordinates(this SeleneElement element) | |
{ | |
element.Should(Be.InDom); | |
var webelement = element.ActualWebElement; | |
var location = webelement.Location; | |
IJavaScriptExecutor js = (IJavaScriptExecutor) GetWebDriver(); | |
js.ExecuteScript( | |
@"return (function(x, y) { | |
document.elementFromPoint(x, y).click(); | |
return null; | |
})(arguments[0], arguments[1]);", | |
location.X, location.Y); | |
return element; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment