tests/ide_tests/sikuliberemiz.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Tue, 11 Oct 2022 13:06:48 +0200
branchwxPython4
changeset 3631 176e1f218c61
parent 3562 165dc0e97f1d
child 3668 142c268124ab
permissions -rw-r--r--
Tests: add opc-ua encrypted test
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     1
"Commons definitions for sikuli based beremiz IDE GUI tests"
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     2
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     3
import os
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     4
import sys
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     5
import subprocess
3447
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
     6
import traceback
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
     7
from threading import Thread, Event, Lock
3437
ce366d67a5b7 Tests: Enhance robustness of stdout driven waiting state in Sikuli based tests.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3432
diff changeset
     8
from time import time as timesec
3432
40b8b2ffb3fd Tests: Various enhancements in Makefile and sikuli common python code.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3431
diff changeset
     9
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    10
import sikuli
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    11
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    12
beremiz_path = os.environ["BEREMIZPATH"]
3431
5bcef31e276d Tests: Added BEREMIZPYTHONPATH to point to Python interpreter to use for tests.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3430
diff changeset
    13
python_bin = os.environ.get("BEREMIZPYTHONPATH", "/usr/bin/python")
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    14
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    15
opj = os.path.join
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    16
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    17
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    18
class KBDShortcut:
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    19
    """Send shortut to app by calling corresponding methods.
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    20
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    21
    example:
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    22
        k = KBDShortcut()
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    23
        k.Clean()
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    24
    """
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    25
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    26
    fkeys = {"Stop":     sikuli.Key.F4,
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    27
             "Run":      sikuli.Key.F5,
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    28
             "Transfer": sikuli.Key.F6,
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    29
             "Connect":  sikuli.Key.F7,
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    30
             "Clean":    sikuli.Key.F9,
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    31
             "Build":    sikuli.Key.F11,
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    32
             "Save":     ("s",sikuli.Key.CTRL),
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    33
             "New":      ("n",sikuli.Key.CTRL),
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    34
             "Address":  ("l",sikuli.Key.CTRL)}  # to reach address bar in GTK's file selector
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    35
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    36
    def __init__(self, app):
3447
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
    37
        self.app = app
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    38
    
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    39
    def __getattr__(self, name):
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    40
        fkey = self.fkeys[name]
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    41
        if type(fkey) != tuple:
3432
40b8b2ffb3fd Tests: Various enhancements in Makefile and sikuli common python code.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3431
diff changeset
    42
            fkey = (fkey,)
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    43
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    44
        def PressShortCut():
3447
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
    45
            self.app.sikuliapp.focus()
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    46
            sikuli.type(*fkey)
3447
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
    47
            self.app.ReportText("Sending " + name + " shortcut")
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    48
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    49
        return PressShortCut
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    50
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    51
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    52
class IDEIdleObserver:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    53
    "Detects when IDE is idle. This is particularly handy when staring an operation and witing for the en of it."
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    54
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    55
    def __init__(self):
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    56
        """
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    57
        Parameters: 
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    58
            app (class BeremizApp)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    59
        """
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    60
        self.r = sikuli.Region(self.sikuliapp.window())
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    61
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    62
        self.idechanged = False
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    63
        
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    64
        # 200 was selected because default 50 was still catching cursor blinking in console
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    65
        # FIXME : remove blinking cursor in console
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    66
        self.r.onChange(200,self._OnIDEWindowChange)
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    67
        self.r.observeInBackground()
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    68
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    69
    def __del__(self):
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    70
        self.r.stopObserver()
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    71
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    72
    def _OnIDEWindowChange(self, event):
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    73
        self.idechanged = True
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    74
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    75
    def WaitIdleUI(self, period=1, timeout=15):
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    76
        """
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    77
        Wait for IDE to stop changing
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    78
        Parameters: 
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    79
            period (int): how many seconds with no change to consider idle
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    80
            timeout (int): how long to wait for idle, in seconds
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    81
        """
