public bool TrySearchText(string textToSearch,
                                  string jsonResult,
                                  IScreen screen,
                                  ScreenSearchArea searchArea,
                                  out IScreenArea area)
        {
            if (string.IsNullOrEmpty(textToSearch))
            {
                throw new ArgumentNullException();
            }

            area = null;
            IAzureRecognizeTextResponse textResponse = Deserialize(jsonResult);

            if (textResponse.RecognitionResult?.Lines?.Length == 0)
            {
                return(false);
            }

            bool isLine = textToSearch.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Length > 1;

            foreach (IAzureLine line in textResponse.RecognitionResult?.Lines)
            {
                (int X, int Y) = line.GetArea().GetCentralPoint();

                if (isLine)
                {
                    if (line.Text.IndexOf(textToSearch, StringComparison.OrdinalIgnoreCase) >= 0 &&
                        screen.IsSearchAreaMatch(searchArea, (X, Y)))
                    {
                        area = line.GetArea();
                        return(true);
                    }
                }
                else if (line.Words.Length > 0)
                {
                    IAzureWord word = Array.Find(line.Words, x => string.Equals(x.Text, textToSearch, StringComparison.OrdinalIgnoreCase));

                    if (word != null)
                    {
                        (int X2, int Y2) = word.GetArea().GetCentralPoint();

                        if (screen.IsSearchAreaMatch(searchArea, (X2, Y2)))
                        {
                            area = word.GetArea();
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Example #2
0
        public async Task <bool> ExecuteAsync()
        {
            if (_step == null ||
                !string.Equals(_step.Target, _targetKeyword, StringComparison.OrdinalIgnoreCase) ||
                string.IsNullOrEmpty(_step.Search))
            {
                return(false);
            }

            string filename = $".\\{GetArtifactFolderValue()}\\FullScreen-{DateTime.Now.ToString("yyyyMMddHHmmss")}.jpg";

            FileUtility.EnsureParentFolder(filename);
            Computer.Screen.SaveFullScreenAsFile(filename);
            bool        foundLocation = false;
            IScreenArea area          = null;

            for (int i = 0; i < OCRServices.Count; i++)
            {
                area = await OCRServices[i].GetOCRResultAsync(filename, _step.Search, _step.SearchArea).ConfigureAwait(false);

                if (area != null)
                {
                    foundLocation = true;
                    break;
                }
            }

            if (!foundLocation || area == null)
            {
                Logger.WriteError(string.Format(EngineResource.SearchTextNotFound, _step.Search));
                string message = string.Format(EngineResource.TextNotFoundInJsonResult, _step.Search);
                OpenCVService.PutText(filename, 1, Computer.Screen.Height / 2, message);
                return(false);
            }

            (int X, int Y) = area.GetCentralPoint();
            Logger.WriteInfo(string.Format(EngineResource.TargetNotFoundInLocation, X, Y));

            // draw a rectangle
            OpenCVService.DrawRedRectangle(filename, area.Left, area.Top, area.Width, area.Height);

            // run action if exists
            if (!string.IsNullOrEmpty(_step.Action))
            {
                ITestActionExecutor actionExecutor = TestActionExecutorGenerator.Generate(Computer,
                                                                                          _step.Action,
                                                                                          _step.ActionArgument,
                                                                                          (X, Y),
                                                                                          Logger);

                actionExecutor?.Execute();
            }

            // wait if exists
            if (_step.WaitingSecond > 0)
            {
                Logger.WriteInfo(string.Format(EngineResource.TextStepWaitMessage, _step.WaitingSecond));
                await Task.Delay(_step.WaitingSecond * 1000).ConfigureAwait(false);
            }

            return(true);
        }