Copy JQueryUI Tooltip text to clipboard in Java using Selenium

  • 2 Replies
  • 1344 Views
*

Haim

  • *
  • 10
Copy JQueryUI Tooltip text to clipboard in Java using Selenium
« on: February 11, 2016, 18:43:53 pm »
I am trying to copy the contents (text) of a JQueryUI tooltip widget. (https://jqueryui.com/resources/demos/tooltip/default.html) via Selenium for Java.

A simple
Code
WebElement.GetText()
does not work since (i gather) it is not treated as a regular tooltip. Nor have i had any success in retrieving the text via the JQueryUI API e.g. :
Code
$('#age').tooltip('option', 'content')
or copying with JavaScript's
Code
document.execCommand("copy")

So i would like to simulate a copy to clipboard :
Code
tooltip.sendKeys(Keys.CONTROL, "c");
but i need my automation to be cross-platform. In Mac OS it would be cmd+C which isn't working from what i gather around stackoverflow.

I found this method of copying to clipboard :

Code
StringSelection stringSelection = new StringSelection("test");
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
But this requires me to produce the string myself (the "test" string), whereas i don't have it yet. i need something to do the actual copying to clipboard. Is there any way to do this at the system level in Java? or alternatively another way to do this ?

*

Ruhit

  • *
  • 22
Re: Copy JQueryUI Tooltip text to clipboard in Java using Selenium
« Reply #1 on: February 14, 2016, 18:04:03 pm »
Hey haim,

you could try using JQuery selectors and JavaScript Executor for selenium to retrieve the current value :

first you will need to 'inject' a custom attribute to your tooltip element, in order to be able to reach it afterwards via JQuery selector :

Code
JavascriptExecutor jsDriver = (JavascriptExecutor) this.getDriver();
jsDriver.executeScript("arguments[0].setAttribute('MyElement', 'true')", tooltipElement);

now you can retrieve that value using your custom attribute within a JQuery selector :
Code
String value = (String) jsDriver.executeScript("return $('[myelement~=\"true\"]').val();");

*

Haim

  • *
  • 10
Re: Copy JQueryUI Tooltip text to clipboard in Java using Selenium
« Reply #2 on: February 15, 2016, 13:49:59 pm »
Thanks for the help Ruhit! that did the trick.

 

Sitemap 1 2 3 4 5 6 7 8