Skip to content

Commit

Permalink
Tweaked GUI tests to convert more code into PO pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
damithc committed Jul 8, 2016
1 parent 4984d26 commit e0b26c0
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 43 deletions.
21 changes: 9 additions & 12 deletions src/test/java/guitests/FullSystemTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package guitests;

import guitests.guihandles.EditPersonDialogHandle;
import guitests.guihandles.ManageTagsDialogHandle;
import guitests.guihandles.NewTagDialogHandle;
import guitests.guihandles.TagPersonDialogHandle;
import guitests.guihandles.*;
import org.junit.Test;

import static org.junit.Assert.*;
Expand Down Expand Up @@ -51,17 +48,17 @@ public void scenarioOne() {
//Remove filter
personListPanel.enterFilterAndApply("");


//TODO: convert the code below to use page object pattern

//Ensure "About" dialog opens
guiRobot.clickOn("Help").clickOn("About").clickOn("OK");
AboutDialogHandle aboutDialog = mainMenu.clickOn("Help", "About").as(AboutDialogHandle.class);
aboutDialog.clickOk();

//Create a new person Ming Lee, check that last name cannot be blank
guiRobot.clickOn("New")
.clickOn("#firstNameField").write("Ming").clickOn("OK")
.targetWindow("Invalid Fields").clickOn("OK")
.clickOn("#lastNameField").write("Lee").clickOn("OK");
EditPersonDialogHandle newPersonDialog = personListPanel.clickNew();
newPersonDialog.enterFirstName("Ming").clickOk();
newPersonDialog.dissmissErrorMessage("Invalid Fields");
newPersonDialog.enterLastName("Lee");
newPersonDialog.clickOk();
assertTrue(personListPanel.contains("Ming", "Lee"));

//Create a new tag 'company' using the 'Manage Tags' dialog
ManageTagsDialogHandle manageTagsDialog = mainMenu.clickOn("Tags", "Manage Tags")
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/guitests/GuiTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public class GuiTestBase {

GuiRobot guiRobot = new GuiRobot();
GuiRobot guiRobot = new GuiRobot(); //TODO: remove this from here, only *Handle objects should use the robot

/* Handles to GUI elements present at the start up are created in advance
* for easy access from child classes.
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/guitests/KeyBindingsGuiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ public void keyBindings() {

//======== hotkeys ============================

guiRobot.push(bindings.APP_MINIMIZE_HOTKEY.get(0));
mainGui.use_APP_MINIMIZE_HOTKEY();
assertTrue(mainGui.isMinimized());

guiRobot.push(bindings.APP_RESIZE_HOTKEY.get(0)); //maximize the window
mainGui.use_APP_RESIZE_HOTKEY(); //maximize the window
assertTrue(mainGui.isMaximized());

guiRobot.push(bindings.APP_RESIZE_HOTKEY.get(0)); //set window to default size
mainGui.use_APP_RESIZE_HOTKEY(); //set window to default size
assertTrue(mainGui.isDefaultSize());

}
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/guitests/guihandles/AboutDialogHandle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package guitests.guihandles;

import guitests.GuiRobot;
import javafx.stage.Stage;

/**
* Provides a handle to the About dialog
*/
public class AboutDialogHandle extends GuiHandle {
public AboutDialogHandle(GuiRobot guiRobot, Stage primaryStage) {
super(guiRobot, primaryStage);
}
}
7 changes: 4 additions & 3 deletions src/test/java/guitests/guihandles/EditPersonDialogHandle.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package guitests.guihandles;

import guitests.GuiRobot;
import javafx.stage.Stage;

public class EditPersonDialogHandle extends GuiHandle {

Expand All @@ -12,8 +13,8 @@ public class EditPersonDialogHandle extends GuiHandle {
private String tagSearchFieldId = "#tagList";
private String cancelButtonText = "Cancel";

public EditPersonDialogHandle(GuiRobot guiRobot) {
super(guiRobot);
public EditPersonDialogHandle(GuiRobot guiRobot, Stage primaryStage) {
super(guiRobot, primaryStage);
}

public String getFirstName(){
Expand Down Expand Up @@ -64,7 +65,7 @@ public EditPersonDialogHandle enterGithubId(String githubId) {
public TagPersonDialogHandle openTagPersonDialog() {
guiRobot.clickOn(tagSearchFieldId)
.sleep(200); // wait for opening animation
return new TagPersonDialogHandle(guiRobot);
return new TagPersonDialogHandle(guiRobot, primaryStage);
}

}
14 changes: 11 additions & 3 deletions src/test/java/guitests/guihandles/GuiHandle.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javafx.scene.Node;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.stage.Stage;

import java.lang.reflect.Constructor;

Expand All @@ -13,18 +14,20 @@
*/
public class GuiHandle {
protected final GuiRobot guiRobot;
protected final Stage primaryStage;

public GuiHandle(GuiRobot guiRobot){
public GuiHandle(GuiRobot guiRobot, Stage primaryStage) {
this.guiRobot = guiRobot;
this.primaryStage = primaryStage;
}

/**
* Creates an object of the specified GuiHandle child class.
*/
public <T> T as(Class<? extends GuiHandle> clazz) {
try {
Constructor<?> ctor = clazz.getConstructor(GuiRobot.class);
Object object = ctor.newInstance(new Object[] { guiRobot });
Constructor<?> ctor = clazz.getConstructor(GuiRobot.class, Stage.class);
Object object = ctor.newInstance(new Object[] { guiRobot, primaryStage });
return (T)object;
} catch (Exception e) {
throw new RuntimeException("Cannot create gui handle of type " + clazz.getName(), e);
Expand Down Expand Up @@ -99,4 +102,9 @@ public GuiHandle rightClickOn(String id) {
public void dismiss() {
pressEsc();
}

public void dissmissErrorMessage(String errorDialogTitle) {
guiRobot.targetWindow(errorDialogTitle);
clickOk();
}
}
25 changes: 16 additions & 9 deletions src/test/java/guitests/guihandles/MainGuiHandle.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
package guitests.guihandles;

import address.keybindings.Bindings;
import guitests.GuiRobot;
import javafx.stage.Stage;

/**
* Provides a handle for the main GUI.
*/
public class MainGuiHandle {
private final GuiRobot guiRobot;
private MainMenuHandle mainMenu;
private Stage primaryStage;
public class MainGuiHandle extends GuiHandle{

public MainGuiHandle(GuiRobot guiRobot, Stage primaryStage) {
this.guiRobot = guiRobot;
this.mainMenu = new MainMenuHandle(guiRobot);
this.primaryStage = primaryStage;
super(guiRobot, primaryStage);
}

public PersonListPanelHandle getPersonListPanel(){
return new PersonListPanelHandle(guiRobot);
return new PersonListPanelHandle(guiRobot, primaryStage);
}

public MainMenuHandle getMainMenu() {
return mainMenu;
return new MainMenuHandle(guiRobot, primaryStage);
}


Expand All @@ -37,4 +33,15 @@ public boolean isMaximized() {
public boolean isDefaultSize() {
return !primaryStage.isMaximized() && !primaryStage.isIconified();
}

public void use_APP_MINIMIZE_HOTKEY() {
guiRobot.push(new Bindings().APP_MINIMIZE_HOTKEY.get(0));
guiRobot.sleep(1000);
}

public void use_APP_RESIZE_HOTKEY() {
guiRobot.push(new Bindings().APP_RESIZE_HOTKEY.get(0));
guiRobot.sleep(1000);
}

}
5 changes: 3 additions & 2 deletions src/test/java/guitests/guihandles/MainMenuHandle.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package guitests.guihandles;

import guitests.GuiRobot;
import javafx.stage.Stage;

import java.util.Arrays;

/**
* Provides a handle to the main menu of the app.
*/
public class MainMenuHandle extends GuiHandle {
public MainMenuHandle(GuiRobot guiRobot) {
super(guiRobot);
public MainMenuHandle(GuiRobot guiRobot, Stage primaryStage) {
super(guiRobot, primaryStage);
}

public GuiHandle clickOn(String... menuText) {
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/guitests/guihandles/ManageTagsDialogHandle.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package guitests.guihandles;

import guitests.GuiRobot;
import javafx.stage.Stage;

/**
* Provides a handle to the dialog used for managing tags.
*/
public class ManageTagsDialogHandle extends GuiHandle {
public ManageTagsDialogHandle(GuiRobot guiRobot) {
super(guiRobot);
public ManageTagsDialogHandle(GuiRobot guiRobot, Stage primaryStage) {
super(guiRobot, primaryStage);
}

}
5 changes: 3 additions & 2 deletions src/test/java/guitests/guihandles/NewTagDialogHandle.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package guitests.guihandles;

import guitests.GuiRobot;
import javafx.stage.Stage;

/**
* Provides a handle to the dialog used for adding a new tag.
Expand All @@ -9,8 +10,8 @@ public class NewTagDialogHandle extends GuiHandle {

private String tagNameFieldId = "#tagNameField";

public NewTagDialogHandle(GuiRobot guiRobot) {
super(guiRobot);
public NewTagDialogHandle(GuiRobot guiRobot, Stage primaryStage) {
super(guiRobot, primaryStage);
}

public String getTagName() {
Expand Down
15 changes: 11 additions & 4 deletions src/test/java/guitests/guihandles/PersonListPanelHandle.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import guitests.GuiRobot;
import javafx.scene.control.ListView;
import javafx.scene.input.KeyCode;
import javafx.stage.Stage;
import org.testfx.api.FxRobot;

/**
Expand All @@ -16,8 +17,8 @@ public class PersonListPanelHandle extends GuiHandle {
private String filterFieldId = "#filterField";
private String personListViewId = "#personListView";

public PersonListPanelHandle(GuiRobot guiRobot) {
super(guiRobot);
public PersonListPanelHandle(GuiRobot guiRobot, Stage primaryStage) {
super(guiRobot, primaryStage);
}

public void use_LIST_ENTER_SHORTCUT() {
Expand Down Expand Up @@ -107,13 +108,13 @@ public EditPersonDialogHandle use_PERSON_EDIT_ACCELERATOR() {
guiRobot.push(new Bindings().PERSON_EDIT_ACCELERATOR);
guiRobot.sleep(500);
guiRobot.targetWindow(EditPersonDialogHandle.TITLE);
return new EditPersonDialogHandle(guiRobot);
return new EditPersonDialogHandle(guiRobot, primaryStage);
}

public TagPersonDialogHandle use_PERSON_TAG_ACCELERATOR() {
guiRobot.push(new Bindings().PERSON_TAG_ACCELERATOR);
guiRobot.sleep(500);
return new TagPersonDialogHandle(guiRobot);
return new TagPersonDialogHandle(guiRobot, primaryStage);
}

public void clickOnPerson(String personName) {
Expand All @@ -127,4 +128,10 @@ public void enterFilterAndApply(String filterText) {
public String getFilterText() {
return getTextFieldText(filterFieldId);
}

public EditPersonDialogHandle clickNew() {
guiRobot.clickOn("#newButton");
guiRobot.sleep(500);
return new EditPersonDialogHandle(guiRobot, primaryStage);
}
}
5 changes: 3 additions & 2 deletions src/test/java/guitests/guihandles/TagPersonDialogHandle.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import guitests.GuiRobot;
import javafx.scene.input.KeyCode;
import javafx.stage.Stage;

/**
* Provides a handle for the dialog used for tagging a person.
Expand All @@ -10,8 +11,8 @@ public class TagPersonDialogHandle extends GuiHandle {

private String tagSearchFieldId = "#tagSearch";

public TagPersonDialogHandle(GuiRobot guiRobot) {
super(guiRobot);
public TagPersonDialogHandle(GuiRobot guiRobot, Stage primaryStage) {
super(guiRobot, primaryStage);
}

public TagPersonDialogHandle enterSearchQuery(String queryText) {
Expand Down

0 comments on commit e0b26c0

Please sign in to comment.