Paste Action (Control + V) not working in Firefox

1,038 views
Skip to first unread message

brianm709

unread,
May 4, 2012, 2:16:23 PM5/4/12
to webdriver
I am attempting to paste from the clipboard using the Actions API.
It is as if the Control key is not being pressed. "V" is typed in the
input field.

I'm using Firefox 10.0.2
Running Selenium 2.21.0 on Windows.
Build info: version: '2.21.0', revision: '16552', time: '2012-04-11
19:09:00'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1',
java.version: '1.6.0_22'

I saw the same behavior with Selenium 2.19.0.
This test passes when I use the ChromeDriver.

This is my simple test scenario

import static org.junit.Assert.assertEquals;

import java.awt.Toolkit;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class SimpleTest
{
@Test
public void simpleTest() throws Exception
{
final WebDriver driver = new FirefoxDriver();
try
{
driver.get( "http://localhost/test.html" );
driver.findElement( By.id( "test" ) );

final WebElement e = driver.findElement( By.id( "input" ) );
e.sendKeys( "typed" );

copyToClipboard( "pasted" );
final Actions builder = new Actions( driver );

builder.click( e ).keyDown( Keys.CONTROL ).sendKeys( "V" ).keyUp( Keys.CONTROL );
final Action paste = builder.build();
paste.perform();

assertEquals( "typedpasted", e.getAttribute( "value" ) );
}
finally
{
driver.quit();
}

}

public static void copyToClipboard( final String text )
{
final StringSelection stringSelection = new
StringSelection( text );

Toolkit.getDefaultToolkit().getSystemClipboard().setContents( stringSelection,
new ClipboardOwner()
{
@Override
public void lostOwnership( final java.awt.datatransfer.Clipboard
clipboard, final Transferable contents )
{
// do nothing
}
} );
}
}

And this is my html page:

<html>
<head></head>
<body>
<p>Hello World</p>
<input type="text" id="input"></input>
</body>
</html>

Am I doing something incorrectly, or does this look like a bug?
Thanks,
Brian

Mark Collin

unread,
May 8, 2012, 1:13:37 AM5/8/12
to webd...@googlegroups.com
CTRL + V is not always paste, why don't you just do a .sendKeys() with the
data you want to paste?
--
You received this message because you are subscribed to the Google Groups
"webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to
webdriver+...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/webdriver?hl=en.


brianm709

unread,
May 8, 2012, 4:08:34 PM5/8/12
to webd...@googlegroups.com
As of right now, our tests all run on Windows.
I am trying to simulate a user pasting formatted text from a word document into a text area.  The formatted text includes tab characters.
If I use sendKeys with the tab character it tabs out of my text area.

Mike Riley

unread,
May 8, 2012, 6:47:11 PM5/8/12
to webd...@googlegroups.com
I am not sure what the equivalent is for WebDriver, but Selenium has contextMenu, but Mark will probably know.  Take a look at this thread:
https://groups.google.com/forum/?fromgroups&hl=en#!searchin/selenium-users/contextmenu/selenium-users/HG-uxS19d-I/X17oP6pRqLoJ

From the context menu you should be able to choose Paste.  If nothing else, maybe via WebDriverBackedSelenium?

Mike

Mark Collin

unread,
May 9, 2012, 9:20:50 AM5/9/12
to webd...@googlegroups.com

Context menu in WebDriver is a little more complex than just clicking, you have to use the Actions implementation:

 

WebElement foo = driver.findElement(By.xpath("//input"));

Actions bar = new Actions(driver);

bar.contextClick(foo).perform();

--

You received this message because you are subscribed to the Google Groups "webdriver" group.

To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/t0WjGPIrkj8J.

brianm709

unread,
May 9, 2012, 12:37:33 PM5/9/12
to webd...@googlegroups.com
I was able to paste in Firefox using the contextClick action.

      final WebElement e = driver.findElement( By.id( "input" ) );
      new Actions( driver ).contextClick( e ).sendKeys( "p" ).perform();

This works for me, but it still seems like there is an issue with keyDown & keyUp actions on Firefox

Thanks for your help
Reply all
Reply to author
Forward
0 new messages