Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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 allow` 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);
share|improve this question
    
I see the same problem with single-click. –  user246 Mar 12 '12 at 23:14
add comment

2 Answers 2

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();
share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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