public interface JUnitSeleniumRC
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:
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 salary 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:
waitForGridDone()
was added to assure the sorting was done before
we ran verifyText, and
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(); } }