Java awt Robot still cant press non-numpad arrows on windows? - java

This bug is known for years, yet is is still present in Java 1.7.0_25 version which I'm using on Windows 8. The following result are same regardless of wether i have numlock turned on or not:
Robot bot = new Robot();
bot.keyPress(KeyEvent.VK_UP); //this in documentation is non-numpad up arrow key
bot.keyRelease(KeyEvent.VK_UP); //pressed the numpad up arrow key
//folowing line is line #43
bot.keyPress(KeyEvent.VK_KP_UP); //this in documentation is numpad up arrow key
bot.keyRelease(KeyEvent.VK_KP_UP); //causes folowing exception:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid key code
at sun.awt.windows.WRobotPeer.keyPress(Native Method)
at java.awt.Robot.keyPress(Robot.java:358)
at test.RobotArrow.main(RobotArrow.java:43)
I know this question was already asked here but over a year ago, so is there any progress? I cant google anything, there is even an ofiicial bug report
So, is there finnaly a solution or not?

//PRESS WINDOWS + ARROW LEFT
Robot divideWindow = new Robot();
divideWindow.keyPress(KeyEvent.VK_WINDOWS);
divideWindow.delay(100);
divideWindow.keyPress(KeyEvent.VK_LEFT);
divideWindow.delay(100);
divideWindow.keyRelease(KeyEvent.VK_LEFT);
divideWindow.delay(100);
divideWindow.keyRelease(KeyEvent.VK_WINDOWS);
Works fine for me :)

A possible workaround is to disable numlock. See this jdk bug comment

Related

Problems with touch input and OpenJDK 11 with JavaFX 11

I'm working on a JavaFX project and would like to switch from Oracle JDK 1.8 to OpenJDK 11. So far the transition has been pretty seamless, but there is still one main problem related to touch/mouse input that's causing some trouble.
The JavaFX UI is supposed to run on a touch-enabled device, which used to work straight out of the box with Oracle JDK 1.8. When I touch the screen, the following sequence of mouse events is fired as expected:
MOUSE_PRESSED
MOUSE_RELEASED
MOUSE_CLICKED
After building the same application with OpenJDK11 (using OpenJFX 11 as an external library as JavaFX is no longer part of the JDK by default) I get the follwing sequence of events:
MOUSE_ENTERED_TARGET
MOUSE_ENTERED_TARGET
MOUSE_EXITED_TARGET
MOUSE_EXITED_TARGET
This explains why I can't click any buttons (or controls in general). So far so good. The question is, how do I get my MOUSE_{PRESSED,RELEASED,CLICKED} events back?
SSCE:
package com.example.jfxtouchtest;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TouchEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class JFXTouchTest {
public static void main(String[] args) {
Application.launch(JFXApp.class, args);
}
public static class JFXApp extends Application {
#Override
public void start(Stage primaryStage) {
primaryStage.addEventFilter(TouchEvent.ANY, e -> System.out.println("touch event: " + e.getEventType()));
primaryStage.addEventFilter(MouseEvent.ANY, e -> System.out.println("mouse event: " + e.getEventType()));
primaryStage.setScene(new Scene(new Pane()));
primaryStage.setWidth(800);
primaryStage.setHeight(600);
primaryStage.show();
}
}
}
I think it's worth noting that all fired events are MouseEvents (not TouchEvents), regardless of whether I'm using the touchscreen or not. That in itself is sort of strange in my opinion, but at least I'm getting the desired behaviour with JDK 8...
Some background information:
OS: Ubuntu 18.04.01 LTS
Kernel: 4.15.0-42-generic
Oracle JDK 1.8.0_191
OpenJDK 11.0.1
Touchscreen (as reported by xinput): Atmel maXTouch Digitizer
The touchscreen works just fine with other applications, click events seem to be handled as expected.
The somehwat related VM-arguments
-Dcom.sun.javafx.isEmbedded=true and
-Dcom.sun.javafx.touch=true
both seem to have no effect on the issue
There seems to be a slight difference in the xev output I'm getting depending on whether I'm using the mouse or the touchscreen:
Mouse (state is 0x0 for ButtonPress, 0x100 for ButtonRelease):
ButtonPress event, serial 34, synthetic NO, window 0x3400001,
root 0x193, subw 0x0, time 16982696, (93,90), root:(964,612),
state 0x0, button 1, same_screen YES
ButtonRelease event, serial 34, synthetic NO, window 0x3400001,
root 0x193, subw 0x0, time 16983364, (93,90), root:(964,612),
state 0x100, button 1, same_screen YES
Touchscreen (state is 0x100 in both cases):
ButtonPress event, serial 34, synthetic NO, window 0x3400001,
root 0x193, subw 0x0, time 17599475, (93,145), root:(964,667),
state 0x100, button 1, same_screen YES
ButtonRelease event, serial 34, synthetic NO, window 0x3400001,
root 0x193, subw 0x0, time 17599537, (93,145), root:(964,667),
state 0x100, button 1, same_screen YES
I'm not exactly sure what this means, though.
Any help would be greatly appreciated, even if it's just a confirmation that the issue is reproducible on another machine with another type of touchscreen! Many thanks in advance!
UPDATE: I have managed to get my hands on a different touchscreen in the meantime, and it seems to work fine with that one. What's interesting is that, just like with regular mouse events, xev reports two different states for ButtonPress and ButtonRelease, so maybe the state field being the same for both event types on the other touchscreen has something to do with this after all?
I had the same problem with my touch screen and JFX. My code works fine with Open JDK 1.8 and its corresponding JFX, it fails with OpenJDK 11 and its corresponding JFX. It works fine with the JDK and JFX from Liberica https://bell-sw.com/pages/java-11.0.7-for-Embedded/
So for me my workaround was to change to the Liberica JDK 11 and JFX distribution.
Other options may be JDK, JFX distributions from Azul or Corretto.
Force JavaFX to use gtk-2 by using the java parameter -Djdk.gtk.version=2. The touchscreen works with that setting although this causes other problems with our application (possibly related to 3rd party libraries).

