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

面试题手册

Appium 如何测试混合应用?

Appium 的混合应用测试是移动应用自动化测试中的重要场景,混合应用结合了原生视图和 WebView,需要特殊处理。以下是 Appium 混合应用测试的详细说明:混合应用概述什么是混合应用混合应用是指同时包含原生视图和 WebView 的移动应用:原生视图:使用平台原生控件构建的界面WebView:嵌入的浏览器组件,用于显示 Web 内容混合应用:在原生应用中嵌入 WebView 来显示部分或全部内容混合应用特点// 混合应用示例结构{ "appType": "Hybrid", "components": [ { "type": "Native", "content": "原生导航栏、底部菜单、原生控件" }, { "type": "WebView", "content": "Web 页面、H5 内容、React/Vue 应用" } ]}上下文切换1. 获取所有上下文// 获取所有可用的上下文const contexts = await driver.getContexts();console.log('Available contexts:', contexts);// 输出示例:// ['NATIVE_APP', 'WEBVIEW_com.example.app']2. 切换到 WebView// 切换到 WebView 上下文const contexts = await driver.getContexts();const webViewContext = contexts.find(ctx => ctx.includes('WEBVIEW'));if (webViewContext) { await driver.context(webViewContext); console.log('Switched to WebView context');} else { console.error('WebView context not found');}3. 切换回原生应用// 切换回原生应用上下文await driver.context('NATIVE_APP');console.log('Switched to Native context');4. 获取当前上下文// 获取当前上下文const currentContext = await driver.getContext();console.log('Current context:', currentContext);WebView 元素定位1. 在 WebView 中定位元素// 切换到 WebView 上下文await driver.context('WEBVIEW_com.example.app');// 使用标准的 WebDriver 定位策略const element = await driver.findElement(By.id('submit_button'));await element.click();// 使用 CSS 选择器const element = await driver.findElement(By.css('.submit-btn'));await element.click();// 使用 XPathconst element = await driver.findElement(By.xpath('//button[@id="submit_button"]'));await element.click();2. 在原生视图中定位元素// 切换到原生应用上下文await driver.context('NATIVE_APP');// 使用 Appium 的定位策略const element = await driver.findElement(By.id('submit_button'));await element.click();// 使用 Accessibility IDconst element = await driver.findElement(By.accessibilityId('submit_button'));await element.click();混合应用测试流程1. 完整的测试流程const { Builder, By, until } = require('selenium-webdriver');describe('Hybrid App Test', () => { let driver; before(async () => { const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/hybrid-app.apk', autoWebview: false // 不自动切换到 WebView }; driver = await new Builder().withCapabilities(capabilities).build(); }); after(async () => { await driver.quit(); }); it('should test hybrid app', async () => { // 1. 在原生视图中操作 await driver.context('NATIVE_APP'); const nativeButton = await driver.findElement(By.id('open_webview_button')); await nativeButton.click(); // 2. 等待 WebView 加载 await driver.wait(async () => { const contexts = await driver.getContexts(); return contexts.some(ctx => ctx.includes('WEBVIEW')); }, 10000); // 3. 切换到 WebView const contexts = await driver.getContexts(); const webViewContext = contexts.find(ctx => ctx.includes('WEBVIEW')); await driver.context(webViewContext); // 4. 在 WebView 中操作 const webInput = await driver.findElement(By.id('username')); await webInput.sendKeys('testuser'); const webButton = await driver.findElement(By.id('submit_button')); await webButton.click(); // 5. 验证结果 const result = await driver.findElement(By.id('result_message')); const text = await result.getText(); assert.strictEqual(text, 'Success'); // 6. 切换回原生视图 await driver.context('NATIVE_APP'); // 7. 在原生视图中继续操作 const closeButton = await driver.findElement(By.id('close_webview_button')); await closeButton.click(); });});WebView 调试1. 启用 WebView 调试// Android WebView 调试配置const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/hybrid-app.apk', // WebView 调试配置 chromeOptions: { androidPackage: 'com.example.app', androidDeviceSerial: 'emulator-5554' }, // 自动切换到 WebView autoWebview: true};2. 检查 WebView 状态// 检查 WebView 是否可用async function isWebViewAvailable(driver) { const contexts = await driver.getContexts(); return contexts.some(ctx => ctx.includes('WEBVIEW'));}const isAvailable = await isWebViewAvailable(driver);console.log('WebView available:', isAvailable);3. 等待 WebView 加载// 等待 WebView 上下文出现await driver.wait(async () => { const contexts = await driver.getContexts(); return contexts.some(ctx => ctx.includes('WEBVIEW'));}, 10000);// 等待 WebView 页面加载完成await driver.wait( until.titleIs('Page Title'), 10000);跨上下文操作1. 在不同上下文中操作// 创建跨上下文操作辅助函数class HybridAppHelper { constructor(driver) { this.driver = driver; } async switchToNative() { await this.driver.context('NATIVE_APP'); } async switchToWebView() { const contexts = await this.driver.getContexts(); const webViewContext = contexts.find(ctx => ctx.includes('WEBVIEW')); if (webViewContext) { await this.driver.context(webViewContext); } else { throw new Error('WebView context not found'); } } async clickNativeButton(id) { await this.switchToNative(); const button = await this.driver.findElement(By.id(id)); await button.click(); } async fillWebForm(data) { await this.switchToWebView(); for (const [key, value] of Object.entries(data)) { const input = await this.driver.findElement(By.id(key)); await input.clear(); await input.sendKeys(value); } } async submitWebForm(buttonId) { await this.switchToWebView(); const button = await this.driver.findElement(By.id(buttonId)); await button.click(); }}// 使用辅助函数const helper = new HybridAppHelper(driver);// 点击原生按钮打开 WebViewawait helper.clickNativeButton('open_webview_button');// 在 WebView 中填写表单await helper.fillWebForm({ username: 'testuser', password: 'password123'});// 提交表单await helper.submitWebForm('submit_button');2. 处理多个 WebView// 处理多个 WebViewconst contexts = await driver.getContexts();console.log('All contexts:', contexts);// 输出示例:// ['NATIVE_APP', 'WEBVIEW_com.example.app', 'WEBVIEW_com.example.app.1']// 切换到特定的 WebViewconst webViewContext = contexts.find(ctx => ctx.includes('WEBVIEW_com.example.app.1'));if (webViewContext) { await driver.context(webViewContext);}混合应用最佳实践1. 上下文管理// 使用上下文管理器class ContextManager { constructor(driver) { this.driver = driver; this.previousContext = null; } async switchTo(context) { this.previousContext = await this.driver.getContext(); await this.driver.context(context); } async restorePreviousContext() { if (this.previousContext) { await this.driver.context(this.previousContext); } } async withNativeContext(callback) { await this.switchTo('NATIVE_APP'); try { return await callback(); } finally { await this.restorePreviousContext(); } } async withWebViewContext(callback) { const contexts = await this.driver.getContexts(); const webViewContext = contexts.find(ctx => ctx.includes('WEBVIEW')); if (webViewContext) { await this.switchTo(webViewContext); try { return await callback(); } finally { await this.restorePreviousContext(); } } else { throw new Error('WebView context not found'); } }}// 使用上下文管理器const contextManager = new ContextManager(driver);// 在原生上下文中执行操作await contextManager.withNativeContext(async () => { const button = await driver.findElement(By.id('native_button')); await button.click();});// 在 WebView 上下文中执行操作await contextManager.withWebViewContext(async () => { const input = await driver.findElement(By.id('web_input')); await input.sendKeys('test');});2. 等待策略// 等待 WebView 可用async function waitForWebView(driver, timeout = 10000) { return driver.wait(async () => { const contexts = await driver.getContexts(); return contexts.some(ctx => ctx.includes('WEBVIEW')); }, timeout);}// 等待 WebView 页面加载async function waitForWebViewPageLoad(driver, timeout = 10000) { return driver.wait( until.titleIs('Expected Page Title'), timeout );}// 等待 WebView 元素async function waitForWebViewElement(driver, locator, timeout = 10000) { const contexts = await driver.getContexts(); const webViewContext = contexts.find(ctx => ctx.includes('WEBVIEW')); if (webViewContext) { await driver.context(webViewContext); return driver.wait(until.elementLocated(locator), timeout); } else { throw new Error('WebView context not found'); }}// 使用等待函数await waitForWebView(driver);await waitForWebViewPageLoad(driver);const element = await waitForWebViewElement(driver, By.id('submit_button'));3. 错误处理// 处理上下文切换错误async function safeSwitchToContext(driver, context) { try { const contexts = await driver.getContexts(); if (contexts.includes(context)) { await driver.context(context); return true; } else { console.error(`Context ${context} not found`); return false; } } catch (error) { console.error('Error switching context:', error); return false; }}// 使用错误处理const success = await safeSwitchToContext(driver, 'WEBVIEW_com.example.app');if (success) { // 在 WebView 中执行操作} else { // 处理错误}常见问题1. WebView 上下文未找到问题:无法找到 WebView 上下文解决方案:// 检查 WebView 是否启用const contexts = await driver.getContexts();console.log('Available contexts:', contexts);// 确保 WebView 调试已启用// 在 AndroidManifest.xml 中添加:// <application android:debuggable="true">// 或者在代码中启用:// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {// WebView.setWebContentsDebuggingEnabled(true);// }2. 上下文切换超时问题:切换上下文时超时解决方案:// 增加超时时间await driver.wait(async () => { const contexts = await driver.getContexts(); return contexts.some(ctx => ctx.includes('WEBVIEW'));}, 20000);// 使用重试机制async function retrySwitchToContext(driver, context, maxRetries = 3) { for (let i = 0; i < maxRetries; i++) { try { await driver.context(context); return true; } catch (error) { if (i === maxRetries - 1) { throw error; } await driver.sleep(1000); } } return false;}3. WebView 元素定位失败问题:在 WebView 中定位元素失败解决方案:// 确保已切换到 WebView 上下文const currentContext = await driver.getContext();console.log('Current context:', currentContext);// 使用正确的定位策略const element = await driver.findElement(By.css('#submit-button'));const element = await driver.findElement(By.xpath('//button[@id="submit_button"]'));// 等待元素加载const element = await driver.wait( until.elementLocated(By.id('submit_button')), 10000);Appium 的混合应用测试需要处理原生视图和 WebView 之间的切换,通过合理的上下文管理和等待策略,可以构建稳定、可靠的混合应用自动化测试。
阅读 0·2月21日 16:20

