Appium JS实战指南:元素查找、滚动与交互全攻略

引言

Appium JS 是一款用于自动化移动应用程序的多功能工具。测试自动化的一个关键部分是与应用程序元素交互,定位元素、滑动找到元素并执行操作。本文总结了Appium JS中元素识别、滚动和交互的最佳实践,希望可以帮助到你编写强大的测试脚本。

了解Appium JS 中的元素定位器

定位器是移动自动化的基础,Appium 支持多种定位器策略:

1.ID

– 适合具有唯一资源ID属性(Android)或可访问性(iOS)的元素。

const element = await driver.findElement("id", "username");
2.XPath

– 对于复杂的层次结构有用,但比其他策略慢。

const element = await driver.findElement("xpath", "//android.widget.TextView[@text='Login']");
3.Accessibility ID

– 非常适合跨平台自动化(Android 和 iOS)。

const element = await driver.findElement("accessibility id", "submit_button");
4.Class Name

– 根据元素的类别类型进行匹配。

const elements = await driver.findElements("class name", "android.widget.Button");
5.UIAutomator(仅限Android)

– 用于自定义查询的高级 Android 特定定位器。

const element = await driver.findElement("-android uiautomator", 'new UiSelector().text("Login")');
6.Predicate String(仅限iOS)

– 使用 iOS 谓词进行自定义元素匹配。

const element = await driver.findElement("-ios predicate string", "label == 'Login'");

定位元素的最佳实践

  • 首选唯一定位器:ID 或可访问性 ID 更快、更可靠。

  • 避免过度使用 XPath:它在动态 UI 中速度较慢且不太稳定。

  • 谨慎使用分层定位器:确保它们不会因 UI 更新而中断。

  • 在 Appium Inspector 中测试定位器:在脚本中使用定位器之前验证定位器。

滚动到元素

滚动对于与窗口外的元素进行交互至关重要。Appium 根据平台提供多种策略:

1.使用UIAutomator在Android中滚动
await driver.findElement("-android uiautomator", 
 'new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView(new UiSelector().text("Target Element"))');
2.在iOS中使用触摸操作进行滚动
await driver.execute('mobile: scroll', { direction: 'down' });
3.使用坐标手势滚动

要在两个平台上自定义滚动,请使用 touchAction:

await driver.touchAction([
 { action: 'press', x: 500, y: 1000 },
 { action: 'moveTo', x: 500, y: 200 },
 'release'
]);

元素交互

找到元素后,Appium JS允许执行各种交互:

点击元素
await element.click();
输入文字
await element.sendKeys("appium");
清除文本
await element.clear();
检查文本
const text = await element.getText();
console.log(text);
处理复选框
if (!(await element.isSelected())) {
 await element.click();
}
在元素上滑动
await driver.touchAction([
 { action: 'press', x: 800, y: 500 },
 { action: 'moveTo', x: 200, y: 500 },
 'release'
]);

高级技术

等待元素

动态加载在移动应用中很常见,使用显式等待来处理它。

const element = await driver.$("//button[@text='Submit']");
await element.waitForDisplayed({ timeout: 5000 });
长按操作

对于上下文菜单或拖放交互很有用。

await driver.touchAction([
 { action: 'longPress', element: targetElement },
 'release'
]);
拖放
await driver.touchAction([
 { action: 'press', element: sourceElement },
 { action: 'moveTo', element: destinationElement },
 'release'
]);

点击弹窗
const alertText = await driver.getAlertText();
await driver.acceptAlert(); 

常见问题

未找到元素

解决方法:

  • 确保定位器准确且唯一;

  • 如果元素需要时间加载,则添加明确的等待;

滚动不起作用

解决方法:

  • 验证可滚动的父元素是否被正确识别;

  • 使用 Appium Inspector 确认元素层次结构;

手势失败

解决方法:

  • 根据设备屏幕尺寸调整手势坐标;

  • 使用真实设备测试复杂的手势;

健壮脚本的最佳实践

  • 利用 Appium Inspector:在编写脚本之前验证定位器和手势;

  • 模块化代码:将测试脚本分解为可重复使用的函数,以提高可维护性;

  • 在真实设备上运行测试:模拟器/模拟器可能无法完全复制真实世界的行为。

  • 使用日志记录:添加日志以有效地调试和监控脚本执行;

结论

掌握Appium JS中的元素定位器、滚动和交互对于构建可靠的自动化脚本至关重要。希望通过本文的总结,你可以轻松处理动态UI,从而提高移动测试工作流程的效率。

你对 Appium JS 有什么建议或疑问吗?在下面的评论中分享你的想法!

猜你喜欢

转载自blog.csdn.net/weixin_39810558/article/details/146613261
今日推荐