Skip to content

Editors

Andrej Podhradsky edited this page Mar 14, 2017 · 54 revisions

Api/Abstract

The main interface to interact with editors in RedDeer is Editor from org.jboss.reddeer.workbench.api plugin. Whenever you implement new editor, this is the interface to begin with.

Implementation

  • DefaultEditor is the most basic implementation of editors in RedDeer. This class should be used, whenever you need nothing more than basic operations (open, close, save, check whether is editor dirty, content assist, ...)
  • TextEditor is extension of DefaultEditor and on top of it provides text operations for searching, selecting and altering text within this editor. This class should in general be used with eclipse editors implementing ITextEditor.
  • BrowserEditor, from plugin org.jboss.reddeer.eclipse is extension of AbstractEditor and is used for instances of org.eclipse.ui.internal.browser.WebBrowserEditor. It provides standard browser operations as forward, reload, change url, ...

Usage

package org.jboss.reddeer.snippet.test;

import org.jboss.reddeer.junit.runner.RedDeerSuite;
import org.jboss.reddeer.workbench.api.Editor;
import org.jboss.reddeer.workbench.impl.editor.DefaultEditor;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(RedDeerSuite.class)
public class EditorTest {

  @Test
  public void test(){
    //Currently active editor will gain focus or the first available
    Editor editor = new DefaultEditor(); 
    // some logic here
    // This will save&close currently active editor
    editor.close(true); 
  }
}

source code

This is the most basic test scenario. This test focuses currently active editor (editor which is currently visible), execute some desired actions and save&close the editor - close(boolean save).

QuickFix

All editors extending AbstractEditor can use QuickFix assistant. QuickFix is always invokend using proper key combination. If none is found, QuickFix assistant will fail.

Usage

package org.jboss.reddeer.snippet.test;

import org.jboss.reddeer.jface.text.contentassist.ContentAssistant;
import org.jboss.reddeer.junit.runner.RedDeerSuite;
import org.jboss.reddeer.workbench.impl.editor.TextEditor;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(RedDeerSuite.class)
public class QuickFixTest {

	@Test
	public void test() {
		TextEditor textEditor = new TextEditor();
		textEditor.selectText("JavaClass");
		ContentAssistant ca = textEditor.openQuickFixContentAssistant();
		ca.chooseProposal("Rename in file");
	}
}

source code

CodeCompletion

All editors extending AbstractEditor can use code assistant. Code Assist is always invokend using proper key combination. If none is found, code assist will fail.

Usage

package org.jboss.reddeer.snippet.test;

import org.jboss.reddeer.jface.text.contentassist.ContentAssistant;
import org.jboss.reddeer.junit.runner.RedDeerSuite;
import org.jboss.reddeer.workbench.impl.editor.TextEditor;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(RedDeerSuite.class)
public class CodeCompletionTest {

	@Test
	public void test() {
		TextEditor textEditor = new TextEditor();
		ContentAssistant ca = textEditor.openContentAssistant();
		ca.chooseProposal("enum");
	}
}

source code

OpenOn

All editors extending AbstractEditor can use OpenOn assistant. OpenOn is always invokend using proper key combination. If none is found, OpenOn assist will fail.

Usage

package org.jboss.reddeer.snippet.test;

import org.jboss.reddeer.jface.text.contentassist.ContentAssistant;
import org.jboss.reddeer.junit.runner.RedDeerSuite;
import org.jboss.reddeer.workbench.impl.editor.TextEditor;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(RedDeerSuite.class)
public class OpenOnTest {

	@Test
	public void test() {
		// lets say that MyClass.java contains basic priltln hello world code
		TextEditor textEditor = new TextEditor("MyClass.java");
		// select println in editor
		textEditor.selectText("println");
		// open OpenOn assistant
		ContentAssistant ca = textEditor.openOpenOnAssistant();
		// choose some proposal
		ca.chooseProposal("Open Declaration");
		// for selected println, Open Declaration choice opens PrintStream.class class
		new TextEditor("PrintStream.class");
	}
}

source code

Validation markers

It's also possible to check validation markers. Also As-You-Type markers are supported.

Usage

package org.jboss.reddeer.snippet.test;

import static org.junit.Assert.*;

import org.jboss.reddeer.common.wait.AbstractWait;
import org.jboss.reddeer.common.wait.TimePeriod;
import org.jboss.reddeer.junit.runner.RedDeerSuite;
import org.jboss.reddeer.workbench.impl.editor.TextEditor;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(RedDeerSuite.class)
public class MarkersValidationTest {

	@Test
	public void test() {
		// lets say that MyClass.java contains basic priltln hello world code
		TextEditor textEditor = new TextEditor("MyClass.java");
		// Change System to Systemx - we will artificially create some validation
		// marker since Systemx cannot be resolved
		textEditor.setText(textEditor.getText().replace("System", "Systemx"));
		textEditor.save();
		// wait for validation
		AbstractWait.sleep(TimePeriod.SHORT);
		assertEquals("Systemx cannot be resolved",
				textEditor.getMarkers().get(0).getText());
	}
}

source code

Editor file

You can get a file which is associated with the editor. Moreover, if it is an XML file then you can evaluate XPath expressions.

Usage

XMLMultiPageEditor xmlEditor = new XMLMultiPageEditor("people.xml");
XMLEditorFile xmlEditorFile = xmlEditor.getAssociatedFile();
xmlEditorFile.xpath("/people/person[1]/@name"));
Clone this wiki locally