我是 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();
});
登录后网址为https://localhost:8443/#/dash
点击链接后的 URL 应为 https://localhost:8443/#/hypera
最佳答案
由于您的链接无法用于其他任何人,因为您在计算机上本地运行它,所以我只能告诉您最好的方法。
如果您正在处理超链接,则需要获取使用“登录”设置的元素。我没有在您的 HTML 代码段中看到它,因此无法为您提供正确的路径。
如果它是一个基本的超链接,您可以使用以下内容轻松找到它:
element(by.linkText('login')).click()
如果您的登录按钮的文本确实是“登录”。如果是其他东西,比如按钮,您需要找到确切的元素,它看起来像:
element(by.css('span[class="login"]')).click()
如果您的登录按钮位于 HTML 页面中的“span”标签下。
关于javascript - 在AngularJS中使用 Protractor 单击单元测试中的超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38826189/