Appium 如何进行数据驱动测试?

Appium 的数据驱动测试是提高测试效率和覆盖率的重要方法,通过使用不同的测试数据来验证应用程序的各种场景。以下是 Appium 数据驱动测试的详细说明:数据驱动测试概述什么是数据驱动测试数据驱动测试(Data-Driven Testing,DDT)是一种测试方法,将测试数据与测试逻辑分离:测试逻辑:测试的执行步骤和验证逻辑测试数据:测试输入和预期输出数据源:外部文件、数据库、API 等数据驱动测试的优势// 数据驱动测试的优势{ "advantages": [ "提高测试覆盖率", "简化测试维护", "支持多场景测试", "减少代码重复", "提高测试效率" ]}数据源类型1. JSON 数据源// test-data.json{ "testCases": [ { "id": "TC001", "description": "Valid login", "username": "testuser", "password": "password123", "expected": "Success" }, { "id": "TC002", "description": "Invalid password", "username": "testuser", "password": "wrongpassword", "expected": "Invalid password" }, { "id": "TC003", "description": "Empty username", "username": "", "password": "password123", "expected": "Username required" } ]}// 使用 JSON 数据源const testData = require('./test-data.json');testData.testCases.forEach((testCase) => { it(`Test case ${testCase.id}: ${testCase.description}`, async () => { // 输入用户名 const usernameInput = await driver.findElement(By.id('username')); await usernameInput.sendKeys(testCase.username); // 输入密码 const passwordInput = await driver.findElement(By.id('password')); await passwordInput.sendKeys(testCase.password); // 点击登录按钮 const loginButton = await driver.findElement(By.id('login_button')); await loginButton.click(); // 验证结果 const resultMessage = await driver.findElement(By.id('result_message')); const actual = await resultMessage.getText(); assert.strictEqual(actual, testCase.expected); });});2. CSV 数据源// test-data.csvid,description,username,password,expectedTC001,Valid login,testuser,password123,SuccessTC002,Invalid password,testuser,wrongpassword,Invalid passwordTC003,Empty username,,password123,Username required// 使用 CSV 数据源const csv = require('csv-parser');const fs = require('fs');const testData = [];fs.createReadStream('./test-data.csv') .pipe(csv()) .on('data', (row) => { testData.push(row); }) .on('end', () => { testData.forEach((testCase) => { it(`Test case ${testCase.id}: ${testCase.description}`, async () => { // 执行测试 const usernameInput = await driver.findElement(By.id('username')); await usernameInput.sendKeys(testCase.username); const passwordInput = await driver.findElement(By.id('password')); await passwordInput.sendKeys(testCase.password); const loginButton = await driver.findElement(By.id('login_button')); await loginButton.click(); const resultMessage = await driver.findElement(By.id('result_message')); const actual = await resultMessage.getText(); assert.strictEqual(actual, testCase.expected); }); }); });3. Excel 数据源// 使用 Excel 数据源const xlsx = require('xlsx');const workbook = xlsx.readFile('./test-data.xlsx');const sheet = workbook.Sheets['Sheet1'];const testData = xlsx.utils.sheet_to_json(sheet);testData.forEach((testCase) => { it(`Test case ${testCase.id}: ${testCase.description}`, async () => { // 执行测试 const usernameInput = await driver.findElement(By.id('username')); await usernameInput.sendKeys(testCase.username); const passwordInput = await driver.findElement(By.id('password')); await passwordInput.sendKeys(testCase.password); const loginButton = await driver.findElement(By.id('login_button')); await loginButton.click(); const resultMessage = await driver.findElement(By.id('result_message')); const actual = await resultMessage.getText(); assert.strictEqual(actual, testCase.expected); });});4. YAML 数据源// test-data.yamltestCases: - id: TC001 description: Valid login username: testuser password: password123 expected: Success - id: TC002 description: Invalid password username: testuser password: wrongpassword expected: Invalid password - id: TC003 description: Empty username username: "" password: password123 expected: Username required// 使用 YAML 数据源const yaml = require('js-yaml');const fs = require('fs');const testData = yaml.load(fs.readFileSync('./test-data.yaml', 'utf8'));testData.testCases.forEach((testCase) => { it(`Test case ${testCase.id}: ${testCase.description}`, async () => { // 执行测试 const usernameInput = await driver.findElement(By.id('username')); await usernameInput.sendKeys(testCase.username); const passwordInput = await driver.findElement(By.id('password')); await passwordInput.sendKeys(testCase.password); const loginButton = await driver.findElement(By.id('login_button')); await loginButton.click(); const resultMessage = await driver.findElement(By.id('result_message')); const actual = await resultMessage.getText(); assert.strictEqual(actual, testCase.expected); });});数据驱动测试框架1. Mocha 数据驱动测试const { Builder, By, until } = require('selenium-webdriver');const assert = require('assert');const testData = require('./test-data.json');describe('Data-Driven Tests with Mocha', () => { let driver; before(async () => { const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk' }; driver = await new Builder().withCapabilities(capabilities).build(); }); after(async () => { await driver.quit(); }); testData.testCases.forEach((testCase) => { it(`Test case ${testCase.id}: ${testCase.description}`, async () => { // 执行测试 const usernameInput = await driver.findElement(By.id('username')); await usernameInput.sendKeys(testCase.username); const passwordInput = await driver.findElement(By.id('password')); await passwordInput.sendKeys(testCase.password); const loginButton = await driver.findElement(By.id('login_button')); await loginButton.click(); const resultMessage = await driver.findElement(By.id('result_message')); const actual = await resultMessage.getText(); assert.strictEqual(actual, testCase.expected); }); });});2. Jest 数据驱动测试const { Builder, By, until } = require('selenium-webdriver');const testData = require('./test-data.json');describe('Data-Driven Tests with Jest', () => { let driver; beforeAll(async () => { const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk' }; driver = await new Builder().withCapabilities(capabilities).build(); }); afterAll(async () => { await driver.quit(); }); testData.testCases.forEach((testCase) => { test(`Test case ${testCase.id}: ${testCase.description}`, async () => { // 执行测试 const usernameInput = await driver.findElement(By.id('username')); await usernameInput.sendKeys(testCase.username); const passwordInput = await driver.findElement(By.id('password')); await passwordInput.sendKeys(testCase.password); const loginButton = await driver.findElement(By.id('login_button')); await loginButton.click(); const resultMessage = await driver.findElement(By.id('result_message')); const actual = await resultMessage.getText(); expect(actual).toBe(testCase.expected); }); });});3. TestNG 数据驱动测试(Java)import org.testng.annotations.*;import org.openqa.selenium.*;import org.openqa.selenium.remote.DesiredCapabilities;import io.appium.java_client.AppiumDriver;import io.appium.java_client.MobileElement;import java.io.FileReader;import com.opencsv.CSVReader;public class DataDrivenAppiumTests { private AppiumDriver<MobileElement> driver; @BeforeClass public void setUp() throws Exception { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("deviceName", "Pixel 5"); capabilities.setCapability("app", "/path/to/app.apk"); driver = new AppiumDriver<>( new URL("http://localhost:4723/wd/hub"), capabilities ); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } @Test(dataProvider = "loginData") public void testLogin(String id, String description, String username, String password, String expected) throws Exception { // 输入用户名 MobileElement usernameInput = driver.findElement(By.id("username")); usernameInput.sendKeys(username); // 输入密码 MobileElement passwordInput = driver.findElement(By.id("password")); passwordInput.sendKeys(password); // 点击登录按钮 MobileElement loginButton = driver.findElement(By.id("login_button")); loginButton.click(); // 验证结果 MobileElement resultMessage = driver.findElement(By.id("result_message")); String actual = resultMessage.getText(); assertEquals(actual, expected); } @DataProvider(name = "loginData") public Object[][] getLoginData() throws Exception { CSVReader reader = new CSVReader(new FileReader("test-data.csv")); List<String[]> records = reader.readAll(); reader.close(); Object[][] data = new Object[records.size() - 1][5]; for (int i = 1; i < records.size(); i++) { String[] record = records.get(i); data[i - 1] = new Object[] { record[0], // id record[1], // description record[2], // username record[3], // password record[4] // expected }; } return data; }}数据驱动测试最佳实践1. 数据验证// 数据验证函数function validateTestData(testData) { const requiredFields = ['id', 'description', 'username', 'password', 'expected']; for (const testCase of testData) { for (const field of requiredFields) { if (!(field in testCase)) { throw new Error(`Missing required field: ${field}`); } } } return true;}// 使用数据验证const testData = require('./test-data.json');validateTestData(testData.testCases);2. 数据清理// 数据清理函数function cleanTestData(testData) { return testData.map((testCase) => { return { id: testCase.id.trim(), description: testCase.description.trim(), username: testCase.username.trim(), password: testCase.password.trim(), expected: testCase.expected.trim() }; });}// 使用数据清理const rawData = require('./test-data.json');const testData = cleanTestData(rawData.testCases);3. 数据过滤// 数据过滤函数function filterTestData(testData, filterFn) { return testData.filter(filterFn);}// 使用数据过滤const testData = require('./test-data.json');const validTests = filterTestData(testData.testCases, (testCase) => { return testCase.username !== '' && testCase.password !== '';});4. 数据分组// 数据分组函数function groupTestData(testData, groupBy) { return testData.reduce((groups, testCase) => { const key = testCase[groupBy]; if (!groups[key]) { groups[key] = []; } groups[key].push(testCase); return groups; }, {});}// 使用数据分组const testData = require('./test-data.json');const groupedTests = groupTestData(testData.testCases, 'category');// 按组执行测试for (const [category, tests] of Object.entries(groupedTests)) { describe(`Category: ${category}`, () => { tests.forEach((testCase) => { it(`Test case ${testCase.id}: ${testCase.description}`, async () => { // 执行测试 }); }); });}高级数据驱动测试1. 动态数据生成// 动态生成测试数据function generateTestData(count) { const testData = []; for (let i = 0; i < count; i++) { testData.push({ id: `TC${String(i + 1).padStart(3, '0')}`, description: `Generated test ${i + 1}`, username: `user${i + 1}`, password: `password${i + 1}`, expected: 'Success' }); } return testData;}// 使用动态生成的数据const testData = generateTestData(100);testData.forEach((testCase) => { it(`Test case ${testCase.id}: ${testCase.description}`, async () => { // 执行测试 });});2. 数据依赖// 处理数据依赖async function runDependentTests(testData) { const results = []; for (const testCase of testData) { if (testCase.dependsOn) { const dependentResult = results.find(r => r.id === testCase.dependsOn); if (!dependentResult || !dependentResult.success) { console.log(`Skipping ${testCase.id} because dependency failed`); continue; } } try { // 执行测试 const result = await executeTest(testCase); results.push({ id: testCase.id, success: true, result }); } catch (error) { results.push({ id: testCase.id, success: false, error }); } } return results;}3. 数据驱动报告// 生成数据驱动测试报告function generateTestReport(results) { const report = { total: results.length, passed: results.filter(r => r.success).length, failed: results.filter(r => !r.success).length, details: results }; return report;}// 使用测试报告const results = await runTests(testData);const report = generateTestReport(results);console.log('Test Report:', JSON.stringify(report, null, 2));常见问题1. 数据文件格式错误问题:数据文件格式不正确解决方案:// 验证数据文件格式function validateDataFormat(data) { if (!Array.isArray(data)) { throw new Error('Data must be an array'); } if (data.length === 0) { throw new Error('Data array is empty'); } return true;}// 使用数据格式验证const testData = require('./test-data.json');validateDataFormat(testData.testCases);2. 数据类型不匹配问题:数据类型与预期不符解决方案:// 转换数据类型function convertDataTypes(testData) { return testData.map((testCase) => { return { ...testCase, age: parseInt(testCase.age), price: parseFloat(testCase.price) }; });}// 使用数据类型转换const rawData = require('./test-data.json');const testData = convertDataTypes(rawData.testCases);3. 测试数据过多问题:测试数据量过大导致测试时间过长解决方案:// 分批执行测试async function runTestsInBatches(testData, batchSize = 10) { const batches = []; for (let i = 0; i < testData.length; i += batchSize) { batches.push(testData.slice(i, i + batchSize)); } for (const batch of batches) { await runTests(batch); await cleanup(); // 清理资源 }}// 使用分批执行const testData = require('./test-data.json');await runTestsInBatches(testData.testCases, 10);Appium 的数据驱动测试为测试人员提供了灵活的测试方法,通过合理使用各种数据源和测试框架,可以构建高效、可维护的自动化测试。
阅读 0·2月21日 16:20

