量角器清除()不起作用

bmw*_*128 19 javascript protractor

我有两个测试:

  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');
    });
Run Code Online (Sandbox Code Playgroud)

第一个测试,搜索框,效果很好.第二个测试status没有通过,因为在queryInput中输入的最后一个值被转移到第二个测试,而queryInput.clear()不起作用.但是,在第二次测试中,如果我调用queryInput.sendKeys("something"),将显示"something".如果我在第二次测试中取出clear(),我会看到"motorolaso​​mething".所以,虽然看起来clear()正在工作,但是我的测试没有通过,如果我在第二次测试中只有clear(),当我运行第二次测试时,我会看到"motorola",即使调用了clear()在第二次测试之前.

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

Mic*_* K. 37

clear()的文档说明如下:

[!webdriver.promise.Promise] clear()

计划命令以清除此元素的{@code值}.如果底层DOM元素既不是文本INPUT元素也不是TEXTAREA元素,则此命令无效.

返回:清除元素时将解析的promise.

所以为了清楚地做你想做的事,你必须要承诺它会回来!这样做你必须使用then()

下面是它的工作原理:

queryInput.clear().then(function() {
    queryInput.sendKeys('motorola');
})
Run Code Online (Sandbox Code Playgroud)

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

  • 你也可以链接这些promises以节省一些空间:queryInput.clear().sendKeys('motorola').sendKeys(protractor.Key.ENTER); (6认同)
  • 不,你不能链接他们.这并不能解决明确的承诺.你必须使用`.then()` (3认同)

小智 6

await element.sendKeys(Key.chord(Key.CONTROL, 'a'));

await element.sendKeys(Key.DELETE);
Run Code Online (Sandbox Code Playgroud)

  • 该解决方案适用于其他平台吗?(如 macOS) (2认同)