Пример #1
0
        private static void _onAllTestRunComplete(TestResponseDTO testResponseDTO)
        {
            try
            {
                if (testResponseDTO.CommandsExecuted.Any(i => i.CommandStatus == CommandResponseStatus.Failed) || testResponseDTO.ResponseStatus != ResponseStatus.Success)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("All Tests completed: FAILED : " + testResponseDTO.TestDetailDTO.Name + " : run number :" + testResponseDTO.TestRunNumber + "." + testResponseDTO.ResponseStatus.ToString() + ":" + testResponseDTO.ResponseMessage);
                    Console.WriteLine("ErrorStack:" + testResponseDTO.ResponseDetail);
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("All Tests completed: SUCCESS : " + testResponseDTO.TestDetailDTO.Name + " : run number :" + testResponseDTO.TestRunNumber + "." + testResponseDTO.ResponseStatus.ToString() + ":" + testResponseDTO.ResponseMessage);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("All Tests completed system error:" + ex.Message);
            }


            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Finished :" + DateTime.Now);
            Console.WriteLine("--------------------------------------------------------------------------------");
            Console.WriteLine("Press any key to exit");

            //end
            Console.ReadLine();
        }
        public void AddTestRunResults(TestResponseDTO testResponseDTO)
        {
            if (EnablePortablDB)
            {
                using (var db = new LiteDatabase(this.DBFullPath))
                {
                    var dateNow      = DateTime.Now;
                    var headerEntity = db.GetCollection <TestHeaderEntity>("TestHeader");

                    // Create your new customer instance
                    var testHeaderEntity = new TestHeaderEntity
                    {
                        TestUniqueCode            = testResponseDTO.TestDetailDTO.TestUniqueCode,
                        AddedDate                 = dateNow,
                        TestId                    = testResponseDTO.TestDetailDTO.TestId,
                        Name                      = testResponseDTO.TestDetailDTO.Name,
                        Author                    = testResponseDTO.TestDetailDTO.Author,
                        Description               = testResponseDTO.TestDetailDTO.Description,
                        TestIncludeToRunFirst     = testResponseDTO.TestDetailDTO.TestIncludeToRunFirst,
                        SimulateNetworkCondition  = JsonConvert.SerializeObject(testResponseDTO.TestDetailDTO.SimulateNetworkCondition),
                        TestStartTime             = testResponseDTO.TestDetailDTO.TimeTaken.StartTime,
                        TestEndTime               = testResponseDTO.TestDetailDTO.TimeTaken.EndTime,
                        IsTestPassed              = !testResponseDTO.IsTestFailed,
                        ResponseMessage           = testResponseDTO.ResponseMessage,
                        OutputResultsFullFilePath = testResponseDTO.TestDetailDTO.OutputFullFilePath,
                        NumberOfIterations        = testResponseDTO.TestDetailDTO.NumberOfIterations,
                    };
                    headerEntity.Insert(testHeaderEntity);

                    //insert commands
                    var commandEntity = db.GetCollection <CommandDTO>("TestCommands");

                    foreach (var item in testResponseDTO.CommandsExecuted)
                    {
                        bool isFailed         = item.CommandStatus != CommandResponseStatus.Success;
                        var  commandEntityRec = item;
                        commandEntityRec.TestId            = testResponseDTO.TestDetailDTO.TestId;
                        commandEntityRec.TestUniqueCode    = testResponseDTO.TestDetailDTO.TestUniqueCode;
                        commandEntityRec.AddedDate         = dateNow;
                        commandEntityRec.DurationStartTime = commandEntityRec.TimeTaken.StartTime;
                        commandEntityRec.DurationEndTime   = commandEntityRec.TimeTaken.EndTime;
                        commandEntityRec.IsFailed          = isFailed;
                        commandEntityRec.ResponseMessage   = item.Message;
                        commandEntity.Insert(commandEntityRec);
                    }
                }
            }
        }