如何优化 Appium 测试性能?

Appium 的性能优化是提高测试效率和稳定性的关键环节,通过合理的优化策略可以显著提升测试执行速度和可靠性。以下是 Appium 性能优化的详细说明:元素定位优化1. 使用高效的定位策略// ❌ 不推荐:使用复杂的 XPathconst element = await driver.findElement( By.xpath('//android.widget.Button[@text="Submit" and @index="0" and contains(@class, "Button")]'));// ✅ 推荐:使用 ID 或 Accessibility IDconst element = await driver.findElement(By.id('submit_button'));const element = await driver.findElement(By.accessibilityId('submit_button'));// ✅ 推荐:使用平台特定的定位策略const element = await driver.findElement( By.androidUIAutomator('new UiSelector().text("Submit")'));2. 减少定位范围// ❌ 不推荐:在整个页面中搜索const element = await driver.findElement(By.id('submit_button'));// ✅ 推荐:在特定容器中搜索const container = await driver.findElement(By.id('form_container'));const element = await container.findElement(By.id('submit_button'));3. 缓存元素引用// ❌ 不推荐:重复定位await driver.findElement(By.id('submit_button')).click();await driver.findElement(By.id('submit_button')).sendKeys('text');await driver.findElement(By.id('submit_button')).click();// ✅ 推荐:缓存元素引用const button = await driver.findElement(By.id('submit_button'));await button.click();await button.sendKeys('text');await button.click();等待机制优化1. 优先使用显式等待// ❌ 不推荐:使用隐式等待await driver.manage().timeouts().implicitlyWait(10000);// ✅ 推荐:使用显式等待const element = await driver.wait( until.elementLocated(By.id('submit_button')), 5000);2. 避免硬编码等待// ❌ 不推荐:使用 sleepawait driver.sleep(5000);const element = await driver.findElement(By.id('submit_button'));// ✅ 推荐:使用条件等待const element = await driver.wait( until.elementLocated(By.id('submit_button')), 5000);3. 并行等待// 并行等待多个元素const [element1, element2] = await Promise.all([ driver.wait(until.elementLocated(By.id('button1')), 5000), driver.wait(until.elementLocated(By.id('button2')), 5000)]);会话管理优化1. 复用会话// ❌ 不推荐:每个测试都创建新会话describe('Test Suite', () => { it('Test 1', async () => { const driver = await new Builder().withCapabilities(capabilities).build(); // 执行测试 await driver.quit(); }); it('Test 2', async () => { const driver = await new Builder().withCapabilities(capabilities).build(); // 执行测试 await driver.quit(); });});// ✅ 推荐:复用会话describe('Test Suite', () => { let driver; before(async () => { driver = await new Builder().withCapabilities(capabilities).build(); }); after(async () => { await driver.quit(); }); it('Test 1', async () => { // 执行测试 }); it('Test 2', async () => { // 执行测试 });});2. 合理配置会话参数// 优化会话参数const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk', // 性能优化 noReset: true, // 不重置应用状态 fullReset: false, // 不完全重置 autoLaunch: true, // 自动启动应用 // 超时优化 newCommandTimeout: 60, // 新命令超时时间 // 跳过不必要的步骤 skipServerInstallation: false, skipDeviceInitialization: false, skipUninstall: false, // 禁用动画 disableWindowAnimation: true, ignoreUnimportantViews: true, // 其他优化 clearSystemFiles: true, eventTimings: false};并行测试优化1. 使用多设备并行测试// 并行测试配置const devices = [ { platformName: 'Android', deviceName: 'Pixel 5' }, { platformName: 'Android', deviceName: 'Pixel 6' }, { platformName: 'Android', deviceName: 'Pixel 7' }];// 使用 Mocha 并行测试devices.forEach((device, index) => { describe(`Test on ${device.deviceName}`, () => { let driver; before(async () => { driver = await new Builder() .withCapabilities({ ...capabilities, ...device }) .build(); }); after(async () => { await driver.quit(); }); it('should submit form', async () => { const element = await driver.findElement(By.id('submit_button')); await element.click(); }); });});2. 使用 TestNG 并行测试// TestNG 并行测试配置@Test(threadPoolSize = 3, invocationCount = 3)public class ParallelAppiumTests { @Test(dataProvider = "devices") public void testOnDevice(String deviceName) throws Exception { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("deviceName", deviceName); capabilities.setCapability("app", "/path/to/app.apk"); AppiumDriver<MobileElement> driver = new AppiumDriver<>( new URL("http://localhost:4723/wd/hub"), capabilities ); try { MobileElement element = driver.findElement(By.id("submit_button")); element.click(); } finally { driver.quit(); } } @DataProvider(name = "devices", parallel = true) public Object[][] getDevices() { return new Object[][] { {"Pixel 5"}, {"Pixel 6"}, {"Pixel 7"} }; }}网络优化1. 使用本地服务器// ❌ 不推荐:使用远程服务器const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk'};const driver = await new Builder() .withCapabilities(capabilities) .usingServer('http://remote-server:4723/wd/hub') .build();// ✅ 推荐:使用本地服务器const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk'};const driver = await new Builder() .withCapabilities(capabilities) .usingServer('http://localhost:4723/wd/hub') .build();2. 优化网络配置// 优化网络超时const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk', // 网络优化 newCommandTimeout: 60, commandTimeouts: { implicit: 0, pageLoad: 300000, script: 30000 }, // 连接优化 wdaConnectionTimeout: 60000, wdaStartupRetries: 4};资源管理优化1. 及时释放资源// 确保资源及时释放describe('Test Suite', () => { let driver; before(async () => { driver = await new Builder().withCapabilities(capabilities).build(); }); after(async () => { if (driver) { await driver.quit(); } }); it('Test 1', async () => { // 执行测试 });});2. 清理临时文件// 清理临时文件const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk', clearSystemFiles: true};测试数据优化1. 使用轻量级测试数据// ❌ 不推荐:使用大量测试数据const testData = require('./large-test-data.json');// ✅ 推荐:使用轻量级测试数据const testData = [ { input: 'test1', expected: 'result1' }, { input: 'test2', expected: 'result2' }];2. 分批执行测试// 分批执行测试const testBatches = [ ['test1', 'test2', 'test3'], ['test4', 'test5', 'test6'], ['test7', 'test8', 'test9']];for (const batch of testBatches) { for (const testName of batch) { await runTest(testName); } // 清理资源 await cleanup();}监控和调试1. 启用性能监控// 启用性能监控const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk', eventTimings: true};// 记录性能数据const timings = await driver.getPerformanceData();console.log('Performance timings:', timings);2. 使用日志分析// 配置详细日志const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk', // 日志配置 showXcodeLog: true, debugLogSpacing: true};// 分析日志const logs = await driver.manage().logs().get('logcat');console.log('Logs:', logs);最佳实践1. 元素定位优先使用 ID 和 Accessibility ID避免使用复杂的 XPath使用相对定位缓存元素引用2. 等待机制优先使用显式等待避免硬编码等待合理设置超时时间使用并行等待3. 会话管理复用会话合理配置会话参数及时释放资源清理临时文件4. 并行测试使用多设备并行测试合理分配测试任务避免资源竞争监控测试进度5. 网络优化使用本地服务器优化网络配置减少网络延迟使用缓存6. 测试数据使用轻量级测试数据分批执行测试避免重复数据优化数据结构性能优化工具1. Appium InspectorAppium Inspector 提供性能分析功能:元素定位性能分析操作执行时间统计内存使用监控2. Chrome DevTools使用 Chrome DevTools 分析 WebView 性能:网络请求分析JavaScript 执行时间内存使用情况3. Android Profiler使用 Android Profiler 分析应用性能:CPU 使用率内存使用情况网络活动Appium 的性能优化需要综合考虑多个方面,通过合理的优化策略,可以显著提升测试效率和稳定性。
阅读 0·2月21日 16:20

