乐闻世界logo
搜索文章和话题

How does Appium perform gesture operations?

2月21日 16:19

Appium's gesture operations are an important feature for simulating user interactions, supporting various touch and gesture operations. Here's a detailed explanation of Appium's gesture operations:

Basic Gesture Operations

1. Tap

javascript
// Single tap await element.click(); // Tap coordinates await driver.touchActions([ { action: 'tap', x: 100, y: 200 } ]); // Multiple taps await element.click(); await element.click(); await element.click();

2. Long Press

javascript
// Long press element const actions = driver.actions({ async: true }); await actions.move({ origin: element }).press().pause(2000).release().perform(); // Long press coordinates await driver.touchActions([ { action: 'press', x: 100, y: 200 }, { action: 'wait', ms: 2000 }, { action: 'release' } ]);

3. Double Tap

javascript
// Double tap element const actions = driver.actions({ async: true }); await actions.move({ origin: element }).doubleClick().perform(); // Using TouchAction const touchAction = new TouchAction(driver); touchAction.tap({ x: 100, y: 200 }).tap({ x: 100, y: 200 }); await touchAction.perform();

4. Swipe

javascript
// Swipe element await driver.touchActions([ { action: 'press', x: 100, y: 500 }, { action: 'moveTo', x: 100, y: 100 }, { action: 'release' } ]); // Using TouchAction const touchAction = new TouchAction(driver); touchAction.press({ x: 100, y: 500 }).moveTo({ x: 100, y: 100 }).release(); await touchAction.perform();

Advanced Gesture Operations

1. Scroll

javascript
// Scroll to element await element.sendKeys('Hello'); // Scroll page const size = await driver.manage().window().getRect(); const startX = size.width / 2; const startY = size.height * 0.8; const endY = size.height * 0.2; await driver.touchActions([ { action: 'press', x: startX, y: startY }, { action: 'moveTo', x: startX, y: endY }, { action: 'release' } ]);

2. Drag and Drop

javascript
// Drag element const actions = driver.actions({ async: true }); await actions.dragAndDrop(sourceElement, targetElement).perform(); // Using TouchAction const touchAction = new TouchAction(driver); touchAction.press({ el: sourceElement }) .moveTo({ el: targetElement }) .release(); await touchAction.perform();

3. Pinch

javascript
// Pinch operation const actions = driver.actions({ async: true }); await actions .move({ origin: element }) .press() .move({ origin: element, x: 50, y: 0 }) .release() .perform();

4. Rotate

javascript
// Rotate operation const actions = driver.actions({ async: true }); await actions .move({ origin: element }) .press() .move({ origin: element, x: 0, y: 50 }) .release() .perform();

Multi-touch Operations

1. Multi-touch

javascript
// Multi-touch operation const actions = driver.actions({ async: true }); const finger1 = actions.move({ origin: element1 }); const finger2 = actions.move({ origin: element2 }); await actions .clear() .move({ origin: finger1 }).press() .move({ origin: finger2 }).press() .pause(100) .move({ origin: finger1 }).release() .move({ origin: finger2 }).release() .perform();

2. Pinch and Spread

javascript
// Pinch operation const actions = driver.actions({ async: true }); const center = { x: 200, y: 200 }; await actions .move({ origin: center, x: -50, y: 0 }).press() .move({ origin: center, x: 50, y: 0 }).press() .pause(500) .move({ origin: center, x: -25, y: 0 }).release() .move({ origin: center, x: 25, y: 0 }).release() .perform();

Gesture Operation Best Practices

1. Use Explicit Waits

javascript
// Wait for element to be interactable await driver.wait( until.elementIsClickable(element), 5000 ); // Execute gesture operation await element.click();

2. Handle Animations

javascript
// Wait for animation to complete await driver.sleep(500); // Execute gesture operation await element.click();

3. Verify Operation Results

javascript
// Execute gesture operation await element.click(); // Verify result const result = await driver.findElement(By.id('result_message')); const text = await result.getText(); assert.strictEqual(text, 'Success');

Gesture Operation Optimization

1. Reduce Gesture Operations

javascript
// ❌ Not recommended: Multiple clicks await element.click(); await element.click(); await element.click(); // ✅ Recommended: Use double click const actions = driver.actions({ async: true }); await actions.move({ origin: element }).doubleClick().perform();

2. Use Relative Coordinates

javascript
// Use element relative coordinates const rect = await element.getRect(); const centerX = rect.x + rect.width / 2; const centerY = rect.y + rect.height / 2; await driver.touchActions([ { action: 'press', x: centerX, y: centerY }, { action: 'release' } ]);

3. Handle Different Screen Sizes

javascript
// Get screen size const size = await driver.manage().window().getRect(); // Calculate relative coordinates const x = size.width * 0.5; const y = size.height * 0.5; await driver.touchActions([ { action: 'press', x: x, y: y }, { action: 'release' } ]);

Common Gesture Operation Issues

1. Gesture Operation Failed

Causes:

  • Element not visible or not clickable
  • Gesture operation blocked by other elements
  • Animation not completed

Solutions:

javascript
// Wait for element to be clickable await driver.wait( until.elementIsClickable(element), 5000 ); // Scroll to element await driver.executeScript('arguments[0].scrollIntoView(true);', element); // Execute gesture operation await element.click();

2. Inaccurate Gesture Operations

Causes:

  • Incorrect coordinate calculation
  • Screen size changes
  • Element position changes

Solutions:

javascript
// Use element location instead of coordinates await element.click(); // Dynamically calculate coordinates const rect = await element.getRect(); const x = rect.x + rect.width / 2; const y = rect.y + rect.height / 2;

3. Multi-touch Not Supported

Causes:

  • Device doesn't support multi-touch
  • Appium version doesn't support

Solutions:

javascript
// Check multi-touch support const capabilities = await driver.getCapabilities(); const supportsMultiTouch = capabilities.supportsMultiTouch; if (!supportsMultiTouch) { console.warn('Multi-touch not supported'); }

Gesture Operation Tools

1. Appium Inspector

Appium Inspector provides gesture operation recording:

  • Record gesture operations
  • Generate code
  • Test gesture operations

2. Custom Gesture Library

javascript
// Create custom gesture library class GestureHelper { constructor(driver) { this.driver = driver; } async swipe(startX, startY, endX, endY, duration = 1000) { await this.driver.touchActions([ { action: 'press', x: startX, y: startY }, { action: 'wait', ms: duration }, { action: 'moveTo', x: endX, y: endY }, { action: 'release' } ]); } async longPress(element, duration = 2000) { const actions = this.driver.actions({ async: true }); await actions.move({ origin: element }).press().pause(duration).release().perform(); } } // Use custom gesture library const gestures = new GestureHelper(driver); await gestures.swipe(100, 500, 100, 100); await gestures.longPress(element, 2000);

Best Practices

  1. Prioritize Element Operations:

    • Use element.click() instead of coordinate tapping
    • More stable and maintainable
    • Adapt to screen size changes
  2. Reasonable Use of Waits:

    • Wait for element to be interactable
    • Handle animations and loading
    • Avoid hard-coded waits
  3. Verify Operation Results:

    • Check state after operation
    • Verify expected results
    • Provide clear error messages
  4. Handle Exceptional Situations:

    • Catch gesture operation exceptions
    • Implement retry mechanisms
    • Record failure reasons

Appium's gesture operations provide testers with powerful user interaction simulation capabilities. Through reasonable use of various gesture operations, you can build realistic and reliable automated tests.

标签:Appium