Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/org/labkey/test/WebDriverWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.labkey.test.util.EscapeUtil;
import org.labkey.test.util.Ext4Helper;
import org.labkey.test.util.ExtHelper;
import org.labkey.test.util.HtmlFragmentSelection;
import org.labkey.test.util.LabKeyExpectedConditions;
import org.labkey.test.util.LogMethod;
import org.labkey.test.util.LoggedParam;
Expand Down Expand Up @@ -3503,14 +3504,14 @@ public void actionClear(WebElement input)
}

/**
* puts the specified text into the clipboard, then pastes it into the specified element,
* puts the specified text and html into the clipboard, then pastes it into the specified element,
* or whatever has focus at the moment.
*/
public void actionPaste(WebElement input, String text)
public void actionPaste(WebElement input, String text, boolean isHtml)
{
Keys cmdKey = WebDriverUtils.MODIFIER_KEY;

setClipboardContent(text);
setClipboardContent(text, isHtml);

if (input == null)
{
Expand All @@ -3531,16 +3532,40 @@ public void actionPaste(WebElement input, String text)
}
}

/**
* puts the specified text into the clipboard, then pastes it into the specified element,
* or whatever has focus at the moment.
*/
public void actionPaste(WebElement input, String text)
{
actionPaste(input, text, false);
}

public void clearClipboardContent()
{
setClipboardContent(" ");
}

protected void setClipboardContent(String text)
protected void setClipboardContent(String text, boolean isHtml)
{
Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection sel = new StringSelection(text);
c.setContents(sel, sel);
Transferable sel;

if (isHtml)
{
sel = new HtmlFragmentSelection(text);
}
else
{
sel = new StringSelection(text);
}

c.setContents(sel, null);
}

protected void setClipboardContent(String text)
{
setClipboardContent(text, false);
}

public String getClipboardContent() throws IOException, UnsupportedFlavorException
Expand Down
45 changes: 45 additions & 0 deletions src/org/labkey/test/util/HtmlFragmentSelection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.labkey.test.util;

import org.jetbrains.annotations.NotNull;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;

/**
* Used when you want to put an HTML payload on the clipboard, allowing you to emulate things like copy/pasting from
* ELN/Google Docs.
*/
public class HtmlFragmentSelection implements Transferable
{
// You might be tempted to add support for DataFlavor.stringFlavor to this class in addition to
// DataFlavor.fragmentHtmlFlavor, because that's what the examples show across the Internet do, and that's what the
// various AI tools will tell you to do, but it simply doesn't work. If you put a text and HTML payload in the Java
// clipboard it will only paste the text, and not the HTML.
private static final DataFlavor[] transferDataFlavors = {DataFlavor.fragmentHtmlFlavor};
private final String html;

public HtmlFragmentSelection(String html) {
this.html = html;
}

@Override
public DataFlavor[] getTransferDataFlavors() {
return transferDataFlavors;
}

@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(DataFlavor.fragmentHtmlFlavor);
}

@Override
@NotNull
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException
{
if (flavor == DataFlavor.fragmentHtmlFlavor) {
return html;
}
throw new UnsupportedFlavorException(flavor);
}
}