示例#1
0
  private static void openweb() throws InterruptedException, AWTException {
    Robot robot = new Robot();
    Thread.sleep(5000);

    String url = "http://www.freeproxylists.net/zh/?page=";
    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();

    for (int i = 2; i <= 50; ++i) {
      String tUrl = url + Integer.toString(i);
      Transferable t = new StringSelection(tUrl);
      cb.setContents(t, null);
      robot.keyPress(KeyEvent.VK_CONTROL);
      robot.keyPress(KeyEvent.VK_T);
      robot.keyRelease(KeyEvent.VK_CONTROL);
      robot.keyRelease(KeyEvent.VK_T);
      Thread.sleep(1000);
      robot.keyPress(KeyEvent.VK_CONTROL);
      robot.keyPress(KeyEvent.VK_V);
      robot.keyRelease(KeyEvent.VK_CONTROL);
      robot.keyRelease(KeyEvent.VK_V);
      Thread.sleep(1000);
      robot.keyPress(KeyEvent.VK_ENTER);
      robot.keyRelease(KeyEvent.VK_ENTER);
      Thread.sleep(1000);
    }
  }
示例#2
0
  /**
   * Runs a sample test procedure
   *
   * @param robot the robot attached to the screen device
   */
  public static void runTest(Robot robot) {
    // simulate a space bar press
    robot.keyPress(' ');
    robot.keyRelease(' ');

    // simulate a tab key followed by a space
    robot.delay(2000);
    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_TAB);
    robot.keyPress(' ');
    robot.keyRelease(' ');

    // simulate a mouse click over the rightmost button
    robot.delay(2000);
    robot.mouseMove(200, 50);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);

    // capture the screen and show the resulting image
    robot.delay(2000);
    BufferedImage image = robot.createScreenCapture(new Rectangle(0, 0, 400, 300));

    ImageFrame frame = new ImageFrame(image);
    frame.setVisible(true);
  }
示例#3
0
  public void start() {
    menu.setMnemonic('f');
    submenu.setMnemonic('m');
    menu.add(submenu);
    submenu.add(item);
    bar.add(menu);
    frame.setJMenuBar(bar);
    frame.pack();

    item.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            Sysout.println(e.toString());
            synchronized (activated) {
              activated.set(true);
              activated.notifyAll();
            }
          }
        });

    frame.setVisible(true);
    Util.waitForIdle(robot);

    boolean isMacOSX = (OSInfo.getOSType() == OSInfo.OSType.MACOSX);
    if (isMacOSX) {
      robot.keyPress(KeyEvent.VK_CONTROL);
      robot.delay(20);
    }
    robot.keyPress(KeyEvent.VK_ALT);
    robot.delay(20);
    robot.keyPress(KeyEvent.VK_F);
    robot.delay(20);
    robot.keyRelease(KeyEvent.VK_F);
    robot.delay(20);
    robot.keyRelease(KeyEvent.VK_ALT);
    if (isMacOSX) {
      robot.keyRelease(KeyEvent.VK_CONTROL);
      robot.delay(20);
    }
    Util.waitForIdle(robot);

    robot.keyPress(KeyEvent.VK_M);
    robot.delay(20);
    robot.keyRelease(KeyEvent.VK_M);
    Util.waitForIdle(robot);

    robot.keyPress(KeyEvent.VK_SPACE);
    robot.delay(20);
    robot.keyRelease(KeyEvent.VK_SPACE);
    Util.waitForIdle(robot);

    if (!Util.waitForCondition(activated, 2000)) {
      throw new TestFailedException("a submenu wasn't activated by mnemonic key press");
    }

    Sysout.println("Test passed.");
  }
  public static void main(String[] args) throws Exception {
    robot = new Robot();
    robot.setAutoDelay(50);

    try {
      SwingUtilities.invokeAndWait(
          new Runnable() {
            @Override
            public void run() {
              createAndShowGUI();
            }
          });

      robot.waitForIdle();

      checkFocusOwner(textField);

      robot.keyPress(KeyEvent.VK_TAB);
      robot.keyRelease(KeyEvent.VK_TAB);
      robot.waitForIdle();

      checkFocusOwner(button);

      robot.keyPress(KeyEvent.VK_SHIFT);
      robot.keyPress(KeyEvent.VK_TAB);
      robot.keyRelease(KeyEvent.VK_TAB);
      robot.keyRelease(KeyEvent.VK_SHIFT);
      robot.waitForIdle();

      checkFocusOwner(textField);

      robot.keyPress(KeyEvent.VK_SHIFT);
      robot.keyPress(KeyEvent.VK_TAB);
      robot.keyRelease(KeyEvent.VK_TAB);
      robot.keyRelease(KeyEvent.VK_SHIFT);
      robot.waitForIdle();

      checkFocusOwner(button);
    } finally {
      SwingUtilities.invokeLater(
          new Runnable() {
            @Override
            public void run() {
              if (frame != null) {
                frame.dispose();
              }
            }
          });
    }
    System.out.println("Test Passed!");
  }
 @Override
 public void operate(Robot robot) {
   try {
     robot.keyPress(getKeyField().getInt(null));
   } catch (IllegalAccessException e) {
     e.printStackTrace();
   }
 }
