ZATS:如何获得动态生成的zk-Elements?

时间:2015-11-11 11:09:52

标签: java css-selectors zk

我正在使用ZATS测试一个复杂的zk-UI。 Gui由几个相关的ZUL组成,包括动态生成的列表框,我需要测试哪些项目。 这是我的GUI的结构:

  

equipmentView.zul - > ribbon.zul - > entityList.zul - > <列表框>
   - > folderTree.zul - > <树>

因此,当用户操纵树状图时,列表框会做出反应并生成实体列表。 这是我的测试:

    <!-- Entity Detail List -->
    <listbox name="listboxName" model="@load(dm.selectedListContent)" mold="paging" pagingPosition="bottom" autopaging="true" vflex="true" emptyMessage="No Data to show" sclass="basiclist reportList"
        selectedItem="@bind(dm.selectedEntity)">

        <!-- List header -->
        <listhead children="@load(dm.listHeader)">

            <!-- Create Header Elements -->
            <template name="children" var="header">

                <!-- set width of icon or releaseSessionButton fix to 50px -->
                <listheader if="${header.macro or header.icon}" label="@load(header.label)" width="50px" />

                <!-- width for label attributes are auto  -->
                <listheader if="${not header.macro and not header.icon}" label="@load(header.label)" />

            </template>

        </listhead>

        <!-- List Content -->
        <template name="model" var="node">

            <listitem sclass="reportList" children="@load(dm.listHeader)" onDoubleClick="@command('moveToNextRibbonElement')">

                <!-- Create List Cell foreach column -->
                <template name="children" var="headerInfo">

                    <!-- Show Icon in Listcell -->
                    <listcell if="${headerInfo.icon}" iconSclass="@load(node.columnValues.get(headerInfo.name))" />

                    <!-- Show attribute value as label in listcell -->
                    <listcell if="${not headerInfo.macro and not headerInfo.icon}" visible="@load(not node.changed)" label="@load(node.columnValues.get(headerInfo.name))" />
                    <!-- Unsaved changed unsaved entities are to be displayed in italics -->
                    <listcell if="${not headerInfo.macro and not headerInfo.icon}" visible="@load(node.changed)" label="@load(node.columnValues.get(headerInfo.name))" style="font-style:italic"/>

                    <!-- Load macro releaseSessionButton in listcell -->
                    <listcell if="${headerInfo.macro}">

                        <!-- Load Macro -->
                        <releaseSessionButton currentRS="@init(sessionScope.CurrentActiveReleaseSession)" entity="@init(node.dto)" />

                    </listcell>

                </template>

            </listitem>

        </template>
    </listbox>
public class EquipmentViewTest extends ZatsTestBase {
    /**
     * logger
     */
    private Logger log = LoggerFactory.getLogger(this.getClass());

    private DesktopAgent equipmentView;
    // selectors
    private String selectNavitemSearch = "navitem[label='Search']";
    private String selectNavitemMasterdata = "navitem[label='Masterdata']";
    private String selectTabFolder = "tab[label='Folder']";
    private String selectFolderTree = "tree[id='folderTree']";
    private String selectTabFilter = "tab[label='Filter']";
    private String selectEntityList = "listbox[name='listboxName']; //listbox

    // zats-agents
    private ComponentAgent searchAgent;
    private ComponentAgent masterAgent;
    private ComponentAgent folderAgent;
    private ComponentAgent folderTreeAgent;
    private ComponentAgent filterAgent;
    private ComponentAgent listboxAgent;

    /**
     * Initialization:<br>
     * - login;<br>
     * - agents initialization and test<br>
     */
    @BeforeClass
    private void initEquipmentViewTest() {
        log.info("Test Suit 'Equipment View Test Suit': begin");
        login();

        // init equipmentView.zul desktop
        equipmentView = client.connect("/ModuleResource/src/main/resources/web/view/equipmentView.zul");
        Assert.assertNotNull(equipmentView);

        // set Agents for "Search" and "Masterdata" navitems
        searchAgent = equipmentView.query(selectNavitemSearch);
        Assert.assertNotNull(searchAgent);

        masterAgent = equipmentView.query(selectNavitemMasterdata);
        Assert.assertNotNull(masterAgent);

        // set Agents for "Folder" and "Filter" navitems
        folderAgent = equipmentView.query(selectTabFolder);
        Assert.assertNotNull(folderAgent);

        filterAgent = equipmentView.query(selectTabFilter);
        Assert.assertNotNull(filterAgent);

        // set agent for folder tree
        folderTreeAgent = equipmentView.query(selectFolderTree);
        Assert.assertNotNull(folderTreeAgent);

        // agent for the entity list
        listboxAgent = equipmentView.query(selectEntityList);
        Assert.assertNotNull(listboxAgent);

    }

    /**
     * Logout
     */
    @AfterClass
    private void finish() {
        logout();
        log.info("Test Suit 'Equipment View Test Suit': end");
    }