3437
ce366d67a5b7 Tests: Enhance robustness of stdout driven waiting state in Sikuli based tests.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3432
diff changeset
    82
        c = max(timeout/period,1)
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    83
        while c > 0:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    84
            self.idechanged = False
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    85
            sikuli.wait(period)
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    86
            if not self.idechanged:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    87
                break
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    88
            c = c - 1
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    89
3447
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
    90
        self.ReportScreenShot("UI is idle" if c != 0 else "UI is not idle")
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
    91
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    92
        if c == 0:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    93
            raise Exception("Window did not idle before timeout")
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    94
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    95
 
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    96
class stdoutIdleObserver:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    97
    "Detects when IDE's stdout is idle. Can be more reliable than pixel based version (false changes ?)"
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    98
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
    99
    def __init__(self):
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   100
        """
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   101
        Parameters: 
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   102
            app (class BeremizApp)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   103
        """
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   104
        self.stdoutchanged = False
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   105
3437
ce366d67a5b7 Tests: Enhance robustness of stdout driven waiting state in Sikuli based tests.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3432
diff changeset
   106
        self.event = Event()
ce366d67a5b7 Tests: Enhance robustness of stdout driven waiting state in Sikuli based tests.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3432
diff changeset
   107
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   108
        self.pattern = None
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   109
        self.success_event = Event()
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   110
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   111
        self.thread = Thread(target = self._waitStdoutProc).start()
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   112
3528
c269f256a7c4 tests: add (broken) debug_project test, fix edit_project
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3448
diff changeset
   113
    def __del__(self):
c269f256a7c4 tests: add (broken) debug_project test, fix edit_project
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3448
diff changeset
   114
        pass  # self.thread.join() ?
c269f256a7c4 tests: add (broken) debug_project test, fix edit_project
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3448
diff changeset
   115
3432
40b8b2ffb3fd Tests: Various enhancements in Makefile and sikuli common python code.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3431
diff changeset
   116
    def _waitStdoutProc(self):
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   117
        while True:
3437
ce366d67a5b7 Tests: Enhance robustness of stdout driven waiting state in Sikuli based tests.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3432
diff changeset
   118
            a = self.proc.stdout.readline()
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   119
            if len(a) == 0 or a is None: 
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   120
                break
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   121
            sys.stdout.write(a)
3447
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   122
            self.ReportOutput(a)
3437
ce366d67a5b7 Tests: Enhance robustness of stdout driven waiting state in Sikuli based tests.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3432
diff changeset
   123
            self.event.set()
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   124
            if self.pattern is not None and a.find(self.pattern) >= 0:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   125
                sys.stdout.write("found pattern in '" + a +"'")
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   126
                self.success_event.set()
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   127
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   128
    def waitForChangeAndIdleStdout(self, period=2, timeout=15):
3437
ce366d67a5b7 Tests: Enhance robustness of stdout driven waiting state in Sikuli based tests.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3432
diff changeset
   129
        """
ce366d67a5b7 Tests: Enhance robustness of stdout driven waiting state in Sikuli based tests.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3432
diff changeset
   130
        Wait for IDE'stdout to start changing
ce366d67a5b7 Tests: Enhance robustness of stdout driven waiting state in Sikuli based tests.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3432
diff changeset
   131
        Parameters: 
ce366d67a5b7 Tests: Enhance robustness of stdout driven waiting state in Sikuli based tests.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3432
diff changeset
   132
            timeout (int): how long to wait for change, in seconds
ce366d67a5b7 Tests: Enhance robustness of stdout driven waiting state in Sikuli based tests.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3432
diff changeset
   133
        """
ce366d67a5b7 Tests: Enhance robustness of stdout driven waiting state in Sikuli based tests.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3432
diff changeset
   134
        start_time = timesec()
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   135
3447
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   136
        wait_result = self.event.wait(timeout)
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   137
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   138
        self.ReportScreenShot("stdout changed" if wait_result else "stdout didn't change")
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   139
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   140
        if wait_result:
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   141
            self.event.clear()
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   142
        else:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   143
            raise Exception("Stdout didn't become active before timeout")
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   144
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   145
        self.waitIdleStdout(period, timeout - (timesec() - start_time))
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   146
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   147
    def waitIdleStdout(self, period=2, timeout=15):
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   148
        """
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   149
        Wait for IDE'stdout to stop changing
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   150
        Parameters: 
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   151
            period (int): how many seconds with no change to consider idle
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   152
            timeout (int): how long to wait for idle, in seconds
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   153
        """
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   154
        end_time = timesec() + timeout
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   155
        self.event.clear()
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   156
        while timesec() < end_time:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   157
            if self.event.wait(period):
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   158
                # no timeout -> got event -> not idle -> loop again
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   159
                self.event.clear()
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   160
            else:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   161
                # timeout -> no event -> idle -> exit
