Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions examples/dotnet/SeleniumDocs/ActionsAPI/ActionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;

namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class ActionsTest : BaseTest
{
[TestMethod]
public void Pause()
{
driver.Url = "https://selenium.dev/selenium/web/mouse_interaction.html";

DateTime start = DateTime.Now;

IWebElement clickable = driver.FindElement(By.Id("clickable"));
new Actions(driver)
.MoveToElement(clickable)
.Pause(TimeSpan.FromSeconds(1))
.ClickAndHold()
.Pause(TimeSpan.FromSeconds(1))
.SendKeys("abc")
.Perform();

TimeSpan duration = DateTime.Now - start;
Assert.IsTrue(duration > TimeSpan.FromSeconds(2));
Assert.IsTrue(duration < TimeSpan.FromSeconds(3));
}

[TestMethod]
public void ReleaseAll()
{
driver.Url = "https://selenium.dev/selenium/web/mouse_interaction.html";

IWebElement clickable = driver.FindElement(By.Id("clickable"));
var actions = new Actions(driver);
actions.ClickAndHold(clickable)
.KeyDown(Keys.Shift)
.KeyDown(Keys.Alt)
.SendKeys("a")
.Perform();

((WebDriver)driver).ResetInputState();

actions.SendKeys("a").Perform();
var value = clickable.GetAttribute("value");
Assert.AreEqual("Å", value[..1]);
Assert.AreEqual("a", value.Substring(1, 1));
}
}
}
93 changes: 93 additions & 0 deletions examples/dotnet/SeleniumDocs/ActionsAPI/KeysTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;

namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class KeysTest : BaseTest
{
[TestMethod]
public void KeyDown()
{
driver.Url = "https://selenium.dev/selenium/web/single_text_input.html";

new Actions(driver)
.KeyDown(Keys.Shift)
.SendKeys("a")
.Perform();

IWebElement textField = driver.FindElement(By.Id("textInput"));
Assert.AreEqual("A", textField.GetAttribute("value"));
}

[TestMethod]
public void KeyUp()
{
driver.Url = "https://selenium.dev/selenium/web/single_text_input.html";

new Actions(driver)
.KeyDown(Keys.Shift)
.SendKeys("a")
.KeyUp(Keys.Shift)
.SendKeys("b")
.Perform();

IWebElement textField = driver.FindElement(By.Id("textInput"));
Assert.AreEqual("Ab", textField.GetAttribute("value"));
}

[TestMethod]
public void SendKeysToActiveElement()
{
driver.Url = "https://selenium.dev/selenium/web/single_text_input.html";

new Actions(driver)
.SendKeys("abc")
.Perform();

IWebElement textField = driver.FindElement(By.Id("textInput"));
Assert.AreEqual("abc", textField.GetAttribute("value"));
}

[TestMethod]
public void SendKeysToDesignatedElement()
{
driver.Url = "https://selenium.dev/selenium/web/single_text_input.html";
driver.FindElement(By.TagName("body")).Click();

IWebElement textField = driver.FindElement(By.Id("textInput"));
new Actions(driver)
.SendKeys(textField, "abc")
.Perform();

Assert.AreEqual("abc", textField.GetAttribute("value"));
}

[TestMethod]
public void CopyAndPaste()
{
driver.Url = "https://selenium.dev/selenium/web/single_text_input.html";

var capabilities = ((WebDriver)driver).Capabilities;
String platformName = (string)capabilities.GetCapability("platformName");

String cmdCtrl = platformName.Contains("mac") ? Keys.Command : Keys.Control;

new Actions(driver)
.SendKeys("Selenium!")
.SendKeys(Keys.ArrowLeft)
.KeyDown(Keys.Shift)
.SendKeys(Keys.ArrowUp)
.KeyUp(Keys.Shift)
.KeyDown(cmdCtrl)
.SendKeys("xvv")
.KeyUp(cmdCtrl)
.Perform();

IWebElement textField = driver.FindElement(By.Id("textInput"));
Assert.AreEqual("SeleniumSelenium!", textField.GetAttribute("value"));
}
}
}
208 changes: 208 additions & 0 deletions examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
using System;
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;

