2016-06-23 40 views
0

我是Angularjs /量角器的新手,无法在我的测试中访问输入标签。在浏览器中运行应用程序会将输入字段设置为初始值。但我似乎无法抓住他们或在我的测试中写入这些字段。量角器的输出结果是:无法访问量角器测试中的输入字段

$protractor conf.js 
>[18:07:56] I/hosted - Using the selenium server at http://localhost:4444/wd/hub 
>[18:07:56] I/launcher - Running 1 instances of WebDriver 
>Started 
>F 
>Failures: 
>1) When loading component loginComp the email/password get init values 
> Message: 
> Expected '' to be '[email protected]'. 
>Stack: 
> Error: Failed expectation 
> ....stack trace 
>1 spec, 1 failure 
>Finished in 1.101 seconds 
>[18:07:58] I/launcher - 0 instance(s) of WebDriver still running 
>[18:07:58] I/launcher - chrome #01 failed 1 test(s) 
>[18:07:58] I/launcher - overall: 1 failed spec(s) 
>[18:07:58] E/launcher - Process exited with error code 1 

这里是index.html的:

index.html: 
<!DOCTYPE html> 
<html> 
<head> 
    <title>Test directives</title> 
</head> 
<body ng-app="docsapp"> 
    <div> Here is the test</div> 
    <div> 
    <login-comp></login-comp> 
    </div> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.6/angular.js"> </script> 
    <script src="app/app.module.js"></script> 
    <script src="app/login-comp/login-comp.module.js"></script> 
    <script src="app/login-comp/login-comp.component.js"></script> 
</body> 
</html> 

这里是loginComp模块:

login-comp.module.js: 
angular.module('loginComp', []) 

这里是loginComp部件DEF:

function LoginCompController() { 
    var vm = this; 
    this.user = { 
    email: "[email protected]", 
    password: "password" 
    } 
} 

angular.module('loginComp') 
.component('loginComp', { 
    templateUrl: './app/login-comp/login-comp.template.html', 
    controller: LoginCompController, 
    controllerAs: 'vm' 
}); 

这是loginComp模板:

login-comp.template.html: 
<input type="email" ng-model="vm.user.email"/> 
<hr> 
<input type="password" ng-model="vm.user.password"/> 

下面是测试文件:

login-comp.component.spec.js: 
describe("When loading component loginComp", function() { 
    beforeEach(function() { 
    browser.get("http://127.0.0.1:8080/"); 
    }); 
    it("the email/password get init values", function() { 
    var email = element(by.model("vm.user.email")); 

    email.sendKeys("[email protected]").then(function() { 
     expect(email.getText()).toBe("[email protected]"); 
    }); 

    }); 
}); 

我曾尝试不同的变化的测试:从电子邮件输入字段读,写它。我也尝试使用“user.email”或“email”来访问输入字段,但这会给出错误(失败:找不到使用locator的元素:by.model(“user.email”))。

如果有人在Stackoverflow上看到过此错误,请将链接发送给我。我已经回顾了很多量角器相关的问题,但没有一个适用于我的案例。

非常感谢。

回答

4

这是从protractor FAQ

的getText从输入元件的结果总是空

这是一个的webdriver夸克。输入和textarea元素总是有空的getText值。请尝试:

element.getAttribute('value') 
+0

谢谢!这工作。在Protractor的PluralSight网站上有一个使用getText的教程。它在教程中起作用,所以我认为还有其他错误。 – user2137480

相关问题