3447
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   162
                self.ReportScreenShot("stdout is idle")
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   163
                return True
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   164
3447
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   165
        self.ReportScreenShot("stdout did not idle")
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   166
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   167
        raise Exception("Stdout did not idle before timeout")
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   168
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   169
    def waitPatternInStdout(self, pattern, timeout, count=1):
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   170
        found = 0
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   171
        self.pattern = pattern
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   172
        end_time = timesec() + timeout
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   173
        self.event.clear()
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   174
        while True:
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   175
            remain = end_time - timesec()
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   176
            if remain <= 0 :
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   177
                res = False
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   178
                break
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   179
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   180
            res = self.success_event.wait(remain)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   181
            if res:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   182
                self.success_event.clear()
3424
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   183
                found = found + 1
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   184
                if found >= count:
7db96e011fe7 Tests: added tests/Makefile and a first test in tests/ide_tests. Test requires sikuli and Xvfb or Xnest.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   185
                    break
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   186
        self.pattern = None
3447
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   187
        self.ReportScreenShot("found pattern" if res else "pattern not found")
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   188
        return res
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   189
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   190
class BeremizApp(IDEIdleObserver, stdoutIdleObserver):
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   191
    def __init__(self, projectpath=None, exemple=None):
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   192
        """
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   193
        Starts Beremiz IDE, waits for main window to appear, maximize it.
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   194
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   195
            Parameters: 
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   196
                projectpath (str): path to project to open
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   197
                exemple (str): path relative to exemples directory
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   198
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   199
            Returns:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   200
                Sikuli App class instance
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   201
        """
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   202
3562
165dc0e97f1d tests: Change OCR options to be more reliable on small fonts.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3547
diff changeset
   203
        sikuli.OCR.Options().smallFont()
165dc0e97f1d tests: Change OCR options to be more reliable on small fonts.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3547
diff changeset
   204
3447
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   205
        self.screenshotnum = 0
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   206
        self.starttime = timesec()
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   207
        self.screen = sikuli.Screen()
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   208
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   209
        self.report = open("report.html", "w")
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   210
        self.report.write("""<!doctype html>
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   211
<html>
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   212
  <head>
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   213
    <meta charset="utf-8">
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   214
    <meta name="color-scheme" content="light dark">
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   215
    <title>Test report</title>
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   216
  </head>
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   217
  <body>
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   218
""")
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   219
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   220
        command = [python_bin, opj(beremiz_path,"Beremiz.py"), "--log=/dev/stdout"]
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   221
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   222
        if exemple is not None:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   223
            command.append(opj(beremiz_path,"exemples",exemple))
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   224
        elif projectpath is not None:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   225
            command.append(projectpath)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   226
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   227
        # App class is broken in Sikuli 2.0.5: can't start process with arguments.
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   228
        # 
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   229
        # Workaround : - use subprocess module to spawn IDE process,
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   230
        #              - use wmctrl to find IDE window details and maximize it
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   231
        #              - pass exact window title to App class constructor
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   232
3447
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   233
        self.ReportText("Launching " + repr(command))
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   234
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   235
        self.proc = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=0)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   236
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   237
        # Window are macthed against process' PID
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   238
        ppid = self.proc.pid
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   239
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   240
        # Timeout 5s
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   241
        c = 50
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   242
        while c > 0:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   243
            # equiv to "wmctrl -l -p | grep $pid"
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   244
            try:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   245
                wlist = filter(lambda l:(len(l)>2 and l[2]==str(ppid)), map(lambda s:s.split(None,4), subprocess.check_output(["wmctrl", "-l", "-p"]).splitlines()))
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   246
            except subprocess.CalledProcessError:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   247
                wlist = []
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   248
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   249
            # window with no title only has 4 fields do describe it
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   250
            # beremiz splashcreen has no title
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   251
            # wait until main window is visible
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   252
            if len(wlist) == 1 and len(wlist[0]) == 5:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   253
                windowID,_zero,wpid,_XID,wtitle = wlist[0] 
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   254
                break
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   255
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   256
            sikuli.wait(0.1)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   257
            c = c - 1
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   258
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   259
        if c == 0:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   260
            raise Exception("Couldn't find Beremiz window")
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   261
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   262
        # Maximize window x and y
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   263
        subprocess.check_call(["wmctrl", "-i", "-r", windowID, "-b", "add,maximized_vert,maximized_horz"])
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   264
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   265
        # switchApp creates an App object by finding window by title, is not supposed to spawn a process
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   266
        self.sikuliapp = sikuli.switchApp(wtitle)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   267
        self.k = KBDShortcut(self)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   268
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   269
        IDEIdleObserver.__init__(self)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   270
        stdoutIdleObserver.__init__(self)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   271
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   272
        # stubs for common sikuli calls to allow adding hooks later