示例#6
0
  /**
   * Press a specific key using its char representation.
   *
   * @param key char representation of the key
   */
  public void keyPress(char key) {
    /* check for CTRL+X where X is [A-Z]
     * CTRL+A = 1, A = 65
     */
    if (key >= 1 && key <= 0x1A) {
      key = (char) (key + 64);
      robot.keyPress(key);
      return;
    }

    nativeKeyboard.keyPress(key);
  }
示例#7
0
  private static void GetProxy()
      throws InterruptedException, AWTException, UnsupportedFlavorException, IOException {
    Robot robot = new Robot();
    Thread.sleep(5000);
    for (int i = 1; i <= 50; ++i) {

      robot.keyPress(KeyEvent.VK_CONTROL);
      robot.keyPress(KeyEvent.VK_A);

      robot.keyRelease(KeyEvent.VK_CONTROL);
      robot.keyRelease(KeyEvent.VK_A);

      Thread.sleep(1000);
      robot.keyPress(KeyEvent.VK_CONTROL);
      robot.keyPress(KeyEvent.VK_C);
      robot.keyRelease(KeyEvent.VK_CONTROL);
      robot.keyRelease(KeyEvent.VK_C);

      Thread.sleep(500);
      Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
      Transferable t = cb.getContents(null);
      String readText = (String) t.getTransferData(DataFlavor.stringFlavor);
      // System.out.println(readText);
      String filePath = "F:\\Proxys\\";
      String fileName = "proxy" + Integer.toString(i) + ".txt";
      writeFile(filePath, fileName, readText);
      robot.keyPress(KeyEvent.VK_CONTROL);
      robot.keyPress(KeyEvent.VK_TAB);
      robot.keyRelease(KeyEvent.VK_CONTROL);
      robot.keyRelease(KeyEvent.VK_TAB);
      Thread.sleep(1000);
    }
  }
 // test BUTTON1, 2 and 3 with ALT key
 public static void testButtonsWithAlt() {
   System.out.println("Testing buttons with ALT modifier.");
   f.addMouseListener(adapterTest4);
   for (int button : mouseButtons) {
     robot.keyPress(KeyEvent.VK_ALT);
     robot.mousePress(button);
     robot.delay(100);
     robot.mouseRelease(button);
     robot.keyRelease(KeyEvent.VK_ALT);
   }
   robot.delay(1000);
   f.removeMouseListener(adapterTest4);
 }
