首先在pubspec.yaml文件中配置,是离网的情况下
dependencies:
flutter:
sdk: flutter
integration_test:
sdk: flutter
flutter_test:
sdk: flutter
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:test_list/main.dart';
void main(){
testWidgets("open App", (tester) async{
await tester.pumpWidget(MyApp());
await tester.pumpAndSettle();
await tester.pumpAndSettle(Duration(seconds: 5));
await tester.drag(find.byKey(ValueKey("scroll")), Offset(0, -720));
await tester.pumpAndSettle();
await tester.pumpAndSettle(Duration(seconds: 2));
await tester.tap(find.byKey(ValueKey("btn10")));
print('开始播放');
await tester.pumpAndSettle();
await tester.pumpAndSettle(Duration(seconds: 10));
print('暂停播放');
exit(0);
});
}
音乐播放测试原生代码
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:test_list/main.dart';
void main(){
testWidgets("open App", (tester) async{
await tester.pumpWidget(MyApp());
await tester.pumpAndSettle();
await tester.pumpAndSettle(Duration(seconds: 5));
await tester.drag(find.byKey(ValueKey("scroll")), Offset(0, -720));
await tester.pumpAndSettle();
await tester.pumpAndSettle(Duration(seconds: 2));
await tester.tap(find.byKey(ValueKey("btn10")));
print('开始播放');
await tester.pumpAndSettle();
await tester.pumpAndSettle(Duration(seconds: 10));
print('暂停播放');
exit(0);
});
}
原生通信
package com.example.test_list;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import java.lang.reflect.Field;
import java.sql.Time;
import java.util.Timer;
import java.util.TimerTask;
import io.flutter.embedding.android.FlutterActivity;
public class PlayerMusicActivity extends FlutterActivity {
private static final String CHANNEL = "com.example.day1";
private MediaPlayer mediaPlayer;
private ImageButton mImgBtn;
private Handler handler;
private Runnable mRunnable;
private int index;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player_music);
handler = new Handler();
try {
initView();
} catch (InterruptedException | IllegalAccessException e) {
e.printStackTrace();
}
}
private void initView() throws InterruptedException, IllegalAccessException {
mImgBtn = findViewById(R.id.mImgBtn);
Field[] fields = R.raw.class.getFields();
int rawId = fields[1].getInt(0);
System.out.println(rawId);
mediaPlayer = MediaPlayer.create(this,rawId);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
backResult();
}
});
mImgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playMusic();
}
});
mRunnable = new Runnable() {
@Override
public void run() {
playMusic();
if(index == 2){
backResult();
}
index++;
handler.postDelayed(this,10000);
}
};
handler.post(mRunnable);
// handler.postDelayed(new Runnable() {
// @Override
// public void run() {
// if(mediaPlayer.isPlaying()){
// playMusic();
// }else{
// playMusic();
// }
// }
// },10000);
// handler.postDelayed(new Runnable() {
// @Override
// public void run() {
// if(mediaPlayer.isPlaying()){
// playMusic();
// }
// }
// },15000);
}
private void playMusic() {
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
mImgBtn.setImageResource(R.mipmap.play_icon);
backResult();
}else{
mediaPlayer.start();
mImgBtn.setImageResource(R.mipmap.pause_icon);
}
}
private void backResult() {
handler.removeCallbacks(mRunnable);
setResult(RESULT_OK);
finish();
}
@Override
protected void onDestroy() {
backResult();
super.onDestroy();
}
}
flutter通信代码
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:test_list/welcome_screen.dart';
class ListScreen extends StatefulWidget {
const ListScreen({Key? key}) : super(key: key);
@override
State<ListScreen> createState() => _ListScreenState();
}
class _ListScreenState extends State<ListScreen> {
void play() async{
final plat = await MethodChannel("com.example.test_list");
final response = await plat.invokeMethod("play");
}
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
height: double.infinity,
width: double.infinity,
child:Form(
key: _formKey,
child: Column(
children:[
GestureDetector(
onLongPress: (){
print("我曹");
},
child: Container(
height: 50,
width: 200,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(30)
),
key: ValueKey("long"),
),
),
Container(
height: 500,
child: ListView.builder(
key: ValueKey("scroll"),
itemCount: 100,
itemBuilder: (_,index){
return GestureDetector(
key: ValueKey("btn$index"),
onTap: (){
play();
},
onLongPress: (){
print("哈哈哈哈");
},
child: Container(
key: ValueKey(index),
height: 70,
color: Colors.blue[index % 9 * 100],
child: ListTile(
title: Text("$index item"),
),
),
);
}),
),
SizedBox(height: 10,),
Container(
height: 50,
width: 200,
child: TextFormField(
validator: (value){
if(value == null || value.trim().toString() == ""){
return "账号为空";
}
},
key: ValueKey("input"),
decoration: InputDecoration(
hintText: "请输入账号"
),
),
),
ElevatedButton(
key: ValueKey("login"),
onPressed: (){
if(_formKey.currentState!.validate()){
}
}, child: Text("登录"))
]
),
)
),
);
}
}
能够应用大部分集成测试环境
import 'dart:io';
import 'package:day1/main.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main(){
int index = 1;
testWidgets("应用程序自动化测试用例", (tester) async{
print("Step No 第${index++}步: 打开myAI App");
await tester.pumpWidget(MyApp());
await tester.pumpAndSettle();
print("Step No 第${index++}步: 点击用户邮箱输入框,并输入");
final email = find.byKey(ValueKey("email"));
await tester.tap(email);
await tester.pumpAndSettle();
await tester.enterText(email, "username");
await tester.pumpAndSettle();
print("Step No 第${index++}步: 点击密码输入框,并输入");
final password = find.byKey(ValueKey("password"));
await tester.tap(password);
await tester.pumpAndSettle();
await tester.enterText(password, "123ab");
await tester.pumpAndSettle();
await tester.testTextInput.receiveAction(TextInputAction.done);
await tester.pumpAndSettle();
print("Step No 第${index++}步: 点击用户协议文本按钮");
await tester.tap(find.text("用户协议"));
await tester.pumpAndSettle();
print("Step No 第${index++}步: 点击同意按钮");
await tester.tap(find.byKey(ValueKey("agreement_btn")));
await tester.pumpAndSettle();
await tester.pumpAndSettle(Duration(seconds: 1));
print("Step No 第${index++}步: 点击登录按钮");
await tester.tap(find.byKey(ValueKey("login")));
await tester.pumpAndSettle();
await tester.pumpAndSettle(Duration(seconds: 1));
print("Step No 第${index++}步: 修改用户邮箱输入框的输入内容、在结尾增加文本");
await tester.tap(email);
await tester.pumpAndSettle();
await tester.enterText(email, "username@gmail.com");
await tester.pumpAndSettle();
await tester.pumpAndSettle(Duration(seconds: 1));
print("Step No 第${index++}步: 修改用户邮箱输入框的输入内容、在结尾增加文本");
await tester.enterText(password, "123ab6");
await tester.pumpAndSettle();
await tester.testTextInput.hide;
await tester.pumpAndSettle();
await tester.testTextInput.receiveAction(TextInputAction.done);
await tester.pumpAndSettle();
print("Step No 第${index++}步: 点击登录按钮");
await tester.tap(find.byKey(ValueKey("login")));
await tester.pumpAndSettle();
await tester.pumpAndSettle(Duration(seconds: 3));
print("Step No 第${index++}步: 点击欢迎页面进入首页");
await tester.tap(find.byKey(ValueKey("goHome")));
await tester.pumpAndSettle();
print("Step No 第${index++}步: X轴滑动汽车列表到 -1000");
await tester.drag(find.byKey(ValueKey("carList")), Offset(-1000,0));
await tester.pumpAndSettle();
print("Step No 第${index++}步: X轴滑动汽车列表到 1000");
await tester.drag(find.byKey(ValueKey("carList")), Offset(1000,0));
await tester.pumpAndSettle();
await tester.pumpAndSettle(Duration(seconds: 1));
print("Step No 第${index++}步: Y轴滑动全局列表 -1000");
await tester.drag(find.byKey(ValueKey("listBtn")), Offset(0.0,-1000));
await tester.pumpAndSettle();
print("Step No 第${index++}步: Y轴滑动全局列表 1000");
await tester.drag(find.byKey(ValueKey("listBtn")), Offset(0.0,1000));
await tester.pumpAndSettle();
print("Step No 第${index++}步: 点击 Book Now 进入到Booking页面");
await tester.tap(find.byKey(ValueKey("bookingBtn0")));
await tester.pumpAndSettle();
print("Step No 第${index++}步: 点击 '+' 加号按钮十次");
for(int i = 0; i < 10; i++){
await tester.tap(find.byKey(ValueKey("add")));
await tester.pumpAndSettle();
}
await tester.pumpAndSettle(Duration(seconds: 3));
print("Step No 第${index++}步: 点击 '-' 加号按钮十次");
for(int i = 0; i < 10; i++){
await tester.tap(find.byKey(ValueKey("subtract")));
await tester.pumpAndSettle();
}
print("Step No 第${index++}步: 点击单选按钮");
await tester.tap(find.byKey(ValueKey("check")));
await tester.pumpAndSettle();
await tester.pumpAndSettle(Duration(seconds: 3));
print("Step No 第${index++}步: 退出程序");
await tester.pumpAndSettle(Duration(seconds: 3));
exit(0);
});
}