UI映射
一个UI映射是一种机制,它存储所有的定位器的测试套件在一个地方,方便修改UI元素的路径标识符或改变在AUT。测试脚本,然后使用UI地图定位以被测试的元件。基本上,UI地图是一个存储库的测试脚本对象,对应于被测试的应用程序的UI元素。
是什么让一个UI地图有帮助吗?其主要目的是测试脚本的管理更加容易。当定位需要编辑,有一个中央位置轻松地找到对象,而不是通过搜索测试脚本代码。此外,它允许改变的标识符在一个地方,而不是在多个地方,以使更改在测试脚本,或为此事,在多个测试脚本。
总之,一个UI地图有两个显着的优点。
● UI对象使用一个集中的位置,而不是让他们分散在整个脚本。这使得脚本维护更高效。
● 神秘的HTML标识符和名称可以被赋予了更多的人类可读的名字,提高测试脚本的可读性。
考虑下面很难理解的测试代码(Java语言)。
public void testNew() throws Exception { selenium.open(http://www.test.com); selenium.type("loginForm:tbUsername", "xxxxxxxx"); selenium.click("loginForm:btnLogin"); selenium.click("adminHomeForm:_activitynew"); selenium.waitForPageToLoad("30000"); selenium.click("addEditEventForm:_IDcancel"); selenium.waitForPageToLoad("30000"); selenium.click("adminHomeForm:_activityold"); selenium.waitForPageToLoad("30000"); } |
该代码很难被那些不熟悉待测应用(AUT)页面源代码的人理解。即使是待测应用的固定用户可能也很难理解这段脚本代码的作用。一个更好的脚本可能是:
public void testNew() throws Exception { selenium.open(http://www.test.com); selenium.type(admin.username, "xxxxxxxx"); selenium.click(admin.loginbutton); selenium.click(admin.events.createnewevent); selenium.waitForPageToLoad("30000"); selenium.click(admin.events.cancel); selenium.waitForPageToLoad("30000"); selenium.click(admin.events.viewoldevents); selenium.waitForPageToLoad("30000"); } |
使用备注和空格换行、再加上UI映射标识,下面的代码更加易读:
public void testNew() throws Exception { // Open app url. selenium.open(http://www.test.com); // Provide admin username. selenium.type(admin.username, "xxxxxxxx"); // Click on Login button. selenium.click(admin.loginbutton); // Click on Create New Event button. selenium.click(admin.events.createnewevent); selenium.waitForPageToLoad("30000"); // Click on Cancel button. selenium.click(admin.events.cancel); selenium.waitForPageToLoad("30000"); // Click on View Old Events button. selenium.click(admin.events.viewoldevents); selenium.waitForPageToLoad("30000"); } |
有多种方法可以实现UI映射。可以创建一个类或结构体来存储字符串变量,每个变量存储一个定位信息。或者,使用一个文本文件来存储键值对。在Java中,一个包含键值对的属性property文件可能是最好的方法。