/**
   * The worker method. It will find the container, create the file if missing or just replace its
   * contents, and open the editor on the newly created file.
   */
  private void doFinish(
      String containerName,
      String fileName,
      String name,
      String description,
      IProgressMonitor monitor)
      throws CoreException {

    // create a sample file
    monitor.beginTask("Creating " + fileName, 2);
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IResource resource = root.findMember(new Path(containerName));
    if (!resource.exists() || !(resource instanceof IContainer)) {
      throwCoreException("Container \"" + containerName + "\" does not exist.");
    }
    IContainer container = (IContainer) resource;
    final IFile file = container.getFile(new Path(fileName));

    try {
      Test emptyTest = createEmptyTest(name, description);
      String xml = new CubicTestXStream().toXML(emptyTest);
      FileUtils.writeStringToFile(file.getLocation().toFile(), xml, "ISO-8859-1");
      file.getParent().refreshLocal(IResource.DEPTH_INFINITE, null);

    } catch (IOException e) {
      ErrorHandler.logAndRethrow(e);
    }
    monitor.worked(1);

    if (openCreatedTestOnFinish) {
      openFileForEditing(monitor, file);
    }
  }
 public static void saveToFile(CustomTestStep customStep, IFile file) {
   String xml = new CubicTestXStream().toXML(customStep);
   try {
     String charset = TestPersistance.getCharset(file.getLocation().toFile());
     String charsetHeader = TestPersistance.getCharsetHeader(charset);
     xml = charsetHeader + "\n" + xml;
     FileUtils.writeStringToFile(file.getLocation().toFile(), xml, charset);
   } catch (IOException e) {
     ErrorHandler.logAndRethrow(e);
   }
 }
  /**
   * Converts a user interactions transition to a list of Selenium commands.
   *
   * @param transition The transition to convert.
   */
  public void handleUserInteractions(
      SeleniumHolder seleniumHolder, UserInteractionsTransition transition) {

    for (UserInteraction userInteraction : transition.getUserInteractions()) {
      IActionElement actionElement = userInteraction.getElement();

      if (actionElement == null) {
        Logger.warn("Action element was null. Skipping user interaction: " + userInteraction);
        continue;
      }

      int waitMillis = seleniumHolder.getNextPageElementTimeout() * 1000;
      int waitIntervalMillis = 100;
      int i = 0;
      while (true) {
        try {
          handleUserInteraction(seleniumHolder, userInteraction);
          break;
        } catch (UserInteractionException e) {
          if (i > waitMillis) {
            handleUserInteractionFailure(seleniumHolder, userInteraction, e);
          }
          try {
            Thread.sleep(waitIntervalMillis);
            i += waitIntervalMillis;
            Logger.warn(
                "Retrying user interaction: "
                    + userInteraction.toString()
                    + " after error: "
                    + ErrorHandler.getCause(e).toString());
          } catch (InterruptedException e2) {
            throw new ExporterException(e2.toString() + " came after " + e.toString(), e);
          }
        }
      }

      // increment the number of steps in test:
      seleniumHolder.addResult(null, TestPartStatus.PASS);
    }

    if (transition.hasCustomTimeout()) {
      seleniumHolder.setNextPageElementTimeout(transition.getSecondsToWaitForResult());
    }
  }
 public static CustomTestStep loadFromFile(File file) {
   String xml = "";
   try {
     String charset = TestPersistance.getCharset(file);
     xml = FileUtils.readFileToString(file, charset);
   } catch (FileNotFoundException e) {
     Logger.error("Error loading test.", e);
     throw new TestNotFoundException(e.getMessage());
   } catch (IOException e) {
     ErrorHandler.logAndRethrow(e);
   }
   CustomTestStep customStep = null;
   try {
     customStep = (CustomTestStep) new CubicTestXStream().fromXML(xml);
     return customStep;
   } catch (StreamException e) {
   }
   if (customStep == null) customStep = new CustomTestStep();
   return customStep;
 }
  /**
   * This method is called when 'Finish' button is pressed in the wizard. We will create an
   * operation and run it using wizard as execution context.
   */
  @Override
  public boolean performFinish() {
    try {
      final String containerName = testDetailsPage.getContainerName();
      final String fileName = testDetailsPage.getFileName();
      this.filePath = containerName + "/" + fileName;
      final String name = testDetailsPage.getName();
      final String description = testDetailsPage.getDescription();

      IRunnableWithProgress op =
          new IRunnableWithProgress() {
            public void run(final IProgressMonitor monitor) throws InvocationTargetException {
              getShell()
                  .getDisplay()
                  .syncExec(
                      new Runnable() {
                        public void run() {
                          try {
                            doFinish(containerName, fileName, name, description, monitor);
                          } catch (CoreException e) {
                            throw new CubicException(e);
                          } finally {
                            monitor.done();
                          }
                        }
                      });
            }
          };
      try {
        getContainer().run(true, false, op);
      } catch (InterruptedException e) {
        return false;
      } catch (InvocationTargetException e) {
        ErrorHandler.logAndShowErrorDialog("Error creating test", e);
        return false;
      }
    } finally {
      done = true;
    }
    return true;
  }
  @Override
  protected void convertCubicTestFile(
      IFile file, IFolder outFolder, IProgressMonitor monitor, boolean isSelected)
      throws Exception {
    Test test = TestPersistance.loadFromFile(file);

    if (!ExportUtils.testIsOkForExport(test)) {
      if (isSelected) {
        // selected file must be OK for export. Show error
        ExportUtils.throwTestNotOkForExportException(test);
        return;
      } else {
        // File is part of multiple files export. Just skip file
        return;
      }
    }

    IFolder destinationFolder = outFolder;
    destinationFolder.refreshLocal(IResource.DEPTH_INFINITE, monitor);

    if (!ExportUtils.isTestFile(outFolder.getName())) {
      destinationFolder = outFolder.getFolder(file.getName());
    }

    if (!destinationFolder.exists()) {
      destinationFolder.create(false, true, null);
    }

    testConverter.convert(test, destinationFolder.getRawLocation().toFile(), null);

    try {
      // copy stylesheet and javascript:
      copy(destinationFolder, "default.css");
      copy(destinationFolder, "cubic.js");
    } catch (Exception e) {
      ErrorHandler.logAndShowErrorDialogAndRethrow(
          "Error copying stylesheet or javascript file! The prototype will have limited functionality.",
          e);
    }
  }
  /**
   * Converts a single user interaction to a Selenium command.
   *
   * @return the Selenium command name invoked.
   */
  private String handleUserInteraction(
      SeleniumHolder seleniumHolder, UserInteraction userInteraction) {

    IActionElement element = userInteraction.getElement();
    ActionType actionType = userInteraction.getActionType();
    boolean withinFrame = false;
    if (element instanceof PageElement
        && seleniumHolder.isPageElementWithinFrame((PageElement) element)) {
      // check if parent frame was found:
      if (TestPartStatus.FAIL == seleniumHolder.getParentFrame((PageElement) element).getStatus()) {
        ErrorHandler.logAndShowErrorDialogAndThrow(
            "Cannot interact with element "
                + element
                + ":\n"
                + "Parent frame "
                + seleniumHolder.getParentFrame((PageElement) element)
                + " not found.");
      }
      withinFrame = true;
      getToRightFrame(seleniumHolder, seleniumHolder.getParentFrame((PageElement) element));
    }
    // Getting selenium commands, locators and values:
    String commandName = SeleniumUtils.getCommandName(actionType);

    String locator = null;
    String inputValue = null;

    if (element instanceof Option) {
      Select selectbox = ((Option) element).getParent();
      locator = "xpath=" + seleniumHolder.getFullContextWithAllElements(selectbox);
      inputValue = SeleniumUtils.getOptionLocator((Option) element);
      if (SELECT.equals(actionType) && selectbox.getIdentifier(MULTISELECT).getProbability() > 0) {
        commandName = "addSelection"; // appropriate for multi-selection
      }
    } else {
      // all other elements
      if (element instanceof PageElement) {
        locator = "xpath=" + seleniumHolder.getFullContextWithAllElements((PageElement) element);
      } else if (element instanceof WebBrowser) {
        locator = userInteraction.getValue();
      } else {
        throw new ExporterException("Unsupported action element type");
      }
      inputValue = SeleniumUtils.getValue(userInteraction);
    }

    try {
      // invoke user interaction by reflection using command name from SeleniumUtil (legacy since
      // Selenese exporter was written first):

      if (SeleniumUtils.hasSeleniumInputColumn(userInteraction)) {
        // two parameters
        seleniumHolder.getSelenium().execute(commandName, locator, inputValue);
      } else {
        // one parameter only
        seleniumHolder.getSelenium().execute(commandName, locator);
      }
    } catch (Throwable e) {
      throw new UserInteractionException(e);
    }

    if (withinFrame && commandName.equals(SeleniumUtils.FIREEVENT)) {
      upToParentFrame(seleniumHolder);
    }
    return commandName;
  }