如何在自定义属性中使用面向方面的编程概念,以避免在福彩12选5走势图中编写try catch

帕沙

知名会员
已加入
2020年4月29日
留言内容
64
编程经验
1-3
您好团队,

在测试中,我想介绍异常处理,如果发生异常,则将错误记录在扩展报告中-为此,我在考虑是否可以使用面向方面的概念来使用属性,但是我面临的唯一问题是如何我如何访问记录器,这在其他页面中有提及



记录器实现:
public class Scenario1 : ManualRegressionSuite.Model.Abstractions.Test.TestBaseMSTestV2.TestBase
    {
        public CommonMethods CommonMethods { get; set; }
        public string countryName = "Austria";

        [TestInitialize]
        public void BeforeTest()
        {
            BaseSetup();
            CommonMethods = new CommonMethods(shared_parameters, _logger);
        }

        [TestCleanup]
        public void AfterTest()
        {
            BaseCleanUp();
        }
        [TestCategory("BOL")]
        [TestMethod]
        public void BOL_GB_3D_Product_EndToEnd_Scenario()
        {
        try{
            LineSeparatorHelper lineSeparator = GetInstance<LineSeparatorHelper>();
            lineSeparator.LineFormatter("Executing 3D Product End to End Scenario for BOL");

            _logger.Info("Reviewing Basket After updating delivery location!!");}
            catch{}
        
        }

public abstract class TestBase : Base
    {
        private ExtentReports _report;

        public List<IWebDriver> Drivers;
        public TestContext TestContext { get; set; }
        protected MasterPaths Paths { get => paths; set => paths = value; }

        private MasterPaths paths;
        private void SetupReport()
        {
            _report = ReportSingleton.GetInstance();
            _logger = _report.CreateTest(TestContext.TestName);
        }

        public IWebDriver SetupDriver(string userAgent, SiteBag siteBag,
            TestConfigurationCDO testConfiguration, bool noLoad = false)
    

public abstract class Base
    {
        public  分享 dParameters shared_parameters;

        public IWebDriver Driver { get; set; }
        public ExtentTest _logger;

        protected TPage GetInstance<TPage, TArgs>(TArgs arg, IWebDriver Driver = default)
            where TPage : ConstructablePage<TArgs>, new()
        {
            TPage pageInstance = new TPage();

            if (Driver == default)
                pageInstance.Driver = this.Driver;
            else
                pageInstance.Driver = Driver;

            pageInstance.shared_parameters = shared_parameters;
            pageInstance._logger = _logger;

            pageInstance.Construct(arg);

            return pageInstance;
        }

现在尝试使用AOP概念进行后期处理


Custom Attribute:
namespace Framework.Attributes
{
    /// <summary>
    /// This attribute depicts that in method static wait is used as no other alternative was found
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
    public class HandleException : OnMethodBoundaryAspect
    {
        public HandleException(AventStack.ExtentReports.ExtentTest _logger )
        {

        }
        public override void CompileTimeInitialize(System.Reflection.MethodBase method, AspectInfo aspectInfo)
        {
            base.CompileTimeInitialize(method, aspectInfo);
        }
        public override void OnException(MethodExecutionArgs args)
        {
            if (args.Exception != null) { }
                //System..Info($"OnException : {(!string.IsNullOrEmpty(args.Exception.Message) ? args.Exception.Message : "")}");


            //var Message = args.Exception.Message;
            //var StackTrace = args.Exception.StackTrace;

            //_logger.Info($"Application has got exception in method-{args.Method.Name} and message is {Message}");
        }
    }
}

现在我不确定如何在HandleException属性中使用_logger,有人可以帮忙吗?
 

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,497
地点
弗吉尼亚州切萨皮克
编程经验
10+
好吧,您的属性的构造函数中包含以下代码行:
C#:
public HandleException(AventStack.ExtentReports.ExtentTest _logger )

我假设您是这样写的,因为您需要将记录器传递到属性中。那么,为什么不在构造期间仅获取记录器的引用,而在以后实际需要记录某些内容时使用它。

快速帮助:
1) The normal naming convention is for public properties and fields to be in Pascal case. _logger is in camel case.
2) A lot of people use the underscore prefix as a form of Hungarian notation to indicate that the field is private or protected. You are going against this common convention by declaring _logger as a public field.
3)您在将AOP应用于单元测试以捕获异常时感到不对劲。单元测试的要点是,如果条件失败或引发异常,则是单元测试引擎捕获失败或异常并进行报告。您为什么要做单元测试引擎的工作?
 

帕沙

知名会员
已加入
2020年4月29日
留言内容
64
编程经验
1-3
好吧,您的属性的构造函数中包含以下代码行:
C#:
public HandleException(AventStack.ExtentReports.ExtentTest _logger )

我假设您是这样写的,因为您需要将记录器传递到属性中。那么,为什么不在构造期间仅获取记录器的引用,而在以后实际需要记录某些内容时使用它。

快速帮助:
1) The normal naming convention is for public properties and fields to be in Pascal case. _logger is in camel case.
2) A lot of people use the underscore prefix as a form of Hungarian notation to indicate that the field is private or protected. You are going against this common convention by declaring _logger as a public field.
3)您在将AOP应用于单元测试以捕获异常时感到不对劲。单元测试的要点是,如果条件失败或引发异常,则是单元测试引擎捕获失败或异常并进行报告。您为什么要做单元测试引擎的工作?
_logger是在基类中声明的,在场景类中是Base._logger的声明,因此我不确定如何将_logger传递给Handle Exception
 
最佳 底部