com.smartgwt.client.docs

## Interface JUnitSeleniumRC

-     public interface JUnitSeleniumRC

  ### JUnit + Selenium RC

  Let's take a look at some JUnit code designed to test a standalone version of the Smart GWT Showcase: Trees >> Editing example. The overall test class, TreeTest, contains a test, testTree1, targeted at the TreeGrid in the example, and a test, testTree2, targeted at the SearchForm/ListGrid. As recommended by the official Selenium documentation, we create the `HttpCommandProcessor` separately from `DefaultSelenium` to provide a way to invoke the new user extension command `waitForElementClickable()` which Smart GWT defines. (See our Selenium User Guide described in `AutomatedTesting`.)

  The test class TreeTest was initially generated by exporting the Selenium script for testTree1 in JUnit 4 format, but it was modified by hand as mentioned above to support the `waitForElementClickable()`. Below we look at the two test cases testTree1 and testTree2. Note that in each case, we maximize the Selenium browser window in accordance with the best practices mentioned in the User Guide.

  If you'd like to experiment with making changes to the sample JUnit code, one improvement that simplifies things is to add a myClick() function that handles both the `waitForElementClickable()` and the `click` on a supplied locator. Even just assigning each unique locator to a local Java variable so it can be reused for multiple calls will make the code simpler to follow and maintain.

        import com.thoughtworks.selenium.*;
        import org.junit.After;
        import org.junit.Before;
        import org.junit.Test;
        import java.util.regex.Pattern;

        public class TreeTest extends SeleneseTestCase {

            HttpCommandProcessor proc;

            void waitForElementClickable(String locator) {
                String[] locatorArg = {locator};
                proc.doCommand("waitForElementClickable", locatorArg);
            }

            @Before
            public void setUp() throws Exception {
                proc = new HttpCommandProcessor("localhost", 4444, "*chrome",
                    "http://localhost:8080/");
                selenium = new DefaultSelenium(proc);
                selenium.start();
            }

            @Test
            public void testTree1() throws Exception {
                selenium.open("treeEdit.html");
                selenium.windowMaximize();

       waitForElementClickable("scLocator=//TreeGrid[ID=\"employeeTree\"]/body/row[EmployeeId=4||Name=Charles%20Madigen||0]/col[fieldName=Name||0]/open");
       selenium.click("scLocator=//TreeGrid[ID=\"employeeTree\"]/body/row[EmployeeId=4||Name=Charles%20Madigen||0]/col[fieldName=Name||0]/open");
       waitForElementClickable("scLocator=//TreeGrid[ID=\"employeeTree\"]/body/row[EmployeeId=189||Name=Gene%20Porter||8]/col[fieldName=Name||0]/open");
       selenium.click("scLocator=//TreeGrid[ID=\"employeeTree\"]/body/row[EmployeeId=189||Name=Gene%20Porter||8]/col[fieldName=Name||0]/open");
       waitForElementClickable("scLocator=//TreeGrid[ID=\"employeeTree\"]/body/row[EmployeeId=264||Name=Cheryl%20Pearson||Salary=5650||10]/col[fieldName=Salary||2]");
       verifyEquals("5650",
       selenium.getText("scLocator=//TreeGrid[ID=\"employeeTree\"]/body/row[EmployeeId=264||Name=Cheryl%20Pearson||Salary=5650||10]/col[fieldName=Salary||2]"));

                checkForVerificationErrors();
            }

  In test testTree1, the idea is to:
  - Open the node for the top level employee, Charles Madigen,
  - Open the node for his report, Gene Porter, and
  - Verify that the Salary of Cheryl Pearson, who reports to Gene, is 5650
  For this test, the locators were generated by Selenium IDE for us but we did modify the code to make use of the function `waitForElementClickable()`.

  Note that though the locator for Cheryl includes the salary, it will match based on the first field, EmployeeId, which is the primary key, so the test will correctly compare the contents of Cheryl's salaray against the value 5650 and fail if it doesn't match. If for some reason your test requires matching a specific field rather than the default fields and ordering generated automatically, you can hand edit the locator.

            public void testTree2() throws Exception {
                selenium.open("treeEdit.html");
                selenium.windowMaximize();

                // Steps 1-3: Load the ListGrid with Joan's Reports
       waitForElementClickable("scLocator=//SearchForm[ID="employeeSearchForm"]/item[index=0||Class=PickTreeItem]/button/");
       selenium.click("scLocator=//SearchForm[ID="employeeSearchForm"]/item[index=0||Class=PickTreeItem]/button/");

       waitForElementClickable("scLocator=//autoID[Class=SelectionTreeMenu||index=8||length=14||classIndex=0||classLength=2||roleIndex=0||roleLength=2||scRole=menu]/body/row[Name=Charles%20Madigen]/col[fieldName=title||0]");
       selenium.mouseMove("scLocator=//autoID[Class=SelectionTreeMenu||index=8||length=14||classIndex=0||classLength=2||roleIndex=0||roleLength=2||scRole=menu]/body/row[Name=Charles%20Madigen]/col[fieldName=title||0]");

       waitForElementClickable("scLocator=//SelectionTreeMenu[ID=\"isc_SelectionTreeMenu_0_childrenSubMenu_0\"]/body/row[EmployeeId=183]/col[fieldName=title||1]");
       selenium.click("scLocator=//SelectionTreeMenu[ID=\"isc_SelectionTreeMenu_0_childrenSubMenu_0\"]/body/row[EmployeeId=183]/col[fieldName=title||1]");

          // Step 4: Sort by salary, descending, and wait for ListGrid to be redrawn with final result
       waitForElementClickable("scLocator=//ListGrid[ID=\"employeeGrid\"]/header/headerButton[fieldName=Salary]/");
       selenium.click("scLocator=//ListGrid[ID=\"employeeGrid\"]/header/headerButton[fieldName=Salary]/");
       waitForElementClickable("scLocator=//ListGrid[ID=\"employeeGrid\"]/header/headerButton[fieldName=Salary]/");
       selenium.click("scLocator=//ListGrid[ID=\"employeeGrid\"]/header/headerButton[fieldName=Salary]/");

                selenium.waitForGridDone("scLocator=//ListGrid[ID='employeeGrid']");

                // Step 5: Verify the top salary
       waitForElementClickable("scLocator=//ListGrid[ID=\"employeeGrid\"]/body/row[0]/col[fieldName=Salary||2]");
       verifyEquals("9400",
       selenium.getText("scLocator=//ListGrid[ID=\"employeeGrid\"]/body/row[0]/col[fieldName=Salary||2]"));

                checkForVerificationErrors();
            }

  In test testTree2, the idea is to:

  1. Click on the SearchForm button, revealing a Charles Madigen popup,
  2. Issue a MouseMove on the Charles Madigen popup, revealing a list of his reports,
  3. Click on his report Joan Little, filling the ListGrid with her reports,
  4. Click on the salary column header twice, sorting by descending salary, and
  5. Verify the salary in the top row (top salary) is 9400

  This test required more hand modification than the previous one. In particular three modifications were made:

  - A mouseMove command was manually added to the Selenium IDE script,
  - A call to `waitForGridDone()` was added to assure the sorting was done before we ran verifyText, and
  - We manually removed all but row qualifier from the automatically generated scLocator for step #5.
  The first modification is required because our user extensions don't record mouseMove events, and the second is needed to ensure the sorts are complete before verifyText runs--for details see the User Guide (described in `AutomatedTesting`). The final modification is just a reflection of what our intent is in step #5; we want to operate on the top row, regardless of its contents, so we don't want our locator matching based on the EmployeeId or Name fields of the records. (Matching by EmployeeId in the locator as automatically generated would make the test verify that Kelly Fetterman's salary is 9400 rather than that 9400 is the highest salary.)

            @After
            public void tearDown() throws Exception {
                selenium.stop();
            }
        }