flutter_boost混合了原生代码和flutter代码,会涉及到flutter和原生界面的跳转。
使用IDEA创建项目:
然后finsh即可。
导入appium-flutter-driver代码
目前appium-flutter-driver还没有放到mvn repository上,所以,我这里是将appium-flutter-driver的kotlin代码拷贝到我的项目里了。
在pom.xml上添加依赖:
<!--添加依赖 --> <dependencies> <dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>7.3.0</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency> <dependency> <groupId>org.jetbrains.kotlinx</groupId> <artifactId>kotlinx-serialization-runtime</artifactId> <version>0.20.0</version> </dependency> </dependencies>
编写测试代码:
import io.appium.java_client.MobileElement; import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.service.local.AppiumDriverLocalService; import org.openqa.selenium.remote.DesiredCapabilities; import pro.truongsinh.appium_flutter.FlutterFinder; import pro.truongsinh.appium_flutter.finder.FlutterElement; import java.io.File; public class AppiumTestDemo { public static String appPtah = "/flutterboost-debug.apk"; public static void main(String[] args) { File rootFile = new File(""); System.out.println("测试app文件路径:" + rootFile.getAbsolutePath()); File appFile = new File(rootFile.getAbsolutePath() + appPtah); System.out.println("测试app文件路径:" + appFile.getAbsolutePath()); appPtah = appFile.getAbsolutePath(); AndroidDriver<MobileElement> driver = getFlutterDriver(); // 切换成本地的代码 driver.context("NATIVE_APP"); MobileElement openNativeWebElement = driver.findElementById("com.taobao.idlefish.flutterboostexample:id/open_flutter"); openNativeWebElement.click(); System.out.println("点击完成跳转至flutter页面完成"); // 切换成flutter端的代码 driver.context("FLUTTER"); FlutterFinder find = new FlutterFinder(driver); FlutterElement buttonFinder = find.byValueKey("openFirstPage"); buttonFinder.click(); System.out.println("openFirstPage页面完成"); try { Thread.sleep(50000); } catch (InterruptedException e) { e.printStackTrace(); } driver.quit(); } public static AndroidDriver<MobileElement> getFlutterDriver() { System.out.println("getFlutterDriver"); DesiredCapabilities flutterCapabilities = new DesiredCapabilities(); flutterCapabilities.setCapability("platformName", "Android"); flutterCapabilities.setCapability("deviceName", "one plus"); flutterCapabilities.setCapability("app", appPtah); flutterCapabilities.setCapability("platformVersion", "10"); flutterCapabilities.setCapability("noReset", true); flutterCapabilities.setCapability("automationName", "Flutter"); AppiumDriverLocalService service = AppiumDriverLocalService.buildDefaultService(); AndroidDriver<MobileElement> driver = new AndroidDriver<MobileElement>(service.getUrl(), flutterCapabilities); System.out.println("getFlutterDriver finish"); return driver; } }