20

我有两个测试:

  it('should filter the phone list as user types into the search box', function() {

    var results = ptor.findElements(protractor.By.repeater('phone in phones').column('phone.name'));
    results.then(function(arr) {
            expect(arr.length).toEqual(3);
    });

    var queryInput = ptor.findElement(protractor.By.input('query'));

    queryInput.sendKeys('nexus');

    results = ptor.findElements(protractor.By.repeater('phone in phones').column('phone.name'));
    results.then(function(arr) {
        expect(arr.length).toEqual(1);
    });

    queryInput.clear();
    queryInput.sendKeys('motorola');

    results = ptor.findElements(protractor.By.repeater('phone in phones').column('phone.name'));
    results.then(function(arr) {
        expect(arr.length).toEqual(2);
    });

});

it('should display the current filter value within an element with id "status"',
    function() {
        //expect(element('#status').text()).toMatch(/Current filter: \s*$/);
        var queryInput = ptor.findElement(protractor.By.input('query'));
        queryInput.clear();

        expect(ptor.findElement(protractor.By.id('status')).getText()).toMatch('Current Filter:');

        //input('query').enter('nexus');

        //queryInput.clear();
        //queryInput.sendKeys('nexus');

        //expect(element('#status').text()).toMatch(/Current filter: nexus\s*$/);
        //expect(ptor.findElement(protractor.By.id('status')).getText()).toMatch('^\Current Filter:.');

        //alternative version of the last assertion that tests just the value of the binding
        //using('#status').expect(binding('query')).toBe('nexus');
    });

第一个测试,搜索框,效果很好。第二个测试 status 没有通过,因为在 queryInput 中输入的最后一个值被传递到第二个测试,并且 queryInput.clear() 不起作用。但是,在第二个测试中,如果我调用 queryInput.sendKeys("something"),则会显示“something”。如果我在第二个测试中取出 clear(),我会看到“motorolaso​​mething”。因此,虽然 clear() 似乎在工作,但如果我在第二个测试中只有 clear(),我的测试没有通过,当我运行第二个测试时,即使调用 clear(),我也会看到“motorola”在第二次测试之前。

我想知道为什么当我没有 sendKeys() 之后, clear() 没有在第二次测试中清除。

4

6 回答 6

41

clear() 的文档说明如下:

[ !webdriver.promise.Promise ]清除( )

安排一个命令来清除此元素的 {@code value}。如果底层 DOM 元素既不是文本 INPUT 元素也不是 TEXTAREA 元素,则此命令无效。

Returns:当元素被清除时将被解决的承诺。

所以为了清楚地做你想做的事,你必须遵守它返回的承诺!为此,您必须使用then()

下面是它的工作原理:

queryInput.clear().then(function() {
    queryInput.sendKeys('motorola');
})

所以clear()返回一个清除输入then()的承诺,并告诉承诺在输入被清除后立即做什么。

于 2014-11-21T19:54:50.410 回答
6
await element.sendKeys(Key.chord(Key.CONTROL, 'a'));

await element.sendKeys(Key.DELETE);
于 2019-05-03T08:09:07.947 回答
4

Clear().then(..)对我不起作用。

所以这是我的工作:

queryInput.sendKeys(protrator.Key.chord(protrator.Key.CONTROL, 'a'));
queryInput.sendKeys('nexus')
于 2019-05-02T21:39:20.950 回答
1

你可以将 Promise 组合成一个链:

queryInput.clear().sendKeys('nexus');

于 2017-06-13T09:57:56.007 回答
0

这对我有用:

         click(`${fieldId}`).then(() => {
                        yourElement.sendKeys(protractor.Key.chord(protractor.Key.CONTROL, "a")).then(() => {
                            yourElement.sendKeys(protractor.Key.BACK_SPACE);
                            yourElement.clear();
}
于 2021-02-25T15:23:44.143 回答
0

使用 PageObject 模式和 async/await,我有这样的代码可以工作:

async clearForm() {
    await this.nameInput.clear();
    await this.descriptionInput.clear();
}
于 2020-04-01T13:03:32.357 回答