public DesktopAppCommand(AppElementCollection app)
 {
     _app = app;
     _specialParameters = new[] { "-i" };
     Init();
     Executor = new DesktopAppCommandExecutor();
 }
예제 #2
0
        public static bool InitializeDesktop(AppElementCollection _app)
        {
            bool returnStatus = false;

            processes = Process.GetProcessesByName(_app.Name);
            if (processes.Length == 1)
            {
                DesktopProcess = processes[0];
            }
            else
            {
                DesktopProcess = new Process
                {
                    StartInfo =
                    {
                        FileName         = _app.Path,
                        WorkingDirectory = Path.GetDirectoryName(_app.Path),
                        UseShellExecute  = false
                    }
                };
                DesktopProcess.Start();



                returnStatus = true;
            }

            return(returnStatus);
        }
예제 #3
0
 public WebCommand(string name, string driverPath, AppElementCollection app)
 {
     _name              = name;
     _driverPath        = driverPath;
     _appElements       = app;
     _specialParameters = new[] { "-i", "-r" };
     Executor           = new WebCommandExecutor();
     Init();
 }
 public AndroidCommand(string name, AppElementCollection app)
 {
     Executor           = new AndroidCommandExecutor();
     _name              = name;
     _appElements       = app;
     _specialParameters = new[] { "-i", "-r" };
     //Init the application at first time.
     Init("FirstInit");
     _currentRegion = new List <string>();
     _currentRegion.Add("Master");
 }
예제 #5
0
        public void Execute(int delay = 1000, bool continueWhenError = true, bool sendEmail = true)
        {
            ICommand currentCommand = null;

            //create new temp image folder
            _tempFilePath = Path.Combine(_tempFilePath, DateTime.Now.ToString("yyyyMMddHHmmssfff"));
            if (!Directory.Exists(_tempFilePath))
            {
                Directory.CreateDirectory(_tempFilePath);
            }

            ReportProgress(TestingStatus.LoadingApplications, 0, 1, "Start Preparing environment", _name);
            //ReportProgress(TestingStatus.LoadingApplications, 0, 1, _name);
            //Load app config
            List <AppElementCollection> appElementCollection = Directory.GetFiles(_applicationConfigPath, "*.xml", SearchOption.AllDirectories).Select(AppElementCollection.LoadFromConfigFile).ToList();

            appElementCollection.ForEach(app => app.Regions.ForEach(region => region.Elements.ForEach(element => element.Signature = element.Signature.Replace("\\r", "\r").Replace("\\n", "\n"))));

            //Load app
            foreach (var t in TestEnvironment)
            {
                AppElementCollection appCollection = appElementCollection.FirstOrDefault(a => a.Name == t.Key);
                if (appCollection.Type.Equals("web", StringComparison.CurrentCultureIgnoreCase))
                {
                    currentCommand = new WebCommand(t.Value, _webDriversPath, appCollection);
                }
                else if (appCollection.Type.Equals("android", StringComparison.CurrentCultureIgnoreCase))
                {
                    currentCommand = new AndroidCommand("android", appCollection);
                }
                else if (appCollection.Type.Equals("ios", StringComparison.CurrentCultureIgnoreCase))
                {
                    //_command = new IOSCommand("ios", appCollection);
                }
                else
                {
                    //_command = new DesktopAppCommand(appCollection);
                }
                _command.Add(t.Key, currentCommand);
                CurrentBrowserType = t.Value;
            }
            //AppElementCollection appCollection = appElementCollection.FirstOrDefault(a => a.Name == Application);


            ReportProgress(TestingStatus.LoadingApplications, 0, 1, "End Preparing environment", _name);
            //ReportProgress(TestingStatus.LoadingApplications, 1, 1, string.Empty);

            #region Execute Test Cases
            //Execute test cases


            string regionName = string.Empty;
            for (int i = 0; i < TestCases.Count; i++)
            {
                string strCommand    = null;
                int    errorBmpCount = 0;
                using (StreamReader sr = new StreamReader(TestCases[i]))
                {
                    int j = 0;
                    try
                    {
                        while ((strCommand = sr.ReadLine()) != null)
                        {
                            j++;
                            //ingore the comments of the testcase
                            if (strCommand.StartsWith("--"))
                            {
                                continue;
                            }

                            if (strCommand.ToLower().StartsWith("SwitchToApp:".ToLower()))
                            {
                                string appName = strCommand.Substring(12).Trim();
                                if (_command.ContainsKey(appName))
                                {
                                    currentCommand     = _command[appName];
                                    CurrentApplication = appName;
                                    CurrentBrowserType = TestEnvironment[appName];
                                }
                                continue;
                            }
                            //if test case include inner testcase
                            if (strCommand.ToLower().StartsWith("innertestcase"))
                            {
                                List <string> parameters            = CommonHelper.SplitCommand(strCommand, " ");
                                string        innerTestcasefilename = Path.Combine(_testcaseConfigPath, parameters[1]);
                                string        innerCommand          = string.Empty;
                                using (StreamReader testcaseSr = new StreamReader(innerTestcasefilename))
                                {
                                    while ((innerCommand = testcaseSr.ReadLine()) != null)
                                    {
                                        //ingore the comments of the testcase
                                        if (innerCommand.StartsWith("--"))
                                        {
                                            continue;
                                        }
                                        //Escape character
                                        innerCommand = System.Text.RegularExpressions.Regex.Unescape(innerCommand);

                                        ReportProgress(TestingStatus.ExecutingInnerTestCase, i, TestCases.Count, innerCommand, TestCases[i] + "{" + parameters[1] + "}");
                                        currentCommand.Execute(innerCommand);
                                        Thread.Sleep(delay);
                                    }
                                }
                                continue;
                            }
                            //Escape character if not inner test case
                            else
                            {
                                strCommand = System.Text.RegularExpressions.Regex.Unescape(strCommand);
                            }
                            ReportProgress(TestingStatus.ExecutingTestCase, i, TestCases.Count, strCommand, TestCases[i]);
                            currentCommand.Execute(strCommand);
                            Thread.Sleep(delay);
                        }
                        ReportProgress(TestingStatus.TestCaseSuccess, i, TestCases.Count, string.Empty, TestCases[i]);
                    }
                    catch (Exception ex)
                    {
                        string errBmp = string.Empty;
                        if (!CurrentBrowserType.ToLower().Equals("ios") && !CurrentBrowserType.ToLower().Equals("android"))
                        {
                            errorBmpCount++;
                            OpenQA.Selenium.IWebDriver ExecutingDriver = currentCommand.Executor.Driver;
                            string oldBmpFile = string.Format("{0}\\{1}){2}({3})-old.jpg", _tempFilePath, i + 1, Path.GetFileNameWithoutExtension(TestCases[i]), errorBmpCount);
                            string bmpFile    = string.Format("{0}\\{1}){2}({3}).jpg", _tempFilePath, i + 1, Path.GetFileNameWithoutExtension(TestCases[i]), errorBmpCount);
                            ((ITakesScreenshot)ExecutingDriver).GetScreenshot().SaveAsFile(oldBmpFile, ScreenshotImageFormat.Jpeg);
                            errBmp = oldBmpFile;
                            Image             oldPic = Image.FromFile(errBmp);
                            EncoderParameters ep     = new EncoderParameters();
                            long qy;
                            try
                            {
                                string s = TestingDossier.ScreenShotQuality;
                                long.TryParse(s, out qy);
                            }
                            catch
                            {
                                qy = 100;
                            }
                            EncoderParameter eParam = new EncoderParameter(Encoder.Quality, qy);
                            ep.Param[0] = eParam;
                            ImageCodecInfo jpegIcIinfo = ImageCodecInfo.GetImageEncoders().First(e => e.FormatDescription.Equals("JPEG"));

                            Bitmap newPic = new Bitmap(oldPic, (int)(oldPic.Width * 0.5), (int)(oldPic.Height * 0.5));
                            newPic.Save(bmpFile, ImageFormat.Jpeg);
                            errBmp = bmpFile + ";";
                        }

                        /*
                         *
                         * List<IntPtr> handles = new List<IntPtr>();
                         * handles.AddRange(_command.GetHandles());
                         *
                         * Bitmap[] b = WinApi.GetWindowPic(handles.ToArray());
                         * string errBmp = string.Empty;
                         * foreach (Bitmap pic in b)
                         * {
                         *
                         *  errorBmpCount++;
                         *  string bmpFile = string.Format("{0}\\{1}){2}({3}).jpg", _tempFilePath, i + 1, Path.GetFileNameWithoutExtension(TestCases[i]), errorBmpCount);
                         *  EncoderParameters ep = new EncoderParameters();
                         *  long qy;
                         *  try
                         *  {
                         *      string s = TestingDossier.ScreenShotQuality;
                         *      long.TryParse(s, out qy);
                         *  }
                         *  catch
                         *  {
                         *      qy = 100;
                         *  }
                         *  EncoderParameter eParam = new EncoderParameter(Encoder.Quality, qy);
                         *  ep.Param[0] = eParam;
                         *  ImageCodecInfo jpegIcIinfo = ImageCodecInfo.GetImageEncoders().First(e => e.FormatDescription.Equals("JPEG"));
                         *  pic.Save(bmpFile, jpegIcIinfo, ep);
                         *  errBmp += bmpFile + ";";
                         *
                         * }
                         */

                        //Get the screen of the android or IOS application if error
                        if (CurrentBrowserType.ToLower().Equals("android", StringComparison.CurrentCultureIgnoreCase) || CurrentBrowserType.ToLower().Equals("ios", StringComparison.CurrentCultureIgnoreCase))
                        {
                            errorBmpCount++;
                            OpenQA.Selenium.Appium.AppiumDriver <OpenQA.Selenium.IWebElement> ExecutingDriver = (OpenQA.Selenium.Appium.AppiumDriver <OpenQA.Selenium.IWebElement>)currentCommand.Executor.Driver;
                            string oldBmpFile = string.Format("{0}\\{1}){2}({3})-old.jpg", _tempFilePath, i + 1, Path.GetFileNameWithoutExtension(TestCases[i]), errorBmpCount);
                            string bmpFile    = string.Format("{0}\\{1}){2}({3}).jpg", _tempFilePath, i + 1, Path.GetFileNameWithoutExtension(TestCases[i]), errorBmpCount);
                            ExecutingDriver.GetScreenshot().SaveAsFile(oldBmpFile, ScreenshotImageFormat.Jpeg);
                            errBmp = oldBmpFile;
                            Image             oldPic = Image.FromFile(errBmp);
                            EncoderParameters ep     = new EncoderParameters();
                            long qy;
                            try
                            {
                                string s = TestingDossier.ScreenShotQuality;
                                long.TryParse(s, out qy);
                            }
                            catch
                            {
                                qy = 100;
                            }
                            EncoderParameter eParam = new EncoderParameter(Encoder.Quality, qy);
                            ep.Param[0] = eParam;
                            ImageCodecInfo jpegIcIinfo = ImageCodecInfo.GetImageEncoders().First(e => e.FormatDescription.Equals("JPEG"));

                            Bitmap newPic = new Bitmap(oldPic, (int)(oldPic.Width * 0.5), (int)(oldPic.Height * 0.5));
                            newPic.Save(bmpFile, ImageFormat.Jpeg);
                            errBmp = bmpFile + ";";
                        }

                        ReportProgress(TestingStatus.TestCaseFail, i, TestCases.Count, strCommand,
                                       string.Format("error at line \"{0}: {1}\" message: {2}", j, strCommand, ex.Message),
                                       errBmp, TestCases[i]);

                        if (TestCaseOnError != null)
                        {
                            TestCaseOnError(currentCommand, i == TestCases.Count - 1);
                        }
                        else
                        {
                            //RaiseResetDesktop(_command);
                        }

                        if (!continueWhenError)
                        {
                            break;
                        }
                    }
                }
            }

            foreach (var command in _command)
            {
                command.Value.Dispose();
            }
            //currentCommand.Dispose();


            ReportProgress(TestingStatus.SendingReport, 1, 1, string.Empty);

            #endregion

            string[] attachments = null;

            //attachments = _attachmentGetter();
            if (sendEmail)
            {
                try
                {
                    SendReport.SendReportByEmail(
                        string.Format("Automated Testing Report: TP: {0} -- {1:yyyyMMdd}", _name, DateTime.Now),
                        _emailFrom, _emailPassword, _emailTo, _ccTo, _emailHost, _emailPort, _enableSsl, _tempFilePath,
                        _testCaseStatus, attachments);
                    ReportProgress(TestingStatus.Completed, 1, 1, string.Empty);
                }
                catch (Exception ex)
                {
                    ReportProgress(TestingStatus.Completed, 1, 1, string.Empty, ex.Message, string.Empty, string.Empty);
                    throw ex;
                }
            }
        }