Пример #1
0
        public static ProBreak Create(WebCommand wc)
        {
            ProBreak res;

            res = new ProBreak();
            res.SetWebService(wc);
            return(res);
        }
Пример #2
0
        internal void Config(WebCommand cmd)
        {
            if (Label != null)
            {
                //这句会同时设置 LabelModified 属性的值。
                cmd.HasLabel(Label);
            }

            if (IsVisible != null)
            {
                cmd.IsVisible = IsVisible.Value;
            }
        }
Пример #3
0
        private void AddCommands(WebEntityViewMeta evm, IList <ToolbarItem> toolbar)
        {
            //当不需要忽略命令,或者使用了扩展视图时,都需要加入命令列表。
            if (!this.Option.ignoreCommands || !string.IsNullOrEmpty(this.Option.viewName))
            {
                IEnumerable <WebCommand> commands = evm.Commands;

                //当使用了扩展视图时,只需要显示定制 UI 按钮就行了。
                if (this.Option.ignoreCommands)
                {
                    var customUI = commands.FirstOrDefault(c => c.Name == WebCommandNames.CustomizeUI);
                    if (customUI != null)
                    {
                        commands = new WebCommand[] { customUI };
                    }
                    else
                    {
                        commands = Enumerable.Empty <WebCommand>();
                    }
                }

                foreach (var jsCmd in commands)
                {
                    if (!jsCmd.IsVisible)
                    {
                        continue;
                    }

                    var ti = new ToolbarItem
                    {
                        command = jsCmd.Name
                    };

                    if (jsCmd.LabelModified)
                    {
                        ti.text = jsCmd.Label;
                    }

                    //自定义属性都直接添加到 toolBarItem 的属性上。
                    foreach (var kv in jsCmd.GetExtendedProperties())
                    {
                        var name  = kv.Key;
                        var value = kv.Value;

                        ti.SetProperty(name, value);
                    }

                    toolbar.Add(ti);
                }
            }
        }
Пример #4
0
 internal WebFeatureCommand(InstallProcessControl parent, IList <WebLoc> webLocs, WebCommand command)
     : base(parent)
 {
     this.webLocs = webLocs;
     this.command = command;
 }
 internal WebFeatureCommand(InstallProcessControl parent, IList<WebLoc> webLocs, WebCommand command)
     : base(parent)
 {
     this.webLocs = webLocs;
     this.command = command;
 }
Пример #6
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;
                }
            }
        }