Getting text(date) from image that changes everyday

Asked by monicar on 2018-12-05

I am trying to get text from image. Text is the date that changes every hour. Sikuli is getting the text what is in image not on the application.

I am comparing the Text(Date) string with the my calculated date. As the Date from the application changes often my scripts are failing.

Here is the logic:

  try {

            //Extract Donor's Status text from the region
            Settings.OcrTextRead = true;
            Settings.OcrTextSearch = true;

            screen.wait(txtImage, 10);
            String extractText = screen.find(txtImage).text();
            extractText = extractText.replaceAll("[a-zA-Z]","/");
           // extractText = extractText.substring(3);
           Date date1 = new SimpleDateFormat(US_DATE_FORMAT_STRING).parse(extractText);
            TestEnvironment.get().getTestLogger().log("Eligibility Date: " + extractText);

            LocalDate today = LocalDate.now();
            LocalDate drawdate = null;

            if (period.toLowerCase().contains("today")){
                drawdate =LocalDate.now();
            } else if (period.toLowerCase().contains("week")){
                drawdate = today.minus(1, ChronoUnit.WEEKS);
            } else if (period.toLowerCase().contains("month")){
                drawdate = today.minus(1, ChronoUnit.MONTHS);
            }else {
                throw new IllegalArgumentException(period);
            }

            LocalDate eligibilityDate = drawdate.plus(days, ChronoUnit.DAYS);
            TestEnvironment.get().getTestLogger().log("Calculated Eligibility Date: " + eligibilityDate);

            TestEnvironment.get().getErrorCollector().checkThat("Eligibility Date:", extractText, is(equalTo(eligibilityDate.format(US_DATE_FORMAT))));

        } catch (FindFailed | ParseException error) {
            TestEnvironment.get().getErrorCollector().addError(error);
        }

extractText changes often in my application, I have to take screenshot when ever it changes and run my scripts. Can you please suggest me any other alternatives to get dynamic text from application instead of the captured image.

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
2018-12-05
Last query:
2018-12-05
Last reply:
2018-12-05
Best RaiMan (raimund-hocke) said : #1

--- general comment
          screen.wait(txtImage, 10);
           String extractText = screen.find(txtImage).text();

This does not really make sense:
          Match theText = screen.wait(txtImage, 10); // gives the region of the image
          String extractText = theText.text();

or even shorter:
          String extractText = screen.wait(txtImage, 10).text()

--- your problem
As I understand, the region on the screen of the found txtImage contains the text you want to read and this area changes.

--- how to:
- find some image near the text region, that does not change.
- define the region with the text relative to this match
- read the text from this region

meta code:
ref = find(someReferenceImage)
textRegion = ref.offset(x, y).grow(....) // x, y and ... have to be evaluated manually
txt = textRegion.text()

monicar (monicar) said : #2

Offset worked.

monicar (monicar) said : #3

Thanks Raiman. Working as expected with offset.

On Wed, Dec 5, 2018, 10:56 AM RaiMan <<email address hidden>
wrote:

> Your question #676619 on Sikuli changed:
> https://answers.launchpad.net/sikuli/+question/676619
>
> Status: Open => Answered
>
> RaiMan proposed the following answer:
> --- general comment
> screen.wait(txtImage, 10);
> String extractText = screen.find(txtImage).text();
>
> This does not really make sense:
> Match theText = screen.wait(txtImage, 10); // gives the region
> of the image
> String extractText = theText.text();
>
> or even shorter:
> String extractText = screen.wait(txtImage, 10).text()
>
> --- your problem
> As I understand, the region on the screen of the found txtImage contains
> the text you want to read and this area changes.
>
> --- how to:
> - find some image near the text region, that does not change.
> - define the region with the text relative to this match
> - read the text from this region
>
> meta code:
> ref = find(someReferenceImage)
> textRegion = ref.offset(x, y).grow(....) // x, y and ... have to be
> evaluated manually
> txt = textRegion.text()
>
> --
> If this answers your question, please go to the following page to let us
> know that it is solved:
> https://answers.launchpad.net/sikuli/+question/676619/+confirm?answer_id=0
>
> If you still need help, you can reply to this email or go to the
> following page to enter your feedback:
> https://answers.launchpad.net/sikuli/+question/676619
>
> You received this question notification because you asked the question.
>