1

有没有办法从组合框中选择一个项目而没有项目的实际值?

假设我们有橘子、苹果和柠檬的组合,这些项目的实际值是我们不知道的键,有没有办法按索引选择?

或者也许是根据 displayValue 检索值?

像这样的工作:

  this.comboBox().setValue(7);

但是,如果我只有想要选择的 displayValue 怎么办?让我们说“橙子”,我如何从组合中选择它?

4

2 回答 2

2

你可以做到这一点的一种方法是通过execute()。想法是在组合框的存储中找到正确的记录,从中检索 id,然后使用 id 设置正确的值。

在浏览器内场景中,您可以直接通过检查组合框的存储来执行此操作。但是,在基于 WebDriver 的场景中,您无法从规范访问应用程序运行的上下文,因此 execute() 是最简单的方法(没有其他方法可以通过 API)进入那里。这是一个简单的例子:

// Component
Ext.define('Sandbox.view.test.combobox.Base', {
    extend: 'Ext.form.field.ComboBox',
    id: 'futureCmp',
    displayField: 'text',
    valueField: 'id',
    store: {
        fields: ['id', 'text'],
        data: [
            [1, 'Apples'],
            [2, 'Oranges']
        ]
    }
});

// Spec
it('should select correct value from displayField', function () {
    var combo = ST.comboBox('@futureCmp');

    combo
        .execute(function (cmp) {
            // we're in the target context now, so we *can* 
            // interact with Ext JS now
            // we could return an object if needed, but we only need the id
            return cmp.getStore().findRecord('text', 'Oranges').get('id');
        })
        .and(function () {
            // "executeResult" is the value returned from execute()
            // in this case, it's the id of the item we want to select
            var id = this.future.data.executeResult;
            combo.setValue(id);
            // assert that the value has been correctly selected
            combo.value(id);
        });
});

这种方法的好处在于它可以在场景类型之间移植。由于我们只使用 API,我们可以轻松地在浏览器内和基于 Web 驱动程序的场景之间切换并运行相同的测试(事实上,这是从一些正是这样做的内部测试中借用的)。

最后,我们在功能请求跟踪器中确实有一个项目,用于将select()样式方法添加到组合框未来 API,类似于 Grid 和 DataView 可用的方法。我不确定他们什么时候能进去,但我个人一直想要它一段时间:)

于 2017-06-27T13:00:45.923 回答
0

您可以执行以下操作:ST.ComboBox('${ST_LOCATOR}').setValue('STRING_VALUE') 其中 STRING_VALUE 是组合框中可用的选项之一。

请注意,STRING_VALUE 不是您在 UI 中看到的值。您可以在 UI3D Pie和组件中拥有3d_pie。第二个是你必须使用的。

如果该组件不是真正的 ComboBox 而是一个更复杂的组件,则您必须找到 DOM 元素和单击操作等的解决方法。

于 2017-06-27T04:37:29.273 回答