Saturday 31 December 2011

How to Tips and Tricks in Eclipse for Selenium automation


12. Tips and Tricks

12.1. Important Preference Settings

Eclipse can make typing more efficient, if you enable a few preference settings.
Select Window → Preferences to open the preferences settings dialog. You can use the filter box to search for specific settings.
Select Java → Editor → Typing. In the section "Automatically insert at correct position”, check the "Semicolons" setting.
You can now type a semicolon in the middle of your code and Eclipse will position it at the end of the current statement.

Type Assists which allow setting the semicolon to the right position

Eclipse can automatically format source code and organize import statement automatically before the source code is saved. You can find this setting under Java → Editor → Save Actions.

Save Actions

12.2. Templates

If you have to frequently type the same code / part of the document, you can create templates which can be activated via autocomplete (Ctrl + Space).
For example, lets assume you are frequently creating public void name(){} methods. You could define a template which creates the method body for you.
To create a template for this, select the menu Window → Preferences → Java → Editor → Templates.

Maintaining code templates

Press New. Create the template show in the following screenshot.

Creating a new code template

${cursor} indicates that the cursor should be placed at this position after applying the template.
In this example the name "npm" is your keyword.
Now every time you type "npm" in the Java editor and press Ctrl+Space the system will allow you to replace your keyword with your template.

Using code templates

12.3. Code Templates

Eclipse generates lots of source code automatically. For example, in several cases comments are added to the source code.
Select Window → Preferences → Java → Code Style → Code Templates to change the code generation templates.
In the code tree you have the templates. Select for example Code → Method Body and press "Edit" to edit this template and to remove the "todo" comment.

Removing the todos from the Java code templates

12.4. Export / Import Preferences

You can export your preference settings from one workspace via File → Export → General → Preferences.
Similarly you can import them again into another workspace.

12.5. Task Management

You can use // TODO comments in your code to add task reminders.
This indicates a task for Eclipse. You find those in the Task View of Eclipse. Via double-clicking on the task you can navigate to the corresponding code.
You can open this View via Window → Show View → Tasks.
For example, add a TODO to your MyFirstClass class to see it in the Tasks View.

    
package de.vogella.eclipse.ide.first;

public class MyFirstClass {

private static final String HELLO = "Hello Eclipse!";

public static void main(String[] args) {
// TODO Provide user interface
System.out.println(HELLO);
int sum = 0;
sum = calculateSum(sum);
System.out.println(sum);
}

private static int calculateSum(int sum) {
for (int i = 0; i <= 100; i++) {
sum += i;
}
return sum;
}
}


Close the editor for the MyFirstClass class. If you now double-click on the tasks, the Java editor opens again and the TODO comment is selected.

12.6. Working Sets

You will create more and more projects in your development career. Therefore the data in your workspace grows and it is hard to find the right information.
You can use working sets to organize your displayed projects / data. To set up your working set select the Package Explorer → open the drop-down menu → Select Working Set...

Showing the working set

Press "New" on the following dialog to create a working set.

Creating new working sets

On the next dialog select "Resource", press Next and select the projects you would like to see and give it a name.

Creating new working sets


Creating new working sets

You can now easily display only the files you want to see.

Filtering resources for the working set