Appium 常见问题如何排查?

Appium 的常见问题排查是测试人员必备的技能,能够快速定位和解决问题是保证测试顺利进行的关键。以下是 Appium 常见问题排查的详细说明:连接问题1. 无法连接到 Appium Server问题现象:Error: Could not connect to Appium server可能原因:Appium Server 未启动端口被占用防火墙阻止连接解决方案:// 检查 Appium Server 是否启动// 方法 1:使用命令行检查// appium -v// 方法 2:检查端口是否监听// lsof -i :4723 (macOS/Linux)// netstat -ano | findstr :4723 (Windows)// 启动 Appium Server// 方法 1:命令行启动// appium// 方法 2:指定端口启动// appium -p 4723// 方法 3:代码中启动const { spawn } = require('child_process');const appiumProcess = spawn('appium', ['-p', '4723']);// 连接到 Appium Serverconst capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk'};const driver = await new Builder() .withCapabilities(capabilities) .usingServer('http://localhost:4723/wd/hub') .build();2. 设备连接失败问题现象:Error: Could not connect to device可能原因:设备未连接USB 调试未开启驱动未安装解决方案:// 检查设备连接// 方法 1:使用 adb 检查// adb devices// 方法 2:检查设备状态const adb = require('adbkit');const client = adb.createClient();const devices = await client.listDevices();console.log('Connected devices:', devices);// 配置设备连接const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', udid: 'emulator-5554', // 指定设备 UDID app: '/path/to/app.apk'};// 如果是模拟器,确保模拟器已启动// 如果是真机,确保 USB 调试已开启元素定位问题1. 找不到元素问题现象:Error: No such element可能原因:定位策略不正确元素尚未加载元素在另一个上下文中解决方案:// 方法 1:使用显式等待const element = await driver.wait( until.elementLocated(By.id('submit_button')), 10000);// 方法 2:检查元素是否存在async function isElementPresent(driver, locator) { try { await driver.findElement(locator); return true; } catch (error) { return false; }}const isPresent = await isElementPresent(driver, By.id('submit_button'));console.log('Element present:', isPresent);// 方法 3:检查上下文const contexts = await driver.getContexts();console.log('Available contexts:', contexts);// 如果元素在 WebView 中,切换上下文if (contexts.includes('WEBVIEW_com.example.app')) { await driver.context('WEBVIEW_com.example.app');}// 方法 4:使用 Appium Inspector 检查元素// 打开 Appium Inspector// 连接到设备// 检查元素属性和定位策略2. 定位到多个元素问题现象:Error: Multiple elements found可能原因:定位策略匹配多个元素需要更精确的定位解决方案:// 方法 1:使用 findElements 查找所有匹配元素const elements = await driver.findElements(By.className('android.widget.Button'));console.log('Found elements:', elements.length);// 方法 2:使用更精确的定位策略const element = await driver.findElement( By.xpath('//android.widget.Button[@text="Submit" and @index="0"]'));// 方法 3:使用索引定位const elements = await driver.findElements(By.className('android.widget.Button'));const element = elements[0];// 方法 4:使用相对定位const container = await driver.findElement(By.id('form_container'));const element = await container.findElement(By.className('android.widget.Button'));应用启动问题1. 应用安装失败问题现象:Error: Failed to install app可能原因:应用文件路径不正确应用文件损坏设备存储空间不足解决方案:// 方法 1:检查应用文件路径const fs = require('fs');const appPath = '/path/to/app.apk';if (fs.existsSync(appPath)) { console.log('App file exists');} else { console.error('App file not found');}// 方法 2:检查应用文件大小const stats = fs.statSync(appPath);console.log('App file size:', stats.size);// 方法 3:使用绝对路径const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/absolute/path/to/app.apk'};// 方法 4:先手动安装应用// adb install /path/to/app.apk// 然后使用 appPackage 和 appActivityconst capabilities = { platformName: 'Android', deviceName: 'Pixel 5', appPackage: 'com.example.app', appActivity: '.MainActivity'};2. 应用启动失败问题现象:Error: Failed to launch app可能原因:appPackage 或 appActivity 不正确应用权限不足应用崩溃解决方案:// 方法 1:检查 appPackage 和 appActivity// 使用 adb dumpsys 查看应用信息// adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'// 方法 2:使用正确的 appPackage 和 appActivityconst capabilities = { platformName: 'Android', deviceName: 'Pixel 5', appPackage: 'com.example.app', appActivity: '.MainActivity', appWaitPackage: 'com.example.app', appWaitActivity: '.MainActivity'};// 方法 3:授予应用权限const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', appPackage: 'com.example.app', appActivity: '.MainActivity', autoGrantPermissions: true};// 方法 4:检查应用日志// adb logcat | grep com.example.app// 方法 5:使用 noReset 避免重置应用状态const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', appPackage: 'com.example.app', appActivity: '.MainActivity', noReset: true};手势操作问题1. 点击操作失败问题现象:Error: Element not clickable at point可能原因:元素被其他元素遮挡元素不可见元素不可点击解决方案:// 方法 1:等待元素可点击const element = await driver.findElement(By.id('submit_button'));await driver.wait( until.elementIsClickable(element), 5000);await element.click();// 方法 2:滚动到元素await driver.executeScript('arguments[0].scrollIntoView(true);', element);await element.click();// 方法 3:使用 JavaScript 点击await driver.executeScript('arguments[0].click();', element);// 方法 4:使用坐标点击const rect = await element.getRect();const x = rect.x + rect.width / 2;const y = rect.y + rect.height / 2;await driver.touchActions([ { action: 'tap', x: x, y: y }]);2. 滑动操作失败问题现象:Error: Swipe failed可能原因:坐标超出屏幕范围滑动距离过短滑动速度过快解决方案:// 方法 1:使用相对坐标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:使用 TouchActionconst TouchAction = require('wd').TouchAction;const action = new TouchAction(driver);action.press({ x: startX, y: startY }) .wait(500) .moveTo({ x: startX, y: endY }) .release();await action.perform();// 方法 3:使用 scrollTo 方法await driver.execute('mobile: scroll', { direction: 'down', element: element.ELEMENT});// 方法 4:使用 swipe 方法await driver.execute('mobile: swipe', { startX: startX, startY: startY, endX: startX, endY: endY, duration: 1000});性能问题1. 测试执行速度慢问题现象:测试执行时间过长元素定位缓慢可能原因:使用了复杂的定位策略等待时间过长网络延迟解决方案:// 方法 1:使用高效的定位策略// ❌ 不推荐:使用复杂的 XPathconst element = await driver.findElement( By.xpath('//android.widget.Button[@text="Submit" and @index="0"]'));// ✅ 推荐:使用 IDconst element = await driver.findElement(By.id('submit_button'));// 方法 2:减少等待时间// ❌ 不推荐:使用隐式等待await driver.manage().timeouts().implicitlyWait(10000);// ✅ 推荐:使用显式等待const element = await driver.wait( until.elementLocated(By.id('submit_button')), 5000);// 方法 3:缓存元素引用const button = await driver.findElement(By.id('submit_button'));await button.click();await button.sendKeys('text');// 方法 4:使用本地服务器const driver = await new Builder() .withCapabilities(capabilities) .usingServer('http://localhost:4723/wd/hub') .build();2. 内存占用过高问题现象:测试进程内存占用持续增长测试运行一段时间后变慢可能原因:未释放资源会话未关闭元素引用未清理解决方案:// 方法 1:及时释放资源describe('Test Suite', () => { let driver; before(async () => { driver = await new Builder().withCapabilities(capabilities).build(); }); after(async () => { if (driver) { await driver.quit(); } }); it('Test 1', async () => { // 执行测试 });});// 方法 2:清理元素引用let element;try { element = await driver.findElement(By.id('submit_button')); await element.click();} finally { element = null;}// 方法 3:使用 noReset 避免重复安装应用const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', appPackage: 'com.example.app', appActivity: '.MainActivity', noReset: true};调试技巧1. 启用详细日志// 配置详细日志const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk', // 启用详细日志 showXcodeLog: true, debugLogSpacing: true};// 查看 Appium Server 日志// appium --log-level debug// 查看设备日志// adb logcat2. 使用 Appium InspectorAppium Inspector 是强大的调试工具:查看应用 UI 结构获取元素属性测试元素定位策略录制和回放操作3. 使用断点调试// 在代码中设置断点const element = await driver.findElement(By.id('submit_button'));debugger; // 断点await element.click();最佳实践预防问题:使用稳定的定位策略合理配置等待机制及时释放资源快速定位问题:启用详细日志使用 Appium Inspector检查设备连接状态系统化排查:从简单到复杂逐一验证假设记录问题和解决方案Appium 的常见问题排查需要经验和技巧,通过不断实践和总结,可以快速定位和解决问题,提高测试效率。
阅读 0·2月21日 16:20

