首页 > 解决方案 > 无法使用 Jasmine(量角器)定位元素

问题描述

登录页面是一个非角度页面,当用户登录时,他/她会被重定向到主页。主页使用角度。

对于登录,我使用过

        browser.ignoreSynchronization = true;
        var loginTxt = browser.driver.findElement(by.id("userNameInput"));
        var pwdTxt = browser.driver.findElement(by.id("passwordInput"));
        var signInBtn = browser.driver.findElement(by.id("submitButton"));
        loginTxt.sendKeys("test1");
        pwdTxt.sendKeys("password!");
        signInBtn.click();

用户登录成功,我使用以下代码与主页中的元素进行交互。

        browser.ignoreSynchronization = false;
        var el = element(by.className("btn item-desktop-only inactive-btn"))
        el.click();

错误代码:消息:失败:等待量角器与页面同步时出错:“angularJS 可测试性和角度可测试性都未定义。这可能是因为这是一个非角度页面,或者因为您的测试涉及客户端导航,这可能会干扰量角器的引导。有关详细信息,请参阅http://git.io/v4gXM "

当我删除

browser.ignoreSynchronization = false;

然后我得到一个错误:

Message:
    Failed: No element found using locator: By(css selector, .btn.item-desktop-only.inactive-btn)
  Stack:
    NoSuchElementError: No element found using locator: By(css selector, .btn.item-desktop-only.inactive-btn)

HTML是:

<button _ngcontent-vsd-c3="" class="btn item-desktop-only inactive-btn" type="button" ng-reflect-klass="btn item-desktop-only" ng-reflect-ng-class="[object Object]"><img _ngcontent-vsd-c3="" class="orders-icon" src="/assets/icons/orders-inactive.png" srcset="/assets/icons/orders-inactive@2x.png 2x,/assets/icons/orders-inactive@3x.png 3x"> Orders </button>

标签: jasmineprotractorqabrowser-automation

解决方案


从错误中可以清楚地看出,主页不是 angular app > version 2。

与..一起处理

browser.ignoreSynchronization = true;

你能检查定位器是否正确。请通过在开发工具中找到它来验证定位器。

人员经验:角度应用程序版本 < 2 工作正常browser.ignoreSynchronization = true;

尝试等到定位器存在。

您可以在量角器中使用 ExpectedConditions。

const EC = require('protractor').ExpectedConditions;
let elm = element(by.id('your id'));  
browser.wait(EC.presenceOf(e), 10000); //2nd parameter is the max timeout
expect(e.isPresent()).toBeTruthy();

推荐阅读