Пример #3
0
        private CommandDTO _executeLoadURL(CommandDTO cmd, ref TestResponseDTO testResponseDTO)
        {
            var cmdExec = cmd;

            cmdExec.CommandStatus = CommandResponseStatus.Success;
            cmdExec.Message       = "Success";
            try
            {
                cmdExec.ScreenShot = _takeScreenShot(cmdExec);
                WebDriver.Navigate().GoToUrl(cmd.Value);
            }
            catch (Exception ex)
            {
                cmdExec = _getErrorCmd(cmdExec, ex);
            }

            testResponseDTO.CommandsExecuted.Add(cmdExec);
            return(cmdExec);
        }
Пример #4
0
        private CommandDTO _executeExecuteWaitSecond(CommandDTO cmd, ref TestResponseDTO testResponseDTO)
        {
            var cmdExec = cmd;

            cmdExec.CommandStatus = CommandResponseStatus.Success;
            cmdExec.Message       = "Success";
            try
            {
                //WebDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(Convert.ToInt32(cmd.Value));
                Thread.Sleep(new TimeSpan(0, 0, Convert.ToInt32(cmd.Value)));
                cmdExec.ScreenShot = _takeScreenShot(cmdExec);
            }
            catch (Exception ex)
            {
                cmdExec = _getErrorCmd(cmdExec, ex);
            }

            testResponseDTO.CommandsExecuted.Add(cmdExec);
            return(cmdExec);
        }
Пример #5
0
        private CommandDTO _executeExecuteJS(CommandDTO cmd, ref TestResponseDTO testResponseDTO)
        {
            var cmdExec = cmd;

            cmdExec.CommandStatus = CommandResponseStatus.Success;
            cmdExec.Message       = "Success";
            try
            {
                IJavaScriptExecutor js = (IJavaScriptExecutor)WebDriver;
                js.ExecuteScript(cmd.Value);
                cmdExec.ScreenShot = _takeScreenShot(cmdExec);
            }
            catch (Exception ex)
            {
                cmdExec = _getErrorCmd(cmdExec, ex);
            }

            testResponseDTO.CommandsExecuted.Add(cmdExec);
            return(cmdExec);
        }
Пример #6
0
        private CommandDTO _executeClickFromList(CommandDTO cmd, ref TestResponseDTO testResponseDTO)
        {
            var cmdExec = cmd;

            cmdExec.CommandStatus = CommandResponseStatus.Success;
            cmdExec.Message       = "Success";
            try
            {
                var elemList = _findElements(cmd);
                cmdExec.ScreenShot = _takeScreenShot(cmdExec);
                elemList[cmd.IndexToClick].Click();
            }
            catch (Exception ex)
            {
                cmdExec = _getErrorCmd(cmdExec, ex);
            }

            testResponseDTO.CommandsExecuted.Add(cmdExec);
            return(cmdExec);
        }
Пример #7
0
        private CommandDTO _executeTypeText(CommandDTO cmd, ref TestResponseDTO testResponseDTO)
        {
            var cmdExec = cmd;

            cmdExec.CommandStatus = CommandResponseStatus.Success;
            cmdExec.Message       = "Success";
            try
            {
                var elem = _findElement(cmd);
                elem.Clear();
                elem.SendKeys(cmd.Value);
                cmdExec.ScreenShot = _takeScreenShot(cmdExec);
            }
            catch (Exception ex)
            {
                cmdExec = _getErrorCmd(cmdExec, ex);
            }

            testResponseDTO.CommandsExecuted.Add(cmdExec);
            return(cmdExec);
        }
Пример #8
0
        private CommandDTO _executeExecuteSelectDropDown(CommandDTO cmd, ref TestResponseDTO testResponseDTO)
        {
            var cmdExec = cmd;

            cmdExec.CommandStatus = CommandResponseStatus.Success;
            cmdExec.Message       = "Success";
            try
            {
                var elem = _findElement(cmd);

                var selectElement = new SelectElement(elem);
                selectElement.SelectByValue(cmd.Value);

                cmdExec.ScreenShot = _takeScreenShot(cmdExec);
            }
            catch (Exception ex)
            {
                cmdExec = _getErrorCmd(cmdExec, ex);
            }

            testResponseDTO.CommandsExecuted.Add(cmdExec);
            return(cmdExec);
        }