什么是 Appium,它有哪些核心特性?

Appium 是一个开源的、跨平台的移动应用自动化测试框架,它遵循 WebDriver 协议,允许测试人员使用标准的 WebDriver API 来自动化移动应用。Appium 的核心优势在于其跨平台特性和对多种编程语言的支持。Appium 的核心特性跨平台支持:支持 iOS、Android 和 Windows 平台使用统一的 API 接口无需为不同平台学习不同的工具多语言支持:Java、Python、JavaScript (Node.js)Ruby、C#、PHP 等测试人员可以使用熟悉的语言编写测试开源免费:完全开源,社区活跃免费使用,无商业限制持续更新和改进WebDriver 协议:遵循 W3C WebDriver 标准与 Selenium 兼容标准化的 API 接口Appium 的架构Appium 采用客户端-服务器架构:Appium Server:接收来自客户端的命令将命令转换为特定平台的操作与移动设备或模拟器通信Appium Client:各种语言的客户端库提供语言特定的 API封装 WebDriver 协议自动化引擎:iOS:使用 XCUITest(iOS 9.3+)或 UIAutomation(iOS 9.2-)Android:使用 UiAutomator2(Android 5.0+)或 UiAutomator(Android 4.2-)Windows:使用 WinAppDriverAppium 的工作原理会话创建:客户端发送创建会话请求服务器根据 desired capabilities 配置启动应用建立与设备的连接命令执行:客户端发送 WebDriver 命令服务器将命令转换为平台特定的操作自动化引擎执行操作并返回结果元素定位:支持多种定位策略ID、XPath、CSS Selector、Accessibility ID 等跨平台统一的定位方式Appium 的优势无需重新编译应用:可以直接测试原生应用无需修改应用代码支持测试商店应用支持混合应用:可以自动化 WebView在原生和 WebView 上下文间切换支持 Cordova、Ionic、React Native 等支持移动 Web:可以自动化移动浏览器支持 Safari、Chrome 等类似于 Selenium 的测试方式丰富的生态系统:大量的插件和工具活跃的社区支持丰富的文档和教程Appium 的应用场景原生应用测试:iOS 和 Android 原生应用功能测试、回归测试兼容性测试混合应用测试:WebView 混合应用跨平台框架应用复杂交互测试移动 Web 测试:移动浏览器应用响应式设计测试跨浏览器测试持续集成:与 CI/CD 工具集成自动化测试流程云测试平台集成Appium 与其他工具的对比Appium vs CalabashAppium:跨平台、WebDriver 标准、多语言支持Calabash:Ruby 为主、Cucumber 集成、学习曲线陡峭Appium vs EspressoAppium:跨平台、无需修改应用、黑盒测试Espresso:Android 专用、白盒测试、性能更好Appium vs XCUITestAppium:跨平台、WebDriver 标准、多语言支持XCUITest:iOS 专用、性能更好、Swift/Objective-CAppium 的版本Appium 1.x:基于 JSON Wire ProtocolAppium 2.0:基于 W3C WebDriver 标准更好的标准化和兼容性改进的性能和稳定性Appium 作为移动应用自动化测试的首选工具,为测试人员提供了强大而灵活的测试能力,通过合理使用 Appium,可以构建高效、稳定的自动化测试体系。
阅读 0·2月21日 16:20

Appium 与 Selenium 有什么区别?

Appium 与 Selenium 是两个不同的自动化测试工具,虽然它们都基于 WebDriver 协议,但在应用场景、架构设计和功能特性上存在显著差异。以下是 Appium 与 Selenium 的详细对比:基本概念SeleniumSelenium 是一个用于 Web 应用程序自动化测试的工具集,主要用于:浏览器自动化测试Web 应用功能测试跨浏览器测试AppiumAppium 是一个用于移动应用程序自动化测试的工具,主要用于:移动应用自动化测试原生应用、混合应用和移动 Web 测试跨平台移动测试主要区别1. 应用场景// Selenium - Web 浏览器测试const { Builder, By, until } = require('selenium-webdriver');const driver = await new Builder() .forBrowser('chrome') .build();await driver.get('https://example.com');const element = await driver.findElement(By.id('submit_button'));await element.click();// Appium - 移动应用测试const { Builder, By, until } = require('selenium-webdriver');const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk'};const driver = await new Builder() .withCapabilities(capabilities) .build();const element = await driver.findElement(By.id('submit_button'));await element.click();区别:Selenium:专注于 Web 浏览器自动化Appium:专注于移动应用自动化2. 支持的平台| 特性 | Selenium | Appium ||------|----------|--------|| Web 浏览器 | ✅ 支持 | ✅ 支持(移动 Web) || Android 原生应用 | ❌ 不支持 | ✅ 支持 || iOS 原生应用 | ❌ 不支持 | ✅ 支持 || Windows 桌面应用 | ❌ 不支持 | ✅ 支持 || 混合应用 | ❌ 不支持 | ✅ 支持 |3. 架构设计Selenium 架构:Test Script → Selenium WebDriver → Browser Driver → BrowserAppium 架构:Test Script → Appium Client → Appium Server → Automation Engine → Mobile Device区别:Selenium:直接与浏览器驱动通信Appium:通过 Appium Server 与设备通信4. 自动化引擎Selenium:使用浏览器内置的自动化引擎每个浏览器有特定的驱动(ChromeDriver, GeckoDriver 等)直接与浏览器 API 交互Appium:使用平台特定的自动化引擎Android:UiAutomator2, EspressoiOS:XCUITestWindows:WinAppDriver5. 元素定位策略Selenium:// Selenium 支持的定位策略By.id('element_id')By.className('element_class')By.tagName('button')By.cssSelector('#submit-button')By.xpath('//button[@id="submit"]')By.name('element_name')By.linkText('Submit')By.partialLinkText('Sub')Appium:// Appium 支持的定位策略(包含 Selenium 的所有策略)By.id('element_id')By.className('element_class')By.xpath('//android.widget.Button[@text="Submit"]')By.accessibilityId('submit_button')By.androidUIAutomator('new UiSelector().text("Submit")')By.iOSNsPredicateString('name == "Submit"')By.iOSClassChain('**/XCUIElementTypeButton[`name == "Submit"`]')区别:Appium 继承了 Selenium 的所有定位策略Appium 增加了移动应用特有的定位策略6. 手势操作Selenium:// Selenium 手势操作有限await element.click();await element.sendKeys('text');await element.clear();Appium:// Appium 支持丰富的手势操作await element.click();await element.sendKeys('text');await element.clear();// 触摸操作await driver.touchActions([ { action: 'press', x: 100, y: 200 }, { action: 'moveTo', x: 100, y: 100 }, { action: 'release' }]);// 多点触控const actions = driver.actions({ async: true });await actions.move({ origin: element1 }).press() .move({ origin: element2 }).press() .pause(100) .move({ origin: element1 }).release() .move({ origin: element2 }).release() .perform();区别:Selenium:手势操作有限Appium:支持丰富的手势和多点触控7. 上下文切换Selenium:// Selenium 不需要上下文切换// 直接操作浏览器元素const element = await driver.findElement(By.id('submit_button'));await element.click();Appium:// Appium 需要处理上下文切换// 获取所有上下文const contexts = await driver.getContexts();console.log('Available contexts:', contexts);// ['NATIVE_APP', 'WEBVIEW_com.example.app']// 切换到 WebViewawait driver.context('WEBVIEW_com.example.app');// 操作 WebView 元素const element = await driver.findElement(By.id('submit_button'));await element.click();// 切换回原生应用await driver.context('NATIVE_APP');区别:Selenium:不需要上下文切换Appium:需要在原生应用和 WebView 之间切换8. 设备能力Selenium:// Selenium 设备能力有限const capabilities = { browserName: 'chrome', platformName: 'Windows', version: 'latest'};Appium:// Appium 支持丰富的设备能力const capabilities = { platformName: 'Android', platformVersion: '11.0', deviceName: 'Pixel 5', udid: 'emulator-5554', app: '/path/to/app.apk', appPackage: 'com.example.app', appActivity: '.MainActivity', autoGrantPermissions: true, noReset: true, fullReset: false, automationName: 'UiAutomator2', language: 'zh-CN', locale: 'zh_CN'};区别:Selenium:设备能力有限Appium:支持丰富的设备配置9. 测试框架集成Selenium:// Selenium 与测试框架集成const { describe, it, before, after } = require('mocha');const { Builder, By, until } = require('selenium-webdriver');describe('Web Application Test', () => { let driver; before(async () => { driver = await new Builder().forBrowser('chrome').build(); }); it('should submit form', async () => { await driver.get('https://example.com'); const element = await driver.findElement(By.id('submit_button')); await element.click(); }); after(async () => { await driver.quit(); });});Appium:// Appium 与测试框架集成const { describe, it, before, after } = require('mocha');const { Builder, By, until } = require('selenium-webdriver');describe('Mobile Application Test', () => { let driver; before(async () => { const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk' }; driver = await new Builder().withCapabilities(capabilities).build(); }); it('should submit form', async () => { const element = await driver.findElement(By.id('submit_button')); await element.click(); }); after(async () => { await driver.quit(); });});区别:两者都可以与测试框架集成Appium 需要配置移动设备能力10. 性能考虑Selenium:运行在浏览器中性能主要取决于浏览器和网络相对稳定和可预测Appium:运行在移动设备上性能取决于设备性能和网络受设备状态和系统资源影响选择建议使用 Selenium 的场景Web 应用测试:测试 Web 应用程序跨浏览器测试响应式设计测试回归测试:Web 应用回归测试持续集成测试性能测试:Web 应用性能测试页面加载时间测试使用 Appium 的场景移动应用测试:原生应用测试混合应用测试移动 Web 测试跨平台测试:Android 和 iOS 应用测试跨设备兼容性测试功能测试:移动应用功能测试用户体验测试总结| 特性 | Selenium | Appium ||------|----------|--------|| 主要用途 | Web 应用测试 | 移动应用测试 || 支持平台 | Web 浏览器 | Android, iOS, Windows || 架构 | 直接与浏览器驱动通信 | 通过 Appium Server 与设备通信 || 自动化引擎 | 浏览器内置引擎 | 平台特定引擎 || 元素定位 | Web 元素定位 | 移动应用元素定位 || 手势操作 | 有限 | 丰富 || 上下文切换 | 不需要 | 需要 || 设备能力 | 有限 | 丰富 || 学习曲线 | 相对简单 | 相对复杂 |Selenium 和 Appium 都是强大的自动化测试工具,选择哪个取决于你的测试需求。如果需要测试 Web 应用,选择 Selenium;如果需要测试移动应用,选择 Appium。
阅读 0·2月21日 16:20

