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

Commit

Permalink
Merge pull request #833 from Magenic/feature/updateElementUtils
Browse files Browse the repository at this point in the history
Make lazy elements work with action builder, element handler and Selenium utilities
  • Loading branch information
TroyWalshProf authored Nov 5, 2021
2 parents d9e39d3 + 5eead9a commit 3d95acb
Show file tree
Hide file tree
Showing 25 changed files with 988 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Framework/AppiumUnitTests/AppiumUnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion Framework/BaseDatabaseTest/BaseDatabaseTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.90" />
<PackageReference Include="Dapper" Version="2.0.123" />
<PackageReference Include="Dapper.Contrib" Version="2.0.78" />
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="5.0.11" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
Expand Down
145 changes: 141 additions & 4 deletions Framework/BaseSeleniumTest/ActionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,17 @@ public static class ActionBuilder
public static void HoverOver(this IWebDriver webDriver, By by)
{
Actions builder = new Actions(webDriver);
IWebElement element = webDriver.Wait().ForClickableElement(by);
IWebElement element = webDriver.Wait().ForElementExist(by);
builder.MoveToElement(element).Build().Perform();
}

/// <summary>
/// Performs a hover over on an element
/// </summary>
/// <param name="element">The web element</param>
public static void HoverOver(this IWebElement element)
{
Actions builder = new Actions(SeleniumUtilities.SearchContextToWebDriver(element));
builder.MoveToElement(element).Build().Perform();
}

Expand All @@ -46,8 +56,105 @@ public static void PressModifierKey(this IWebDriver webDriver, string key)
/// <param name="pixelsOffset">Integer of pixels to be moved (Positive or negative)</param>
public static void SlideElement(this IWebDriver webDriver, By element, int pixelsOffset)
{
Actions builder = new Actions(webDriver);
builder.DragAndDropToOffset(webDriver.FindElement(element), pixelsOffset, 0).Build().Perform();
IWebElement sourceElement = webDriver.Wait().ForElementExist(element);

sourceElement.DragAndDropToOffset(pixelsOffset, 0);
}

/// <summary>
/// Slider method which will take an offset of X pixels
/// </summary>
/// <param name="element">Element to be used</param>
/// <param name="pixelsOffset">Integer of pixels to be moved (Positive or negative)</param>
public static void SlideElement(this IWebElement element, int pixelsOffset)
{
element.DragAndDropToOffset(pixelsOffset, 0);
}

/// <summary>
/// Drag and drop an element
/// </summary>
/// <param name="webDriver">The IWebDriver</param>
/// <param name="source">Element to drag and drop</param>
/// <param name="destination">Where to drop the element</param>
public static void DragAndDrop(this IWebDriver webDriver, By source, By destination)
{
IWebElement sourceElement = webDriver.Wait().ForElementExist(source);
IWebElement destinationElement = webDriver.Wait().ForElementExist(destination);

sourceElement.DragAndDrop(destinationElement);
}

/// <summary>
/// Drag and drop an element
/// </summary>
/// <param name="source">Element to drag and drop</param>
/// <param name="destination">Where to drop the element</param>
public static void DragAndDrop(this IWebElement source, IWebElement destination)
{
Actions builder = new Actions(SeleniumUtilities.SearchContextToWebDriver(source));
builder.DragAndDrop(source, destination).Build().Perform();
}

/// <summary>
/// Drag and drop an element, plus or minus and X and Y offsets
/// </summary>
/// <param name="webDriver">The IWebDriver</param>
/// <param name="source">Element to drag and drop</param>
/// <param name="destination">Where to drop the element, plus or minus the offsets</param>
/// <param name="pixelsXOffset">Integer of pixels to be moved (Positive or negative) horizontally</param>
/// <param name="pixelsYOffset">Integer of pixels to be moved (Positive or negative) vertically </param>
public static void DragAndDropToOffset(this IWebDriver webDriver, By source, By destination, int pixelsXOffset, int pixelsYOffset)
{
IWebElement sourceElement = webDriver.Wait().ForElementExist(source);
IWebElement destinationElement = webDriver.Wait().ForElementExist(destination);

DragAndDropToOffset(sourceElement, destinationElement, pixelsXOffset, pixelsYOffset);
}