How to keyDown SHIFT + "w" while clicking - selenium java

I'm trying to open a link in a new window using selenium with Chrome driver. I'd like to use keyDown to hold SHIFT and "w" while clicking the webelement. These attempts have not worked:
Actions act = new Actions(driver);
Action series = act.keyDown(englishButton, Keys.SHIFT).keyDown(englishButton, "w").click(englishButton).build();
series.perform();
The ".keyDown(englishButton, "w")" portion gives me an error there. And I've also tried using java robot:
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_W);
englishButton.click();
but after importing Robot, KeyEvent wasn't recognizing VK_SHIFT or VK_W as valid entries.
First, in Google Chrome, to open a link in a new tab you don't need to hold down W, just SHIFT.
Second, .keyDown(englishButton, "w") shouldn't compile because it takes an org.openqa.selenium.Keys instance (not a char). If you want to send characters you should use Actions.sendKeys(CharSequence...) (it doesn't release the modifier keys unlike WebElement.sendKeys(CharSequence...)):
Action series = act.sendKeys(Keys.SHIFT, "w").click(englishButton).keyUp(Keys.SHIFT).build()
This should first key-down on SHIFT+W, then click your button/link, and then release SHIFT (as W isn't a modifier key you don't need to release it).
And if you don't care about holding down W but just SHIFT then you can try the following:
Action series = act.keyDown(Keys.SHIFT).click(englishButton).keyUp(Keys.SHIFT).build()
This should simply key-down SHIFT, click your button/link, and then release SHIFT.
"w" button should be "pressed" using sendKeys() method. But as #mfulton26 has mentioned, there is no need to do it if you want to open a new tab in Chrome. Also there is no need to use englishButton as a first argument in keyDown() or keyUp() methods. Please take a look:
Actions act = new Actions(driver);
Action series = act.
keyDown(Keys.SHIFT). // Press Shift key
click(englishButton). // Click the link
keyUp(Keys.SHIFT). // Release Shift key
build(); // Build the chain of actions
series.perform();
Hope this helps.