示例#9
0
  /**
   * Press a specific key using its keycode.
   *
   * @param keycode the Java keycode, all available keycodes can be found in java.awt.event.KeyEvent
   *     class (VK_A, VK_SPACE, ...)
   * @see java.awt.event.KeyEvent
   */
  public void keyPress(int keycode) {
    if (OSUtils.IS_WINDOWS || OSUtils.IS_MAC) {
      /* do not allow modifiers for Windows (as
       * they are handled in native code with
       * VkScanCode) and Mac OS X
       */
      if (keycode == KeyEvent.VK_ALT
          || keycode == KeyEvent.VK_SHIFT
          || keycode == KeyEvent.VK_ALT_GRAPH) {
        return;
      }
    }

    /* AltGr does not seems to work with robot, handle it via our
     * JNI code
     */
    if (keycode == KeyEvent.VK_ALT_GRAPH) {
      symbolPress("altgr");
    } else {
      robot.keyPress(keycode);
    }
  }
  /**
   * This methods print outs public profile informations of given user sets the plusProfile with all
   * those information. getPlusProfile() can be used to get the information.
   *
   * @param userName : user name of a Google plus user to crawl the public profile informations.
   * @return no return value
   */
  public void startCrawling(String userName) {
    try {

      InputStream is =
          new URL("https://www.googleapis.com/plus/v1/people/" + userName + "?key=" + API_KEY)
              .openStream();

      JsonReader rdr = Json.createReader(is);
      JsonObject obj = rdr.readObject();

      // retrieve user's profile information

      System.out.println("\nGoogle+ Profile crawler started...\n");
      System.out.println("____________________________________");
      System.out.println("         Profile");
      System.out.println("====================================");
      String displayName = obj.getJsonString("displayName").getString();
      System.out.println("Name: " + displayName);
      getPlusProfile().setName(displayName);

      String user_id = obj.getJsonString("id").getString();
      System.out.println("ID: " + user_id);
      // getPlusProfile().setUserId(user_id);//number format user id
      getPlusProfile().setUserId(userName);

      if (obj.containsKey("gender")) {
        String gender = obj.getJsonString("gender").getString();
        System.out.println("Gender: " + gender);
        getPlusProfile().setGender(gender);
      } else {
        System.out.println("Gender: " + "");
        getPlusProfile().setGender("");
      }

      getPlusProfile().setDateOfBirth("");
      JsonString occupationJson = obj.getJsonString("occupation");
      if (occupationJson != null) {
        String occupation = obj.getJsonString("occupation").getString();
        System.out.println("Occupation: " + occupation);
      }

      // retrieve user's work and education information
      JsonArray results = obj.getJsonArray("placesLived");
      if (results != null) {
        System.out.println("____________________________________");
        System.out.println("         Locations Lived");
        System.out.println("====================================");
        int i = 0;
        for (JsonObject result : results.getValuesAs(JsonObject.class)) {
          String location = result.getString("value");
          System.out.println(location);
          if (i == 0) getPlusProfile().setCurrentLocation(location);
          else getPlusProfile().setHomeLocation(location);

          i++;
        }
        if (i == 1) getPlusProfile().setHomeLocation(getPlusProfile().getCurrentLocation());

      } else {
        System.out.println("Location info NOT AVAILABLE");
        getPlusProfile().setCurrentLocation("");
        getPlusProfile().setHomeLocation("");
      }

      // retrieve user's work and education information
      Set<String> wrkEdu = new TreeSet<String>();
      results = obj.getJsonArray("organizations");
      if (results != null) {
        System.out.println("____________________________________");
        System.out.println("         Work and Education");
        System.out.println("====================================");
        for (JsonObject result : results.getValuesAs(JsonObject.class)) {

          if (result.containsKey("name")) {
            String workEdu = result.getString("name");
            System.out.println(workEdu);
          } else {
            wrkEdu.add("");
            System.out.println("Company name NOT AVAILABLE");
          }
        }

        getPlusProfile().setEmployer(wrkEdu);
        getPlusProfile().setEducation(wrkEdu);
      } else {
        System.out.println("Work and Education info NOT AVAILABLE");
        getPlusProfile().setEmployer(wrkEdu);
        getPlusProfile().setEducation(wrkEdu);
      }
      List<String> languages = new ArrayList<String>();
      getPlusProfile().setLanguages(languages);

      firefoxDriver = new FirefoxDriver();
      firefoxDriver.get("https://plus.google.com/" + userName + "/about");
      Thread.sleep(3000);

      // get list of circles
      List<WebElement> circle = firefoxDriver.findElements(By.className("bkb"));
      Set<String> circles = new TreeSet<String>();

      if (circle.size() != 0) {
        String circleText = circle.get(0).getText();
        System.out.println(circleText);
        String circleCountstr = circleText.split("\\s+")[0];
        if (circleCountstr.contains(",")) {
          circleCountstr = circleCountstr.replace(",", "");
        }
        int circleCount = Integer.parseInt(circleCountstr);
        System.out.println("Circle Count:" + circleCount);

        WebElement circleLink =
            firefoxDriver.findElement(By.xpath("//span[contains(text(),'" + circleText + "')]"));
        circleLink.click();
        Thread.sleep(3000);

        WebElement friendListBox = firefoxDriver.findElement(By.className("G-q-B"));

        Robot robot = new Robot();

        int loop = 0;
        while (loop < circleCount / 10) {
          if (circles.size() == circleCount) break;

          Thread.sleep(3000);

          List<WebElement> friendList = friendListBox.findElements(By.tagName("a"));

          for (WebElement friend : friendList) {
            System.out.println(friend.getText());
            circles.add(friend.getText());
          }

          int count = 0;
          while (count < 500) {
            robot.keyPress(java.awt.event.KeyEvent.VK_DOWN);
            count++;
          }

          loop++;
        }
      }
      List<String> friends = new ArrayList<String>();
      for (String friend : circles) {
        friends.add(friend);
      }
      getPlusProfile().setFriends(friends);

      System.out.println(
          "_________________________________________________________________________________________");

      System.out.println("Finish Crawling...");
      closePlusWebDriverNode();

    } catch (AWTException ex) {
      System.out.println("Problem in key press:" + ex);
    } catch (IOException ex) {
      System.out.println(ex);
    } catch (InterruptedException ex) {
      System.out.println();
    }
  }
  void test() {

    robot.waitForIdle();

    if (!text.isFocusOwner()) {
      robot.mouseMove(text.getLocationOnScreen().x + 5, text.getLocationOnScreen().y + 5);
      robot.delay(100);
      robot.mousePress(MouseEvent.BUTTON1_MASK);
      robot.delay(100);
      robot.mouseRelease(MouseEvent.BUTTON1_MASK);

      int iter = 10;
      while (!text.isFocusOwner() && iter-- > 0) {
        robot.delay(200);
      }
      if (iter <= 0) {
        Sysout.println("Test: text field couldn't be focused!");
        return;
      }
    }

    robot.keyPress(KeyEvent.VK_A);
    robot.delay(100);
    robot.keyRelease(KeyEvent.VK_A);

    robot.waitForIdle();

    String charA = text.getText();
    System.err.println("Test: character typed with VK_A: " + charA);

    robot.keyPress(KeyEvent.VK_BACK_SPACE);
    robot.delay(100);
    robot.keyRelease(KeyEvent.VK_BACK_SPACE);

    robot.waitForIdle();

    if (jdk.testlibrary.OSInfo.getOSType() == jdk.testlibrary.OSInfo.OSType.MACOSX) {
      robot.keyPress(KeyEvent.VK_CONTROL);
    }
    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyPress(KeyEvent.VK_F);
    robot.delay(100);
    robot.keyRelease(KeyEvent.VK_F);
    robot.keyRelease(KeyEvent.VK_ALT);
    if (jdk.testlibrary.OSInfo.getOSType() == jdk.testlibrary.OSInfo.OSType.MACOSX) {
      robot.keyRelease(KeyEvent.VK_CONTROL);
    }

    robot.waitForIdle();

    String string = text.getText();

    robot.keyPress(KeyEvent.VK_I);
    robot.delay(100);
    robot.keyRelease(KeyEvent.VK_I);

    robot.waitForIdle();

    Sysout.println("Test: character typed after mnemonic key press: " + text.getText());

    if (!text.getText().equals(string)) {
      throw new RuntimeException("Test failed!");
    }

    robot.keyPress(KeyEvent.VK_A);
    robot.delay(100);
    robot.keyRelease(KeyEvent.VK_A);

    robot.waitForIdle();

    System.err.println("Test: chracter typed with VK_A: " + text.getText());

    if (!charA.equals(text.getText())) {
      throw new RuntimeException("Test failed!");
    }

    Sysout.println("Test passed.");
  }
  public static void main(String[] args) throws Throwable {

    sun.awt.SunToolkit toolkit = (sun.awt.SunToolkit) Toolkit.getDefaultToolkit();

    SwingUtilities.invokeAndWait(
        new Runnable() {
          public void run() {
            test = new TaskbarPositionTest();
          }
        });

    // Use Robot to automate the test
    Robot robot;
    robot = new Robot();
    robot.setAutoDelay(125);

    // 1 - menu
    Util.hitMnemonics(robot, KeyEvent.VK_1);

    toolkit.realSync();
    isPopupOnScreen(menu1.getPopupMenu(), screenBounds);

    // 2 menu with sub menu
    robot.keyPress(KeyEvent.VK_RIGHT);
    robot.keyRelease(KeyEvent.VK_RIGHT);
    Util.hitMnemonics(robot, KeyEvent.VK_S);

    toolkit.realSync();
    isPopupOnScreen(menu2.getPopupMenu(), screenBounds);

    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

    // Focus should go to non editable combo box
    toolkit.realSync();
    Thread.sleep(500);

    robot.keyPress(KeyEvent.VK_DOWN);

    // How do we check combo boxes?

    // Editable combo box
    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_TAB);
    robot.keyPress(KeyEvent.VK_DOWN);
    robot.keyRelease(KeyEvent.VK_DOWN);

    // combo1.getUI();

    // Popup from Text field
    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_TAB);
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_DOWN);
    robot.keyRelease(KeyEvent.VK_DOWN);
    robot.keyRelease(KeyEvent.VK_CONTROL);

    // Popup from a mouse click.
    Point pt = new Point(2, 2);
    SwingUtilities.convertPointToScreen(pt, panel);
    robot.mouseMove((int) pt.getX(), (int) pt.getY());
    robot.mousePress(InputEvent.BUTTON3_MASK);
    robot.mouseRelease(InputEvent.BUTTON3_MASK);

    toolkit.realSync();
    SwingUtilities.invokeAndWait(
        new Runnable() {
          public void run() {
            test.setLocation(-30, 100);
            combo1.addPopupMenuListener(new ComboPopupCheckListener());
            combo1.requestFocus();
          }
        });

    robot.keyPress(KeyEvent.VK_DOWN);
    robot.keyRelease(KeyEvent.VK_DOWN);
    robot.keyPress(KeyEvent.VK_ESCAPE);
    robot.keyRelease(KeyEvent.VK_ESCAPE);

    toolkit.realSync();
    Thread.sleep(500);
  }
