How to use Mouse class of WebDriverAPI package

Best WinAppDriver code snippet using WebDriverAPI.Mouse

Mouse.cs

Source:Mouse.cs Github

copy

Full Screen

...20using System;21namespace WebDriverAPI22{23 [TestClass]24 public class Mouse : CalculatorBase25 {26 [ClassInitialize]27 public static void ClassInitialize(TestContext context)28 {29 Setup(context);30 }31 [ClassCleanup]32 public static void ClassCleanup()33 {34 TearDown();35 }36 [TestInitialize]37 public virtual void TestInit()38 {39 // Set focus on the application by switching window to itself40 session.SwitchTo().Window(session.CurrentWindowHandle);41 // Restore the Calculator window if it is currently maximized42 WindowsElement maximizeButton = session.FindElementByAccessibilityId("Maximize");43 if (!maximizeButton.Text.Contains("Maximize"))44 {45 maximizeButton.Click();46 }47 }48 [TestMethod]49 public void MouseClick()50 {51 // Locate the calculatorResult element52 WindowsElement calculatorResult = session.FindElementByAccessibilityId("CalculatorResults");53 Assert.IsNotNull(calculatorResult);54 // Implicitly invoke /session/:sessionId/moveto and /session/:sessionId/click55 WindowsElement num8Button = session.FindElementByAccessibilityId("num8Button");56 session.Mouse.Click(num8Button.Coordinates);57 Assert.AreEqual("8", calculatorResult.Text.Replace("Display is", string.Empty).Trim());58 // Explicitly invoke /session/:sessionId/moveto and then /session/:sessionId/click on the current position59 WindowsElement clearButton = session.FindElementByAccessibilityId("clearButton");60 session.Mouse.MouseMove(clearButton.Coordinates);61 session.Mouse.Click(null);62 Assert.AreEqual("0", calculatorResult.Text.Replace("Display is", string.Empty).Trim());63 // Open a context menu on the application title bar to expose the context menu and verify that it contains minimize.64 // The context menu is parented on the desktop instead of the application. Thus, a desktop session is used to find it.65 // This command implicitly invoke /session/:sessionId/moveto and /session/:sessionId/click with button 2 parameter66 WindowsElement appNameTitle = session.FindCalculatorTitleByAccessibilityId();67 session.Mouse.ContextClick(appNameTitle.Coordinates);68 Thread.Sleep(TimeSpan.FromSeconds(1.5));69 WindowsDriver<WindowsElement> desktopSession = Utility.CreateNewSession(CommonTestSettings.DesktopAppId);70 try71 {72 Assert.IsNotNull(desktopSession.FindElementByName("System").FindElementByName("Minimize"));73 clearButton.Click(); // Dismiss the context menu74 }75 finally76 {77 if (desktopSession != null)78 {79 desktopSession.Quit();80 }81 }82 }83 [TestMethod]84 public void MouseDoubleClick()85 {86 WindowsElement maximizeButton = session.FindElementByAccessibilityId("Maximize");87 Assert.IsNotNull(maximizeButton);88 // Verify that the window is currently not maximized89 Assert.IsTrue(maximizeButton.Text.Contains("Maximize"));90 // Perform mouse double click on the title bar to maximize the Calculator window91 WindowsElement appNameTitle = session.FindCalculatorTitleByAccessibilityId();92 session.Mouse.MouseMove(appNameTitle.Coordinates);93 session.Mouse.DoubleClick(null); // Pass null as this command omit the given parameter94 Thread.Sleep(TimeSpan.FromSeconds(1));95 Assert.IsFalse(maximizeButton.Text.Contains("Maximize"));96 // Perform mouse double click on the title bar to restore the Calculator window97 session.Mouse.MouseMove(appNameTitle.Coordinates);98 session.Mouse.DoubleClick(null); // Pass null as this command omit the given parameter99 Thread.Sleep(TimeSpan.FromSeconds(1));100 Assert.IsTrue(maximizeButton.Text.Contains("Maximize"));101 }102 [TestMethod]103 public void MouseDownMoveUp()104 {105 const int offset = 100;106 WindowsElement appNameTitle = session.FindCalculatorTitleByAccessibilityId();107 Assert.IsNotNull(appNameTitle);108 // Save application window original position109 Point originalPosition = session.Manage().Window.Position;110 Assert.IsNotNull(originalPosition);111 // Send mouse down, move, and up actions combination to perform a drag and drop 112 // action on the app title bar. These actions reposition Calculator window.113 session.Mouse.MouseMove(appNameTitle.Coordinates);114 session.Mouse.MouseDown(null); // Pass null as this command omit the given parameter115 session.Mouse.MouseMove(appNameTitle.Coordinates, offset, offset);116 session.Mouse.MouseUp(null); // Pass null as this command omit the given parameter117 Thread.Sleep(TimeSpan.FromSeconds(1));118 // Verify that application window is now re-positioned from the original location119 Assert.AreNotEqual(originalPosition, session.Manage().Window.Position);120 Assert.IsTrue(originalPosition.Y < session.Manage().Window.Position.Y);121 // Restore application window original position122 session.Manage().Window.Position = originalPosition;123 Assert.AreEqual(originalPosition, session.Manage().Window.Position);124 }125 }126}...

Full Screen

Full Screen

Mouse

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using OpenQA.Selenium;7using OpenQA.Selenium.Chrome;8{9 {10 static void Main(string[] args)11 {12 IWebDriver driver = new ChromeDriver();13 IWebElement drag = driver.FindElement(By.Id("draggable"));14 IWebElement drop = driver.FindElement(By.Id("droppable"));15 Actions action = new Actions(driver);16 action.DragAndDrop(drag, drop).Perform();17 }18 }19}

Full Screen

Full Screen

Mouse

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using OpenQA.Selenium;7using OpenQA.Selenium.Chrome;8{9 {10 static void Main(string[] args)11 {12 IWebDriver driver = new ChromeDriver();13 driver.Manage().Window.Maximize();14 IWebElement searchbox = driver.FindElement(By.Name("q"));15 searchbox.SendKeys("selenium");16 searchbox.SendKeys(Keys.Enter);17 driver.Quit();18 }19 }20}21using System;22using System.Collections.Generic;23using System.Linq;24using System.Text;25using System.Threading.Tasks;26using OpenQA.Selenium;27using OpenQA.Selenium.Chrome;28{29 {30 static void Main(string[] args)31 {32 IWebDriver driver = new ChromeDriver();33 driver.Manage().Window.Maximize();34 IWebElement searchbox = driver.FindElement(By.Name("q"));35 searchbox.SendKeys("selenium");36 searchbox.SendKeys(Keys.Enter);37 IWebElement link = driver.FindElement(By.LinkText("Selenium - Web Browser Automation"));38 IJavaScriptExecutor js = (IJavaScriptExecutor)driver;39 js.ExecuteScript("arguments[0].scrollIntoView();", link);40 driver.Quit();41 }42 }43}44using System;45using System.Collections.Generic;46using System.Linq;47using System.Text;48using System.Threading.Tasks;49using OpenQA.Selenium;50using OpenQA.Selenium.Chrome;51using OpenQA.Selenium.Support.UI;52{53 {54 static void Main(string[] args)55 {56 IWebDriver driver = new ChromeDriver();57 driver.Manage().Window.Maximize();58 IWebElement searchbox = driver.FindElement(By.Name("q"));59 searchbox.SendKeys("selenium");60 searchbox.SendKeys(Keys.Enter);61 IWebElement link = driver.FindElement(By.LinkText("Selenium - Web Browser Automation"));62 IJavaScriptExecutor js = (IJavaScriptExecutor)driver;63 js.ExecuteScript("arguments[0].scrollIntoView();", link);64 link.Click();

Full Screen

Full Screen

Mouse

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using OpenQA.Selenium;7using OpenQA.Selenium.Firefox;8using OpenQA.Selenium.Support.UI;9{10 {11 static void Main(string[] args)12 {13 IWebDriver driver = new FirefoxDriver();14 driver.Manage().Window.Maximize();15 driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));16 IWebElement searchBox = driver.FindElement(By.Id("lst-ib"));17 searchBox.SendKeys("Selenium");18 IWebElement searchButton = driver.FindElement(By.Name("btnG"));19 searchButton.Click();20 driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));21 driver.Close();22 }23 }24}25using System;26using System.Collections.Generic;27using System.Linq;28using System.Text;29using System.Threading.Tasks;30using OpenQA.Selenium;31using OpenQA.Selenium.Firefox;32using OpenQA.Selenium.Support.UI;33{34 {35 static void Main(string[] args)36 {37 IWebDriver driver = new FirefoxDriver();38 driver.Manage().Window.Maximize();39 driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));40 IWebElement searchBox = driver.FindElement(By.Id("lst-ib"));41 searchBox.SendKeys("Selenium");42 IWebElement searchButton = driver.FindElement(By.Name("btnG"));43 searchButton.Click();44 driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));45 driver.Close();46 }47 }48}49using System;50using System.Collections.Generic;51using System.Linq;52using System.Text;53using System.Threading.Tasks;54using OpenQA.Selenium;55using OpenQA.Selenium.Firefox;56using OpenQA.Selenium.Support.UI;57{58 {

Full Screen

Full Screen

Mouse

Using AI Code Generation

copy

Full Screen

1using OpenQA.Selenium;2using OpenQA.Selenium.Chrome;3using OpenQA.Selenium.Support.UI;4using System;5using System.Collections.Generic;6using System.Linq;7using System.Text;8using System.Threading.Tasks;9{10 {11 static void Main(string[] args)12 {13 IWebDriver driver = new ChromeDriver();14 driver.Manage().Window.Maximize();15 IWebElement searchTextBox = driver.FindElement(By.Name("q"));16 searchTextBox.SendKeys("Selenium WebDriver");17 IWebElement searchButton = driver.FindElement(By.Name("btnK"));18 searchButton.Click();19 driver.Close();20 }21 }22}23using OpenQA.Selenium;24using OpenQA.Selenium.Chrome;25using OpenQA.Selenium.Support.UI;26using System;27using System.Collections.Generic;28using System.Linq;29using System.Text;30using System.Threading.Tasks;31{32 {33 static void Main(string[] args)34 {35 IWebDriver driver = new ChromeDriver();36 driver.Manage().Window.Maximize();37 IWebElement searchTextBox = driver.FindElement(By.Name("q"));38 searchTextBox.SendKeys("Selenium WebDriver");39 IWebElement searchButton = driver.FindElement(By.Name("btnK"));40 searchButton.Click();41 driver.Close();42 }43 }44}45using OpenQA.Selenium;

Full Screen

Full Screen

Mouse

Using AI Code Generation

copy

Full Screen

1using OpenQA.Selenium;2using OpenQA.Selenium.Chrome;3using System;4{5 {6 static void Main(string[] args)7 {8 IWebDriver driver = new ChromeDriver();9 driver.FindElement(By.Name("q")).SendKeys("Selenium");10 driver.FindElement(By.Name("btnK")).Click();11 driver.Close();12 }13 }14}

Full Screen

Full Screen

Mouse

Using AI Code Generation

copy

Full Screen

1using OpenQA.Selenium;2using OpenQA.Selenium.Chrome;3using System;4{5 {6 static void Main(string[] args)7 {8 IWebDriver driver = new ChromeDriver();9 driver.FindElement(By.Name("q")).SendKeys("Selenium");10 driver.FindElement(By.Name("btnK")).Click();11 driver.Close();12 }13 }14}

Full Screen

Full Screen

Mouse

Using AI Code Generation

copy

Full Screen

1using OpenQA.Selenium;2using OpenQA.Selenium.Chrome;3using OpenQA.Selenium.Interactions;4{5 public static void Main()6 {7 IWebDriver driver = new ChromeDriver();8 Actions action = new Actions(driver);9 action.MoveToElement(driver.FindElement(By.Name("q"))).Click().SendKeys("Selenium").SendKeys(Keys.Enter).Build().Perform();10 }11}12using OpenQA.Selenium;13using OpenQA.Selenium.Chrome;14using OpenQA.Selenium.Interactions;15{romeDriver();

Full Screen

Full Screen

Mouse

Using AI Code Generation

copy

Full Screen

1using OpenQA.Selenium;2using OpenQA.Selenium.Firefox;3using OpenQA.Selenium.Inteactins;4using Syste.Threading;5{6 static void Main()7 {8 driver.Manage().Window.Maximize();9 Thread.Sleep(2000);10 IWebElement searchBox = driver.FindElement(By.Name("q"));11 Actions builder = new Actions(driver);12 builder.MoveToElement(searchBox).Click().SendKeys("Selenium").Build().Perform();13 Thread.Sleep(2000);14 driver.Close();15 }16}

Full Screen

Full Screen

Mouse

Using AI Code Generation

copy

Full Screen

1using System;2using OpenQA.Selenium;3using OpenQA.Selenium.Firefox;4using OpenQA.Selenium.Interactions;5using OpenQA.Selenium.Remote;6using OpenQA.Selenium.Support.UI;7{8{9static void Main(string[] args)10{11IWebDriver driver = new FirefoxDriver();12string mainWindowHandle = driver.CurrentWindowHandle;13IWebElement searchBox = driver.FindElement(By.Id("lst-ib"));14searchBox.SendKeys("Selenium");15IWebElement searchButton = driver.FindElement(By.Name("btnK"));16searchButton.Click();17IWait<IWebDriver> wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30.00));18wait.Until(driver1 => driver1.FindElement(By.ClassName("r")));19IWebElement resultList = driver.FindElement(By.ClassName("r"));20IWebElement firstResult = resultList.FindElement(By.TagName("a"));21Actions mouse = new Actions(driver);22mouse.MoveToElement(firstResult);23mouse.Click();24mouse.Build().Perform();25wait.Until(driver2 => driver2.WindowHandles.Count == 2);26IReadOnlyCollection<string> windowHandles = driver.WindowHandles;27foreach (string handle in windowHandles)28{29if (handle != mainWindowHandle)30{31driver.SwitchTo().Window(handle);32break;33}34}35driver.Close();36driver.SwitchTo().Window(mainWindowHandle);37driver.Quit();38}39}40}41 {42 IWebDriver driver = new ChromeDriver();43 Actions action = new Actions(driver);44 action.MoveToElement(driver.FindElement(By.Name("q"))).Click().SendKeys("Selenium").SendKeys(Keys.Enter).Build().Perform();45 }46}47using OpenQA.Selenium;48using OpenQA.Selenium.Chrome;49using OpenQA.Selenium.Interactions;50{51 public static void Main()52 {53 IWebDriver driver = new ChromeDriver();54 Actions action = new Actions(driver);55 action.MoveToElement(driver.FindElement(By.Name("q"))).Click().SendKeys("Selenium").SendKeys(Keys.Enter).Build().Perform();56 }57}58using OpenQA.Selenium;59using OpenQA.Selenium.Chrome;60using OpenQA.Selenium.Interactions;61{62 public static void Main()63 {64 IWebDriver driver = new ChromeDriver();65 Actions action = new Actions(driver);66 action.MoveToElement(driver.FindElement(By.Name("q"))).Click().SendKeys("Selenium").SendKeys(Keys.Enter).Build().Perform();67 }68}69using OpenQA.Selenium;70using OpenQA.Selenium.Chrome;71using OpenQA.Selenium.Interactions;72{73 public static void Main()74 {75 IWebDriver driver = new ChromeDriver();

Full Screen

Full Screen

Mouse

Using AI Code Generation

copy

Full Screen

1using OpenQA.Selenium;2using OpenQA.Selenium.Firefox;3using OpenQA.Selenium.Interactions;4using System.Threading;5{6 static void Main()7 {8 IWebDriver driver = new FirefoxDriver();9 driver.Manage().Window.Maximize();10 Thread.Sleep(2000);11 IWebElement searchBox = driver.FindElement(By.Name("q"));12 Actions builder = new Actions(driver);13 builder.MoveToElement(searchBox).Click().SendKeys("Selenium").Build().Perform();14 Thread.Sleep(2000);15 driver.Close();16 }17}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run WinAppDriver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful