9

I am writing automated tests for a site at work and we have certain screens that when we send a specific set of keystrokes, a section of the page will become visible and allows us to see information related to pricing or a document.

I would like to automate this using WebDriver's KeyDown or SendKeys command but I have so far been unable to.

The command I am trying to automate is Control + Alt + "Double Click". This is the code I have, but I have tried other variations using SendKeys instead of KeyDown. I know that at least one of the keys is being pressed on the page because the text within the element highlights, mimicing either a control or alt click, and the double click is also firing correctly.

Actions builder = new Actions(this.driver);

builder.KeyDown(Keys.LeftControl);
builder.KeyDown(Keys.LeftAlt);
builder.DoubleClick(BrowserHelper.GetElement(this.driver, locator, additionalLocatorPath));
builder.KeyUp(Keys.LeftControl);
builder.KeyUp(Keys.LeftAlt).Build().Perform();

I am using the latest version of firefox with native events enabled and webdriver 2.20

UPDATE I wound up ditching the action builder and used the javascript executor.

string jsScript = "var doubleClickElement = document.getElementById('"+ locator + "'); if(document.createEventObject){var evt = document.createEventObject();evt.altKey = true;evt.ctrlKey = true;doubleClickElement.fireEvent('ondblclick', evt);}else{var evt = document.createEvent('MouseEvents');evt.initMouseEvent('dblclick', true, true, window, null, 1, 1, 1, 1, true, true, false, false, null, doubleClickElement);doubleClickElement.dispatchEvent(evt);}";

((IJavaScriptExecutor)this.driver).ExecuteScript(jsScript);
  • I see the same problem with single-click. – user246 Mar 12 '12 at 23:14
2

I have no way of testing this and if I had sufficient rep would make this a comment but I don't so...

Have you tried sending .Build().Perform() before the key up commands?

Actions builder = new Actions(this.driver);
builder.KeyDown(Keys.LeftControl);
builder.KeyDown(Keys.LeftAlt);
builder.DoubleClick(BrowserHelper.GetElement(this.driver, locator, additionalLocatorPath));
builder.build().perform();

builder.KeyUp(Keys.LeftControl);
builder.KeyUp(Keys.LeftAlt).Build().Perform();

Also, I really don't think this will make a difference but the API states:

Parameters:

theKey - Either Keys.SHIFT, Keys.ALT or Keys.CONTROL. If the provided key is none of those, IllegalArgumentException is thrown.

While you are using Keys.LeftControl and Keys.LeftAlt.

0

I've had some problems getting Click(element) to work when giving the element to click as an argument. The workaround I use is to use MoveToElement(element) first and then Click() with no parameters. It seems to work reliably. It might work for DoubleClick() too but I'm not able to test it now...

// Get your element first
// I'm not familiar with the BrowserHelper extension so this line might not be correct...
IWebElement myElement = BrowserHelper.GetElement(this.driver, locator, additionalLocatorPath));


// Then perform the actions
Actions builder = new Actions(this.driver);
builder.MoveToElement(myElement);
builder.KeyDown(Keys.LeftControl);
builder.KeyDown(Keys.LeftAlt);
builder.DoubleClick();
builder.KeyUp(Keys.LeftControl);
builder.KeyUp(Keys.LeftAlt).Build().Perform();
builder.build().perform();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.