Introduction
Selenium is a widely used automation framework for web applications. It provides a range of functionalities to automate the web application testing process. One of the most useful features of Selenium is the Action class. The Action class allows users to perform complex actions such as drag and drop, mouse hover, and keyboard actions on a web page.
What is the Action class?
The Action class is a built-in class in Selenium that allows users to perform advanced user interactions with a web page. It enables users to perform a series of actions on a web page, such as clicking on an element, typing text, and dragging an element. The Action class is used to create a sequence of actions that can be performed on a web page.
How to use the Action class in Selenium C#
To use the Action class in Selenium C#, we need to create an instance of the Actions class. We can then use the various methods of the Actions class to perform different actions on the web page. Here is an example of how to use the Action class in Selenium C#: “` IWebElement element = driver.FindElement(By.XPath(“//input[@id=’username’]”)); Actions actions = new Actions(driver); actions.MoveToElement(element).Click().Perform(); “` In this example, we first locate the element we want to interact with using the `FindElement` method. We then create an instance of the Actions class and call the `MoveToElement` method to move the mouse pointer to the element. We then call the `Click` method to click on the element and finally call the `Perform` method to execute the action.
Advanced Actions in Selenium C#
The Action class allows for more advanced user interactions with a web page. Some of the advanced actions that can be performed using the Action class in Selenium C# are:
Drag and Drop
To perform a drag and drop action using the Action class, we need to first locate the element we want to drag, create an instance of the Actions class, call the `ClickAndHold` method to hold the element, move the mouse pointer to the target element using the `MoveToElement` method, and finally call the `Release` method to release the element. “` IWebElement sourceElement = driver.FindElement(By.XPath(“//div[@id=’source’]”)); IWebElement targetElement = driver.FindElement(By.XPath(“//div[@id=’target’]”)); Actions actions = new Actions(driver); actions.ClickAndHold(sourceElement).MoveToElement(targetElement).Release().Perform(); “` In this example, we first locate the source and target elements using the `FindElement` method. We then create an instance of the Actions class and call the `ClickAndHold` method to hold the source element. We then move the mouse pointer to the target element using the `MoveToElement` method and finally call the `Release` method to release the source element.
Mouse Hover
To perform a mouse hover action using the Action class, we need to first locate the element we want to hover over, create an instance of the Actions class, and call the `MoveToElement` method to move the mouse pointer to the element. “` IWebElement element = driver.FindElement(By.XPath(“//div[@id=’hover’]”)); Actions actions = new Actions(driver); actions.MoveToElement(element).Perform(); “` In this example, we first locate the element we want to hover over using the `FindElement` method. We then create an instance of the Actions class and call the `MoveToElement` method to move the mouse pointer to the element.
Keyboard Actions
To perform keyboard actions using the Action class, we need to create an instance of the Actions class, call the `SendKeys` method to type text, and call the `Perform` method to execute the action. “` Actions actions = new Actions(driver); actions.SendKeys(Keys.Enter).Perform(); “` In this example, we create an instance of the Actions class and call the `SendKeys` method to type the Enter key. We then call the `Perform` method to execute the action.
Conclusion
The Action class is a powerful feature of Selenium that allows users to perform advanced user interactions with a web page. It provides a range of functionalities to automate the web application testing process. In this article, we have discussed how to use the Action class in Selenium C# to perform advanced actions such as drag and drop, mouse hover, and keyboard actions. With the help of the Action class, users can create more complex test scenarios and improve the quality of their web applications.