/// <summary>
/// Drag and drop an item to a destination, plus or minus and X and Y offsets
/// </summary>
/// <param name="source">Element to drag and drop</param>
/// <param name="destination">Where to drop the element, plus or minus the offsets</param>
/// <param name="pixelsXOffset">Integer of pixels to be moved (Positive or negative) horizontally</param>
/// <param name="pixelsYOffset">Integer of pixels to be moved (Positive or negative) vertically </param>
public static void DragAndDropToOffset(IWebElement source, IWebElement destination, int pixelsXOffset, int pixelsYOffset)
{
Actions builder = new Actions(SeleniumUtilities.SearchContextToWebDriver(source));

// Move to element goes to the top left corner so compensate for that
int horizontalOffset = (destination.Size.Width / 2) + pixelsXOffset;
int verticalOffset = (destination.Size.Height / 2) + pixelsYOffset;

builder.ClickAndHold(source).MoveToElement(destination, horizontalOffset, verticalOffset).Release().Build().Perform();
}

/// <summary>
/// Drag and drop an element to an X and Y offsets
/// </summary>
/// <param name="webDriver">The IWebDriver</param>
/// <param name="source">Element to drag and drop</param>
/// <param name="pixelsXOffset">Integer of pixels to be moved (Positive or negative) horizontally</param>
/// <param name="pixelsYOffset">Integer of pixels to be moved (Positive or negative) vertically </param>
public static void DragAndDropToOffset(this IWebDriver webDriver, By source, int pixelsXOffset, int pixelsYOffset)
{
IWebElement sourceElement = webDriver.Wait().ForElementExist(source);

sourceElement.DragAndDropToOffset(pixelsXOffset, pixelsYOffset);
}

/// <summary>
/// Drag and drop an element to an X and Y offsets
/// </summary>
/// <param name="source">Element to drag and drop</param>
/// <param name="pixelsXOffset">Integer of pixels to be moved (Positive or negative) horizontally</param>
/// <param name="pixelsYOffset">Integer of pixels to be moved (Positive or negative) vertically </param>
public static void DragAndDropToOffset(this IWebElement source, int pixelsXOffset, int pixelsYOffset)
{
Actions builder = new Actions(SeleniumUtilities.SearchContextToWebDriver(source));

builder.DragAndDropToOffset(source, pixelsXOffset, pixelsYOffset).Build().Perform();
}

/// <summary>
Expand All @@ -57,9 +164,39 @@ public static void SlideElement(this IWebDriver webDriver, By element, int pixel
/// <param name="by">By selector for the element</param>
public static void RightClick(this IWebDriver webDriver, By by)
{
Actions builder = new Actions(webDriver);
IWebElement element = webDriver.Wait().ForClickableElement(by);
element.RightClick();
}

/// <summary>
/// Performs a right-click on an element
/// </summary>
/// <param name="element">The web element</param>
public static void RightClick(this IWebElement element)
{
Actions builder = new Actions(SeleniumUtilities.SearchContextToWebDriver(element));
builder.ContextClick(element).Build().Perform();
}

/// <summary>
/// Performs a right-click on an element
/// </summary>
/// <param name="webDriver">The IWebDriver</param>
/// <param name="by">By selector for the element</param>
public static void DoubleClick(this IWebDriver webDriver, By by)
{
IWebElement element = webDriver.Wait().ForClickableElement(by);
element.DoubleClick();
}

/// <summary>
/// Performs a right-click on an element
/// </summary>
/// <param name="element">The web element</param>
public static void DoubleClick(this IWebElement element)
{
Actions builder = new Actions(SeleniumUtilities.SearchContextToWebDriver(element));
builder.DoubleClick(element).Build().Perform();
}
}
}
2 changes: 1 addition & 1 deletion Framework/BaseSeleniumTest/BaseSeleniumTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Selenium.Axe" Version="3.1.1" />
<PackageReference Include="Selenium.Axe" Version="3.1.2" />
<PackageReference Include="Selenium.Support" Version="4.0.1" />
<PackageReference Include="Selenium.WebDriver" Version="4.0.1" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
Expand Down
Loading

0 comments on commit 3d95acb

Please sign in to comment.