Пример #9
0
        private CommandDTO _executeAssertTextExists(CommandDTO cmd, ref TestResponseDTO testResponseDTO)
        {
            var cmdExec = cmd;

            cmdExec.CommandStatus = CommandResponseStatus.Success;
            cmdExec.Message       = "Success";
            try
            {
                cmdExec.ScreenShot = _takeScreenShot(cmdExec);
                IWebElement body = WebDriver.FindElement(By.TagName("body"));
                if (!body.Text.Contains(cmd.Value))
                {
                    cmdExec.CommandStatus = CommandResponseStatus.Failed;
                    cmdExec.Message       = "Text(" + cmd.Value + ") does not exist on the page!";
                }
            }
            catch (Exception ex)
            {
                cmdExec = _getErrorCmd(cmdExec, ex);
            }

            testResponseDTO.CommandsExecuted.Add(cmdExec);
            return(cmdExec);
        }
Пример #10
0
        private CommandDTO _executeClickAnchorTagHref(CommandDTO cmd, ref TestResponseDTO testResponseDTO)
        {
            var cmdExec = cmd;

            cmdExec.CommandStatus = CommandResponseStatus.Success;
            cmdExec.Message       = "Success";
            try
            {
                var elem = _findElement(cmd);
                if (elem.GetAttribute("href") != null)
                {
                    var linkHref = elem.GetAttribute("href");
                    cmdExec.ScreenShot = _takeScreenShot(cmdExec);
                    WebDriver.Navigate().GoToUrl(linkHref);
                }
            }
            catch (Exception ex)
            {
                cmdExec = _getErrorCmd(cmdExec, ex);
            }

            testResponseDTO.CommandsExecuted.Add(cmdExec);
            return(cmdExec);
        }