    /**
     * Verification of list items in the entityList.zul
     */
    private Boolean verifyEntityList(String message) {

        String expectedEmptyMessage = "No Data to show";
        Boolean empty;

        // list header test
        ComponentAgent listHeaderName = listboxAgent.query("listheader[label='Name']");
        Assert.assertNotNull(listHeaderName);
        ComponentAgent listHeaderDescr = listboxAgent.query("listheader[label='Description']");
        Assert.assertNotNull(listHeaderDescr);
        ComponentAgent listHeaderState = listboxAgent.query("listheader[label='State']");
        Assert.assertNotNull(listHeaderState);

        // empty message
        String actualEmptyMessage = listboxAgent.as(Listbox.class).getEmptyMessage();
        Assert.assertEquals("empty messages must be equal", expectedEmptyMessage, actualEmptyMessage);

        // get all list items

        List<ComponentAgent> entityList = listboxAgent.queryAll("listitem");
        Assert.assertNotNull("listbox must not be null", entityList);

        int numberOfItems = entityList.size();
        log.info("Number of the elements = " + numberOfItems+" in "+message);
        if (numberOfItems < 1) {
            Assert.assertEquals("list must be empty", numberOfItems, 0);
            log.info("Entity List is empty.");
            empty = true;
        } else {
            Assert.assertTrue("list must not be empty", numberOfItems > 0);
            empty = false;
            // TODO test the list items
        }

        return empty;

    }

    @Test(enabled = true, groups = {"TEST_GROUP_APPROVED"})
    public void testFolderNoInput() {
        String selectorFolder = "tree[id='folderTree']";

        // "Search"-navitem is selected
        Navitem searchNavitem = searchAgent.as(Navitem.class);
        Assert.assertTrue(searchNavitem.isSelected());

        // "Masterdata"-navitem is not selected
        Navitem masterNavitem = masterAgent.as(Navitem.class);
        Assert.assertFalse(masterNavitem.isSelected());

        // "Folder"-tab is selected
        Tab folderTab = folderAgent.as(Tab.class);
        Assert.assertTrue(folderTab.isSelected());

        // "Folder"-tab is not selected
        Tab filterTab = filterAgent.as(Tab.class);
        Assert.assertFalse(filterTab.isSelected());

        // tree items closed
        List<ComponentAgent> listOfTreeitemAgents = equipmentView.query(selectorFolder).queryAll("treeitem");
        for (ComponentAgent agent : listOfTreeitemAgents) {
            Assert.assertEquals(1, agent.as(Treeitem.class).getVisibleItemCount());
        }
        // list items verification of EQ
        Boolean empty = verifyEntityList("testFolderNoInput");
        Assert.assertTrue("the entity list must empty", empty);

        // TODO CookiesTest

    }

    private void openTreeTest(List<ComponentAgent> treeitems) {
        Boolean empty;
        for (ComponentAgent item : treeitems) {
            // listitem is closed
            if (item.as(Treeitem.class).getVisibleItemCount() == 1) {
                item.as(OpenAgent.class).open(true);
            }
            Assert.assertTrue("the listitem must be opend", item.as(Treeitem.class).isOpen());

            // test the entityList - opened
            empty = verifyEntityList("openTreeTest:opened");
            Assert.assertTrue("the entity list must be empty", empty);

            // test the entityList - clicked
            item.click();
            verifyEntityList("openTreeTest:clicked");

            // //test the entityList - clicked
            // item.as(SelectAgent.class).select();
            // verifyEntityList();

            // get all treeitems
            List<ComponentAgent> fullList = item.queryAll("treeitem");
            Assert.assertNotNull("list must not be empty", fullList);

            List<ComponentAgent> newTreeitems = fullList.subList(1, fullList.size());
            log.info("===>" + item.as(Treeitem.class).getLabel());
            if (newTreeitems.size() > 1) {
                openTreeTest(newTreeitems);
            }

        }
    }

    /**
     * Folder-tab test
     */
    @Test(enabled = true, groups = {"TEST_GROUP_APPROVED"}, dependsOnMethods = {"testFolderNoInput"})
    public void testFolder() {
        // test if the tab is selected
        Tab folderTab = folderAgent.as(Tab.class);
        if (!folderTab.isSelected()) {
            // if tab is not selected - select
            folderAgent.select();
        }
        Assert.assertTrue("tab must be selected", folderTab.isSelected());

        // get the folders
        List<ComponentAgent> folders = folderTreeAgent.queryAll("treeitem");
        Assert.assertNotNull(folders);

        // test the tree content
        openTreeTest(folders);

    }
}

无法通过id(failed-GUIexception)/ name(不返回listitems)/ class(测试失败)来提取这些列表项,但我需要以某种方式进行。有没有机会提取动态元素?有人试过ID-Generator?是否可以使用包含通配符的选择器查询元素? THX

1 个答案:

答案 0 :(得分:1)

所以,为了解决这个问题,我刚刚实现了IDGenerator。并使用TAG_COUNTER作为zu-Components和没有id的页面的uuid。 :)))

将这段代码发送到WEB-INF

中的zk.xml
<system-config>
    <id-generator-class>com.mmm.mmm.initializer.IDGenerator</id-generator-class>
</system-config>



@Override
public String nextComponentUuid(Desktop desktop, Component comp, ComponentInfo compInfo) {

    // get last id
    String number = (String) desktop.getAttribute(LAST_ID);
    if (number == null) {
        number = "0";           
    }

    // increment last id
    int counter = Integer.parseInt(number);
    counter++;

    // write last id back to desktop
    desktop.setAttribute(LAST_ID, String.valueOf(counter));

    String componentID = "";
    String id = comp.getId();
    String tag = null;
    if(compInfo != null){
        tag = compInfo.getTag();
    }       

    // add id if known
    if (id != null && !id.isEmpty()) {
        componentID = componentID + id;
    }

    // add tag if known
    if (tag != null && !tag.isEmpty()) {

        if (componentID.isEmpty()) {
            componentID = componentID + tag;
        } else {
            componentID = componentID + ":" + tag;
        }

    }

    // add counter
    if (componentID.isEmpty()) {
        componentID = componentID + "zkcomp_" + counter;
    } else {
        componentID = componentID + ":" + counter;
    }

    return componentID;
}