3528
c269f256a7c4 tests: add (broken) debug_project test, fix edit_project
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3448
diff changeset
   273
        for name in ["click","doubleClick","type","rightClick","wait"]:
3447
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   274
            def makeMyMeth(n):
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   275
                def myMeth(*args, **kwargs):
3534
7f62da4362fd tests: nore details, please
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3528
diff changeset
   276
                    self.ReportScreenShot("Begin: " + n + "(" + repr(args) + "," + repr(kwargs) + ")")
3544
f2dff88db448 tests: IDE: force reporting screenshot even after failing command
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3534
diff changeset
   277
                    try:
f2dff88db448 tests: IDE: force reporting screenshot even after failing command
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3534
diff changeset
   278
                        getattr(sikuli, n)(*args, **kwargs)
f2dff88db448 tests: IDE: force reporting screenshot even after failing command
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3534
diff changeset
   279
                    finally:
f2dff88db448 tests: IDE: force reporting screenshot even after failing command
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3534
diff changeset
   280
                        self.ReportScreenShot("end: " + n + "(" + repr(args) + "," + repr(kwargs) + ")")
3447
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   281
                return myMeth
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   282
            setattr(self, name, makeMyMeth(name))
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   283
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   284
    def close(self):
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   285
        self.sikuliapp.close()
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   286
        self.sikuliapp = None
3447
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   287
        self.report.write("""
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   288
  </body>
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   289
</html>""")
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   290
        self.report.close()
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   291
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   292
    def __del__(self):
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   293
        if self.sikuliapp is not None:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   294
            self.sikuliapp.close()
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   295
        IDEIdleObserver.__del__(self)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   296
        stdoutIdleObserver.__del__(self)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   297
3447
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   298
    def ReportScreenShot(self, msg):
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   299
        elapsed = "%.3fs: "%(timesec() - self.starttime)
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   300
        fname = "capture"+str(self.screenshotnum)+".png"
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   301
        cap = self.screen.capture(self.r)
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   302
        cap.save(".", fname)
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   303
        self.screenshotnum = self.screenshotnum + 1
3448
523f6fcc7a28 Tests: Fix missing <br/> in generated html report.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3447
diff changeset
   304
        self.report.write( "<p>" + elapsed + msg + "<br/><img src=\""+ fname + "\">" + "</p>")
3447
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   305
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   306
    def ReportText(self, text):
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   307
        elapsed = "%.3fs: "%(timesec() - self.starttime)
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   308
        self.report.write("<p>" + elapsed + text + "</p>")
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   309
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   310
    def ReportOutput(self, text):
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   311
        elapsed = "%.3fs: "%(timesec() - self.starttime)
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   312
        self.report.write("<pre>" + elapsed + text + "</pre>")
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   313
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   314
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   315
def run_test(func, *args, **kwargs):
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   316
    app = BeremizApp(*args, **kwargs)
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   317
    try:
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   318
        success = func(app)
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   319
    except:
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   320
        # sadly, sys.excepthook is broken in sikuli/jython 
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   321
        # purpose of this run_test function is to work around it.
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   322
        # and catch exception cleanly anyhow
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   323
        e_type, e_value, e_traceback = sys.exc_info()
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   324
        err_msg = "\n".join(traceback.format_exception(e_type, e_value, e_traceback))
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   325
        sys.stdout.write(err_msg)
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   326
        app.ReportOutput(err_msg)
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   327
        success = False
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   328
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   329
    app.close()
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   330
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   331
    if success:
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   332
        sikuli.exit(0)
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   333
    else:
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   334
        sikuli.exit(1)
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   335
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   336
65c5f66e9298 Tests: add HTML report generation and a workaround to bad exception handling in sikuli.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3446
diff changeset
   337