【问题标题】:Promise for getting the title of a web page never resolves with Protractor and JasmineProtractor 和 Jasmine 永远不会解决获取网页标题的承诺
【发布时间】:2015-01-20 22:29:27
【问题描述】:

下面的量角器/jasmine测试代码只打印出1和2,然后就挂了,超时了。

这似乎是按钮元素上的 click() 操作或浏览器对象上的 getTitle 方法上的承诺存在问题,或两者兼而有之。

有没有人对此有解决方案,或者有更好的方法来做我正在做的事情?

代码:

it('should allow successful login', function() {   
    browser.get('http://192.168.0.100/src/');
    browser.waitForAngular();

    var titlePromise = browser.getTitle();
    titlePromise.then(function(text){
      console.log("1**************", text);
    });

    var titlePromise = browser.getTitle();
    titlePromise.then(function(text){
      console.log("2**************", text);
    });

    element.all(by.model('credentials.username')).first().sendKeys('foo');
    element.all(by.model('credentials.password')).first().sendKeys('bar');
    var loginBtn = element.all(by.cssContainingText('.btn', 'Login')).first();
    loginBtn.click();
    browser.sleep(5000);

    var titlePromise = browser.getTitle();
    titlePromise.then(function(text){
      console.log("3**************", text);
    });    
  });
}); 

错误:

错误:等待量角器与页面同步超时 11 秒后。请参见 https://github.com/angular/protractor/blob/master/docs/faq.md

【问题讨论】:

    标签: javascript angularjs selenium-webdriver jasmine protractor


    【解决方案1】:

    我可能没有足够的信息,但这里有一些事情可以尝试:

    1. 很明显,您是否仔细阅读过每个会导致链接文档https://github.com/angular/protractor/blob/master/docs/faq.md 超时的案例?如果你在 Angular 应用程序中使用了 $timeout,那么 Protractor 将永远不会加载。

    2. 您确定您选择的 loginBtn 正确吗?您可能希望以交互方式测试量角器:https://github.com/angular/protractor/blob/master/docs/debugging.md。从量角器目录/node_modules/protractor:

      $节点./bin/elementexplorer.jshttp://192.168.0.100/src/

    3. 如果您正在登录并转到另一个页面,而不是休眠等待下一页加载,请等到它更改:

      browser.driver.wait(function() { return browser.driver.getCurrentUrl().then(function(url) { return /logged-in-url/.test(url); }); });

    【讨论】:

    • 闻起来像你提到的问题 #1,因为我确实在我的应用程序中使用了 $timeout、polling 和 $interval。但是,在运行此测试的过程中没有使用任何东西,这让我感到困惑。正确选择了 loginBtn。我看着它在 chrome protractor 实例启动时工作。尝试了第三个结果没有变化。这开始让我烦恼了。用 selenium 试验了 5 天,我还没有找到任何测试我的应用的好方法。
    • 元素是量角器特定的对象。前 2 个测试有效,因为它们是硒,而不是量角器。我的猜测是量角器无法加载。我强烈建议您阅读该文档。
    • 我放弃了,决定单独​​使用 selenium-webdriver。我终于让它工作了,没有跳过任何东西或挂在任何地方。老实说,很多这种硒的东西看起来就像西部荒野,令人失望,也令人沮丧。 Angular 如何支持一个不适用于所有 Angular 应用的测试框架!?
    • 很多人说使用 selenium 作为登录页面,然后在登录后使用量角器特定。是的,这是狂野的西部。当您在公司时间进行实验时,这还不错;)。
    【解决方案2】:

    您忘记了与文档的所有交互都是通过Promises 完成的。您的代码应该类似于下面的未经测试的代码块。

    还要注意browser.waitForAngularnot needed,“Protractor 会在每个 WebDriver 操作之前自动应用此命令。”

    不知道你为什么这么频繁地调用getTitle,但我把它留在了,以防它使重构更加清晰。

    it('should allow successful login', function() {   
        browser.get('http://192.168.0.100/src/')
        .then(function(){
            return browser.getTitle()
        })
        .then(function(text){
            console.log("1**************", text);
            return browser.getTitle();
        })
        .then(function(text) {
            console.log("2**************", text);
            return browser.getTitle()
        })
        .then(function (text) {
            console.log("3**************", text);
            element.all(by.model('credentials.username')).first().sendKeys('foo');
        })
        .then(function () {
            element.all(by.model('credentials.password')).first().sendKeys('bar');
        })
        .then(function () {
            element.all(by.cssContainingText('.btn', 'Login')).first().click();
        })
        .then(function () {
            browser.sleep(5000); // Better to use ExpectedConditions to wait something
        })
        .then(function () {
            var titlePromise = browser.getTitle();
            return browser.getTitle()
        })
        .then(function (text) {
            console.log("3**************", text);
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2016-05-06
      • 2019-11-29
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 2020-04-15
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多