使用重试模式来提升自动化测试的稳定性

文章介绍了在自动化测试中增强稳定性的几种重试模式,包括动态重试、轮询重试、异常重试和指数退避。动态重试基于最大重试次数,轮询重试结合超时和轮询,异常重试针对不同异常定制策略,指数退避则通过递增的等待时间减少系统压力。这些方法有助于处理不可预见的异常,提高测试的可靠性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看到一篇非常好的关于在自动化测试中引入重试模式的文章,https://www.thegreenreport.blog/articles/enhancing-automation-reliability-with-retry-patterns/enhancing-automation-reliability-with-retry-patterns.html 忍不住跟大家分享一下。

自动化测试,特别是ui自动化的稳定性提升是非常困难的,出错了就重试这个简单的策略可以帮助我们处理一些难以预料的异常情景,从而提升测试的整体稳定性。

这篇文章的作者就分享了几种重试的设计模式,通俗易懂而且相当实用。

动态重试

async function dynamicRetry(actions, maxRetries) {
    let retryCount = 0;
                          
    async function retryAction() {
        try {
            return await actions();
        } catch (error) {
            if (retryCount < maxRetries) {
                retryCount++;
                return retryAction();
            } else {
                throw error;
            }
        }
    }
    return await retryAction();
}

               
it("Testing the dynamic retry pattern", async () => {
    await demoPage.navigateToDemoPageTextBoxes();
    await browser.pause(1000);
    await dynamicRetry(async () => {
        await demoPage.fillFullName("John Doe");
        await demoPage.fillEmail("test@gmail.com");
        await demoPage.fillCurrentAddress("test address");
        await demoPage.clickFakeButton();
    }, 3);
});

该模式的核心是设置一个最大的重试次数,每次重试过后次数加一,直到达到阈值。

轮询重试

另一种常见的重试模式是使用超时和轮询。这种方法会重复检查一组操作是否成功完成或者是否达到了超时时间。

               
async function pollRetry(actions, timeout, pollInterval) {
    const startTime = Date.now();
    let lastError;
                          
    while (Date.now() - startTime < timeout) {
        try {
            await actions();
            return;
        } catch (error) {
            lastError = error;
            await sleep(pollInterval);
        }
    }
                          
    throw lastError;
}
                          
function sleep(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
}

               
it("Testing the poll retry pattern", async () => {
    await demoPage.navigateToDemoPageTextBoxes();
    await browser.pause(1000);
    await pollRetry(
        async () => {
            await demoPage.fillFullName("John Doe");
            await demoPage.fillEmail("test@gmail.com");
            await demoPage.fillCurrentAddress("test address");
            await demoPage.clickFakeButton();
        },
        20000,
        5000
    );
});

这个模式的核心是设置一个总的超时时间,如果在这个时间段内发生了异常,那么就每隔一段时间重试一下。

selenium的wait unitl 接口就是这种重试模式。

异常重试

这是最常见的一种重试方式,好处是可以针对不同的异常执行不同的重试策略。

               
function errorHandlingRetry(actions) {
    return new Promise(async (resolve, reject) => {
        try {
            await actions();
            resolve();
        } catch (error) {
            if (error instanceof ElementNotFoundError) {
                // Handle the case when the element is not found
       console.error("Element not found:", error.elementSelector);
       await sleep(1000);
       await actions();
            } else if (error instanceof TimeoutError) {
                // Handle the case when a timeout occurs
       console.error("Timeout occurred:", error.message);
       await sleep(500);
       await actions();
            } else {
                // Handle other types of errors
    console.error("An error occurred:", error);
            }
            reject(error);
        }
    });
}

it("Testing the error handling retry pattern", async () => {
    await demoPage.navigateToDemoPageTextBoxes();
    await browser.pause(1000);
    await errorHandlingRetry(async () => {
        await demoPage.fillFullName("John Doe");
        await demoPage.fillEmail("test@gmail.com");
        await demoPage.fillCurrentAddress("test address");
        await demoPage.clickFakeButton();
    });
});
               

指数退避

指数退避是一种重试策略,其中每次尝试之间的间隔呈指数递增。这种方法有助于减轻拥塞并减少对系统的负载。以下是一个示例实现:

               
async function retryWithExponentialBackoff(actions, maxRetries, initialDelay) {
    let retryCount = 0;
    let delay = initialDelay;
                          
    while (retryCount < maxRetries) {
        try {
            await actions();
            return;
        } catch (error) {
            console.error("Error:", error);
        }
        await sleep(delay);
        delay *= 2;
        retryCount++;
    }
    throw new Error(
        "Retry limit exceeded: Actions did not complete successfully."
    );
}

               
it("Testing the retry with exponential backoff pattern", async () => {
    await demoPage.navigateToDemoPageTextBoxes();
    await browser.pause(1000);
    await retryWithExponentialBackoff(
        async () => {
            await demoPage.fillFullName("John Doe");
            await demoPage.fillEmail("test@gmail.com");
            await demoPage.fillCurrentAddress("test address");
            await demoPage.clickFakeButton();
        },
        4,
        1000
    );
});

其实这种方式也是设置一个最大的重试次数,不过因为增加了指数退让的关系,所以每次重试的间隔都会变长,避免短时间反复重试给被测系统造成巨大压力。

随机间隔重试

随机间隔为重试机制增加了随机因素,可以帮助避免执行中的同步问题和模式。其实就是每次重试等待的时间是是随机的。

               
async function retryWithRandomizedInterval(
    actions,
    maxRetries,
    minDelay,
    maxDelay
) {
    let retryCount = 0;
                          
    while (retryCount < maxRetries) {
        try {
            await actions();
            return;
        } catch (error) {
            console.error("Error:", error);
        }
        const delay = Math.floor(
            Math.random() * (maxDelay - minDelay + 1) + minDelay
        );
        await sleep(delay);
        retryCount++;
    }
    throw new Error(
        "Retry limit exceeded: Actions did not complete successfully."
    );
}

               
it("Testing the retry with a randomized interval pattern", async () => {
    await demoPage.navigateToDemoPageTextBoxes();
    await browser.pause(1000);
    await retryWithRandomizedInterval(
        async () => {
            await demoPage.fillFullName("John Doe");
            await demoPage.fillEmail("test@gmail.com");
            await demoPage.fillCurrentAddress("test address");
            await demoPage.clickFakeButton();
        },
        3,
        1000,
        3000
    );
});

总结

记住下面几种重试模式

  • 动态重试

  • 异常重试

  • 轮询重试

  • 指数退避

最后:下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取【保证100%免费】

软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值