Q:

How to launch javascript script and pass DOM element from java while having plain html?

I am automating web application which uses SmartClient and now I need some robust method to build locators for forms and populate input fields. We have test framework based on Selenium and SmartClient's scLocators. Test designer provides label of the form cell he wants to fill and the cell is in the sibling DOM element. I can parse his input and current html in order to produce the desired form cell's inner html. I decided to use javascript function getLocator provided by SmartGWT:
 https://code.google.com/p/smartgwt/source/browse/trunk/main/src/com/smartclient/public/sc/system/tools/AutoTest.js?r=37
as I have seen it working well in SC extension for Selenium IDE. How to create DOMElement required by this function and execute this javascript code?

A:

I would share with you common approach for js executing from java ( while working with selenium webDriver on java) and share some functions which I used :
I would recommend you to use JavascriptExecutor interface for this purpose like: 
extract text with javascriptExecutor:
 String cssSelector="abracadabra";    
JavascriptExecutor js = (JavascriptExecutor) driver;        
StringBuilder stringBuilder = new StringBuilder();        
stringBuilder.append("var x = $(\""+cssSelector+"\");");
stringBuilder.append("return x.text().toString();");      
String res= (String) js.executeScript(stringBuilder.toString());
Assert.assertTrue(res.trim().contains("Assigned VDIs")   );

click on the element
either:
driver.executeScript(“document.getElementById(‘addToCart’).click();”);

or
public void jsClick(String cssSelector){
JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("var x = $(\'"+cssSelector+"\');");
        stringBuilder.append("x.click();");
        js.executeScript(stringBuilder.toString());
}

=================
to summarize executing javascript using WebDriver approach:
Problem examples to solve: 
-you may want to fire an event on a particular element. (calling blur event directly)
-you may want to execute some script which will do some other operation. (instead of clicking directly call onclick targeted script)
Implementation example:
Cast the WebDriver instance to JavaScriptExecutor 
Execute the script using executeScript() method
Piece of code: 
WebDriver driver = new FirefoxDriver();
JavaScriptExecutor js=(JavaScriptExecutor)driver;  //casting driver instance
js.executeScript("return document.title");  //for getting window tile
js.executeScript("return window.name");  //for getting window name
js.executeScript("$('#elementId').blur()");  //firing blur event on an element

Hope this info helps you.

