When We Should Use KeyPress Instead of setText in Open Script??

Last week I was testing an application which was based on servlet and client validation was on some javascript most likely ajax.

Scenario::
A customer needed to fill up a form for signup.As usual it was having First name,Last name,Address,City,Country, Mobile No.


What I did ??
I tried to record the scenario. While recording OATS recorded all the text filed with setText() method. Script looked cool.

But??
While playing back,It did fill up the form but application was throwing error. It was saying that all fields were mandatory and user needed to fill it.

Findings:
I noticed there was a ajax call which was sensing the key press event and trying to read the record on focus change.So manually when I tried to reproduce the case,application was behaving correctly. I changed the setText() method to keyPress() method and instead of clicking on the next field. I pressed TAB key. It worked!!!!.



  • I general form fill up, When we click on submit button,all text filed data go to server. In this case we can use setText()
  • In a case where application directly does not read the user input via a submit button. Rather it reads on keyPress or keyUp[Nothing but a javascript (ajax call).]And process data accordingly. We must use keyPress()
  • There should not be any space in data inside keyPress() method. Like keyPress("Test1234") is valid but keyPress("Test 1234") is Invalid
  • setText does a copy paste operation. So in such places where copy paste is prohibited we must use keyPress.
When We Should Use KeyPress Instead of setText in Open Script?? When We Should Use KeyPress Instead of setText in Open Script?? Reviewed by Animesh Chatterjee on May 19, 2015 Rating: 5

