Flutter 驱动程序:在屏幕上找不到元素(文本)

问题描述 投票:0回答:2

我需要查找的元素位于(在子元素中):

 static Widget _buildProjectCategoryWidget(BuildContext context, String name) {
final themeData = Theme.of(context);
final primaryTextTheme = themeData.primaryTextTheme;
return Container(
  padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 5.0),
  decoration: BoxDecoration(borderRadius: BorderRadius.circular(13.0), color: FlAppStyles.altButtonColor),
  child: Text(name, style: primaryTextTheme.textSmall),
);

我试图用这个找到子元素,但我有 DriverError: 由于远程错误而无法实现 Tap:

SerializableFinder message = find.text("mytext");
    await driver.waitFor(message);
    expect(await driver.getText(message), "mytext"); await driver.tap(buttonChangeProfession);
    

我是flutter集成测试的初学者,不知道出了什么问题,请帮忙。我还尝试添加一个键并通过它查找元素,但重点是我需要查找该元素的文本。

flutter testing integration flutter-test flutterdriver
2个回答
3
投票

如果您想验证屏幕上是否存在此文本,请尝试以下操作:

expect(
      await driver.getText(find.text("enter text you want to find")),
      "enter text you want to find");

如果您想点击该文本,请尝试以下操作:

  final elementFinder= find.text('enter text you want to tap');
  await driver.waitFor(elementFinder);
  await driver.tap(elementFinder);

如果您不确定,请通过使用 Devtool 检查来获取该文本,希望这会有所帮助


0
投票

Text and Visibility check:
The getText() and isDisplayed() methods are not implemented in the Element class of Flutter Driver. Instead, you can use the getTextFinder() and waitFor() methods to retrieve the text of an element and check if it is visible.
final element = find.byValueKey('my_element_key');
final textFinder = find.descendant(of: element, matching: find.text('my_text'));

await driver.waitFor(element);
final text = await driver.getText(textFinder);
print(text);

await driver.waitFor(element);
final isVisible = await driver.waitFor(element).isPresent;
print(isVisible);

© www.soinside.com 2019 - 2024. All rights reserved.