Appium 的工作原理是什么?

Appium 的工作原理基于客户端-服务器架构和 WebDriver 协议,通过自动化引擎与移动设备进行交互。以下是 Appium 工作原理的详细说明:架构组件1. Appium ServerAppium Server 是核心组件,负责:接收来自客户端的 HTTP 请求解析 WebDriver 命令将命令转换为平台特定的操作与移动设备或模拟器通信返回执行结果给客户端2. Appium ClientAppium Client 是各种语言的客户端库:提供语言特定的 API封装 HTTP 请求简化测试代码编写支持多种编程语言3. 自动化引擎不同平台使用不同的自动化引擎:iOS:XCUITest(iOS 9.3+)、UIAutomation(iOS 9.2-)Android:UiAutomator2(Android 5.0+)、UiAutomator(Android 4.2-)Windows:WinAppDriver工作流程1. 会话创建// 客户端代码const { Builder } = require('selenium-webdriver');const capabilities = { platformName: 'Android', deviceName: 'emulator-5554', app: '/path/to/app.apk', automationName: 'UiAutomator2'};const driver = await new Builder() .withCapabilities(capabilities) .usingServer('http://localhost:4723/wd/hub') .build();步骤:客户端发送 POST /session 请求服务器解析 desired capabilities根据平台选择合适的自动化引擎启动应用并建立会话返回会话 ID 给客户端2. 元素定位// 通过 ID 定位const element = await driver.findElement(By.id('com.example.app:id/button'));// 通过 XPath 定位const element = await driver.findElement(By.xpath('//android.widget.Button[@text="Submit"]'));// 通过 Accessibility ID 定位const element = await driver.findElement(By.accessibilityId('submit-button'));定位过程:客户端发送元素定位请求服务器将定位策略转换为平台特定的查询自动化引擎在设备上执行查询返回匹配的元素3. 元素操作// 点击元素await element.click();// 输入文本await element.sendKeys('Hello World');// 获取属性const text = await element.getText();操作过程:客户端发送操作命令服务器将命令转换为平台特定的操作自动化引擎在设备上执行操作返回操作结果WebDriver 协议Appium 遵循 W3C WebDriver 标准:HTTP 端点POST /session # 创建新会话DELETE /session/:id # 删除会话GET /session/:id/element # 查找元素POST /session/:id/element/:id/click # 点击元素JSON Wire Protocol请求和响应都使用 JSON 格式:// 请求{ "desiredCapabilities": { "platformName": "Android", "deviceName": "emulator-5554" }}// 响应{ "value": { "element-6066-11e4-a52e-4f735466cecf": "0.123456789" }, "status": 0}平台特定实现Android 实现const capabilities = { platformName: 'Android', automationName: 'UiAutomator2', appPackage: 'com.example.app', appActivity: '.MainActivity', deviceName: 'Android Emulator', platformVersion: '11.0'};工作原理:Appium Server 启动 UiAutomator2 服务器在设备上安装测试 APK通过 ADB 与设备通信使用 UiAutomator2 API 执行操作iOS 实现const capabilities = { platformName: 'iOS', automationName: 'XCUITest', bundleId: 'com.example.app', deviceName: 'iPhone 14', platformVersion: '16.0', udid: 'auto'};工作原理:Appium Server 使用 XCUITest 框架通过 WebDriverAgent 与设备通信使用 XCUITest API 执行操作支持真机和模拟器混合应用处理上下文切换// 获取所有上下文const contexts = await driver.getContexts();console.log(contexts); // ['NATIVE_APP', 'WEBVIEW_com.example.app']// 切换到 WebViewawait driver.context('WEBVIEW_com.example.app');// 在 WebView 中操作const element = await driver.findElement(By.css('#submit-button'));await element.click();// 切换回原生上下文await driver.context('NATIVE_APP');处理流程:检测应用中的 WebView获取所有可用的上下文切换到 WebView 上下文使用 WebDriver API 操作 WebView切换回原生上下文Desired CapabilitiesDesired Capabilities 是配置会话的关键参数:const capabilities = { // 平台相关 platformName: 'Android', platformVersion: '11.0', deviceName: 'Pixel 5', // 应用相关 app: '/path/to/app.apk', appPackage: 'com.example.app', appActivity: '.MainActivity', bundleId: 'com.example.app', // 自动化相关 automationName: 'UiAutomator2', noReset: true, fullReset: false, // 其他配置 newCommandTimeout: 60, autoGrantPermissions: true};通信机制HTTP 通信Appium 使用 HTTP 协议进行通信:Client (HTTP Request) → Appium Server → Automation Engine → DeviceClient (HTTP Response) ← Appium Server ← Automation Engine ← DeviceWebSocket 通信Appium 2.0 支持 WebSocket,提供更好的性能:const { Builder } = require('selenium-webdriver');const driver = await new Builder() .usingServer('ws://localhost:4723') .withCapabilities(capabilities) .build();最佳实践合理使用 Desired Capabilities:只配置必要的参数使用默认值减少配置根据平台调整配置优化元素定位:优先使用稳定的定位策略避免使用脆弱的 XPath使用 Accessibility ID 提高可维护性处理异步操作:使用显式等待避免硬编码等待时间处理加载状态错误处理:捕获和处理异常提供清晰的错误信息实现重试机制Appium 的工作原理通过标准化的 WebDriver 协议和平台特定的自动化引擎,为移动应用自动化测试提供了强大而灵活的解决方案。
阅读 0·2月21日 16:19

Appium 的等待机制有哪些?

