跳到主要内容

tap

执行点击手势:

  • 在给定元素上。它将自动滚动以找到元素(如果元素不在可视区域)。
  • 或通过提供xy坐标在移动设备屏幕上点击

内部实现使用:

  • 元素点击:
    • 对于Web环境(Chrome/Safari浏览器或混合应用),使用click命令
    • 对于原生应用,使用Android的mobile: clickGesture 或iOS的mobile: tap,包括用于自动滚动的scrollIntoView命令
  • 屏幕点击:
    • 对于Web环境(Chrome/Safari浏览器或混合应用),使用action命令
    • 对于原生应用,使用Android的mobile: clickGesture 或iOS的mobile: tap

这种差异使tap命令成为移动应用中比click命令更可靠的替代方案。

对于原生应用,该命令与click命令的不同之处在于它将自动滑动到元素,使用scrollIntoView命令,而click命令不支持原生应用的这一功能。在混合应用或Web环境中,clicktap命令都支持自动滚动。

信息

此命令只适用于以下最新组件:

  • Appium服务器(版本2.0.0或更高)
  • appium-uiautomator2-driver(用于Android)
  • appium-xcuitest-driver(用于iOS)

确保定期更新本地或基于云的Appium环境,以避免兼容性问题。

关于屏幕点击

如果你想点击屏幕上的特定坐标,并使用截图来确定坐标,请记住iOS的坐标是基于设备的屏幕尺寸,而不是截图尺寸。由于设备像素比例,截图尺寸会更大。 直到iPhone 8和当前iPad的平均设备像素比例为2,而iPhone X之后的iPhone比例为3。这意味着截图尺寸是设备屏幕尺寸的2倍或3倍,因此如果你在截图上找到坐标,需要将它们除以设备像素比例才能得到正确的屏幕坐标。例如:

const screenshotCoordinates = { x: 600, y: 900 };
const dpr = 3; // 例如iPhone 16
const screenCoordinates = {
x: screenshotCoordinates.x / dpr,
y: screenshotCoordinates.y / dpr
};
await browser.tap(screenCoordinates);
Parameters
NameTypeDetails
options
optional
TapOptions点击选项(可选)
元素点击选项
options.x
optional
number数字(可选,如果设置了y则必填)
仅用于屏幕点击,不用于元素点击
options.y
optional
number数字(可选,如果设置了x则必填)
仅用于屏幕点击,不用于元素点击
屏幕点击选项
options.direction
optional
string可以是downupleftright之一,默认为down
仅用于元素点击,不用于屏幕点击
仅限移动原生应用
options.maxScrolls
optional
number搜索元素时的最大滚动次数,默认为10
仅用于元素点击,不用于屏幕点击
仅限移动原生应用
options.scrollableElement
optional
Element用于滚动的元素。如果未提供元素,则iOS将使用以下选择器-ios predicate string:type == "XCUIElementTypeApplication",Android将使用以下选择器//android.widget.ScrollView'。如果多个元素匹配默认选择器,则默认情况下它将选择第一个匹配的元素。
仅用于元素点击,不用于屏幕点击
仅限移动原生应用
Examples
element.tap.example.js
it('should be able to tap an on element', async () => {
const elem = $('~myElement')
// It will automatically scroll to the element if it's not already in the viewport
await elem.tap()
})

element.tap.scroll.options.example.js
it('should be able to swipe right 3 times in a custom scroll areas to an element and tap on the element', async () => {
const elem = $('~myElement')
// Swipe right 3 times in the custom scrollable element to find the element
await elem.tap({
direction: 'right',
maxScrolls: 3,
scrollableElement: $('#scrollable')
})
})

screen.tap.example.js
it('should be able to tap on screen coordinates', async () => {
await browser.tap({ x: 200, y: 400 })
})

Welcome! How can I help?

WebdriverIO AI Copilot