Пример #11
0
        public TestResponseDTO ExecuteCommands(List <TestObjectDTO> testObjectDTO, OnTestRunComplete onTestRunComplete,
                                               OnTestCommandComplete onTestCommandComplete, OnAllTestRunComplete onAllTestRunComplete)
        {
            TestResponseDTO returnValue = new TestResponseDTO();

            returnValue.CommandsExecuted = new List <CommandDTO>();
            int currentTestRunNumber = 1;


            try
            {
                //init the browser object
                foreach (var testObj in testObjectDTO)
                {
                    returnValue.TestDetailDTO = new TestDetailDTO();
                    returnValue.TestDetailDTO = testObj.TestDetail;

                    //impersonate windows user
                    if (returnValue.TestDetailDTO.ImpersonateUser.Apply == true)
                    {
                        _impersonateUser.Impersonate(returnValue.TestDetailDTO.ImpersonateUser.UserName, returnValue.TestDetailDTO.ImpersonateUser.Password);
                    }

                    if (testObj.TestDetail.TestIncludeToRunFirst != _lastTestIncludeRan)
                    {
                        if (!string.IsNullOrEmpty(testObj.TestDetail.TestIncludeToRunFirst))
                        {
                            if (File.Exists(testObj.TestDetail.TestIncludeToRunFirst))
                            {
                                _lastTestIncludeRan = testObj.TestDetail.TestIncludeToRunFirst;
                                var testInclObjectDTO = _parseTestFile(testObj.TestDetail.TestIncludeToRunFirst);
                                List <TestObjectDTO> testInclObjectDTOList = new List <TestObjectDTO>();
                                testInclObjectDTOList.Add(testInclObjectDTO);
                                _isTestIncludeRunning = true;
                                ExecuteCommands(testInclObjectDTOList, onTestRunComplete, onTestCommandComplete, onAllTestRunComplete);
                            }
                        }
                    }



                    int totalIterations = testObj.TestDetail.NumberOfIterations;
                    if (testObj.TestDetail.ExcelDocument.Data.Count > 0)
                    {
                        totalIterations = testObj.TestDetail.ExcelDocument.Data.Count;
                    }

                    if (totalIterations <= 0)
                    {
                        testObj.TestDetail.NumberOfIterations = 1;
                    }

                    testObj.TestDetail.NumberOfIterations = totalIterations;
                    int rowIndex = 0;
                    for (int i = 1; i <= testObj.TestDetail.NumberOfIterations; i++)
                    {
                        List <CommandDTO> listCurrentCommands = new List <CommandDTO>();
                        currentTestRunNumber = i;

                        if (!_isTestIncludeRunning)
                        {
                            if (WebDriver == null)
                            {
                                _setWebDriver(testObj.TestDetail);
                            }
                        }
                        else
                        {
                            if (WebDriver == null)
                            {
                                _setWebDriver(testObj.TestDetail);
                            }
                        }


                        //start video recording
                        testObj.TestDetail.RecordVideo = _recordVideo(testObj.TestDetail, currentTestRunNumber);


                        //now execute the command for each test object
                        CommandDTO currentCommandExecuted = new CommandDTO();
                        foreach (var cmd in testObj.Commands.Where(c => c.Execute == true).ToList())
                        {
                            cmd.Value = _applyRandomToValue(cmd.Value, cmd.AppendRandomToValue);
                            if (cmd.ExcelColIndexValue > -1)
                            {
                                cmd.Value = _getExcelColValue(cmd.Value, rowIndex, cmd.ExcelColIndexValue, testObj.TestDetail.ExcelDocument);
                            }
                            cmd.Value = _getDateNowValue(cmd.Value, cmd.DateNowValue);

                            switch (cmd.CommandType)
                            {
                            case CommandType.Click:
                                currentCommandExecuted    = _executeClick(cmd, ref returnValue);
                                returnValue.TestRunNumber = currentTestRunNumber;

                                break;

                            case CommandType.ClickAnchorTagHref:
                                currentCommandExecuted    = _executeClickAnchorTagHref(cmd, ref returnValue);
                                returnValue.TestRunNumber = currentTestRunNumber;
                                break;

                            case CommandType.ClickFromList:
                                currentCommandExecuted    = _executeClickFromList(cmd, ref returnValue);
                                returnValue.TestRunNumber = currentTestRunNumber;
                                break;

                            case CommandType.LoadURL:
                                currentCommandExecuted    = _executeLoadURL(cmd, ref returnValue);
                                returnValue.TestRunNumber = currentTestRunNumber;
                                break;

                            case CommandType.TypeText:
                                currentCommandExecuted    = _executeTypeText(cmd, ref returnValue);
                                returnValue.TestRunNumber = currentTestRunNumber;
                                break;

                            case CommandType.AssertTextExists:
                                currentCommandExecuted    = _executeAssertTextExists(cmd, ref returnValue);
                                returnValue.TestRunNumber = currentTestRunNumber;
                                break;

                            case CommandType.ExecuteJS:
                                currentCommandExecuted    = _executeExecuteJS(cmd, ref returnValue);
                                returnValue.TestRunNumber = currentTestRunNumber;
                                break;

                            case CommandType.WaitSecond:
                                currentCommandExecuted    = _executeExecuteWaitSecond(cmd, ref returnValue);
                                returnValue.TestRunNumber = currentTestRunNumber;
                                break;

                            case CommandType.SelectDropDown:
                                currentCommandExecuted    = _executeExecuteSelectDropDown(cmd, ref returnValue);
                                returnValue.TestRunNumber = currentTestRunNumber;
                                break;
                            }

                            onTestCommandComplete(currentCommandExecuted);
                            listCurrentCommands.Add(currentCommandExecuted);

                            if (currentCommandExecuted.CommandStatus != CommandResponseStatus.Success)
                            {
                                if (testObj.TestDetail.RecordVideo.Record == true)
                                {
                                    _screenRecorderHelper.Dispose();
                                }

                                returnValue.ResponseMessage = currentCommandExecuted.Message;
                                _createOutput(testObj, currentTestRunNumber, listCurrentCommands);
                                throw new Exception(returnValue.ResponseMessage);
                            }
                        }

                        //stop video recording
                        if (testObj.TestDetail.RecordVideo.Record == true)
                        {
                            _screenRecorderHelper.Dispose();
                        }


                        onTestRunComplete(returnValue);
                        _createOutput(testObj, currentTestRunNumber, listCurrentCommands);
                        rowIndex++;

                        if (!_isTestIncludeRunning)
                        {
                            CleanUp();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                returnValue.ResponseDetail  = ex.StackTrace;
                returnValue.ResponseMessage = ex.Message;
                returnValue.ResponseStatus  = ResponseStatus.SystemError;
                returnValue.TestRunNumber   = currentTestRunNumber;
                CleanUp();
            }


            if (returnValue.ResponseStatus != ResponseStatus.Success)
            {
                var data = returnValue.CommandsExecuted.FirstOrDefault(i => i.CommandStatus == CommandResponseStatus.Failed);
                if (data != null)
                {
                    returnValue.ResponseDetail = "Cmd failed:" + data.CommandType.ToString() + "(" + data.IDToClick + ")" + "." + returnValue.ResponseDetail;
                }
            }



            if (_isTestIncludeRunning)
            {
                _isTestIncludeRunning = false;
                if (returnValue.ResponseStatus == ResponseStatus.Success)
                {
                    ExecuteCommands(this.TestObjectDTO, onTestRunComplete,
                                    onTestCommandComplete, onAllTestRunComplete);
                }
            }
            else
            {
                onAllTestRunComplete(returnValue);
            }


            return(returnValue);
        }
Пример #12
0
 private static void _onTestRunComplete(TestResponseDTO testResponseDTO)
 {
     Console.WriteLine("Test completed:" + testResponseDTO.TestDetailDTO.Name + " : run number :" + testResponseDTO.TestRunNumber + "." + testResponseDTO.ResponseStatus.ToString() + "");
 }
Пример #13
0
        /// <summary>
        /// socket read byte array.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="message"></param>
        public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            var packet = message as PacketRequest <IMessage>;

            if (packet == null)
            {
                return;
            }

            PacketResponse <IMessage> packetResponse = null;

            try
            {
                switch (packet.CommandId)
                {
                //获取雇员信息
                case 1001:
                    //解析
                    var requestDTO = packet.Body as TestRequestDTO;

                    //回复
                    var response = new PacketResponse <TestResponseDTO>();
                    response.CommandId = 1001;
                    var errorResponse = new TestResponseDTO {
                        Code = 400, Message = "Illegal message parsing failed!!!"
                    };

                    if (requestDTO == null)
                    {
                        response.Body = errorResponse;
                    }
                    else
                    {
                        var hashVal = requestDTO.HashValue;
                        //var uid = requestDTO.Account;
                        //var pwd = requestDTO.Passworld;
                        var nonce          = requestDTO.Nonce;
                        var sign           = requestDTO.Sign;
                        var time           = requestDTO.ValidateTime;
                        var isVerification = EncryptionUtil.Verification(hashVal, nonce, sign, time);
                        if (isVerification)
                        {
                            response.Body = new TestResponseDTO {
                                Code = 200, Employees = new List <string> {
                                    "zhang san", "li si", "wang wu", "zhao liu"
                                }
                            };
                        }
                        else
                        {
                            response.Body = errorResponse;
                        }
                        if (!isVerification)
                        {
                            context.WriteAndFlushAsync(packetResponse);
                            context.CloseAsync();
                        }
                    }
                    context.WriteAndFlushAsync(packetResponse);


                    break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Server request read error { ex.Message }");
            }
        }