如何解决查找没有键的元素
在测试中使用flutter驱动程序仍然是我的新手,但据我所知,我们很少有标识符可以用来定位/识别元素,例如按文本,按类型等
但是问题是,我要测试的应用程序没有可用于定位它们的标识符(如果我错了,请更正我)..该应用程序的小部件代码如下
Widget _buildNextButton() {
return Align(
alignment: Alignment.bottomRight,child: Container(
child: IconButton(
icon: Icon(Icons.arrow_forward),onPressed: () => _controller.nextPage(),),);
}
该小部件在扩展StatefulWidget
的类上的位置。
如何在测试脚本中找到该图标并单击它?我可以使用这样的东西吗?我应该使用哪种类型的取景器? (byValueKey?bySemanticLabel?byType?还是什么?)
static final arrowKey = find.byValueKey(LoginKey.nextButton);
TestDriverUtil.tap(driver,arrowKey);
解决方法
我们在Flutter驱动程序中进行了文本和值检查,但是如果没有,您可以随时进入应用程序的层次结构。 我所说的层次结构是指按钮具有固定权限或特定的父级权限?
让我们以您的示例为例,我们有Align> Container> IconButton> Icon小部件层次结构,这对于其他人来说不是正确的,例如可能有IconButton但对于Container父级则不是。 或StreamBuilder或我们能想到的任何东西。
API.registration(name: name,email: email,password: password,schoolID: selectedSchool?.id ?? 0)
.print("Registration")
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { (completion) in
switch completion {
case let .failure(error):
switch error {
case .login(let loginError):
print("Login failed,\(loginError.emailError),\(loginError.usernameError)")
default:
print(error)
}
case .finished: break
}
},receiveValue: { value in
print(value)
})
.store(in: &disposables)
该层次结构对于顶部或底部方法应该是最理想的。
现在,我所说的“从上到下”方法是“对齐”必须具有IconButton,而对于“自下而上”的方法,我们是说IconButton必须具有“ Align”小部件作为父级。
这里我采用了自上而下的方法,因此我从下面的代码中检查的是找到落后于Align Widget的IconButton。 我还添加了firstMatchOnly true,因为我正在检查如果两者出现相同的层次结构会发生什么情况
<ImageBackground
style={{height: 100,width: 100}}
imageStyle={{ borderRadius: 6}} < -- like this -->
source={{ uri: 'www.imageislocatedhere.com }}
>
要检查与父级具有相同Align的多个IconButton,我们需要有所不同,例如父级应该具有“文本”视图或其他小部件。
Widget _buildNextButton() {
return Align(
alignment: Alignment.bottomRight,child: Container(
child: IconButton(
icon: Icon(Icons.arrow_forward),onPressed: () => print("clicked button"),),);
}
通常,我在上面类似的地方将代码拆分成单独的文件,然后检查该小部件。
但是最终找到有关该小部件的一些独特信息,然后您就可以进行操作了。
,**In Lib directory dart class for connecting that widget**
class Testing extends StatelessWidget {
Testing();
// This widget is the root of your application.
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,visualDensity: VisualDensity.adaptivePlatformDensity,home: YourClass(),// Next button containing class that need to test
);
}
}
**In Test directory**
testWidgets('Next widget field test',(WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(Testing());
// find Widget
var buttonFind = find.byIcon(Icons.arrow_forward);
expect(buttonFind,findsOneWidget);
IconButton iconButton = tester.firstWidget(buttonFind);
expect(iconButton.color,Colors.blue);
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。