Search code examples
javaselenium-webdrivertestngawtrobot

Unable to write in Windows Run prompt through Selenium


I need to access one shared path through selenium Webdriver but I am unable to write that shared path "\\18.187.980.12\\Logs\\abc.log" in Windows Run prompt through Selenium. I am using Robot classes to open run prompt

    Robot robot = new Robot();  // Robot class throws AWT Exception  
    Thread.sleep(2000); 
    robot.keyPress(KeyEvent.VK_WINDOWS);    
    Thread.sleep(2000);
    robot.keyPress(KeyEvent.VK_R);  
    Thread.sleep(2000);

This code is opening run prompt but I am unable to write the shared path "\\18.187.980.12\\Logs\\abc.log" in run prompt. Please suggest next step. My new Code is given below:

  package ForNewFramework;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

class RobotClass {

    Robot robot = new Robot(); // Robot class throws AWT Exception

    public static void main(String[] args) throws AWTException,
            InterruptedException {
        // WebDriver driver = new FirefoxDriver();
        new RobotClass();
    }

    public RobotClass() throws AWTException {

        robot.keyPress(KeyEvent.VK_WINDOWS); // press arrow down key of keyboard
                                                // to navigate and select Save
                                                // radio button
        robot.keyPress(KeyEvent.VK_R); // press arrow down key of keyboard to
                                        // navigate and select Save radio button
        type("Prateek");

    }

    private void type(String s) {
        byte[] bytes = s.getBytes();
        for (byte b : bytes) {
            int code = b;
            // keycode only handles [A-Z] (which is ASCII decimal [65-90])
            if (code > 96 && code < 123)
                code = code - 32;
            robot.delay(40);
            robot.keyPress(code);
            robot.keyRelease(code);
        }
    }
}

Solution

  • Well at the end of the day all strings are result of key presses so you can press the key in keyboard by passing the correct ASCII value (key code).

    Please find the entire code that you need:

    package ForNewFramework;
    
    import java.awt.AWTException;
    import java.awt.Robot;
    import java.awt.event.KeyEvent;
    
    class RobotClass {
    
        Robot robot = new Robot(); // Robot class throws AWT Exception
    
        public static void main(String[] args) throws AWTException, InterruptedException {
            // WebDriver driver = new FirefoxDriver();
            new RobotClass().runWindows("\\\\18.187.980.12\\\\Logs\\\\abc.log");
        }
    
    
        public RobotClass() throws AWTException, InterruptedException {
    
        }
    
        public void runWindows(String run) throws InterruptedException{
    
            robot.keyPress(KeyEvent.VK_WINDOWS); // press arrow down key of keyboard
            robot.keyPress(KeyEvent.VK_R); // press arrow down key of keyboard to
            robot.keyRelease(KeyEvent.VK_R);
            robot.keyRelease(KeyEvent.VK_WINDOWS);
            type(run);
            robot.keyPress(KeyEvent.VK_ENTER);
        }
    
        private void type(String s) throws InterruptedException  {
            Thread.sleep(2000); 
            byte[] bytes = s.getBytes();
            for (byte b : bytes)
            {
              int code = b;
              // keycode only handles [A-Z] (which is ASCII decimal [65-90])
              if (code >=65 && code<=90){
                  System.out.println(code);
                  robot.delay(1000);
                  robot.keyPress(KeyEvent.VK_SHIFT );
    
                  robot.keyPress(code);
                  robot.keyRelease(code);
                  robot.keyRelease(KeyEvent.VK_SHIFT);
    
    
              }else if (code > 96 && code < 123) {
                  code = code - 32;
                  System.out.println(code);
                  robot.delay(2000);
                  robot.keyPress(code);
                  robot.keyRelease(code);
              }
              else{
                  robot.delay(2000);
                  robot.keyPress(code);
                  robot.keyRelease(code);
              }
    
            }
    }
    }
    

    This code tries to open that directory and works perfectly, I have tested it.

    It took some time to figure out what was going wrong. You didn't release the keys, that's why it was not working for you and also you had to handle the different key codes that might come. This robot class only writes A-Z.

    Here is a list of key events which might come handy - http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html#VK_R