当前位置: 动力学知识库 > 问答 > 编程问答 >

java - ZATS:How to get the dynamically generated zk-Elements?

问题描述:

I'm testing a complecated zk-UI using ZATS. The Gui consists of several related ZULs and includes dynamically generated listbox which items I need to test.

This is the structure of my GUI:

equipmentView.zul -> ribbon.zul -> entityList.zul -> < listbox>

                                                    -> folderTree.zul -> < tree >

So when user manipulates the treeitems the listbox reacts and generates an entity list.

This is my Test:

 <!-- 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);

}

}

There is no way to extract these listitems by means of id(failed-GUIexception)/name(returns no listitems)/class(test failed) but i need to do it somehow. Is there any chance to extract the dynamic elements? Somebody tried IDs-Generator? Is it possible to query the element with a selector that contains wildcards? THX

网友答案:

So, to solve the problem i have just implemented IDGenerator. And used TAG_COUNTER as uuid for those of zk-Components and pages which had no ids. :)))

this piece of code to the zk.xml in WEB-INF

<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;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: