`
jiangduxi
  • 浏览: 453169 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Selenium IDE测试ExtJs一种测试解决办法

阅读更多
最近发现要使用ExtJs测试其实很麻烦,因为ExtJs的id是变化的,而Selenium IDE录制完后,ExtJs的下次打开页面,就无法进行回放了。因此很麻烦,不过通过一些网友进行交流得到如下一些测试方法:
引用

  1. 使用Xpath进行定位
  2. 适当使用selenium..runScript()方法
  3.应用Selenium IDE的extensions

下面我只针对第3种方法进行总结。
  如果你想知道第三种方法的原理:请你在你的firefox浏览器上
输入
引用

chrome://selenium-ide/content/recorder-handlers.js
chrome://selenium-ide/content/locatorBuilders.js

通过上面两个js你大致可以了解Selenium IDE录制的原理。
要用Selen现在我ium IDE的extensions。
步骤:在Selenium IDE的Options进入,在点击Options显示如下一个节目

在该页面你要注意:
Selenium Core extensions(user-extensions.js)
Selenium IDE extensions
下面我们准备两个脚本来进行extesions。
  Selenium.prototype.assertExtEqual = function(expression, text) {
	/**
	 * the euqal assertion of ext
	 * @param expression ext expression , just like "button1.text" or "text1.getValue()"
	 * @param String target value
	 */
	var result = this.extEval(expression)
	if (result != text) {
		Assert.fail("the value of [" + result + "] " + expression
				+ " is not equal with " + text);
	}
};

Selenium.prototype.assertExtGreaterThan = function(expression, text) {
	/**
	 * the greater than assertion of ext
	 * @param expression ext expression , just like "button1.text" or "text1.getValue()"
	 * @param String target value
	 */
	var result = this.extEval(expression)
	if (result <= text) {
		Assert.fail("the value of [" + result + "] " + expression
				+ " is not greater than " + text);
	}
}

Selenium.prototype.assertExtGreaterEqualThan = function(expression, text) {
	/**
	 * the greater and equal than assertion of ext
	 * @param expression ext expression , just like "button1.text" or "text1.getValue()"
	 * @param String target value
	 */
	var result = this.extEval(expression)
	if (result < text) {
		Assert.fail("the value of [" + result + "] " + expression
				+ " is not greater equal than " + text);
	}
}

Selenium.prototype.assertExtLessThan = function(expression, text) {
	/**
	 * the less than assertion of ext
	 * @param expression ext expression , just like "button1.text" or "text1.getValue()"
	 * @param String target value
	 */
	var result = this.extEval(expression)
	if (result >= text) {
		Assert.fail("the value of [" + result + "] " + expression
				+ " is not less than " + text);
	}
}

Selenium.prototype.assertExtLessEqualThan = function(expression, text) {
	/**
	 * the less and equal than assertion of ext
	 * @param expression ext expression , just like "button1.text" or "text1.getValue()"
	 * @param String target value
	 */
	var result = this.extEval(expression)
	if (result > text) {
		Assert.fail("the value of [" + result + "] " + expression
				+ " is not less equal than " + text);
	}
}

Selenium.prototype.doExecuteExtFunction = function(expression, text) {
	/**
	 * do ext function ,if the expression end with ")" ,the params is not useful
	 * @param expression ext expression return a ext function, just like "button1.getText" or "text1.getValue()"
	 * @param String params ,just like "a,b,c"
	 */
	if (expression.lastIndexOf(")") == expression.length - 1) {
		this.extEval(expression);
	} else {
		var scopeObj = this.extEval(expression.substring(0, expression
				.lastIndexOf(".")));
		var func = this.extEval(expression);
		if (typeof(func) != "function") {
			Assert.fail("the value of [" + func + "] " + expression
					+ " is not a function");
		}
		var params = [];
		if (text) {
			params = text.split(",");
		}
		try {
			func.apply(scopeObj, params);
		} catch (e) {
			Assert.fail("error execute function [" + func + "] " + expression);
		}
	}
}

Selenium.prototype.assertExtTrue = function(expression) {
	/**
	 * the true assertion of ext
	 * @param expression ext expression , just like "button1.hidden"
	 */
	var result = this.extEval(expression);
	if (result !== true) {
		Assert.fail("the value of [" + result + "] " + expression
				+ " is not true");
	}
}

Selenium.prototype.assertExtFalse = function(expression) {
	/**
	 * the false assertion of ext
	 * @param expression ext expression , just like "button1.hidden"
	 */
	var result = this.extEval(expression);
	if (result !== true) {
		Assert.fail("the value of [" + result + "] " + expression
				+ " is not false");
	}
}


Selenium.prototype.assertExtNull = function(expression, text) {
	/**
	 * the null assertion of ext
	 * @param expression ext expression , just like "button1.text"
	 */
	var result = this.extEval(expression);
	if (result !== null) {
		Assert.fail("the value of [" + result + "] " + expression
				+ " is not null");
	}
}


Selenium.prototype.assertExtNotNull = function(expression, text) {
	/**
	 * the not null assertion of ext
	 * @param expression ext expression , just like "button1.text"
	 */
	var result = this.extEval(expression);
	if (result === null) {
		Assert.fail("the value of [" + result + "] " + expression + " is null");
	}
}


Selenium.prototype.assertExtUndefined = function(expression, text) {
	/**
	 * the undefined assertion of ext
	 * @param expression ext expression , just like "button1"
	 */
	var result = this.extEval(expression);
	if (result !== undefined) {
		Assert.fail("the value of [" + result + "] " + expression
				+ " is not undefined");
	}
}


Selenium.prototype.assertExtNotUndefined = function(expression, text) {
	/**
	 * the not undefined assertion of ext
	 * @param expression ext expression , just like "button1"
	 */
	var result = this.extEval(expression);
	if (result === undefined) {
		Assert.fail("the value of [" + result + "] " + expression
				+ " is undefined");
	}
}


Selenium.prototype.assertExtPresent = function(expression, text) {
	/**
	 * the present assertion of ext
	 * @param expression ext expression , just like "button1"
	 */
	var result = this.extEval(expression);
	if (result == null || result == undefined) {
		Assert.fail("the value of [" + result + "] " + expression
				+ " is not present");
	}
}

Selenium.prototype.assertExtNotPresent = function(expression, text) {
	/**
	 * the not present assertion of ext
	 * @param expression ext expression , just like "button1"
	 */
	var result = this.extEval(expression);
	if (result != null || result != undefined) {
		Assert.fail("the value of [" + result + "] " + expression
				+ " is present");
	}
}

Selenium.prototype.assertExtMatches = function(expression, text) {
	/**
	 * the matches assertion of ext
	 * @param expression ext expression , just like "button1.text" or "text1.getValue()"
	 * @param String target value
	 */
	var result = this.extEval(expression);
	var reg = new RegExp(text);
	if (!reg.test(result)) {
		Assert.fail("the value of [" + result + "] " + expression
				+ " is not match " + text);
	}
}

Selenium.prototype.assertExtContains = function(expression, text) {
	/**
	 * the contains assertion of ext
	 * @param expression ext expression , just like "button1.text" or "text1.getValue()"
	 * @param String target value
	 */
	var result = this.extEval(expression);
	if (typeof(result) == "undefined" || result == null) {
		Assert.fail("the value of " + expression + " dos not contains " + text);
	} else if (result.indexOf) {
		if (result.indexOf(text) < 0) {
			Assert.fail("the value of [" + result + "] " + expression
					+ " dos not contains " + text);
		}
	} else {
		Assert.fail("the value of [" + result + "] " + expression
				+ " is not a String or Array");
	}
}

Selenium.prototype.assertExtTypeof = function(expression, text) {
	/**
	 * the typeof assertion of ext
	 * @param expression ext expression , just like "button1.text" or "text1.getValue()"
	 * @param String target value
	 */
	var type = typeof(this.extEval(expression));
	if (type != text) {
		Assert.fail("the type of [" + type + "] " + expression + " is not "
				+ text);
	}
}

PageBot.prototype.getWrappedWindow = function(extpath) {
	var win = this.getCurrentWindow() || {};
	return win.wrappedJSObject;
}


Selenium.prototype.getWrappedWindow = function(extpath) {
	return this.browserbot.getWrappedWindow();
}


Selenium.prototype.extEval = function(expression) {
	var script = expression;
	if (expression) {
		var expArr = expression.split(".");
		expArr[0] = "(window.Ext.getCmp('" + expArr[0] + "')||window.Ext.get('"
				+ expArr[0] + "')||window.Ext.StoreMgr.lookup('" + expArr[0]
				+ "'))";
		expression = expArr.join(".");
	}
	try {
		return this.doEval(expression);
	} catch (e) {
		throw new SeleniumError("the expression " + script
				+ " is not a Ext expression !");
	}
};
// I have to rewrite the eval function to get the context of window
Selenium.prototype.doEval = function(expression) {
	/**
	 * execute js ecpression
	 *
	 * @param {Object}
	 *            expression js expression
	 */
	try {
		var win = this.getWrappedWindow();
		var result = eval(expression, win);
		return result;
	} catch (e) {
		throw new SeleniumError("the expression " + expression
				+ " is not a Ext expression !");
	}
}

该代码命名为user-extensions.js通过Selenium Core extensions(user-extensions.js)后的Browser选择中自己写的user-extensions.js.或者也可以不要改js但是下面这个js是一定要的(放入Selenium IDE extensions)。

var EXT_PREFIX = "ext-gen";
function findExtLocator(e) {
	function getElementIndex(el, p) {
		var childs = p.childNodes;
		for (var i = 0; i < childs.length; i++) {
			var curr = childs[i];
			if (curr == el) {
				return "[" + (i + 1) + "]";
			}
		}
	}
	if (e.id) {
		var elId = e.id;
		if (elId.indexOf(EXT_PREFIX) == 0) {
			var currNode = e;
			var locator = "";
			while (currNode && currNode.tagName.toLowerCase() != "body") {
				parentNode = currNode.parentNode;
				locator = this.relativeXPathFromParent(currNode) + locator;//
				if (parentNode.id && parentNode.id.length > 0
						&& parentNode.id.indexOf(EXT_PREFIX) != 0) {
					locator = "//" + parentNode.tagName + "[@id='"
							+ parentNode.id + "']" + locator;
					return locator;
				}
				currNode = currNode.parentNode;
			}
		}
	}
	return null;
}
LocatorBuilders.add('ext', findExtLocator);
// You can change the priority of builders by setting LocatorBuilders.order.
LocatorBuilders.order = ['ext', 'id', 'link', 'name', 'dom:name', 'xpath:link',
		'xpath:img', 'xpath:attributes', 'xpath:href', 'dom:index',
		'xpath:position'];

该段代码是必须的。产生的效果是让Selenium IDE在录制Extjs的时候能够回放成功。


总结下使用步骤:
引用

  第一:将第一段js代码放入Selenium Core extensions(user-extensions.js)
  第二:将第二段js代码放入Selenium IDE extensions 这是必须的。

现在你可以使用Selenium IDE来测试ExtJs了。你可以看看是不是可以正常进行录制和回放。ExtJs没有ZK厚道,ZK官网提供了一个接口来实现Selenium IDE的测试。
2
0
分享到:
评论
2 楼 feisi0003731843 2012-08-30  
不好意思我没有重启,重启后好多了,可有的地方回放还是不成功的。
1 楼 feisi0003731843 2012-08-30  
这个好像不行吧,我试过了不好使啊。还是用id来做的。不能用啊。

相关推荐

    Selenium IDE测试ExtJs一种测试解决办法.docx

    在IT测试领域,尤其是Web应用程序自动化测试中,Selenium IDE是一个非常流行的工具,它允许用户录制和回放用户的浏览器操作,以验证Web应用的功能。然而,对于使用动态ID的库,如ExtJs,Selenium IDE的默认录制和...

    selenium IDE最新版本插件资源安装包(免费)

    Selenium IDE是一款强大的自动化测试工具,专为Web应用程序设计,尤其受到测试工程师的青睐。它是一个免费的插件,能够录制、回放和编辑测试脚本,极大地提升了测试效率。在本文中,我们将深入探讨Selenium IDE的...

    Selenium_IDE_官方文档翻译.pdf

    Selenium IDE 官方文档翻译 本文档翻译自 Selenium IDE 官方文档,旨在帮助...Selenium IDE 是一款功能强大且易于使用的 web 自动化测试工具,通过本文档,读者可以快速了解和掌握 Selenium IDE 的使用方法和功能。

    chrome 浏览器 selenium IDE插件

    Selenium IDE 是一款强大的自动化测试工具,专为 Web 应用程序设计。它基于 Firefox 和 Chrome 浏览器的插件形式存在,允许用户录制、编辑和回放浏览器操作,以进行功能性和回归测试。在 Chrome 浏览器上使用的 ...

    ,selenium IDE安装包及离线安装教程

    在这种情况下,离线安装包提供了一种替代方案,使得用户仍能顺利安装Selenium IDE进行自动化测试工作。 在"Selenium IDE"这个"标签"下,我们可以深入探讨Selenium IDE的一些主要特点和功能: 1. **录制与回放**:...

    Selenium IDE 插件 免费下载

    Selenium IDE 插件

    火狐Firefox浏览器安装Selenium_IDE的步骤

    Selenium IDE是一款基于Firefox的集成开发环境工具,它允许用户通过简单的点击操作来记录和回放测试脚本,无需编写任何代码。这对于刚开始接触自动化测试的用户来说是一个很好的起点。同时,Selenium IDE还可以将...

    seleniumIDE2.9.1-xpi下载

    在给定的标题和描述中,我们了解到当前的Firefox浏览器不再支持旧版本的Selenium IDE,因此需要寻找兼容的解决方案,特别是对于那些依赖于Selenium IDE导出测试脚本的用户。 **Selenium IDE的功能和特点** 1. **...

    Selenium IDE是firefox的一个插件,可以帮助刚入门的自动化测试供测试,可实现脚本的录制、开发、回放

    Selenium IDE是firefox的一个插件,可以帮助刚入门的自动化测试供测试,在脚本语言不太熟练的情况下,可以通过Selenium IDE实现脚本的录制、开发、回放。 众所周知,「Selenium IDE」是一种记录和回放工具。现在它将...

    Selenium IDE

    总的来说,Selenium IDE是Web应用自动化测试的重要工具,离线包提供了一种在无法访问官方渠道时安装和学习该工具的途径。通过详细的安装教程和使用说明,用户可以有效地掌握这一工具,从而提升Web应用的测试质量和...

    selenium IDE 安装包 。。。。

    **Selenium IDE:一款强大的自动化测试工具** Selenium IDE(集成开发环境)是Selenium测试框架的一个重要组成部分,主要用于Web应用程序的功能自动化测试。它是一款基于Firefox浏览器的插件,允许用户录制、回放和...

    selenium IDE使用手册

    selenium IDE使用手册,写的很好,请大家参考,多多学习

    Selenium IDE.zip

    Selenium IDE是一款强大的自动化测试工具,专为Web应用程序设计,尤其在Chrome浏览器中表现卓越。它基于Mozilla Firefox的原生开发环境,但通过修改扩展名,我们也可以在Chrome浏览器上使用。这个压缩包"**Selenium ...

    selenium-ide-2.5.0工具

    Selenium IDE 2.5.0 是一个强大的自动化测试工具,专为Web应用程序设计,尤其适合功能测试。这款工具是Selenium Suite的一部分,基于Firefox浏览器的插件形式存在,允许用户录制、编辑和回放测试脚本。在本文中,...

    selenium_ide-2.9.1-fx

    Selenium IDE 是一款强大的自动化测试工具,主要用于Web应用程序的测试。它基于Firefox浏览器插件的形式存在,版本2.9.1是它的一个经典版本。在本文中,我们将深入探讨Selenium IDE的功能、用途以及如何使用它来提升...

    selenium IDE安装包 V2.9.0 .rar

    Selenium IDE是一款强大的自动化测试工具,专为Web应用程序设计,它是Selenium套件的一部分。这个压缩包“selenium IDE安装包 V2.9.0 .rar”包含了Selenium IDE的版本2.9.0,这是一个相对早期但稳定版本的工具。下面...

    Selenium IDE AND Firefoxbug

    在自动化测试领域,Selenium IDE是一个非常重要的工具,它是Selenium套件的一部分,专为Firefox浏览器设计。它作为一个直观的录制和回放工具,使得测试脚本的创建变得简单快捷。然而,由于浏览器的更新和扩展的兼容...

    selenium IDE工具界面详解

    Selenium IDE 工具界面详解 ...Selenium IDE 是一个简单易用的工具,能够快速地创建测试脚本。但是,它的功能局限,需要用户具备基本的 HTML、Javascript 和 DOM 知识,并且需要注意测试脚本的保存格式。

Global site tag (gtag.js) - Google Analytics