java: how to send accelerator key - java

I am using Junit4 under eclipse.
I would like to write a test which can be able to send the action : ctrl+shift+P
I tried this using JTable as I don't know for which component I could use the sendAcceleratorKey :
myTable.sendAcceleratorKey(InputEvent.CTRL, InputEvent.SHIFT_DOWN_MASK)
but I can't add a third argument to say KeyEvent.P.
How can I send this action which changes the menu?
Thanks!

I guess you can use the Robot class.
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_P);
Thread.sleep(1000); // Time for your code to react to the event
assert(...);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_P);

I can't find any reference to sendAcceleratorKey(). But if it really exists and it does what you want, it looks logic to me to use the the method this way, using key modifiers:
myTable.sendAcceleratorKey(InputEvent.CTRL | InputEvent.SHIFT_DOWN_MASK,
KeyEvent.VK_P);
Otherwise, try to swap the parameters, depending on the methods signature.
myTable.sendAcceleratorKey(KeyEvent.VK_P,
InputEvent.CTRL | InputEvent.SHIFT_DOWN_MASK);

Related

How to open search box on web page using keyboard key (ctrl-f) in selenium web driver

How to open search box using windows keys ctrl+f in selenium webdriver:
Selenium doesn't provide a possibility to simulate keyboard actions. But to be able to do it, you can use a Robot class in Java.
You have not specified on which language are you developing. Thats why I have stored some useful links to different analogues of Robot:
For Python
For C#
FIRST INSTALL PYWINAUTO BY run pip install pywinauto in your python ide
from selenium import webdriver
from pywinauto.keyboard import SendKeys
import time
driver = webdriver.Chrome(executable_path="PATH OF CHROMEDRIVER")
driver.maximize_window()
driver.get("https://www.google.com")
time.sleep(5)
SendKeys("^F")
Finally, guys, I have found a solution, It is working fine for me, Thanks Andrei Suvorkov to suggest me read about robot class.
try {
Robot robot = new Robot();
// Simulate a mouse click
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
// ctrl + F
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_F);
// CTRL+F is now pressed
robot.keyRelease(KeyEvent.VK_F);
robot.keyRelease(KeyEvent.VK_CONTROL);
} catch (AWTException e) {
e.printStackTrace();
}

How to close a dialog box using Java, that pops up during runtime?

I am trying to make a project that performs OCR (image to text conversion).
I am using the AspriseOCR jar file and i set up my project accordingly.
It works fine.
Whenever i run the project, when the call is made to aspriseOCR API, a dialog box pops up in windows asking if i want to go to their website. I need to automate the whole process. So i dont want this dialog box to appear.
Is there any way by which i can close this dialog box in my code itself, in case it appears? Is there a way i can press the Enter button (it will close accordingly). I am using Java, Eclipse.
Note: This is not on web browsers. So I cannot use Selenium related commands.
Edit:-
I tried to use this :-
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
But it wouldn't solve my problem as I need to add this in the API source code, which I do not have access to. Is there any other approach or method to close the dialog box?
So a part of the code is :-
`{
String s = ocr.recognize( new File[] {new File("C:\\work\\"+file)},
Ocr.RECOGNIZE_TYPE_ALL, Ocr.OUTPUT_FORMAT_PLAINTEXT);
Robot robot;
robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
System.out.println("\n\n pressed enter\n\n");
System.out.println("Result:\n\n"+s);
ocr.stopEngine();
}`
So when the control goes to the function ocr.recognize(...) the dialog box comes up and I do not have access to that source code which is present in that jar file.
Is there any way that I can parallelly run another thread which waits for the opening of this dialog box and closes it ( this thread parallelly running with ocr.recognize). Kindly let me know how to identify if a windows dialog box has been opened using java code.
You can use Robot class for this purpose as below:
//if you want to press alt+f4; to close window or
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_F4);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_F4);
// to press enter
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

function keys jsch (Java)

I use jsch for connect to a router Cisco and execute somme commands. But in some cases it ask me to press function keys as F1, F2...
I want to know if this is possible and how?
Thanks a lot!!!
Check Robot and KeyEvent. The second class has static int fields representing almost every key on the keyboard. F1 is VK_F1, F2 is VK_2 ... and so on.
To send a 'F1' key stroke use this :
Robot robot = new Robot();
robot.keyPress(VK_F1);

How to press "ALT+S" in Selenium WebDriver using Java

I need to Send ALT+S Key event using Selenium Web Driver for an ``EditBox. Cursor Position is already set to EditBox I am using following code
driver.switchTo().activeElement().sendKeys(Keys.chord(Keys.ALT+"S"))
but it's not giving me desired result. It's Typing character 'S' in the Edit Box.
I have tried another code but got the same result.
Actions action =new Actions(driver);
action.keyDown(Keys.ALT).sendKeys(String.valueOf('\u0053')).perform();
Thanks in Advance
I want to Add one more thing here. The code is working Properly in Firefox 12 but its not working properly in IE9
Cross-browser issues are rather hard to investigate as they are specific to particular driver and not WebDriver API.
Another variant that might work.
driver.findElement(By.xpath("your editbox's XPath")).sendKeys(Keys.chord(Keys.ALT, "s"));
As workaround I might recommend to take a look to AutoIT (Official site) or Robot (Java Doc)
Try this. It might work, I haven't tried though
driver.findElement(By.xpath("your editbox's XPath"))
.sendKeys(Keys.chord(Keys.ALT + Keys.S));
You can achieve this by using Robot class of java
try{
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_ALT);
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_S);
}
catch(Exception ex){
System.out.println(ex.getMessage());
}

Robot - java.lang.NullPointerException

I'm trying to create an object of the Robot Class. However, I'm getting ' java.lang.NullPointerException' when trying to do the same.
static Robot robot = null;
try {
robot = new Robot();
if(keyAction.equals("TABPRESS")){
robot.keyPress(KeyEvent.VK_TAB);
}else if(keyAction.equals("TABRELEASE")){
robot.keyRelease(KeyEvent.VK_TAB);
}
}catch(AWTException e){
e.printStackTrace();
}
Can you please suggest how to handle this? This code was functioning properly earlier.
Although you have not sent the code that creates keyAction this is the only thing that can cause NPE in this code fragment. Check it first.
Moreover the better practice to call equals() method is to call it on constant and pass other object as a parameter. This is null-safe:
if("TABPRESS".equals(keyAction)){
robot.keyPress(KeyEvent.VK_TAB);
}else if("TABRELEASE".equals(keyAction)){
robot.keyRelease(KeyEvent.VK_TAB);
}
I have figured out the solution. It was not because of any programming issue. Actually, there was a clash of Jre Versions on my system. I uninstalled/deleted all the jdks and installed a new one and everything started working fine.
Thanks, Shreyas.

Categories

Resources