namespace SeleniumDocs.ActionsAPI
{
[TestClass]
public class MouseTest : BaseTest
{
[TestMethod]
public void ClickAndHold()
{
driver.Url = "https://selenium.dev/selenium/web/mouse_interaction.html";

IWebElement clickable = driver.FindElement(By.Id("clickable"));
new Actions(driver)
.ClickAndHold(clickable)
.Perform();

Assert.AreEqual("focused", driver.FindElement(By.Id("click-status")).Text);
}

[TestMethod]
public void ClickAndRelease()
{
driver.Url = "https://selenium.dev/selenium/web/mouse_interaction.html";

IWebElement clickable = driver.FindElement(By.Id("click"));
new Actions(driver)
.Click(clickable)
.Perform();

Assert.IsTrue(driver.Url.Contains("resultPage.html"));
}

[TestMethod]
public void RightClick()
{
driver.Url = "https://selenium.dev/selenium/web/mouse_interaction.html";

IWebElement clickable = driver.FindElement(By.Id("clickable"));
new Actions(driver)
.ContextClick(clickable)
.Perform();

Assert.AreEqual("context-clicked", driver.FindElement(By.Id("click-status")).Text);
}

[TestMethod]
public void BackClick()
{
driver.Url = "https://selenium.dev/selenium/web/mouse_interaction.html";

driver.FindElement(By.Id("click")).Click();
Assert.AreEqual("We Arrive Here", driver.Title);

ActionBuilder actionBuilder = new ActionBuilder();
PointerInputDevice mouse = new PointerInputDevice(PointerKind.Mouse, "default mouse");
actionBuilder.AddAction(mouse.CreatePointerDown(MouseButton.Back));
actionBuilder.AddAction(mouse.CreatePointerUp(MouseButton.Back));
((IActionExecutor)driver).PerformActions(actionBuilder.ToActionSequenceList());

Assert.AreEqual("BasicMouseInterfaceTest", driver.Title);
}

[TestMethod]
public void ForwardClick()
{
driver.Url = "https://selenium.dev/selenium/web/mouse_interaction.html";

driver.FindElement(By.Id("click")).Click();
driver.Navigate().Back();
Assert.AreEqual("BasicMouseInterfaceTest", driver.Title);

ActionBuilder actionBuilder = new ActionBuilder();
PointerInputDevice mouse = new PointerInputDevice(PointerKind.Mouse, "default mouse");
actionBuilder.AddAction(mouse.CreatePointerDown(MouseButton.Forward));
actionBuilder.AddAction(mouse.CreatePointerUp(MouseButton.Forward));
((IActionExecutor)driver).PerformActions(actionBuilder.ToActionSequenceList());

Assert.AreEqual("We Arrive Here", driver.Title);
}

[TestMethod]
public void DoubleClick()
{
driver.Url = "https://selenium.dev/selenium/web/mouse_interaction.html";

IWebElement clickable = driver.FindElement(By.Id("clickable"));
new Actions(driver)
.DoubleClick(clickable)
.Perform();

Assert.AreEqual("double-clicked", driver.FindElement(By.Id("click-status")).Text);
}

[TestMethod]
public void Hovers()
{
driver.Url = "https://selenium.dev/selenium/web/mouse_interaction.html";

IWebElement hoverable = driver.FindElement(By.Id("hover"));
new Actions(driver)
.MoveToElement(hoverable)
.Perform();

Assert.AreEqual("hovered", driver.FindElement(By.Id("move-status")).Text);
}

[TestMethod]
[Obsolete("Obsolete")]
public void MoveByOffsetFromTopLeftOfElement()
{
driver.Url = "https://selenium.dev/selenium/web/mouse_interaction.html";

IWebElement tracker = driver.FindElement(By.Id("mouse-tracker"));
new Actions(driver)
.MoveToElement(tracker, 8, 11, MoveToElementOffsetOrigin.TopLeft)
.Perform();

string[] result = driver.FindElement(By.Id("relative-location")).Text.Split(", ");
Assert.IsTrue(Math.Abs(int.Parse(result[0]) - 8) < 2);
Assert.IsTrue(Math.Abs(int.Parse(result[1]) - 11) < 2);
}

[TestMethod]
public void MoveByOffsetFromCenterOfElement()
{
driver.Url = "https://selenium.dev/selenium/web/mouse_interaction.html";

IWebElement tracker = driver.FindElement(By.Id("mouse-tracker"));
new Actions(driver)
.MoveToElement(tracker, 8, 11, MoveToElementOffsetOrigin.Center)
.Perform();

string[] result = driver.FindElement(By.Id("relative-location")).Text.Split(", ");
Assert.IsTrue(Math.Abs(int.Parse(result[0]) - 100 - 8) < 2);
Assert.IsTrue(Math.Abs(int.Parse(result[1]) - 100 - 11) < 2);
}

[TestMethod]
public void MoveByOffsetFromViewport()
{
driver.Url = "https://selenium.dev/selenium/web/mouse_interaction.html";

ActionBuilder actionBuilder = new ActionBuilder();
PointerInputDevice mouse = new PointerInputDevice(PointerKind.Mouse, "default mouse");
actionBuilder.AddAction(mouse.CreatePointerMove(CoordinateOrigin.Viewport,
8, 12, TimeSpan.Zero));
((IActionExecutor)driver).PerformActions(actionBuilder.ToActionSequenceList());

string[] result = driver.FindElement(By.Id("absolute-location")).Text.Split(", ");
Assert.IsTrue(Math.Abs(int.Parse(result[0]) - 8) < 2);
Assert.IsTrue(Math.Abs(int.Parse(result[1]) - 12) < 2);
}

[TestMethod]
public void MoveByOffsetFromCurrentPointerLocation()
{
driver.Url = "https://selenium.dev/selenium/web/mouse_interaction.html";

ActionBuilder actionBuilder = new ActionBuilder();
PointerInputDevice mouse = new PointerInputDevice(PointerKind.Mouse, "default mouse");
actionBuilder.AddAction(mouse.CreatePointerMove(CoordinateOrigin.Viewport,
8, 12, TimeSpan.Zero));
((IActionExecutor)driver).PerformActions(actionBuilder.ToActionSequenceList());

new Actions(driver)
.MoveByOffset(13, 15)
.Perform();

string[] result = driver.FindElement(By.Id("absolute-location")).Text.Split(", ");
Assert.IsTrue(Math.Abs(int.Parse(result[0]) - 8 - 13) < 2);
Assert.IsTrue(Math.Abs(int.Parse(result[1]) - 12 - 15) < 2);
}

[TestMethod]
public void DragToElement()
{
driver.Url = "https://selenium.dev/selenium/web/mouse_interaction.html";

IWebElement draggable = driver.FindElement(By.Id("draggable"));
IWebElement droppable = driver.FindElement(By.Id("droppable"));
new Actions(driver)
.DragAndDrop(draggable, droppable)
.Perform();

Assert.AreEqual("dropped", driver.FindElement(By.Id("drop-status")).Text);
}

[TestMethod]
public void DragByOffset()
{
driver.Url = "https://selenium.dev/selenium/web/mouse_interaction.html";

IWebElement draggable = driver.FindElement(By.Id("draggable"));
Point start = draggable.Location;
Point finish = driver.FindElement(By.Id("droppable")).Location;
new Actions(driver)
.DragAndDropToOffset(draggable, finish.X - start.X, finish.Y - start.Y)
.Perform();

Assert.AreEqual("dropped", driver.FindElement(By.Id("drop-status")).Text);
}
}
}
Loading