Appium 的等待机制是处理异步操作和动态加载的关键功能,确保测试脚本的稳定性和可靠性。以下是 Appium 等待机制的详细说明:等待类型Appium 提供了三种主要的等待机制:1. 隐式等待(Implicit Wait)设置全局等待时间,在查找元素时自动应用:// 设置隐式等待await driver.manage().timeouts().implicitlyWait(10000); // 10秒// 查找元素时会自动等待const element = await driver.findElement(By.id('submit_button'));特点:全局生效,影响所有元素查找设置一次,持续有效可能导致不必要的等待2. 显式等待(Explicit Wait)针对特定条件进行等待:const { until } = require('selenium-webdriver');// 等待元素出现const element = await driver.wait( until.elementLocated(By.id('submit_button')), 10000);// 等待元素可见await driver.wait( until.elementIsVisible(element), 5000);// 等待元素可点击await driver.wait( until.elementIsClickable(element), 5000);特点:针对特定条件更精确的等待推荐使用3. 流畅等待(Fluent Wait)提供更灵活的等待方式:// 使用流畅等待const element = await driver.wait( async () => { const el = await driver.findElement(By.id('submit_button')); if (el) { return el; } return false; }, 10000, 'Element not found');常用等待条件1. 元素存在// 等待元素存在于 DOM 中const element = await driver.wait( until.elementLocated(By.id('submit_button')), 10000);2. 元素可见// 等待元素可见const element = await driver.findElement(By.id('submit_button'));await driver.wait( until.elementIsVisible(element), 5000);3. 元素可点击// 等待元素可点击const element = await driver.findElement(By.id('submit_button'));await driver.wait( until.elementIsClickable(element), 5000);4. 元素包含文本// 等待元素包含特定文本await driver.wait( until.elementTextContains(element, 'Submit'), 5000);5. 元素属性包含值// 等待元素属性包含特定值await driver.wait( until.elementAttributeContains(element, 'class', 'active'), 5000);6. 标题包含文本// 等待页面标题包含特定文本await driver.wait( until.titleContains('Dashboard'), 5000);自定义等待条件1. 基本自定义等待// 自定义等待条件async function waitForElementToBeEnabled(driver, locator, timeout = 10000) { const startTime = Date.now(); while (Date.now() - startTime < timeout) { try { const element = await driver.findElement(locator); const isEnabled = await element.isEnabled(); if (isEnabled) { return element; } } catch (error) { // 元素未找到,继续等待 } await driver.sleep(500); // 等待 500ms } throw new Error(`Element not enabled within ${timeout}ms`);}// 使用自定义等待const element = await waitForElementToBeEnabled( driver, By.id('submit_button'), 10000);2. 复杂自定义等待// 等待多个元素async function waitForMultipleElements(driver, locators, timeout = 10000) { const startTime = Date.now(); const elements = {}; while (Date.now() - startTime < timeout) { let allFound = true; for (const [name, locator] of Object.entries(locators)) { if (!elements[name]) { try { elements[name] = await driver.findElement(locator); } catch (error) { allFound = false; } } } if (allFound) { return elements; } await driver.sleep(500); } throw new Error('Not all elements found within timeout');}// 使用自定义等待const elements = await waitForMultipleElements(driver, { submitButton: By.id('submit_button'), cancelButton: By.id('cancel_button')});等待最佳实践1. 优先使用显式等待// ✅ 推荐:使用显式等待const element = await driver.wait( until.elementLocated(By.id('submit_button')), 10000);// ❌ 不推荐:使用硬编码等待await driver.sleep(10000);const element = await driver.findElement(By.id('submit_button'));2. 合理设置超时时间// 根据网络和设备性能调整超时const timeout = process.env.SLOW_NETWORK ? 20000 : 10000;const element = await driver.wait( until.elementLocated(By.id('submit_button')), timeout);3. 提供清晰的错误信息// 自定义错误信息const element = await driver.wait( until.elementLocated(By.id('submit_button')), 10000, 'Submit button not found within 10 seconds');4. 组合多个等待条件// 等待元素可见且可点击const element = await driver.findElement(By.id('submit_button'));await driver.wait( until.elementIsVisible(element), 5000);await driver.wait( until.elementIsClickable(element), 5000);等待常见问题1. 等待超时原因:超时时间设置过短元素定位策略不正确元素在另一个上下文中解决方案:// 增加超时时间const element = await driver.wait( until.elementLocated(By.id('submit_button')), 20000);// 检查上下文const contexts = await driver.getContexts();console.log('Available contexts:', contexts);// 切换上下文await driver.context('WEBVIEW_com.example.app');2. 不必要的等待原因:使用了隐式等待硬编码了等待时间解决方案:// 避免使用隐式等待// await driver.manage().timeouts().implicitlyWait(10000);// 使用显式等待const element = await driver.wait( until.elementLocated(By.id('submit_button')), 10000);3. 等待条件不明确原因:等待条件不够具体没有验证元素状态解决方案:// ❌ 不够具体const element = await driver.wait( until.elementLocated(By.id('submit_button')), 10000);// ✅ 更具体const element = await driver.findElement(By.id('submit_button'));await driver.wait( until.elementIsVisible(element), 5000);await driver.wait( until.elementIsClickable(element), 5000);等待性能优化1. 减少等待时间// 使用更精确的等待条件const element = await driver.wait( until.elementIsVisible(await driver.findElement(By.id('submit_button'))), 5000);2. 并行等待// 并行等待多个元素const [element1, element2] = await Promise.all([ driver.wait(until.elementLocated(By.id('button1')), 5000), driver.wait(until.elementLocated(By.id('button2')), 5000)]);3. 使用轮询间隔// 设置轮询间隔const element = await driver.wait( until.elementLocated(By.id('submit_button')), 10000, 'Element not found', 500 // 轮询间隔 500ms);最佳实践优先使用显式等待:更精确的等待更好的性能更清晰的错误信息合理设置超时:根据实际情况调整避免过短或过长考虑网络和设备性能避免硬编码等待:不使用 sleep()使用条件等待提高测试稳定性处理等待超时:提供清晰的错误信息实现重试机制记录超时原因Appium 的等待机制为测试人员提供了强大的异步操作处理能力,通过合理使用各种等待策略,可以构建稳定、可靠的自动化测试。
阅读 0·2月21日 16:19

Appium 如何与测试框架集成?

