使用量角器进行AngularJS测试-即使已使用'undefined'元素,也无法读取未定义的属性'click()' [英] AngularJS testing with Protractor- Cannot read property 'click()' of undefined, even though 'undefined' element has already been used

查看:86
本文介绍了使用量角器进行AngularJS测试-即使已使用'undefined'元素,也无法读取未定义的属性'click()'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为AngularJS应用开发一个测试套件,并且在编写我似乎无法解决的测试脚本之一时遇到了一个奇怪的问题.

I am developing a test suite for an AngularJS app, and have hit a strange problem while writing one of the test scripts which I can't seem to work out.

测试是:

it('should navigate to the Charts page', function() {
    console.log("Start Charts page test");
    browser.waitForAngularEnabled(false);
    browser.wait(EC.elementToBeClickable(chartsMenuBtn), 5000).then(
        browser.actions().
        mouseMove(chartsMenuBtn).
        chartsMenuBtn.click().
        browser.wait(EC.urlIs(site + '/#/charts'), 5000).
        perform()
    );
})

运行测试时,测试失败,原因如下:

When I run the test, it fails, giving the reason:

失败:无法读取未定义的属性点击"

Failed: Cannot read property 'click' of undefined

调用click()的元素是chartsMenuBtn,我已使用以下命令全局定义了该元素:

The element that click() is being called on is chartsMenuBtn, which I have defined globally with:

var chartsMenuBtn = element(by.linkText("Charts"));

因此该元素已明确定义.最重要的是,量角器似乎只是认为在调用click()时它是未定义的,但是实际上在它之前被使用了两次,因此,如果在量角器是声称是,肯定会在之前的两个用法中也未定义...?

So the element clearly is defined. On top of that, Protractor only seems to think that it's undefined at the point at which a call to click() is called on it- but it was actually used twice directly preceding that, so if it was undefined at the point that Protractor is claiming it is, it would surely have been undefined in the two prior uses too...?

我实际上在browser.wait(EC.elementToBeClickable(chartsMenuBtn), 5000).then(行之前放置了browser.call(console.log("chartsMenuBtn: ", chartsMenuBtn));行,只是为了确保它不是未定义的,并且这会在控制台中显示以下内容:

I actually put a browser.call(console.log("chartsMenuBtn: ", chartsMenuBtn)); line in just before the browser.wait(EC.elementToBeClickable(chartsMenuBtn), 5000).then( line, just to make sure that it was not undefined, and this prints the following in my console:

chartsMenuBtn:  ElementFinder {
browser_:
 ProtractorBrowser {
   controlFlow: [Function],
   schedule: [Function],
   setFileDetector: [Function],
   getExecutor: [Function],
   getSession: [Function],
   getCapabilities: [Function],
   quit: [Function],
   actions: [Function],
   touchActions: [Function],
   executeScript: [Function], ...

清楚地说明了该元素是在使用该元素的点定义的.

showing that the element clearly is defined at the point at which it is used.

任何人都不知道该测试失败的真正原因是什么?如何使它正常运行?

Anyone have any ideas what the actual reason why this test is failing is? How can I get it to run correctly?

推荐答案

正如注释中所指出的,您的then()看起来有些奇怪.同样,您实际上不需要它.

As already pointed in the comments your then() looks a bit strange. Also you actually don't need it in your case.

这是我的建议:

it('should navigate to the Charts page', function() {
    console.log("Start Charts page test");
    browser.waitForAngularEnabled(false);
    browser.wait(EC.elementToBeClickable(chartsMenuBtn), 5000);
    //browser.wait(EC.invisibilityOf(blockingElement), 5000);
    chartsMenuBtn.click(); //works, if the element is defined as you said
    browser.wait(EC.urlIs(site + '/#/charts'), 5000);
})

如果(如您所说)按钮不可单击,请在按click()时尝试执行browser.wait(EC.invisibilityOf(blockingElement), 5000).

If (as you say) your button is not clickable, when you click() it, try to do browser.wait(EC.invisibilityOf(blockingElement), 5000).

与无法读取未定义的属性"相比,"isNotClickable"消息的错误程度要小得多. "isNotClickable"表示它是有效的代码,但是该网页当前不允许您执行此操作. 无法读取属性"告诉您,您的代码是错误的.

That "isNotClickable" Message is bytheway less wrong than "cannot read property of undefined". "isNotClickable" says it's valid code, but the webpage currently does not let you execute this action. "cannot read property" tells you, your code is wrong.

通常,如果您仅在此处检查量角器API ,就可以找到通常的解决方案.此处列出了所有有用的命令.

In general you find your solutions usually, if you just check the Protractor API here. All useful commands are listed there.

这篇关于使用量角器进行AngularJS测试-即使已使用'undefined'元素,也无法读取未定义的属性'click()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