自动化代码更健壮

阅读目录

多用WaitForXXXX
实现自己的WaitFor方法
若失败再重试机制,让你的代码更健壮
用重试次数来控制大的模块或整条Case的执行
总结

在做自动化测试时,尤其是UI级自动化,如何让你的代码更健壮可能是你经常要考虑的问题,这里分享几个小Tips。

多用WaitForXXXX
严格意义上讲,任何长时间的“硬等待”都是可取的!!到处随意的Sleep()只能显示你技能的匮乏,写出的Case也会效率低下。所以当此之时, 我们都应该多用WaitFor方法,而且任何的自动化框架都是有类似方法的,像Robotium的:

solo.waitForDialogToClose() //waits for the dialog to close
solo.waitForActivity() // if there is a activity change
solo.waitForText() //if a certain text appears after the loading is done
solo.waitForView() //if a certain view is shown after the load screen is don
e.

Ranorex也有很多这样的的功能来帮你等待特定元素出现.

但是如果从框架里找不到类似的方法来满足实际测试的场景,怎么办? OK, 那我们就只能自己来实现了.

实现自己的WaitFor方法
其实仔细想想,这样的方法归根到底还是需要sleep动作的,毕竟是要等待,只不过我们要做的是让它要更聪明的去等待. 比如下面:

 * Wait for the view with id to be shown in the timeout
 * 
 * @param id the id of the view
 * @param timeout the amount of time in milliseconds to wait
 * @return true that the view is shown, false otherwise
 */
public <T extends View> boolean waitForViewShown(final int id, final int timeout){
    final long endTime = SystemClock.uptimeMillis() + timeout;
    boolean foundMatchingView = false;
    View view;

    while (SystemClock.uptimeMillis() < endTime) {
        solo.sleep(Global.SLEEP_FLASH_TIME);

        view = solo.getView(id);
        if (view != null)
            foundMatchingView = view.isShown();

        if (foundMatchingView)
            return true;
    }
    return false;
}

我们可以设置个Timeout时间,这样在时间范围内,只要找到目标,就立马返回,如果没找到,那么再进行下一轮循环. 这样当要操作的对象确实出来之后,在进行下一步操作, 出错的可能就会少很多.

另外,出错少可不代表不出错,也许就会有一些莫名其妙的情况让你的代码执行失败. 虽然努力分析错误,找出最终导致Case失败的Root Cause, 毋庸置疑是非常重要的。但也许考虑到付出与得到的性价比,也许一个简单的失败再重试,会对你更有用。

若失败再重试机制,让你的代码更健壮

这里提到的失败重试机制不是指让整条Case的重新去执行一遍,而是当你发现某个一或几个动作,有可能因为一些特殊的场景导致失败的话,而这种特殊场景又不可控的话,那么我们就可以使用简单的几行代码,让风险更可控。比如下面这些方法:

    /// <summary>
    /// Open the bottom app bar
    /// </summary>
    public static void OpenBottomAppBar()
    {
        Report.Info("Running in OpenBottomAppBar method.");
        
        System.DateTime endtime = System.DateTime.Now.AddSeconds(30);
        while(System.DateTime.Compare(System.DateTime.Now, endtime) <= 0)
        {
            //Move cursor to center of HP Connected Drive view and right click to open filter menu
            Size point = repo.HPConnectedDriveView.Self.Element.Size;
            Mouse.MoveTo(point.Width/2 < 400?400:point.Width/2, 10);
            Delay.Seconds(1);
            Mouse.Click(System.Windows.Forms.MouseButtons.Right);

            if(Utility.FindElement(repo.HPConnectedDriveView.BottomAppBar.BottomAppBarInfo, 1000 * 2))
            {
                return;
            }
        }
        Report.Screenshot();
    }

同样是利用前面提到的WaitFor机制,使用场景却更广泛,这里会循环判断,到底这个BottomAppBar出来没,要是没有的话,我就再点以下,并且在30秒之内,直至它打开为止。

要注意的是,这里是用时间也就是Timeout来控制重试范围,不过它适用单步或者有限几步操作的容错控制,如果是大的模块(Module),或者整条Case的,也许直接用重试次数会更好,毕竟当上升到大的模块,我们的首要目标还是让Case能执行过去而不是考虑时间范围。

用重试次数来控制大的模块或整条Case的执行
比如下面:

    /// <summary>
    /// Create a snapfish account via snapfish website
    /// http://www.snapfish.com/snapfish/home
    /// http://us1.sfint1.qa.snapfish.com/snapfish/home
    /// </summary>
    /// <param name="stack"></param>
    /// <returns></returns>
    public static string CreateSFAccount(string stack)
    {
        int i =0;
        string result = null;
        while(i<2)
        {
            i++;
            OpenSnapfishSignUpPage(stack);
            Delay.Seconds(4);
            ClickSignUPLink();
            Delay.Seconds(1);                
            string emailPrefix = "asgqa.hpcd.SF";
            string emailPassword = "asg111111";
            string suffix = Utility.GetUniqueNumber();                
            string email = emailPrefix + suffix + Utility.MapStack(stack) +"@hp.com";
                         
            email = InputSFRegisteredAccountInformation(email, emailPassword, emailPassword);
            ClickSFSubmitButton();
            result =  email + "|" + emailPassword;
            if(Utility.FindElement(repo.Snapfish.SignOutInfo, 1000*60))
            {
                Validate.IsTrue(true, "Register Snapfish account successfully email=" + email + " password=" + emailPassword);
                return result;
            }
            else
            {
                Report.Warn("Failed to get the successful infor after registering the SF account, take screenshot for reference");
                Report.Screenshot();
                
                Report.Info("Click F5 to refresh the page");
                repo.Snapfish.Self.PressKeys("{F5}");
                if(Utility.FindElement(repo.Snapfish.SignOutInfo, 1000*30))
                {
                    Validate.IsTrue(true, "Register Snapfish account successfully email=" + email + " password=" + emailPassword);
                    return result;
                }
            }
        }
        
        Report.Warn("still Unable to create snapfish account after try twice, take screenshot for reference");
        Report.Screenshot();
        
        Report.Warn("Sometimes i found even we registerd successfully, Snapfish website page still didn't refreshed, so i will use the registered account to login HPCD to try it again");
        return result;
    }

这是例子目的就是要去一个网站,创建帐号,我的目的就是只要能创建好帐号就行,并不是测这个网站,所以这里的操作如果失败了,我就会重试,直至三次机会用完,那我相信即使在重试多次,效果也是一样的,所以还是直接返回吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值