示例#13
0
  private void test(TestState currentState) throws InterruptedException, InvocationTargetException {

    synchronized (LOCK) {
      this.currentState = currentState;
      System.out.println(this.currentState);

      List list;
      if (currentState.getMultiple()) {
        list = multiple;
      } else {
        list = single;
      }

      Robot r;
      try {
        r = new Robot();
      } catch (AWTException e) {
        throw new RuntimeException(e.getMessage());
      }

      r.delay(10);
      Point loc = this.getLocationOnScreen();

      r.mouseMove(loc.x + 10, loc.y + 10);
      r.mousePress(InputEvent.BUTTON1_MASK);
      r.delay(10);
      r.mouseRelease(InputEvent.BUTTON1_MASK);
      r.delay(10);

      list.requestFocusInWindow();
      LOCK.wait(ACTION_TIMEOUT);
      if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != list) {
        throw new RuntimeException("Test failed - list isn't focus owner.");
      }

      list.deselect(0);
      list.deselect(1);
      list.deselect(2);
      list.deselect(3);
      list.deselect(4);
      list.deselect(5);
      list.deselect(6);
      list.deselect(7);
      list.deselect(8);

      int selectIndex = 0;
      int visibleIndex = 0;

      if (currentState.getScrollMoved()) {

        if (currentState.getKeyID() == KeyEvent.VK_PAGE_UP
            || currentState.getKeyID() == KeyEvent.VK_HOME) {
          selectIndex = 8;
          visibleIndex = 8;
        } else if (currentState.getKeyID() == KeyEvent.VK_PAGE_DOWN
            || currentState.getKeyID() == KeyEvent.VK_END) {
          selectIndex = 0;
          visibleIndex = 0;
        }

      } else {

        if (currentState.getKeyID() == KeyEvent.VK_PAGE_UP
            || currentState.getKeyID() == KeyEvent.VK_HOME) {

          if (currentState.getSelectedMoved()) {
            selectIndex = 1;
            visibleIndex = 0;
          } else {
            selectIndex = 0;
            visibleIndex = 0;
          }

        } else if (currentState.getKeyID() == KeyEvent.VK_PAGE_DOWN
            || currentState.getKeyID() == KeyEvent.VK_END) {

          if (currentState.getSelectedMoved()) {
            selectIndex = 7;
            visibleIndex = 8;
          } else {
            selectIndex = 8;
            visibleIndex = 8;
          }
        }
      }

      list.select(selectIndex);
      list.makeVisible(visibleIndex);

      r.delay(10);

      if (currentState.getKeyID() == KeyEvent.VK_HOME
          || currentState.getKeyID() == KeyEvent.VK_END) {
        r.keyPress(KeyEvent.VK_CONTROL);
      }

      r.delay(10);
      r.keyPress(currentState.getKeyID());
      r.delay(10);
      r.keyRelease(currentState.getKeyID());
      r.delay(10);

      if (currentState.getKeyID() == KeyEvent.VK_HOME
          || currentState.getKeyID() == KeyEvent.VK_END) {
        r.keyRelease(KeyEvent.VK_CONTROL);
      }

      r.waitForIdle();
      r.delay(200);

      if (currentState.getTemplate() != currentState.getAction())
        throw new RuntimeException("Test failed.");
    }
  }