Printing in android studio - folding sections

When I was printing java code in Eclipse, the way of printing was WYSIWYG. So if I folded some selected sections (for example import section, but also any other foldable section), it was printed as folded.
In Android Studio code is printed allways fully unfolded. Does anybody know some way (plugin or some preferences setting etc.), how to set it to print also in the folding WYSIWYG style?
My problem is that I have done the printing part in android device when the printer is already configured on the device.
If the printer is not configured on the device, the exception occurs can you tell that how u know that printer is configured on device
e1=(EditText) findViewById(R.id.editText);
String name=e1.getText().toString();
PrintManager printManager=(PrintManager) this.getSystemService(Context.PRINT_SERVICE);
//PrintDocumentAdapter printAdapter = view.createPrintDocumentAdapter();
//Toast.makeText(getApplicationContext(),"hi nikhil"+name+""+printManager.getPrintJobs(),Toast.LENGTH_LONG).show();
String text=this.getString(R.string.app_name)+"Document";
// printManager.getPrintJobs();
printManager.print(text, new MyPrintDocumentAdapter(this), null);
Log.d("print", "print" + printManager);
on Android-Studio 4.0, I noticed that you can select the parts of a file you wish to print, and in the print dialog box, it will allow you to just print the selection. This isn't exactly what you're looking for, but it is a way to bypass the long lines of import statements and comments at the head of a file.

Can Selenium WebDriver open a link in a new tab from the current window [duplicate]

I want to be able to open a link in a new tab in Selenium 2. Also i want to close the tab when i am finished interacting with the page. How is this possible if I have a WebElement of an <a> tag?
I am using the Java API of Selenium 2 with the Firefox Driver, running on Firefox 4.
The way I figure out for selenium 2, work fine for Chrome and firefox, IE has security check issue:
Set<String> winSet = webDriver.getWindowHandles();
List<String> winList = new ArrayList<String>(winSet);
String newTab = winList.get(winList.size() - 1);
webDriver.close(); // close the original tab
webDriver.switchTo().window(newTab); // switch to new tab
At the moment, the Selenium WebDriver API doesn't have any way of handling tabs. The project would really need a consistent, cross-browser set of methods for managing tabs before I would expect to see an implementation in one of the language bindings like Java. Until then, your JavaScript solution may be the only way, and remember that your code would then be responsible for managing the lifetime of that tab.
to use selenium at its best we at sol-logics combine it with java.awt.robot class. you can send keys that can close a browser window. try using
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_W);
and reply if it works
Took awhile (~2 weeks) for me to track down the right sequence of commands, but this is by far the easiest method I've found for a Win7/Chrome setup to open a link in a new tab AND switch to the new tab automatically.
WARNING! Make sure to always perform the keyUp actions. If you fail to perform keyUp your system will keep those keys pressed until a reboot or keyUp occurs.
Windows 7/Chrome:
WebElement elem = driver.findElement(By.linkText("MyLinkText"));
// Chrome key combos:
// SHIFT + CTRL + click = Open in new tab (and switch to new tab)
// SHIFT + CTRL + RETURN = Open in new tab (in background)
Actions act = new Actions(driver);
act.keyDown(Keys.LEFT_CONTROL).keyDown(Keys.LEFT_SHIFT).perform();
// Wrap in a try/catch during implementation to ensure you perform keyUp(s).
elem.click();
act.keyUp(Keys.LEFT_CONTROL).keyDown(Keys.LEFT_SHIFT).perform();
Note: I know it's an old thread, I just wanted to catalog the solution here because I couldn't find a more elegant solution and wanted to save someone else a little time (hopefully :).
Edit: Typo
Here is how i did it using Python.
This solution is a bit dirty but it works if you want to close the tab.
Im mimicking the mac shortcut CMD + W to close a tab, if you are running windows you may have to implement a different key combination.
import from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.amazon.com/gp/search/ref=sr_in_-2_p_lbr_brands_browse-_2895?rh=n%3A172282%2Cn%3A!493964%2Cn%3A502394%2Cp_lbr_brands_browse-bin%3ALytro")
action_chains = ActionChains(driver)
action_chains.key_down(Keys.COMMAND + "w")
action_chains.perform()
action_chains.key_up(Keys.COMMAND + "w")
driver.implicitly_wait(5)
What I use is the Robor class.
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_W);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_W);
This makes the Robot rapidly press and release the CTRL + W keys to simulate a user interaction. If you only use keyPress event, this will close all the tabs and windows of the WebDriver.
Hope I helped you.

