Automated Tester

.science

This content shows Simple View

Actions

Selenium Builder Actions

There are some complex actions which may not be obvious how to do in Selenium but by leveraging the use of the builder actions we can add more user interactions into our scripts where needed.

using OpenQA.Selenium.Interactions;

So what can we do with this? Lets have a quick look at what is contained within and then have a look at an example.

  • Click
  • Double Click
  • Click and Hold
  • Right Click (Context Click)
  • Drag and Drop
  • Drag and Drop to Offset
  • Key Down
  • Key Up
  • Move By Offset
  • Move To Element
  • Perform
  • Build
  • Release
  • SendKeys

We can see that there is a lot of advanced user functionality that you don’t find in the usual Selenium bindings. Let’s look an example using KeyUp and KeyDown.

Lets try and open a new tab with the builder actions when we click a particular link.

First we’ll new up the actions builder, hold down the CTRL key whilst clicking and then release the CTRL key.

Actions builder = new Actions(Context.Driver);
var linkToClick = elementToCheck.FindElement(By.LinkText("Continue"));
builder.KeyDown(Keys.Control).Click(linkToClick).Perform();
builder.KeyUp(Keys.Control).Perform();

SwitchToNewTab();

We instruct the builder to do our bidding and follow this up with .Perform to build/ execute the action. This particular piece of code (although not Mac friendly, in C# .Net their is a ‘Command’ key listed under Keys) We are depressing CTRL and clicking the link, building and performing it and then releasing the key press and then building and performing that.

This opens the desired link in a new tab, you can argue this isn’t the best way of doing so but it will do for demo purposes.

The actions builder is particularly useful for hovering the mouse over certain elements on the page with MoveToElement or performing unconventional clicking actions such as double and right clicks. Keep them in mind if you come across difficult functionality.




top