Appium 的测试框架集成是构建完整自动化测试体系的关键环节,支持与多种测试框架和工具链集成。以下是 Appium 测试框架集成的详细说明:支持的测试框架1. MochaMocha 是一个流行的 JavaScript 测试框架:const { describe, it, before, after, beforeEach, afterEach } = require('mocha');const { Builder, By, until } = require('selenium-webdriver');const assert = require('assert');describe('Appium Test with Mocha', () => { let driver; before(async () => { const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk' }; driver = await new Builder().withCapabilities(capabilities).build(); }); after(async () => { await driver.quit(); }); beforeEach(async () => { // 每个测试前的准备工作 }); afterEach(async () => { // 每个测试后的清理工作 }); it('should submit form successfully', async () => { const element = await driver.findElement(By.id('submit_button')); await element.click(); const result = await driver.findElement(By.id('result_message')); const text = await result.getText(); assert.strictEqual(text, 'Success'); }); it('should display error message', async () => { const element = await driver.findElement(By.id('submit_button')); await element.click(); const result = await driver.findElement(By.id('error_message')); const text = await result.getText(); assert.strictEqual(text, 'Error'); });});2. JestJest 是 Facebook 开发的 JavaScript 测试框架:const { Builder, By, until } = require('selenium-webdriver');describe('Appium Test with Jest', () => { let driver; beforeAll(async () => { const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk' }; driver = await new Builder().withCapabilities(capabilities).build(); }); afterAll(async () => { await driver.quit(); }); test('should submit form successfully', async () => { const element = await driver.findElement(By.id('submit_button')); await element.click(); const result = await driver.findElement(By.id('result_message')); const text = await result.getText(); expect(text).toBe('Success'); });});3. JasmineJasmine 是一个行为驱动开发(BDD)测试框架:const { Builder, By, until } = require('selenium-webdriver');describe('Appium Test with Jasmine', () => { let driver; beforeAll(async () => { const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk' }; driver = await new Builder().withCapabilities(capabilities).build(); }); afterAll(async () => { await driver.quit(); }); it('should submit form successfully', async () => { const element = await driver.findElement(By.id('submit_button')); await element.click(); const result = await driver.findElement(By.id('result_message')); const text = await result.getText(); expect(text).toBe('Success'); });});4. TestNG (Java)TestNG 是一个流行的 Java 测试框架:import org.testng.annotations.*;import org.openqa.selenium.*;import org.openqa.selenium.remote.DesiredCapabilities;import io.appium.java_client.AppiumDriver;import io.appium.java_client.MobileElement;public class AppiumTestWithTestNG { private AppiumDriver<MobileElement> driver; @BeforeClass public void setUp() throws Exception { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("deviceName", "Pixel 5"); capabilities.setCapability("app", "/path/to/app.apk"); driver = new AppiumDriver<>(new URL("http://localhost:4723/wd/hub"), capabilities); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } @Test public void testSubmitForm() throws Exception { MobileElement element = driver.findElement(By.id("submit_button")); element.click(); MobileElement result = driver.findElement(By.id("result_message")); String text = result.getText(); assertEquals(text, "Success"); }}5. PyTest (Python)PyTest 是一个流行的 Python 测试框架:import pytestfrom appium import webdriverfrom selenium.webdriver.common.by import By@pytest.fixturedef driver(): capabilities = { 'platformName': 'Android', 'deviceName': 'Pixel 5', 'app': '/path/to/app.apk' } driver = webdriver.Remote('http://localhost:4723/wd/hub', capabilities) yield driver driver.quit()def test_submit_form(driver): element = driver.find_element(By.ID, 'submit_button') element.click() result = driver.find_element(By.ID, 'result_message') text = result.text assert text == 'Success'持续集成集成1. Jenkinspipeline { agent any stages { stage('Install Dependencies') { steps { sh 'npm install' } } stage('Run Appium Tests') { steps { sh 'npm run test:appium' } } stage('Generate Reports') { steps { sh 'npm run test:report' } } } post { always { junit 'test-results/**/*.xml' publishHTML([ allowMissing: false, alwaysLinkToLastBuild: true, keepAll: true, reportDir: 'test-results/html', reportFiles: 'index.html', reportName: 'Appium Test Report' ]) } }}2. GitHub Actionsname: Appium Testson: push: branches: [ main ] pull_request: branches: [ main ]jobs: test: runs-on: macos-latest steps: - uses: actions/checkout@v2 - name: Set up Node.js uses: actions/setup-node@v2 with: node-version: '16' - name: Install dependencies run: npm install - name: Start Appium Server run: npx appium & - name: Run Appium tests run: npm run test:appium - name: Upload test results uses: actions/upload-artifact@v2 if: always() with: name: test-results path: test-results/3. GitLab CIstages: - testappium_tests: stage: test image: node:16 before_script: - npm install script: - npx appium & - npm run test:appium artifacts: when: always paths: - test-results/ reports: junit: test-results/**/*.xml测试报告1. Allure Reportconst { Builder, By, until } = require('selenium-webdriver');const allure = require('allure-commandline');describe('Appium Test with Allure', () => { let driver; before(async () => { const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk' }; driver = await new Builder().withCapabilities(capabilities).build(); }); after(async () => { await driver.quit(); }); it('should submit form successfully', async () => { allure.step('Click submit button', async () => { const element = await driver.findElement(By.id('submit_button')); await element.click(); }); allure.step('Verify result', async () => { const result = await driver.findElement(By.id('result_message')); const text = await result.getText(); assert.strictEqual(text, 'Success'); }); });});2. Mochawesomeconst { Builder, By, until } = require('selenium-webdriver');describe('Appium Test with Mochawesome', () => { let driver; before(async () => { const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk' }; driver = await new Builder().withCapabilities(capabilities).build(); }); after(async () => { await driver.quit(); }); it('should submit form successfully', async () => { const element = await driver.findElement(By.id('submit_button')); await element.click(); const result = await driver.findElement(By.id('result_message')); const text = await result.getText(); assert.strictEqual(text, 'Success'); });});数据驱动测试1. 使用 JSON 数据const { Builder, By, until } = require('selenium-webdriver');const testData = require('./test-data.json');describe('Data-Driven Tests', () => { let driver; before(async () => { const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk' }; driver = await new Builder().withCapabilities(capabilities).build(); }); after(async () => { await driver.quit(); }); testData.forEach((data, index) => { it(`Test case ${index + 1}: ${data.description}`, async () => { const input = await driver.findElement(By.id('input_field')); await input.sendKeys(data.input); const button = await driver.findElement(By.id('submit_button')); await button.click(); const result = await driver.findElement(By.id('result_message')); const text = await result.getText(); assert.strictEqual(text, data.expected); }); });});2. 使用 Excel 数据const { Builder, By, until } = require('selenium-webdriver');const xlsx = require('xlsx');describe('Data-Driven Tests with Excel', () => { let driver; let testData; before(async () => { const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk' }; driver = await new Builder().withCapabilities(capabilities).build(); // 读取 Excel 数据 const workbook = xlsx.readFile('./test-data.xlsx'); const sheet = workbook.Sheets['Sheet1']; testData = xlsx.utils.sheet_to_json(sheet); }); after(async () => { await driver.quit(); }); testData.forEach((data, index) => { it(`Test case ${index + 1}: ${data.description}`, async () => { const input = await driver.findElement(By.id('input_field')); await input.sendKeys(data.input); const button = await driver.findElement(By.id('submit_button')); await button.click(); const result = await driver.findElement(By.id('result_message')); const text = await result.getText(); assert.strictEqual(text, data.expected); }); });});并行测试1. 使用 Mocha 并行测试const { Builder, By, until } = require('selenium-webdriver');describe('Parallel Tests', () => { it('Test 1', async () => { const capabilities = { platformName: 'Android', deviceName: 'Pixel 5', app: '/path/to/app.apk' }; const driver = await new Builder().withCapabilities(capabilities).build(); try { const element = await driver.findElement(By.id('submit_button')); await element.click(); } finally { await driver.quit(); } }); it('Test 2', async () => { const capabilities = { platformName: 'Android', deviceName: 'Pixel 6', app: '/path/to/app.apk' }; const driver = await new Builder().withCapabilities(capabilities).build(); try { const element = await driver.findElement(By.id('submit_button')); await element.click(); } finally { await driver.quit(); } });});2. 使用 TestNG 并行测试import org.testng.annotations.*;import org.openqa.selenium.*;import org.openqa.selenium.remote.DesiredCapabilities;import io.appium.java_client.AppiumDriver;import io.appium.java_client.MobileElement;@Test(threadPoolSize = 3, invocationCount = 3)public class ParallelAppiumTests { @Test(dataProvider = "devices") public void testOnDevice(String deviceName) throws Exception { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("deviceName", deviceName); capabilities.setCapability("app", "/path/to/app.apk"); AppiumDriver<MobileElement> driver = new AppiumDriver<>( new URL("http://localhost:4723/wd/hub"), capabilities ); try { MobileElement element = driver.findElement(By.id("submit_button")); element.click(); } finally { driver.quit(); } } @DataProvider(name = "devices") public Object[][] getDevices() { return new Object[][] { {"Pixel 5"}, {"Pixel 6"}, {"Pixel 7"} }; }}最佳实践选择合适的测试框架:根据团队技术栈选择考虑框架的生态系统评估学习成本配置持续集成:自动化测试执行生成测试报告及时反馈测试结果实现并行测试:提高测试效率缩短测试时间充分利用资源使用数据驱动:提高测试覆盖率简化测试维护支持多场景测试生成详细报告:记录测试结果分析测试趋势改进测试质量Appium 的测试框架集成为测试人员提供了灵活的测试解决方案,通过合理配置和优化,可以构建高效、稳定的自动化测试体系。
阅读 0·2月21日 16:19

Appium 如何进行手势操作?

Appium 的手势操作是模拟用户交互的重要功能,支持各种触摸和手势操作。以下是 Appium 手势操作的详细说明:基本手势操作1. 点击(Tap)// 单击await element.click();// 点击坐标await driver.touchActions([ { action: 'tap', x: 100, y: 200 }]);// 多次点击await element.click();await element.click();await element.click();2. 长按(Long Press)// 长按元素const actions = driver.actions({ async: true });await actions.move({ origin: element }).press().pause(2000).release().perform();// 长按坐标await driver.touchActions([ { action: 'press', x: 100, y: 200 }, { action: 'wait', ms: 2000 }, { action: 'release' }]);3. 双击(Double Tap)// 双击元素const actions = driver.actions({ async: true });await actions.move({ origin: element }).doubleClick().perform();// 使用 TouchActionconst touchAction = new TouchAction(driver);touchAction.tap({ x: 100, y: 200 }).tap({ x: 100, y: 200 });await touchAction.perform();4. 滑动(Swipe)// 滑动元素await driver.touchActions([ { action: 'press', x: 100, y: 500 }, { action: 'moveTo', x: 100, y: 100 }, { action: 'release' }]);// 使用 TouchActionconst touchAction = new TouchAction(driver);touchAction.press({ x: 100, y: 500 }).moveTo({ x: 100, y: 100 }).release();await touchAction.perform();高级手势操作1. 滚动(Scroll)// 滚动到元素await element.sendKeys('Hello');// 滚动页面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)// 拖拽元素const actions = driver.actions({ async: true });await actions.dragAndDrop(sourceElement, targetElement).perform();// 使用 TouchActionconst touchAction = new TouchAction(driver);touchAction.press({ el: sourceElement }) .moveTo({ el: targetElement }) .release();await touchAction.perform();3. 缩放(Pinch)// 缩放操作const actions = driver.actions({ async: true });await actions .move({ origin: element }) .press() .move({ origin: element, x: 50, y: 0 }) .release() .perform();4. 旋转(Rotate)// 旋转操作const actions = driver.actions({ async: true });await actions .move({ origin: element }) .press() .move({ origin: element, x: 0, y: 50 }) .release() .perform();多点触控1. 多点触控(Multi-touch)// 多点触控操作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)// 捏合操作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();手势操作最佳实践1. 使用显式等待// 等待元素可交互await driver.wait( until.elementIsClickable(element), 5000);// 执行手势操作await element.click();2. 处理动画// 等待动画完成await driver.sleep(500);// 执行手势操作await element.click();3. 验证操作结果// 执行手势操作await element.click();// 验证结果const result = await driver.findElement(By.id('result_message'));const text = await result.getText();assert.strictEqual(text, 'Success');手势操作优化1. 减少手势操作// ❌ 不推荐:多次点击await element.click();await element.click();await element.click();// ✅ 推荐:使用双击const actions = driver.actions({ async: true });await actions.move({ origin: element }).doubleClick().perform();2. 使用相对坐标// 使用元素相对坐标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. 处理不同屏幕尺寸// 获取屏幕尺寸const size = await driver.manage().window().getRect();// 计算相对坐标const x = size.width * 0.5;const y = size.height * 0.5;await driver.touchActions([ { action: 'press', x: x, y: y }, { action: 'release' }]);手势操作常见问题1. 手势操作失败原因:元素不可见或不可点击手势操作被其他元素阻挡动画未完成解决方案:// 等待元素可点击await driver.wait( until.elementIsClickable(element), 5000);// 滚动到元素await driver.executeScript('arguments[0].scrollIntoView(true);', element);// 执行手势操作await element.click();2. 手势操作不准确原因:坐标计算错误屏幕尺寸变化元素位置变化解决方案:// 使用元素定位而非坐标await element.click();// 动态计算坐标const rect = await element.getRect();const x = rect.x + rect.width / 2;const y = rect.y + rect.height / 2;3. 多点触控不支持原因:设备不支持多点触控Appium 版本不支持解决方案:// 检查多点触控支持const capabilities = await driver.getCapabilities();const supportsMultiTouch = capabilities.supportsMultiTouch;if (!supportsMultiTouch) { console.warn('Multi-touch not supported');}手势操作工具1. Appium InspectorAppium Inspector 提供手势操作录制功能:录制手势操作生成代码测试手势操作2. 自定义手势库// 创建自定义手势库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(); }}// 使用自定义手势库const gestures = new GestureHelper(driver);await gestures.swipe(100, 500, 100, 100);await gestures.longPress(element, 2000);最佳实践优先使用元素操作:使用 element.click() 而非坐标点击更稳定和可维护适应屏幕尺寸变化合理使用等待:等待元素可交互处理动画和加载避免硬编码等待验证操作结果:检查操作后的状态验证预期结果提供清晰的错误信息处理异常情况:捕获手势操作异常实现重试机制记录失败原因Appium 的手势操作为测试人员提供了强大的用户交互模拟能力,通过合理使用各种手势操作,可以构建真实、可靠的自动化测试。
阅读 0·2月21日 16:19