Write a Java test
Write a simple test scenario, compile it, and execute it.
Before you begin:
- The tested application is the price application used in the Quick Start: Generate and execute a test scenario.
- All source files are available in GGCDIR/src/samples directory.
- It is assumed that you have recorded a log file, for example
myapp_test2.guilog. For details of recording logs, see Recording logs and generating scenarios.
-
Begin by running the Genero Ghost Client ggcgen tool to
generate a scenario from a log file.
ggcgen java --package-name test2scenario --check-form myapp_test2.guilogThe myapp_test2_provider.java file is created in the package named "test2scenario".Note:The user actions recorded from the log are generated for the test. TheIf you do not specify a package name, the GGC creates the package name "scenario" by default.
--check-formoption in the command adds tests for the app's form names and titles:/* * Generated using Genero Ghost Client 4.00.04-202201201755 */ package test2scenario; import com.fourjs.ggc.ScenarioProvider; import com.fourjs.ggc.Scenario; import com.fourjs.ggc.Client; import com.fourjs.ggc.generator.ScenarioChecks; import java.util.concurrent.ConcurrentLinkedDeque; import com.fourjs.ggc.DependencyHandler; public class myapp_test2_provider implements ScenarioProvider { private final ConcurrentLinkedDeque<Scenario> scenarios; DependencyHandler dh = null; /* */ /* Instance initializer */ public myapp_test2_provider() { scenarios = new ConcurrentLinkedDeque<>(); /* Register scenario functions */ scenarios.add(new scenario_0()); } /* Scenario myapp_test2 id: 0 */ private class scenario_0 implements Scenario { @Override public void play(Client client) { ScenarioChecks.checkFormName(client, "price"); ScenarioChecks.checkFormTitle(client, "Product and prices"); client.wait(70); client.sortTable("sr_prices", "price".equals("null") ? null : "price", com.fourjs.ggc.auitree.SortType.get("desc")); ScenarioChecks.checkFormName(client, "price"); ScenarioChecks.checkFormTitle(client, "Product and prices"); client.wait(88); client.setTableSize("sr_prices", 12); ScenarioChecks.checkFormName(client, "price"); ScenarioChecks.checkFormTitle(client, "Product and prices"); client.wait(63); client.action("sr_prices.edit"); /* sr_prices.edit */ ScenarioChecks.checkFormName(client, "edit_price"); ScenarioChecks.checkFormTitle(client, "Edit product price"); client.wait(89); client.setFocus("formonly.price"); client.setValue("$25.00"); client.action("accept"); /* OK */ ScenarioChecks.checkFormName(client, "price"); ScenarioChecks.checkFormTitle(client, "Product and prices"); client.wait(81); client.action("cancel"); /* Exit */ } } @Override public Scenario nextScenario() { return scenarios.removeFirst(); } }In this scenario:- The code begins by importing the Genero Ghost Client Java classes.
- The
myapp_test2_provider()method implements thecom.fourjs.ggc.ScenarioProviderinterface. - A function (
scenario_0) with the id of 0 is registered in thescenariosvariable. A test may register multiple test scenarios in this variable. They run in the sequence registered. - A
scenario_0class is declared. This implements thecom.fourjs.ggc.Scenariointerface, which requires the methodplayto run the test. -
Tests for the user actions are implemented by the
com.fourjs.ggc.Clientinterface. In between these tests, there are request for the client to "wait" for periods (measured in 100 milliseconds). These are delays between user actions as recorded in the log. -
The
com.fourjs.ggc.ScenarioChecksinterface is implemented to test for the app's form names and titles.
-
To the play method of the
scenario_0class add code to retrieve the window name from the client.Note:The window name must be retrieved before the action to
cancelis triggered./* * Generated using Genero Ghost Client 4.00.04-202201201755 */ package test2scenario; import com.fourjs.ggc.ScenarioProvider; import com.fourjs.ggc.Scenario; import com.fourjs.ggc.Client; import com.fourjs.ggc.generator.ScenarioChecks; import java.util.concurrent.ConcurrentLinkedDeque; import com.fourjs.ggc.DependencyHandler; public class myapp_test2_provider implements ScenarioProvider { private final ConcurrentLinkedDeque<Scenario> scenarios; DependencyHandler dh = null; /* */ /* Instance initializer */ public myapp_test2_provider() { scenarios = new ConcurrentLinkedDeque<>(); /* Register scenario functions */ scenarios.add(new scenario_0()); } /* Scenario myapp_test2 id: 0 */ private class scenario_0 implements Scenario { @Override public void play(Client client) { ScenarioChecks.checkFormName(client, "price"); ScenarioChecks.checkFormTitle(client, "Product and prices"); client.wait(70); client.sortTable("sr_prices", "price".equals("null") ? null : "price", com.fourjs.ggc.auitree.SortType.get("desc")); ScenarioChecks.checkFormName(client, "price"); ScenarioChecks.checkFormTitle(client, "Product and prices"); client.wait(88); client.setTableSize("sr_prices", 12); ScenarioChecks.checkFormName(client, "price"); ScenarioChecks.checkFormTitle(client, "Product and prices"); client.wait(63); client.action("sr_prices.edit"); /* sr_prices.edit */ ScenarioChecks.checkFormName(client, "edit_price"); ScenarioChecks.checkFormTitle(client, "Edit product price"); client.wait(89); client.setFocus("formonly.price"); client.setValue("$25.00"); client.action("accept"); /* OK */ ScenarioChecks.checkFormName(client, "price"); ScenarioChecks.checkFormTitle(client, "Product and prices"); client.wait(81); String windowTitle = client.getUserInterface().getCurrentWindow().getTitle(); System.out.println("Window title is: " + windowTitle); System.out.println("Exiting with action 'cancel'"); client.action("cancel"); /* Exit */ } } @Override public Scenario nextScenario() { return scenarios.removeFirst(); } }