量角器清除()不起作用

时间:2013-11-13 22:59:24

标签: 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');
    });

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

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

6 个答案:

答案 0 :(得分:36)

clear()的文档说明如下:

  

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

     

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

     

返回:在元素拥有时将解析的承诺   已被清除。

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

这是它的工作原理:

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

所以clear()会向您返回清除输入的承诺,then()告诉承诺一旦输入被清除就会做什么。

答案 1 :(得分:2)

您可以将promisses组合成一个链:

PRODUCT_TYPE=(('TL','Tubeless Tyre'), ('TT','Tubed Tyre'), ('NA','Not applicable')) class Product(models.Model): product_group=models.ForeignKey('productgroup.ProductGroup', null=False,blank=False) manufacturer=models.ForeignKey(Manufacturer, null=False,blank=False) product_type=models.CharField(max_length=2, choices=PRODUCT_TYPE,) opening_stock=models.PositiveIntegerField(default=0) def __str__(self): return '%s (%s, %s, %s) o.stock = %d ' % (self.product_group, self.manufacturer, self.product_type ,self.opening_stock) unique_together = ('product_group', 'manufacturer','product_type') def get_total_stock_in(self): Stock.objects.filter(product=self.id,ttype='I').aggregate(Sum('quantity')) def get_total_stock_out(self): Stock.objects.filter(product=self.id,ttype='I').aggregate(Sum('quantity'))

答案 2 :(得分:2)

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

await element.sendKeys(Key.DELETE);

答案 3 :(得分:0)

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

这就是我的工作

    queryInput.sendKeys(Key.chord(Key.CONTROL, 'a'));
    queryInput.sendKeys('nexus')

答案 4 :(得分:0)

使用PageObject模式和异步/等待,我有这样的代码可以工作:

async clearForm() {
    await this.nameInput.clear();
    await this.descriptionInput.clear();
}

答案 5 :(得分:0)

这对我有用:

         click(`${fieldId}`).then(() => {
                        yourElement.sendKeys(protractor.Key.chord(protractor.Key.CONTROL, "a")).then(() => {
                            yourElement.sendKeys(protractor.Key.BACK_SPACE);
                            yourElement.clear();
}