I use the following lines to simulate a Control_A [ select all ] key action in Java with robot, but the clipboard is not getting the text, why ?
Robot robot=null;
try { robot=new Robot(); }
catch (AWTException ex) { System.err.println("Can't start Robot: " + ex); }
robot.mouseMove(260,500);
robot.mousePress(InputEvent.BUTTON1_MASK);
// robot.mouseMove(660,700);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.keyPress(KeyEvent.VK_CONTROL); // Select all
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_CONTROL); // Copy
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_CONTROL);
Transferable t=Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
try
{
if (t!=null && t.isDataFlavorSupported(DataFlavor.stringFlavor))
{
String text=(String)t.getTransferData(DataFlavor.stringFlavor);
System.out.println(text);
}
}
catch (Exception ex) { ex.printStackTrace(); }
I have a browser open so at [260,500] on screen there is text at that area. What have I missed ?
Edit:
I just found something strange, when I opened a browser, the text in the browser is not copies, but if I open a notepad/wordpad, the text in them will be copies, so why the browser didn't do it ?
All of your code should be inside the try block where you instantiate the Robot because you could end up trying to work with a null reference and get a NullPointerException. And if your Robot never got created and never copied contents, there'd be no point in you trying to access the contents from the clipboard either.
I'm not entirely sure why but adding a small delay before trying to read from the clipboard fixes things. I'm guessing it might have to do with a race condition between Java getting hold of the clipboard before the system has had time to update it.
This updated code should work:
Robot robot = null;
try
{
robot = new Robot();
robot.mouseMove(260, 500);
robot.mousePress(InputEvent.BUTTON1_MASK);
// robot.mouseMove(660,700);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.keyPress(KeyEvent.VK_CONTROL); // Select all
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_CONTROL); // Copy
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_CONTROL);
try
{
//sleep just a little to let the clipboard contents get updated
Thread.sleep(25);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
try
{
if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor))
{
String text = (String) t.getTransferData(DataFlavor.stringFlavor);
System.out.println(text);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
catch (AWTException ex)
{
System.err.println("Can't start Robot: " + ex);
}
Not sure of your code context but I had the same problem trying to extract the text from a PDF document on the browser.
It was misleading because the ctrl-a was highlighting the text but ctlr-c copied nothing. My solution was first to simulate a click anywhere on the document first, then ctrl-a and ctrl-c. My code:
robot = new Robot();
//Get window size
Dimension d = driver.manage().window().getSize();
System.out.println("Dimension x and y :"+d.getWidth()+" "+d.getHeight());
int x = (d.getWidth()/4)+20;
int y = (d.getHeight()/10)+50;
robot.mouseMove(x,y);
//Clicks Left mouse button
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(25);
// Select all
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);
Thread.sleep(100);
// Copy to clipboard
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_CONTROL);
Thread.sleep(100);
Hope this helps.
Related
I have an Error and I don't get it. I am not getting an Exception.
I have to copy a text with the Robot.
// mark the text
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_A);
robot.delay(1000);
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);
// robot copy the text
robot.keyPress((KeyEvent.VK_CONTROL));
robot.keyPress((KeyEvent.VK_C));
robot.delay(1000);
robot.keyRelease((KeyEvent.VK_C));
robot.keyRelease((KeyEvent.VK_CONTROL));
then I get the Text via Clipboard
txt = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
After that, I want to set the text to the Clipboard, so I can put it in the text field again with the Robot.
StringSelection stringSelection = new StringSelection(txt);
clipboard.setContents(stringSelection, stringSelection);
// robot mark the hole text
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_A);
robot.delay(1000);
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);
// robot put the text in
robot.keyPress((KeyEvent.VK_CONTROL));
robot.keyPress((KeyEvent.VK_V));
robot.delay(1000);
robot.keyRelease((KeyEvent.VK_V));
robot.keyRelease((KeyEvent.VK_CONTROL));
This is everything in a loop
while (j < liUnderElementList.size()) {}
The first time it works as expected but on the second pass, I get the first copied text and not the new text.
BUT in my Clipboard is the new Text.
If i wait with
TimeUnit.SECONDS.sleep(2)
nothing changes.
Anyone can help me?
I would be very thankfull.
Seems issue with the order of keys, in the way you are performing. Release event should be from last one. Use below one:
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_A);
robot.delay(1000);
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);
There are many questions which are similar to this has been asked in Stackoverflow. But this is something different
I am now currently automating an web application in which at the end of the application, PDF will be generated (Which will be displayed as a link, on clicking it PDF will open in a new window).
I am now currently using AUTOIT to automate Save AS dialog box. Is there any other way to download without using AutoIT.
I tried fetching the PDF URL from the link in the application under tag, but it is actually invoking a JS to open a PDF in new Window
In new window PDF is actually in Embed tag in which src is about::blank
I fetched PDF complete src but when I used that URL without clearing any cookies, I am unable to get the PDF
Is there any suggestions for this issue?
Upload File Using ROBOT Class
public String imagePath = System.getProperty("user.dir") +"\\src\\test\\java\\filepath.pdf";
public void uploadFileWithRobot(String imagePath) {
StringSelection stringSelection = new StringSelection(imagePath);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
robot.delay(250);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.delay(150);
robot.keyRelease(KeyEvent.VK_ENTER);
}
Now Click on PDF and its open in new window than Verify text is present or not in PDF
public String readPDFInURL(String text) throws EmptyFileException, IOException {
System.out.println("Enters into READ PDF");
String output = "";
URL url = new URL(driver.getCurrentUrl());
System.out.println("url : " + url);
InputStream is = url.openStream();
BufferedInputStream fileToParse = new BufferedInputStream(is);
PDDocument document = null;
try {
document = PDDocument.load(fileToParse);
output = new PDFTextStripper().getText(document);
if (output.contains(text)) {
System.out.println("Element is matched in PDF is : " + text);
test.log(LogStatus.INFO, "Element is displayed in PDF " + text);
} else {
System.out.println("Element is not matched in PDF");
test.log(LogStatus.ERROR, "Element is not displayed in PDF :: " + text);
throw new AssertionError("Element is not displayed" + text);
}
} finally {
if (document != null) {
document.close();
}
fileToParse.close();
is.close();
}
return output;
}
Yes. You can download a file without using third party like AutoIT. Below is the code that works perfectly fine,
First you have to switch to the window.
Then apply some wait. So that the page will be loaded completely.
Now you have to trigger key events i.e. ctrl + s
Pop-up will be appeared on the page to save your file.
Just hit enter. So that you can save your file.
String str = TestUtil.switchToWindow();
pause(3000);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(2000);
Robot robot = new Robot();
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_CONTROL);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_TAB); // file replace move to yes button
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_ENTER);
TestUtil.switchBackToParentWindow(str);
All operations are:
1, click the windows logo to open the "All Program"
2, click "All Program"
3, move mouse up to the program list
4, wheeling down
Robot robot = new Robot();
robot.mouseMove(35,1062); //click windows logo
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(1000);
System.out.println("open windows");
robot.mouseMove(70,947);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
System.out.println("open all program");
robot.delay(1000);
robot.mouseMove(88,510);
System.out.println("move to chrome");
robot.delay(1000);
for(int index = 0; index < 10; index++){
robot.mouseWheel(1);
robot.delay(1000);
}
but the mouseWheel doesn't work.
I tested your code and robot.mouseWheel works correctly.
Its problem may be of not being active window in your situation.
But I recommend use following code to open chrome and search in it:
Robot robot = new Robot();
//-- open start menu
robot.keyPress(KeyEvent.VK_WINDOWS);
robot.keyRelease(KeyEvent.VK_WINDOWS);
robot.delay(1000);
//-- type ch to find chrome
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_C);
robot.keyPress(KeyEvent.VK_H);
robot.keyRelease(KeyEvent.VK_H);
robot.delay(1000);
//-- press Enter to Open it
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(1000);
//-- type hi to search
robot.keyPress(KeyEvent.VK_H);
robot.keyRelease(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_I);
robot.keyRelease(KeyEvent.VK_I);
robot.delay(1000);
//-- press Enter to search it
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
To scroll down on programs in windows 10 I wrote the following code and it works Correctly on my computer with 1920x1080 resolution (but using it is not recommended because the manner may differ in various condition):
Robot robot = new Robot();
//-- open start menu
robot.keyPress(KeyEvent.VK_WINDOWS);
robot.keyRelease(KeyEvent.VK_WINDOWS);
robot.delay(1000);
//-- to move on programs
robot.mouseMove(250,700);
//-- scroll down
for(int index = 0; index < 10; index++) {
robot.mouseWheel(1);
robot.delay(1000);
}
I am a beginner in java, and I am trying to write a simple screen-capture program. I wrote a simple SWING desktop app with a button and a text-field, and what I am trying to do is, when a user clicks that button the app takes a snapshot of the screen using awt.Robot, and sends that image and the text to a PHP script on my server.
My snapshot function so far is:
private void takeSnapShot(){
try {
Robot robot = new Robot();
Rectangle area = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage bufferedImage = robot.createScreenCapture(area);
//Try to save the captured image
try {
File file = new File("screenshot_full.png");
ImageIO.write(bufferedImage, "png", file);
} catch (IOException ex) {
Logger.getLogger(ScrCaptFrm.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (AWTException ex) {
Logger.getLogger(ScrCaptFrm.class.getName()).log(Level.SEVERE, null, ex);
}
}
As you can see it's fairly simple so far, however I am not sure how to send that image to my PHP script without actually storing the image on user's PC.
Oh and I am using apache httpClient library for communicating to the web server. For the text I guess I can pass it in the URL as a get query, but I am not sure what to do about the image.
ImageIO.write can to an OutputStream of your choice.
So if you don't want to write the image to a File, you can simply write it to a different stream instead...
For example...
OutputStream os = null;
try {
Robot robot = new Robot();
Rectangle area = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage bufferedImage = robot.createScreenCapture(area);
//Try to save the captured image
try {
os = ...;
ImageIO.write(bufferedImage, "png", os);
} catch (IOException ex) {
Logger.getLogger(ScrCaptFrm.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
os.close();
} catch (Exception exp) {
}
}
} catch (AWTException ex) {
Logger.getLogger(ScrCaptFrm.class.getName()).log(Level.SEVERE, null, ex);
}
Of course, I have no idea where you're sending the data, so you'll need to define the OutputStream yourself.
If you have the memory for it, you could write it a ByteArrayOutputStream and then write this to whatever output stream you need in the future...
To slightly modify your existing method, perhaps you could use a temporarily file and then delete it when you are finished with it. Perhaps it might look something like:
private void takeSnapShot(){
try {
Robot robot = new Robot();
Rectangle area = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage bufferedImage = robot.createScreenCapture(area);
//Try to save the captured image
try {
File file = File.createTempFile(Long.toString(System.currentTimeMillis()), ".png");
ImageIO.write(bufferedImage, "png", file);
//send image
file.delete();
} catch (IOException ex) {
Logger.getLogger(ScrCaptFrm.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (AWTException ex) {
Logger.getLogger(ScrCaptFrm.class.getName()).log(Level.SEVERE, null, ex);
}
}
Another alternative would be to construct an int[][] from your BufferedImage which will hold the RGB values for every pixel of the image:
public int[][] getColors(final BufferedImage image){
assert image != null;
final int[][] colors = new int[image.getWidth()][image.getHeight()];
for(int x = 0; x < colors.length; x++)
for(int y = 0; y < colors[x].length; y++)
colors[x][x] = image.getRGB(x, y);
return colors;
}
I am a little unsure about what you hope to achieve; What do you plan on doing with the image?
Why don't you make this a WebsService and let your PHP consume it? You could send the binary data through the WebsService using some sort of Base64 encoder.
You could do this to get the bytes of the BufferedImage:
byte[] binaryData = ((DataBufferByte) bufferedImage.getData().getDataBuffer()).getData();
How do I move the mouse cursor (windows) with Java?
I have a pair of values constantly being updated. I want to use them to control the cursor.
Robot.mouseMove(x,y)
As mentioned in:
https://stackoverflow.com/a/2941373/1150918 by OscarRyz.
You will need to use the Robot class.
Robot r = null;
try {
r = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
r.mouseMove(x, y);//x is the horizontal position on the screen, y is the vertical