Open and maximise a browser window in OATS Java (not javascript)

I am a tester and just installed oracle application test suite to use testing eBus apps
Anyway the only language it supports for coding test scripts (I don't want to use the recorder for a number of reasons). The problem I am having is that everything I search or google is javascript not java (even googling with -script I still ended up looking at javascript. This just gets rejected by the oats editor
The only other examples I have seen, appear to be defining a variable then setting the value of that variable as the window they want to maximize. Aside from the fact that my java skills are not up to doing that - I do not need to do this for a newly opened browser window do I? (The assumption is that this will be the only browser window open (ie test is executed with browser closed)
Is there any easy way to do this?
Below is the very simple initiate of the browser which is generated from a recording plus part of the first step which loads the url the test starts at: (I realize the first step is not complete below -I didn't paste it all, just enough to hopefully allow someone to show me what I need to edit to force the browser to load maximized, or maximize it immediately after loading?
public void initialize() throws Exception {
browser.launch();
}
/**
* Add code to be executed each iteration for this virtual user.
*/
public void run() throws Exception {
beginStep("[1] Login (/RF.jsp)", 0);
{
web
.window(2,
"/web:window[#index='0' or #title='about:blank']")
.navigate(
"http://somepageiwantolaunch");
web.window(4, "/web:window[#index='0' or #title='Login']")
.waitForPage(null);
I am not sure whether you already got the answer for this.. if not this code should help you
browser.launch();
DOMBrowser currentExecutionBrowser = web.window("/web:window[#index='0' or #index='1']");
currentExecutionBrowser.maximize();
Let me know if this helps!
There is a function in the Oracle Functional Tester API Reference which has a build in function called object.WindowState It says you can get or set using this function and it has values
0 - Normal, 1- minimized and 2-maximised.
Only issue is that these examples look more like VB than Javascript but presumably there is a similar function built into to the Oracle libraries for Java.
I did a quick search for Oracle Openscript API and came up with this link which asks for the same thing. They suggest using Help->Search from within the openscript application and then searching for "openscript API" which should provide a list of the functions available.
Hope that helps.
To Maximize browser in OATS, follow the below code
Open script ha in built methods which helps coding easy
browser.launch();
web.window(12, "/web:window[#index='0' or #title='about:blank']").navigate("http://www.google.com/");
web.window(12, "/web:window[#index='0' or #title='about:blank']").maximize();
for more OATS Tips/Tricks follow here
http://www.testinghive.com/category/oracle-application-testing-suite-tips
If it is the only browser window open, you can use the below code. It must be used with caution since the code maximizes any window that is open above the browser window.
try {
Robot a = new Robot();
a.keyPress(KeyEvent.VK_ALT);
a.keyPress(KeyEvent.VK_SPACE);
a.keyRelease(KeyEvent.VK_SPACE);
a.keyRelease(KeyEvent.VK_ALT);
a.keyPress(KeyEvent.VK_X);
a.keyRelease(KeyEvent.VK_X);
} catch (AWTException e) {
}

Categories