在angularjs中使用protator进行单元测试时,点击超链接

click on hyperlinks in unit testing using protactor in angularjs

本文关键字:单元测试 超链接 angularjs protator      更新时间:2023-09-26

我是angularjs单元测试的新手。我从:

https://github.com/angular/protractor/blob/master/docs/tutorial.md。

我想点击超链接,但我不知道怎么才能做到这一点。

下面是我的代码:
<li ng-repeat="menu in sidebarLinks" ng-if="menu.visible == true " ng-class="{active: isActive('/{{menu.action}}')}">
                    <a ng-href="#/{{menu.action}}" title="{{menu.name}}" ng-click="loadSubmenus(menu.action)"  ng-class="{active: isActive('/{{menu.action}}')}">
                        <div class="icon {{menu.icon}}" ng-class="{active: isActive('/{{menu.action}}')}"></div> 
                           <span>{{menu.name}} </span>
                    </a>
                </li>   

在单元测试中:

describe('Protractor Demo App', function() {
    browser.driver.get('https://localhost:8443/login.html');
browser.driver.findElement(by.id('name')).sendKeys('test');
browser.driver.findElement(by.id('password')).sendKeys('test');
browser.driver.findElement(by.id('login')).click();
});

登录后url为https://localhost:8443/#/dash

点击链接后url应为https://localhost:8443/#/hypera

由于您的链接在您的机器上本地运行,因此无法为其他人工作,因此我只能告诉您最好的方法。

如果你正在处理超链接,你需要得到你用'login'设置的元素。我没有在你的HTML片段中看到它,所以我不能给你正确的路径。

如果它是一个基本的超链接,你可以很容易地找到它,使用如下命令:

element(by.linkText('login')).click()

如果你的登录按钮的文本确实是'login'。如果它是其他东西,比如按钮,您需要找到确切的元素,它看起来像:

element(by.css('span[class="login"]')).click()

如果你的登录按钮在HTML页面的'span'标签下