Angularjs 未将量角器getText()设置为变量

Angularjs 未将量角器getText()设置为变量,angularjs,protractor,Angularjs,Protractor,我正在测试我的管理员编辑其他用户的能力。user.firstname模型就是我要检查的。我希望能够在测试后将第一个名称设置回原来的名称 it("should be able to edit a different profile.", function () { browser.get('#/user/2'); var fname = element(by.model('user.firstName')); var originalName = '

我正在测试我的管理员编辑其他用户的能力。
user.firstname
模型就是我要检查的。我希望能够在测试后将第一个名称设置回原来的名称

it("should be able to edit a different profile.", function () {
        browser.get('#/user/2');
        var fname = element(by.model('user.firstName'));
        var originalName = '';
        fname.getText().then(function (txt) {
            originalName = txt;
        });
        console.log('here');
        console.log(originalName);
        fname.sendKeys('New Name');
}
我还没有谈到
expect
部分。现在,我无法获取存储在变量中的当前名称(Bob)。它打印出空白:

Using the selenium server at http://localhost:4444/wd/hub
[launcher] Running 1 instances of WebDriver
Started
..............here

....
如果我在函数中输入console.log,它会打印出正确的名称,但看起来我的承诺要到后来才能兑现

        fname.getText().then(function (txt) {
            console.log(txt);
            originalName = txt;
        });
        console.log('here');
        console.log(originalName);
        fname.sendKeys('New Name');
}
给我这个:

Using the selenium server at http://localhost:4444/wd/hub
[launcher] Running 1 instances of WebDriver
Started
..............here

Bob
....

如何获得承诺之外的值(通过变量)?我只是做错了吗?

简言之,是的,你做错了。
then()
中的代码是在它后面的代码之后执行的,因为在后面的代码运行之后承诺就实现了。坦率地说,这是编写量角器测试中比较棘手的部分之一

有几种可能的解决方案:

  • 将代码添加到
    then()
  • 将代码放入后续的
    then()
    块中
  • “等待”浏览器执行此操作
第一个很明显,第二个看起来像:

fname.getText().then(function (txt) {
    console.log(txt);
    originalName = txt;
}).then(function () {
    console.log('here');
    console.log(originalName);
});
fname.sendKeys('New Name');
fname.getText().then(function (txt) {
    console.log(txt);
    originalName = txt;
});
// ... do stuff ...
browser.wait(function() {
    return originalName !== undefined;
}).then(function() {
    console.log('here');
    console.log(originalName);
});
fname.sendKeys('New Name');
第三个看起来像:

fname.getText().then(function (txt) {
    console.log(txt);
    originalName = txt;
}).then(function () {
    console.log('here');
    console.log(originalName);
});
fname.sendKeys('New Name');
fname.getText().then(function (txt) {
    console.log(txt);
    originalName = txt;
});
// ... do stuff ...
browser.wait(function() {
    return originalName !== undefined;
}).then(function() {
    console.log('here');
    console.log(originalName);
});
fname.sendKeys('New Name');

这里还值得注意的是,只有当您关心
console.log()
business时才需要这样做。您注意到
fname.sendKeys()
代码在块外–量角器将负责等待上一个操作完成,而不管在执行下一个操作之前如何。如果您在jasmine
expect()

中使用getText(),则Gradurator还将为您履行承诺。简言之,是的,您做得不对。
then()
中的代码是在它后面的代码之后执行的,因为在后面的代码运行之后承诺就实现了。坦率地说,这是编写量角器测试中比较棘手的部分之一

有几种可能的解决方案:

  • 将代码添加到
    then()
  • 将代码放入后续的
    then()
    块中
  • “等待”浏览器执行此操作
第一个很明显,第二个看起来像:

fname.getText().then(function (txt) {
    console.log(txt);
    originalName = txt;
}).then(function () {
    console.log('here');
    console.log(originalName);
});
fname.sendKeys('New Name');
fname.getText().then(function (txt) {
    console.log(txt);
    originalName = txt;
});
// ... do stuff ...
browser.wait(function() {
    return originalName !== undefined;
}).then(function() {
    console.log('here');
    console.log(originalName);
});
fname.sendKeys('New Name');
第三个看起来像:

fname.getText().then(function (txt) {
    console.log(txt);
    originalName = txt;
}).then(function () {
    console.log('here');
    console.log(originalName);
});
fname.sendKeys('New Name');
fname.getText().then(function (txt) {
    console.log(txt);
    originalName = txt;
});
// ... do stuff ...
browser.wait(function() {
    return originalName !== undefined;
}).then(function() {
    console.log('here');
    console.log(originalName);
});
fname.sendKeys('New Name');

这里还值得注意的是,只有当您关心
console.log()
business时才需要这样做。您注意到
fname.sendKeys()
代码在块外–量角器将负责等待上一个操作完成,而不管在执行下一个操作之前如何。如果您在jasmine
expect()

中使用getText(),Gradutor还将为您履行承诺。在等待您的回答时,我选择了选项1。。。我可能最终选择了选项3,因为在我看来它是最干净的。我花了大约6个小时找到了解决我问题的答案。这个答案非常有用!我不明白为什么它只有138个可视化和3张选票。我错过什么了吗?还有其他像这样的好答案我找不到?我在等待你的答案时选择了选项1。。。我可能最终选择了选项3,因为在我看来它是最干净的。我花了大约6个小时找到了解决我问题的答案。这个答案非常有用!我不明白为什么它只有138个可视化和3张选票。我错过什么了吗?还有其他像这样的好答案我没能找到?