7 comments:

  1. How can a random value coming from databank be used with keyPress?

    ReplyDelete
  2. How to use inputs with space or some special charecters like @,_,. Ex: I have email id field where inputs contains (@) and (.) chars. I am able to handle @ by using "" in keypress method but unable to handle underscore and dot charecters.Please help

    ReplyDelete
  3. public void upload_parseChars(String letter, Robot robot) throws AbstractScriptException {
    for (int i = 0; i < letter.length(); i++) {
    //info("inside uf_Vik_parseChars method ");
    char chary = letter.charAt(i);
    //info("Current character = "+letter.charAt(i));
    upld_typeCharacter(Character.toString(chary), robot);
    }
    }

    ReplyDelete
    Replies
    1. public void upld_typeCharacter(String letter, Robot robot) throws AbstractScriptException {
      // info("Pressed event ");
      if (Character.isLetterOrDigit(letter.charAt(0))) {
      try {
      boolean upperCase = Character.isUpperCase(letter.charAt(0));
      String variableName = "VK_" + letter.toUpperCase();
      KeyEvent ke = new KeyEvent(new JTextField(), 0, 0, 0, 0, ' ');
      @SuppressWarnings("rawtypes")
      Class clazz = ke.getClass();
      Field field = clazz.getField(variableName);
      int keyCode = field.getInt(ke);
      robot.delay(80);
      if (upperCase)
      robot.keyPress(KeyEvent.VK_SHIFT);
      robot.keyPress(keyCode);
      robot.keyRelease(keyCode);

      Delete
    2. if (upperCase)
      robot.keyRelease(KeyEvent.VK_SHIFT);
      } catch (Exception e) {
      System.out.println(e);
      }
      } else {
      if (letter.equals("!")) {
      robot.keyPress(KeyEvent.VK_SHIFT);
      robot.keyPress(KeyEvent.VK_1);
      robot.keyRelease(KeyEvent.VK_1);
      robot.keyRelease(KeyEvent.VK_SHIFT);
      } else if (letter.equals("@")) {
      robot.keyPress(KeyEvent.VK_SHIFT);
      robot.keyPress(KeyEvent.VK_2);
      robot.keyRelease(KeyEvent.VK_2);
      robot.keyRelease(KeyEvent.VK_SHIFT);
      } else if (letter.equals("#")) {
      robot.keyPress(KeyEvent.VK_SHIFT);
      robot.keyPress(KeyEvent.VK_3);
      robot.keyRelease(KeyEvent.VK_3);
      robot.keyRelease(KeyEvent.VK_SHIFT);
      } else if (letter.equals("#")) {
      robot.keyPress(KeyEvent.VK_SHIFT);
      robot.keyPress(KeyEvent.VK_3);
      robot.keyRelease(KeyEvent.VK_3);
      robot.keyRelease(KeyEvent.VK_SHIFT);
      } else if (letter.equals("$")) {
      robot.keyPress(KeyEvent.VK_SHIFT);
      robot.keyPress(KeyEvent.VK_4);
      robot.keyRelease(KeyEvent.VK_4);
      robot.keyRelease(KeyEvent.VK_SHIFT);
      } else if (letter.equals("%")) {
      robot.keyPress(KeyEvent.VK_SHIFT);
      robot.keyPress(KeyEvent.VK_5);
      robot.keyRelease(KeyEvent.VK_5);
      robot.keyRelease(KeyEvent.VK_SHIFT);
      } else if (letter.equals("^")) {
      robot.keyPress(KeyEvent.VK_SHIFT);
      robot.keyPress(KeyEvent.VK_6);
      robot.keyRelease(KeyEvent.VK_6);
      robot.keyRelease(KeyEvent.VK_SHIFT);
      } else if (letter.equals("&")) {
      robot.keyPress(KeyEvent.VK_SHIFT);
      robot.keyPress(KeyEvent.VK_7);
      robot.keyRelease(KeyEvent.VK_7);
      robot.keyRelease(KeyEvent.VK_SHIFT);

      Delete
    3. } else if (letter.equals("*")) {
      robot.keyPress(KeyEvent.VK_SHIFT);
      robot.keyPress(KeyEvent.VK_8);
      robot.keyRelease(KeyEvent.VK_8);
      robot.keyRelease(KeyEvent.VK_SHIFT);
      } else if (letter.equals("=")) {
      robot.keyPress(KeyEvent.VK_EQUALS);
      robot.keyRelease(KeyEvent.VK_EQUALS);
      } else if (letter.equals(" ")) {
      robot.keyPress(KeyEvent.VK_SPACE);
      robot.keyRelease(KeyEvent.VK_SPACE);
      } else if (letter.equals("/")) {
      robot.keyPress(KeyEvent.VK_BACK_SLASH);
      robot.keyRelease(KeyEvent.VK_BACK_SLASH);
      } else if (letter.equals("\\")) {
      robot.keyPress(KeyEvent.VK_BACK_SLASH);
      robot.keyRelease(KeyEvent.VK_BACK_SLASH);
      } else if (letter.equals("_")) {
      robot.keyPress(KeyEvent.VK_SHIFT);
      robot.keyPress(KeyEvent.VK_MINUS);
      robot.keyRelease(KeyEvent.VK_MINUS);
      robot.keyRelease(KeyEvent.VK_SHIFT);
      } else if (letter.equals(":")) {
      robot.keyPress(KeyEvent.VK_SHIFT);
      robot.keyPress(KeyEvent.VK_SEMICOLON);
      robot.keyRelease(KeyEvent.VK_SEMICOLON);
      robot.keyRelease(KeyEvent.VK_SHIFT);
      } else if (letter.equals(";")) {
      robot.keyPress(KeyEvent.VK_SEMICOLON);
      robot.keyRelease(KeyEvent.VK_SEMICOLON);
      } else if (letter.equals(",")) {
      robot.keyPress(KeyEvent.VK_COMMA);
      robot.keyRelease(KeyEvent.VK_COMMA);
      } else if (letter.equals("-")) {
      robot.keyPress(KeyEvent.VK_SHIFT);
      robot.keyPress(KeyEvent.VK_SUBTRACT);
      robot.keyRelease(KeyEvent.VK_SUBTRACT);
      robot.keyRelease(KeyEvent.VK_SHIFT);
      } else if (letter.equals("?")) {
      robot.keyPress(KeyEvent.VK_SHIFT);
      robot.keyPress(KeyEvent.VK_SLASH);
      robot.keyRelease(KeyEvent.VK_SLASH);
      robot.keyRelease(KeyEvent.VK_SHIFT);
      } else if (letter.equals(" ")) {
      robot.keyPress(KeyEvent.VK_SPACE);
      robot.keyRelease(KeyEvent.VK_SPACE);
      } else if (letter.equals(".")) {
      robot.keyPress(KeyEvent.VK_PERIOD);
      robot.keyRelease(KeyEvent.VK_PERIOD);
      }

      }
      }

      Delete
  4. Here is the details described here :https://stackoverflow.com/questions/48184732/to-automate-file-upload-in-oracle-open-script-oats

    ReplyDelete

Powered by Blogger.