gpt4 book ai didi

flutter-alertdialog - Flutter Driver - 未找到 Flutter AlertDialog 及其元素

转载 作者:行者123 更新时间:2023-12-05 07:16:42 26 4
gpt4 key购买 nike

我的 flutter 应用程序中出现了一个 Flutter AlertDiaog。使用 Flutter Driver,我无法点击 Flutter AlertDialog 或 AlertDialog 上的任何元素。有没有办法点击我的应用程序中出现的任何 AlertDialog?

我已经尝试了以下所有方法,但仍然没有成功:

  1. 首先:
    await driver.tap(find.byType('ModalBarrier'));
print('*******TAPPED - ModalBarrier*******');
  1. 第二
    await driver.tap(find.byType('AlertDialog'));
print('*******TAPPED - AlertDialog*******');
  1. 第三
     await driver.tap(find.text('Gallery')); // Tapping 'Gallery', can access pics
print('*******TAPPED - Gallery *******');

最佳答案

想法是首先通过唯一键识别AlertDialog 中的文本,将其告知驱动程序,然后对其执行tap 操作。在您的主代码中,将 key 属性添加到具有您要点击的 Text 的父窗口小部件,例如:我正在展示一个简单的 AlertDialog 显示 2 个选项,如下所示:

enter image description here

代码是:

showDialog(context: context,
builder: (BuildContext context) {
return AlertDialog(
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
GestureDetector(
key: Key('firstOptionKey'),
child: Text('Take a picture'),
onTap: () {},
),
Padding(
padding: EdgeInsets.all(8.0),
),
GestureDetector(
key: Key('secondOptionKey'),
child: Text('Select from gallery'),
onTap: () {},
),
],
),
),
);
});

如您所见,我用 GestureDetector 包裹了两个文本,让我们可以点击它们。此外,我为每个 GestureDetector 使用了 key 属性来唯一标识这两个文本。

在您的驱动程序测试中,您只需使用 byValueKey 识别每个文本并告诉驱动程序点击它,如下所示:

test('test dialog', () async {

final firstText = find.byValueKey('firstOptionKey');
await driver.waitFor(find.text('Tap'));
await driver.tap(find.text('Tap')); // tap on this button to open alertDialog
await driver.waitFor(firstText); // wait for flutter driver to locate and identify the element on screen
await driver.tap(firstText);
print('tapped on it');
});

enter image description here

希望这能回答您的问题。

关于flutter-alertdialog - Flutter Driver - 未找到 Flutter AlertDialog 及其元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59128807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com