I'm try show Popup in java program(web).
I try ScriptEngine with javascript, nashorn but fail. Because alert,confirm, prompt is not method of js.
Code in java program:
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval(new FileReader("script.js"));
Invocable invocable = (Invocable) engine;
invocable.invokeFunction("openPopup", "ABC XYZ");
and in script.js:
function openPopup(str){
alert(str);
}
Run it, error show:
"alert" is undefined
Explanation
Opening a JavaScript popup in Java is not possible. The JavaScript Engine Nashorn does not provide this method. It usually is a functionality provided by browsers.
That is also why you are getting:
"alert" is undefined
Solution
You can open popup windows with different tools in Java, for example Swing or JavaFX. Those are tools with which you can create programs with graphical user interfaces (GUI), i.e. that have windows.
Here is an official tutorial by Oracle on how to create dialogs with Swing.
The relevant method for creating just a simple popup is:
JOptionPane.showMessageDialog(frame, "Hello world!");
Where frame is a reference to the window which should be the parent of this popup. However you can simply pass null for a quick and dirty popup:
JOptionPane.showMessageDialog(null, "Hello world!");
Just use the above code, import the JOptionPane and it should work:
import javax.swing.JOptionPane;
The class has more interesting methods to check out, like input dialogs. Here is its documentation.
The JavaFX solution is a bit more complicated as it requires you to setup the frame and handle several events.
You may check out this other question SO: Popup window with table view in JavaFX 2.0. It uses the designated class Popup (documentation).
Related
I am using Winium + Java for automation testing of Windows application, and trying to access tool bar menu.
When I tried to detect elements using UI Automation Verify, I couldn't see child elements under tool bar element like below screenshot.
enter image description here
But my tool bar definitely has sub menu items like screenshot and I need to access them.
enter image description here
I tried below java code, but it didn't work
WebElement el = driver.findElement(By.id('59398'));
el.click();
WebElement child = el.findElement(By.name('Start'));
child.click();
when I tried
driver.findElement(By.name"Start').click();
it clicked my windows start menu, not my application's menu.
Is there any way to access items under this tool bar?
You can try use another UI Inspector
eg. UI SPY or Inspector.exe
Probably your ID is not a AutomationID (process id?)
You should find a main window (parent of your app) (Example for calc) and get a parameter like AutomationId, ClassName or Name
I see this is MFC application, and this is an app side MFC library problem. If you hover mouse over toolbar button using Inspect.exe, the info is available but you can't reach this button from the hierarchy (the buttons have no parent somehow). Possible workaround involves combined Win32 API and UI Automation approach:
get button rectangle using Win32 API (but there is no text).
use ElementFromPoint method of UI Automation API and get actual texts to choose the right button.
P.S. My suggestion is applicable for Java + Winium in theory. But I can't estimate the complexity because I'm not a Java expert. So below is Python solution.
We have plans to implemented this mixed way in pywinauto. See issue #413. It contains Python code sample how to do that. We've had no chance to integrate it yet.
from ctypes.wintypes import tagPOINT
import pywinauto
app = pywinauto.Application().start(r'.\apps\MFC_samples\RebarTest.exe')
menu_bar = app.RebarTest.MenuBar.wrapper_object()
point = menu_bar.button(0).rectangle().mid_point()
point = menu_bar.client_to_screen(point)
elem = pywinauto.uia_defines.IUIA().iuia.ElementFromPoint(tagPOINT(point[0], point[1]))
element = pywinauto.uia_element_info.UIAElementInfo(elem)
print(element.name)
I need to perform a key press combination on Selenium Chrome driver.
The action is not sending test to text box or clicking on a button.
I am actually not interested in sending keys to any specific web element.
For example, I would like to perform command+R (reload on Mac OS).
(Reloading is just an example for the explanation, not my ultimate goal)
My code is the following:
public static void keyPressCombnaiton() {
Actions action = new Actions(browser);
action.keyDown(Keys.COMMAND)
.sendKeys("r")
.keyUp(Keys.COMMAND)
.build()
.perform();
}
I have spend hours searching and trying only got no luck.
Any help is appreciated!
The WebDriver spec is element-focussed, and doesn't define any method to send keys to the window, the screen, to browser chrome - only to elements.
Use of the Selenium Actions class for Cmd-R works on my Mac in Firefox (45), but only when run in the foreground - and seemingly not at all in Chrome. Presumably this is down to differences in the implementations of the remote Keyboard implementation, which it's probably best not to rely upon.
The most efficient way and non-platform-specific way to request a page reload is using JavaScript:
((JavascriptExecutor) driver).executeScript("document.location.reload(true)");
However, JavaScript doesn't let you "just send keys".
The only other way is via the Java AWT Robot class:
Robot robot = new java.awt.Robot();
robot.keyPress(KeyEvent.VK_META); // See: http://stackoverflow.com/a/15419192/954442
robot.keyPress(KeyEvent.VK_R);
robot.keyRelease(KeyEvent.VK_R);
robot.keyRelease(KeyEvent.VK_META);
This "blindly" sends key combinations to whichever windows / components are on screen at the time, so if your browser window has been hidden or minimised, this will not work.
I am new to using java for XPages development, I want to know if its posible to use JOptionPane Dialog in XPages Project? if yes how? or can i display a dialog component with Java. i tried the below code but notting happened
import javax.swing.*;
public class JOptionPaneMultiInput {
public static void main(String[] args) {
JTextField username = new JTextField();
JTextField password = new JPasswordField();
Object[] message = {
"Username test:", username,
"Password:", password
};
int option = JOptionPane.showConfirmDialog(null, message, "Login", JOptionPane.OK_CANCEL_OPTION);
}
}
I would like to use Java to display a dialog and save retured value in a variable
Swing is not compatible with XPages. XPages is built on JSF and I don't believe any JSF framework supports Swing. Swing is not used for web clients, it is only used for thick clients like Windows or Mac. Xpages is made for presenting to a web browser.
Note: JOptionPane is a part of the Swing framework. You can see this in your import which likely looks like this: javax.swing.*;
UPDATE:
You can definitely accomplish what you are asking. XPages is very feature packed and a great way to develop web applications. It does have a fairly steep learning curve, but thankfully there are already many great free resources out there. I would start with these two:
TLCC has a free introductory to XPages http://www.tlcc.com/admin/tlccsite.nsf/pages/free-xpages-training I also recommend their other paid courses which are very well done.
Notes in 9, (http://www.notesin9.com/) which is a series of free how to videos. Start with the hour long Intro to XPages: http://xpages.tv/xtv3.nsf/allEpisodes.xsp# which is where it all started to click with me.
I am trying to open a browser and then simulate a user in Java. Previously I would have accomplished this with simple Applescript, with something like this
tell application "Safari"
activate
open location "http://google.co.uk"
delay 1
do JavaScript "
document.getElementById('gbqfq').value ='software is hard';
" in document 1
end tell
So it opens Safari, then types into Google's query box.
In Java, so far I have
import javax.script.*;
public class VirtualUser {
public static void main(String[] args) throws Exception {
Runtime.getRuntime().exec("open /Applications/Safari.app");
Thread.sleep(1000);
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
engine.eval("print('Hello World')");
}
}
Which opens Safari (Google homepage), and prints Hello World in the IDE (verification that JS is working within Java?)
In the Java example, how can I then further enter text into Google's search bar? And is utilising JavaScript within Java the best/only solution for this?
I've had some luck with Selenium in the past. It automates a browser and you can run commands exactly like you are describing. Definitely go check it out.
I want to design new Git client with a clean GUI.
Is it possible to use the power of HTML, CSS and JavaScript in a java application?
I would like to use Java + JGit for models, Java for controllers and HTML + CSS + JavaScript for views.
I don't want a client-server model. I would like to integrate Java and HTML nicely. A DOM event would fire events directly to a Java controller. This way it would be possible to create rich offline application.
You can embed web browser component into your Java Swing/JavaFX Desktop application that displays GUI built with HTML5+CSS+JavaScript. You can see an article that describes how to do this at https://jxbrowser-support.teamdev.com/docs/tutorials/cross-desktop-apps.html
One of the Java Swing/JavaFX libraries that allows embedding Chromium into Java applications is JxBrowser. Using JxBrowser API you can load any web page and work with its DOM and JavaScript. You can even call Java methods from JavaScript code and vice versa. For example:
import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.JSFunctionCallback;
import com.teamdev.jxbrowser.chromium.JSObject;
import com.teamdev.jxbrowser.chromium.JSValue;
import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent;
import com.teamdev.jxbrowser.chromium.events.LoadAdapter;
public class JavaScriptJavaSample {
public static void main(String[] args) {
Browser browser = new Browser();
browser.addLoadListener(new LoadAdapter() {
#Override
public void onFinishLoadingFrame(FinishLoadingEvent event) {
if (event.isMainFrame()) {
Browser browser = event.getBrowser();
JSObject window = (JSObject)
browser.executeJavaScriptAndReturnValue("window");
window.setProperty("MyFunction", new JSFunctionCallback() {
#Override
public Object invoke(Object... args) {
for (Object arg : args) {
System.out.println("arg = " + arg);
}
return "Hello!";
}
});
JSValue returnValue = browser.executeJavaScriptAndReturnValue(
"MyFunction('Hello JxBrowser!', 1, 2, 3, true);");
System.out.println("return value = " + returnValue);
}
}
});
browser.loadURL("about:blank");
}
}
It's not really feasible. Rich clients in Java are done using Swing or SWT.
If you want to use HTML/CSS for your user interface, you need to use the server/client model. It can be as simple as creating a local server and launching a browser that connects to it, but it would still be that model.
If you absolutely need to have HTML/CSS as your UI framework and can't go to a server/client model, your best bet is probably looking at something like Google Native Client, but that uses C/C++ bindings on the backend. I haven't used Native Client so I can't personally give much more information on that front.
Edit to add:
One option is to embed a native browser into your Swing app using something like: http://djproject.sourceforge.net/ns/
There are some pure Java HTML renderers, however, they most likely won't be fully HTML5/CSS3 compliant, let alone possibly have Javascript bugs as well.
See here for some of those options: Pure Java HTML viewer/renderer for use in a Scrollable pane
Like #Reverand Gonzo says, you will need some form of server/client. But you could easily embed a Jetty server into a Java app and then use GWT for your client code.
You can bring in the power of HTML,CSS,JavaScript into your Swing app using JFXPanel to embed JavaFX WebView. Have a look at the SimpleSwingBrowser demo in this link:https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/swing-fx-interoperability.htm
WebView allows to call JavaScript functions from Java and vice versa. It is also a nice way to enhance your legacy Java app with web technologies.
JavaFX 2.2 brought this functionality to providing a user interface component (GUI) that has web view and full browsing functionality.
For more details, see Adding HTML Content to JavaFX Applications.
Use Angular.js with HTML and rest of the things as same in Java, just use classes for business logic, no need to write code for awt/swing. Angular with spring boot are rapid development in Java for webapp with less code in Java without swing use to create best webapp .