tests/ide_tests/sikuliberemiz.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Thu, 07 Apr 2022 07:40:32 +0200
branchwxPython4
changeset 3447 65c5f66e9298
parent 3446 de8cc85b688a
child 3448 523f6fcc7a28
permissions -rw-r--r--
Tests: add HTML report generation and a workaround to bad exception handling in sikuli.

In case of exception in python code, and since a thread is running
to observe stdout, sikuli was never terminated after an exception.
Unfortunately sys.exepthook doesn't work in that version of jython/sikuli.
Test are now written inside functions witch are passed to run_test to deal
with exception.
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
3432
40b8b2ffb3fd Tests: Various enhancements in Makefile and sikuli common python code.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3431
diff changeset
   113
    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
   114
        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
   115
            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
   116
            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
   117
                break
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   118
            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
   119
            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
   120
            self.event.set()
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   121
            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
   122
                sys.stdout.write("found pattern in '" + a +"'")
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   123
                self.success_event.set()
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   124
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   125
    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
   126
        """
ce366d67a5b7 Tests: Enhance robustness of stdout driven waiting state in Sikuli based tests.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3432
diff changeset
   127
        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
   128
        Parameters: 
ce366d67a5b7 Tests: Enhance robustness of stdout driven waiting state in Sikuli based tests.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3432
diff changeset
   129
            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
   130
        """
ce366d67a5b7 Tests: Enhance robustness of stdout driven waiting state in Sikuli based tests.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3432
diff changeset
   131
        start_time = timesec()
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   132
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
   133
        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
   134
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
   135
        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
   136
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
        if wait_result:
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   138
            self.event.clear()
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   139
        else:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   140
            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
   141
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   142
        self.waitIdleStdout(period, timeout - (timesec() - start_time))
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   143
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   144
    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
   145
        """
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
   146
        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
   147
        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
   148
            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
   149
            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
   150
        """
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   151
        end_time = timesec() + timeout
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   152
        self.event.clear()
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   153
        while timesec() < end_time:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   154
            if self.event.wait(period):
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   155
                # no timeout -> got event -> not idle -> loop again
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   156
                self.event.clear()
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   157
            else:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   158
                # 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
   159
                self.ReportScreenShot("stdout is idle")
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   160
                return True
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   161
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 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
   163
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   164
        raise Exception("Stdout did not idle before timeout")
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   165
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   166
    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
   167
        found = 0
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   168
        self.pattern = pattern
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   169
        end_time = timesec() + timeout
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   170
        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
   171
        while True:
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   172
            remain = end_time - timesec()
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   173
            if remain <= 0 :
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   174
                res = False
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   175
                break
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   176
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   177
            res = self.success_event.wait(remain)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   178
            if res:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   179
                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
   180
                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
   181
                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
   182
                    break
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   183
        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
   184
        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
   185
        return res
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   186
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   187
class BeremizApp(IDEIdleObserver, stdoutIdleObserver):
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   188
    def __init__(self, projectpath=None, exemple=None):
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
        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
   191
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   192
            Parameters: 
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   193
                projectpath (str): path to project to open
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   194
                exemple (str): path relative to exemples directory
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   195
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   196
            Returns:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   197
                Sikuli App class instance
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
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
   200
        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
   201
        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
   202
        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
   203
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
   204
        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
   205
        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
   206
<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
   207
  <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
   208
    <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
   209
    <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
   210
    <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
   211
  </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
   212
  <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
   213
""")
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
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   215
        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
   216
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   217
        if exemple is not None:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   218
            command.append(opj(beremiz_path,"exemples",exemple))
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   219
        elif projectpath is not None:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   220
            command.append(projectpath)
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
        # 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
   223
        # 
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   224
        # Workaround : - use subprocess module to spawn IDE process,
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   225
        #              - 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
   226
        #              - pass exact window title to App class constructor
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   227
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
   228
        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
   229
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   230
        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
   231
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   232
        # Window are macthed against process' PID
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   233
        ppid = self.proc.pid
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   234
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   235
        # Timeout 5s
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   236
        c = 50
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   237
        while c > 0:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   238
            # equiv to "wmctrl -l -p | grep $pid"
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   239
            try:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   240
                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
   241
            except subprocess.CalledProcessError:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   242
                wlist = []
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   243
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   244
            # 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
   245
            # beremiz splashcreen has no title
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   246
            # wait until main window is visible
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   247
            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
   248
                windowID,_zero,wpid,_XID,wtitle = wlist[0] 
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   249
                break
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   250
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   251
            sikuli.wait(0.1)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   252
            c = c - 1
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   253
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   254
        if c == 0:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   255
            raise Exception("Couldn't find Beremiz window")
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   256
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   257
        # Maximize window x and y
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   258
        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
   259
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   260
        # 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
   261
        self.sikuliapp = sikuli.switchApp(wtitle)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   262
        self.k = KBDShortcut(self)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   263
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   264
        IDEIdleObserver.__init__(self)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   265
        stdoutIdleObserver.__init__(self)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   266
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   267
        # stubs for common sikuli calls to allow adding hooks later
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
   268
        for name in ["click","doubleClick","type"]:
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
   269
            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
   270
                def myMeth(*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
   271
                    getattr(sikuli, n)(*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
   272
                    self.ReportScreenShot(n + "(" + repr(args) + "," + repr(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
   273
                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
   274
            setattr(self, name, makeMyMeth(name))
3446
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   275
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   276
    def close(self):
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   277
        self.sikuliapp.close()
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   278
        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
   279
        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
   280
  </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
   281
</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
   282
        self.report.close()
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 __del__(self):
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   285
        if self.sikuliapp is not None:
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   286
            self.sikuliapp.close()
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   287
        IDEIdleObserver.__del__(self)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   288
        stdoutIdleObserver.__del__(self)
de8cc85b688a Tests: refactored sikuli based test
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3437
diff changeset
   289
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
   290
    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
   291
        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
   292
        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
   293
        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
   294
        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
   295
        # self.report.write("ReportScreenShot " + msg + " " + fname + "\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
   296
        self.screenshotnum = self.screenshotnum + 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
   297
        self.report.write( "<p>" + elapsed + msg + "<img src=\""+ fname + "\">" + "</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
   298
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
    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
   300
        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
   301
        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
   302
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
    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
   304
        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
   305
        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
   306
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
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
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
   309
    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
   310
    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
   311
        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
   312
    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
   313
        # 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
   314
        # 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
   315
        # 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
   316
        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
   317
        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
   318
        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
   319
        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
   320
        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
   321
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
    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
   323
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
    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
   325
        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
   326
    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
   327
        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
   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
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