Beispiel #1
0
  public Object apply(Selenium selenium, Map<String, ?> parameters) {
    StringWriter sw = new StringWriter();
    try {
      new JSONWriter(sw)
          .object()
          .key("script")
          .value(parameters.get("script"))
          .key("args")
          .value(parameters.get("args"))
          .key("timeout")
          .value(timeoutMillis)
          .endObject();
    } catch (JSONException e) {
      throw new WebDriverException(e);
    }

    String script = "core.script.execute(" + sw + ")";
    String value;
    try {
      value = selenium.getEval(script);
    } catch (SeleniumException e) {
      if (e.getMessage().startsWith("ScriptTimeoutError")) {
        throw new TimeoutException(e.getMessage(), e);
      }
      throw e;
    }
    return populateReturnValue(value);
  }
 private boolean isWindowInFocus(SeleniumException e) {
   if (e.getMessage().contains("There is no cursor on this page")) {
     System.out.println("Test failed because window does not have focus");
     return false;
   }
   return true;
 }
  private String getCommandOutput(ExtendedSeleniumCommand command, String[] values) {
    try {
      if (command.returnTypeIsArray()) {
        return executeArrayCommand(command.getSeleniumCommand(), values);
      } else {
        return executeCommand(command.getSeleniumCommand(), values);
      }
    } catch (final SeleniumException e) {
      String output = "Execution of command failed: " + e.getMessage();
      LOG.error(output);

      if (!(command.isAssertCommand() || command.isVerifyCommand() || command.isWaitForCommand())) {
        throw e;
      }

      return output;
    }
  }
 public void testSeleniumSearch() throws Exception {
   selenium.setSpeed("2000");
   // selenium.open("file:///C:/Documents%20and%20Settings/James
   // Albersheim.NREL_NT/My%20Documents/bookmark.htm");
   selenium.open("http://*:8880/spreadsheet/#initState");
   try {
     selenium.waitForPageToLoad("10000");
   } catch (SeleniumException se) {
   }
   try {
     // selenium.click("link=Spreadsheet Ingest");
     selenium.click("link=Search Spreadsheets");
     try {
       selenium.waitForPageToLoad("2000");
     } catch (SeleniumException se) {
     }
     // selenium.select("//td[@id='sheetContainer']/div/div/div[3]/table/tbody/tr[1]/td/table/tbody/tr[2]/td[2]/div/table/tbody/tr[2]/td/table/tbody/tr/td[2]/select", "label=TestConfig");
     // selenium.click("//option[@value='TestConfig']");
     SpreadSheetServiceImpl ssImpl = new SpreadSheetServiceImpl();
     List<NameValue> headers = ssImpl.getHeaders("TestConfig", "Digestion");
     Iterator<NameValue> hit = headers.iterator();
     while (hit.hasNext()) {
       NameValue nv = hit.next();
       String header = nv.getName();
       selenium.select(
           "//td[@id='sheetContainer']/div/div/div[3]/table/tbody/tr[1]/td/table/tbody/tr[2]/td[2]/div/table/tbody/tr[6]/td/table/tbody/tr[2]/td[1]/select",
           "label=" + header);
       selenium.click("//option[@value='" + header + "']");
       List<String> operators = ssImpl.getOperators("TestConfig", header);
       // Iterator<String> oit = operators.iterator();
       // while (oit.hasNext()) {
       String operator = operators.get(0); // oit.next();
       selenium.select(
           "//td[@id='sheetContainer']/div/div/div[3]/table/tbody/tr[1]/td/table/tbody/tr[2]/td[2]/div/table/tbody/tr[6]/td/table/tbody/tr[2]/td[2]/select",
           "label=" + operator);
       List<String> values = ssImpl.getStringValues("TestConfig", header);
       String value = values.get(0);
       long type = nv.getDataType();
       if (type == LONG) {
         selenium.type(
             "//td[@id='sheetContainer']/div/div/div[3]/table/tbody/tr[1]/td/table/tbody/tr[2]/td[2]/div/table/tbody/tr[6]/td/table/tbody/tr[2]/td[3]/input",
             value);
       } else if (type == REAL) {
         selenium.type(
             "//td[@id='sheetContainer']/div/div/div[3]/table/tbody/tr[1]/td/table/tbody/tr[2]/td[2]/div/table/tbody/tr[6]/td/table/tbody/tr[2]/td[3]/input",
             value);
       } else if (type == STRING) {
         selenium.type(
             "//td[@id='sheetContainer']/div/div/div[3]/table/tbody/tr[1]/td/table/tbody/tr[2]/td[2]/div/table/tbody/tr[6]/td/table/tbody/tr[2]/td[3]/input",
             value);
       } else if (type == DATE) {
         Calendar cal = Calendar.getInstance();
         int day = cal.get(Calendar.DAY_OF_MONTH);
         day -= 7;
         cal.set(Calendar.DAY_OF_MONTH, day); // subtract week
         String date =
             String.valueOf(cal.get(Calendar.MONTH) + 1)
                 + "/"
                 + String.valueOf(cal.get(Calendar.DAY_OF_MONTH))
                 + "/"
                 + String.valueOf(cal.get(Calendar.YEAR));
         value = date;
         System.out.println("date=" + date);
         selenium.click(
             "//td[@id='sheetContainer']/div/div/div[3]/table/tbody/tr[1]/td/table/tbody/tr[2]/td[2]/div/table/tbody/tr[6]/td/table/tbody/tr[2]/td[3]/input");
         selenium.type(
             "//td[@id='sheetContainer']/div/div/div[3]/table/tbody/tr[1]/td/table/tbody/tr[2]/td[2]/div/table/tbody/tr[6]/td/table/tbody/tr[2]/td[3]/input",
             date);
       }
       // selenium.click("//tr[2]/td/table/tbody/tr[5]/td[2]");
       selenium.click("//button[@type='button']");
       try {
         selenium.waitForPageToLoad("30000");
       } catch (SeleniumException se) {
       }
       if (selenium.isAlertPresent()) {
         assertEquals("Your criteria returned no data.", selenium.getAlert());
         System.out.println("FAILED for " + header + ";" + operator + ";" + value);
       } else {
         boolean ret = selenium.isTextPresent("Count");
         // assertTrue(ret);
         if (!ret) {
           System.out.println("FAILED for " + header + ";" + operator + ";" + value);
         } else {
           if (selenium.isTextPresent("Select")) {
             try {
               try {
                 selenium.click("gwt-uid-3");
                 selenium.waitForPageToLoad("10000");
               } catch (SeleniumException se) {
               }
               ret = selenium.isTextPresent("Project");
               if (!ret) System.out.println("FAILED for " + header + ";" + operator + ";" + value);
               else {
                 ret = selenium.isTextPresent(value);
                 if (!ret)
                   System.out.println("FAILED for " + header + ";" + operator + ";" + value);
               }
               try {
                 selenium.click("gwt-uid-4");
                 selenium.waitForPageToLoad("10000");
               } catch (SeleniumException se) {
               }
               ret = selenium.isTextPresent("Project");
               if (!ret) System.out.println("FAILED for " + header + ";" + operator + ";" + value);
               else {
                 ret = selenium.isTextPresent(value);
                 if (!ret)
                   System.out.println("FAILED for " + header + ";" + operator + ";" + value);
               }
               try {
                 selenium.click("gwt-uid-5");
                 selenium.waitForPageToLoad("10000");
               } catch (SeleniumException se) {
               }
               ret = selenium.isTextPresent("Project");
               if (!ret) System.out.println("FAILED for " + header + ";" + operator + ";" + value);
               else {
                 ret = selenium.isTextPresent(value);
                 if (!ret)
                   System.out.println("FAILED for " + header + ";" + operator + ";" + value);
               }
               try {
                 selenium.click("gwt-uid-6");
                 selenium.waitForPageToLoad("10000");
               } catch (SeleniumException se) {
               }
               ret = selenium.isTextPresent("Project");
               if (!ret) System.out.println("FAILED for " + header + ";" + operator + ";" + value);
               else {
                 ret = selenium.isTextPresent(value);
                 if (!ret)
                   System.out.println("FAILED for " + header + ";" + operator + ";" + value);
               }
               try {
                 selenium.click("gwt-uid-7");
                 selenium.waitForPageToLoad("10000");
               } catch (SeleniumException se) {
               }
               ret = selenium.isTextPresent("Project");
               if (!ret) System.out.println("FAILED for " + header + ";" + operator + ";" + value);
               else {
                 ret = selenium.isTextPresent(value);
                 if (!ret)
                   System.out.println("FAILED for " + header + ";" + operator + ";" + value);
               }
             } catch (SeleniumException se1) {
               System.out.println(se1.getMessage());
               se1.printStackTrace();
             }
           }
         }
       }
       selenium.click(
           "//td[@id='sheetContainer']/div/div/div[3]/table/tbody/tr[1]/td/table/tbody/tr[2]/td[2]/div/table/tbody/tr[7]/td/table/tbody/tr/td[2]/button");
       // }
     }
   } catch (SeleniumException se) {
     System.out.println(se.getMessage());
     se.printStackTrace();
   }
 }
  /**
   * This method returns a LoggingSelenium instance. Launches the browser with a new Selenium
   * session. Use this only if you want to have one selenium instance.
   *
   * @return Selenium session
   */
  public LoggingSelenium createDefaultSeleniumInstance(String browser, String url) {

    boolean quit = false;
    // if (selenium == null) {
    try {
      final String RESULT_FILE_ENCODING = "UTF-8";
      final String SCREENSHOT_PATH = "screenshots";

      String resultsPath = new File(RESULTS_BASE_PATH).getAbsolutePath();
      String screenshotsResultsPath =
          new File(RESULTS_BASE_PATH + File.separator + SCREENSHOT_PATH).getAbsolutePath();

      if (!new File(resultsPath).exists()) {
        new File(resultsPath).mkdirs();
      }
      if (!new File(screenshotsResultsPath).exists()) {
        new File(screenshotsResultsPath).mkdirs();
      }

      currentScreenShotPath = screenshotsResultsPath;

      String partialFileName =
          File.separator + testName + "report_" + timeStampForFileName() + ".html";

      resultHtmlFileName = resultsPath + partialFileName;
      relativeResultHtmlFileName = RESULTS_BASE_PATH + partialFileName;

      System.err.println("resultHtmlFileName=" + resultHtmlFileName);
      loggingWriter = LoggingUtils.createWriter(resultHtmlFileName, RESULT_FILE_ENCODING, true);

      LoggingResultsFormatter htmlFormatter =
          new HtmlResultFormatter(loggingWriter, RESULT_FILE_ENCODING);

      htmlFormatter.setScreenShotBaseUri(SCREENSHOT_PATH + "/");
      htmlFormatter.setAutomaticScreenshotPath(screenshotsResultsPath);
      LoggingCommandProcessor myProcessor =
          new LoggingCommandProcessor(
              ConfigFileReader.getConfigItemValue("selenium.server.host"),
              4444,
              browser,
              url,
              htmlFormatter);
      myProcessor.setExcludedCommands(
          new String[] {"captureEntirePageScreenshotToString", "captureScreenshot"});
      selenium = new LoggingDefaultSelenium(myProcessor);

      /*			RemoteControlConfiguration rcc = server.getConfiguration();
      File profileLocation = new File(ConfigFileReader.getConfigItemValue("selenium.firefox.profile"));
      rcc.setProfilesLocation(profileLocation);
      rcc.setFirefoxProfileTemplate(profileLocation);
      rcc.setReuseBrowserSessions(true);
      selenium.start(rcc);

      BrowserConfigurationOptions bb = new BrowserConfigurationOptions();
      bb.setProfile(profile)*/
      selenium.start();

      selenium.setSpeed(ConfigFileReader.getConfigItemValue("selenium.speed"));
      selenium.setTimeout("120000");
      // selenium.deleteAllVisibleCookies();

      try {
        selenium.open(url);
      } catch (Exception e) {
        // selenium.refresh();
        selenium.waitForElementPresent("css=a:contains('Sign In')");
      }

      // selenium.waitForPageToLoad(TestConsts.PAGE_LOAD_TIMEOUT);
      selenium.waitForElementPresent("css=a:contains('Sign In')");
      selenium.windowMaximize();
      selenium.windowFocus();

    } catch (SeleniumException ex) {
      logger.error(ex.getMessage());
      quit = true;
      // }
    }
    if (quit) {
      stopSeleniumInstance();
      System.exit(0);
    }

    return selenium;
  }