首页 > 解决方案 > 量角器/角度无法点击可点击图标

问题描述

我有以下量角器/角度脚本:

clickAppMenuIcon() {
    var EC = protractor.ExpectedConditions;
    var menuLayer = element(by.id('cdk-overlay-2'));
    expect(menuLayer.isPresent()).toBe(true)
    .then(function (){
        console.log('menu Layer shows');
        var menuGrid = element(by.css('.menu-grid'));
        expect(menuGrid.isPresent()).toBe(true)
        .then(function () {
            console.log('menu-grid shows');
            browser.executeScript('' +
                'var zIndexElement0 = document.getElementById("app-container");' +
                'zIndexElement0.style.zIndex = "-20";' +
                'var zIndexElement1 = document.getElementById("cdk-overlay-2");' +
                'zIndexElement1.style.zIndex = "20";'

            );
            menuGrid.click();
            var menuIcon = element(by.css('a[title=Configuration]'));
            expect(menuIcon.isPresent()).toBe(true)
            .then(function() {
                console.log("menu Icon is present");
                menuIcon.click();
                var routerLinkIcon = element.all(by.css('.material-icons'));
                expect(routerLinkIcon.isPresent()).toBe(true)
                .then(function (){
                   console.log("it's there");
                   routerLinkIcon.click();
                   // browser.wait(function (){
                       // expect(browser.driver.getCurrentUrl()).toEqual('http://10.22.1.68:8080/configuration');
                   // },5000);
                   expect(browser.driver.getCurrentUrl()).toEqual('http://10.22.1.68:8080/configuration');
                });
            });
        });
    });
}

适用于 Angular HTML:

<mat-card _ngcontent-c8="" class="app-menu mat-card ng-star-inserted">
  <div _ngcontent-c8="" class="menu-grid">
    <a _ngcontent-c8="" title="NPW">
      <i _ngcontent-c8="" class="material-icons">account_balance</i>
    </a>
    <a _ngcontent-c8="" title="Upload">
      <i _ngcontent-c8="" class="material-icons">file_upload</i>
    </a>
    <a _ngcontent-c8="" title="Correlate">
      <i _ngcontent-c8="" class="material-icons">share</i>
    </a>
    <a _ngcontent-c8="" routerlink="workspaces" title="Workspace" ng-reflect-query-params="[object Object]" ng-reflect-router-link="workspaces" href="/workspaces">
      <i _ngcontent-c8="" class="material-icons">content_copy</i>
    </a>
    <a _ngcontent-c8="" title="Help">
      <i _ngcontent-c8="" class="material-icons">help</i>
    </a>
    <a _ngcontent-c8="" title="Send Notification">
      <i _ngcontent-c8="" class="material-icons">add_alert</i>
    </a>
    <a _ngcontent-c8="" routerlink="configuration" title="Configuration" ng-reflect-router-link="configuration" href="/configuration">
      <i _ngcontent-c8="" class="material-icons">settings_applications</i>
    </a>
  </div>
</mat-card>

它在控制台中返回它找到它,然后它应该可以单击以将 URL 重定向到新的 URL,但它似乎坐在那里什么都不做,除了返回此错误:

**************************************************
*                    Failures                    *
**************************************************

1) ConfigNavCheck should navigate to Configuration page on click
  - Failed: Element not clickable at point (450,676). Other element would receive the click: <div class="mat-button-ripple mat-ripple mat-button-ripple-round" ng-reflect-disabled="false" ng-reflect-trigger="[object HTMLButtonElement]" matRipple="" ng-reflect-centered="true"></div>
  Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T15:15:08.936Z'
  System info: host: 'A0000872', ip: '10.22.73.23', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_77'
  Driver info: driver.version: unknown

Executed 1 of 1 spec (1 FAILED) in 20 secs.
[13:58:50] I/local - Shutting down selenium standalone server.
[13:58:50] I/launcher - 0 instance(s) of WebDriver still running
[13:58:50] I/launcher - internet explorer #01 failed 1 test(s)
[13:58:50] I/launcher - overall: 1 failed spec(s)
[13:58:50] E/launcher - Process exited with error code 1

可以做些什么来将页面重定向到单击的链接。我们尝试过等待,但它超时了。我们还有一个更简单的版本,可以在 Chrome 和 Firefox 上正常运行,但不适用于 IE11。必须支持IE,所以这是必要的。

标签: angularprotractorinternet-explorer-11url-redirection

解决方案


我知道这样做会引起一些不好的评论,但它至少在 IE、Chrome 和 Firefox 中有效:

browser.executeScript('document.querySelector(\'[routerlink="configuration"]\').click();');

现在,如果您在单击某个元素时遇到问题,因为会单击其他内容,正如我在很多 Protractor 登录问题中看到的那样,您可以使用:

changeZindexById(zIndexLayer, level) {
    console.log('zIndex level: ' + level);
    browser.executeScript('' +
        'var zIndexElement = document.getElementByID("' + zIndexLayer + '");' +
        'zIndexElement.style.zIndex = "' + level + '";'
    );
}

changeZindexByClass(zIndexLayer, level) {
    console.log('zIndex level: ' + level);
    browser.executeScript('' +
        'var zIndexElement = document.getElementsByClassName("' + zIndexLayer + '");' +
        'zIndexElement[0].style.zIndex = "' + level + '";'
    );
}

推荐阅读