0

登录后用户的此代码应发布一些问候消息。

describe("launching Telekha",function(){
    it("navigating to signin page",function(){
        browser.get("www");
        element(by.model("credentials.email")).sendKeys("abnsd6@gmail.com");
        element(by.model("credentials.password")).sendKeys("123456");
        var ptr = element( by.css('[ng-click="login()"]') );
        ptr.click();
        });

    it("on dashboard",function(){
        element(by.model("post.postText")).sendKeys("hello");
            element( by.css('[ng-click="postit()"]') ).click();
        });
});

按钮的 HTML 代码

<textarea id="post-editor" placeholder="Tell your friends" ng-model="post.postText" class="textareanoborder col-xs-12 col-md-12 ng-pristine ng-valid ng-isolate-scope ng-touched" autocomplete="off" aria-invalid="false"> </textarea>
4

2 回答 2

1

这是一个猜测,但根据经验,它通常不会太远。当您导航到的页面被加载时,是否有任何动画、AJAX 请求或其他延迟,与可能发生的 AngularJS 分开?Protractor 不会等待这些,而是​​会执行代码:

element(by.model("post.postText")).sendKeys("hello");

尽快。如果执行此操作时,所有动画尚未完成,则量角器将看不到您的元素,并且您将看到遇到的错误。

要快速测试这一点,browser.sleep(10000);请在失败的行之前添加一个。browser.sleep();是一个命令,它将强制浏览器等待一段时间,在本例中为 10.000 毫秒(10 秒)。

如果这确实有效,您可能希望将其更改为更优雅的内容,例如:

browser.wait(function() {
  return element(by.model("post.postText")).isPresent();
}, 10000);

这将等待您的元素变得可见,但最多只能等待 10 秒,之后它将继续。

EDIT1:(效果很好):

HTML(从http://localhost:8080提供服务)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Example</title>
        <script src="bower_components/angular/angular.min.js"></script>
        <script>
        var app = angular.module('app', []);
        app.controller('myCtrl', function($scope) {
        });
    </script>
</head>
    <body ng-app="app" ng-controller="myCtrl">
         <textarea id="post-editor" placeholder="Tell your friends" ng-model="post.postText" class="textareanoborder col-xs-12 col-md-12 ng-pristine ng-valid ng-isolate-scope ng-touched" autocomplete="off" aria-invalid="false"> </textarea>
    </body>
</html>

规范.js

describe('angularjs homepage todo list', function() {
  it('should add a todo', function() {
    browser.get('http://localhost:8080');
    $("#post-editor").sendKeys("testlol");
    browser.sleep(2000);
  });
});

conf.js

exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub',
  specs: ['spec.js']
};
于 2016-11-21T10:36:00.447 回答
0

您可以实现protractor.ExpectedConditions.visibilityOf()方法等到元素在 UI 上可见,然后将数据发送到输入字段。

    var EC=protractor.ExpectedConditions;

    it("on dashboard",function(){
       var ele=element(by.id("post-editor"));
       browser.wait(EC.visibilityOf(ele),8000,'Ele is not presented');
       ele.sendKeys("hello");
       element( by.css('[ng-click="postit()"]') ).click();
    });
于 2016-11-21T11:59:16.677 回答