[gnome-logs/wip/test] Test search functionality using behave
- From: Rashi Aswani <aswanirashi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-logs/wip/test] Test search functionality using behave
- Date: Tue, 14 Jul 2015 22:58:34 +0000 (UTC)
commit 2c44a5f5502f59dd265859e19832b984367c51be
Author: Rashi Aswani <aswanirashi19 gmail com>
Date: Wed Jul 15 04:27:46 2015 +0530
Test search functionality using behave
tests/common_steps.py | 179 ++++++++++++++++++++++++++++++++++++++++++++++++
tests/general.feature | 9 +++
tests/steps/general.py | 26 +++++++
3 files changed, 214 insertions(+), 0 deletions(-)
---
diff --git a/tests/common_steps.py b/tests/common_steps.py
new file mode 100644
index 0000000..a82a0f0
--- /dev/null
+++ b/tests/common_steps.py
@@ -0,0 +1,179 @@
+# -*- coding: UTF-8 -*-
+from dogtail.utils import isA11yEnabled, enableA11y
+if isA11yEnabled() is False:
+ enableA11y(True)
+
+from time import time, sleep
+from functools import wraps
+from os import strerror, errno, system
+from signal import signal, alarm, SIGALRM
+from subprocess import Popen, PIPE
+from behave import step
+from gi.repository import GLib, Gio
+import fcntl, os
+
+from dogtail.rawinput import keyCombo, absoluteMotion, pressKey
+from dogtail.tree import root
+from unittest import TestCase
+
+
+# Create a dummy unittest class to have nice assertions
+class dummy(TestCase):
+ def runTest(self): # pylint: disable=R0201
+ assert True
+
+
+def wait_until(my_lambda, element, timeout=30, period=0.25):
+ """
+ This function keeps running lambda with specified params until the result is True
+ or timeout is reached
+ Sample usages:
+ * wait_until(lambda x: x.name != 'Loading...', context.app)
+ Pause until window title is not 'Loading...'.
+ Return False if window title is still 'Loading...'
+ Throw an exception if window doesn't exist after default timeout
+
+ * wait_until(lambda element, expected: x.text == expected, element, ('Expected text'))
+ Wait until element text becomes the expected (passed to the lambda)
+
+ """
+ exception_thrown = None
+ mustend = int(time()) + timeout
+ while int(time()) < mustend:
+ try:
+ if my_lambda(element):
+ return True
+ except Exception as e:
+ # If lambda has thrown the exception we'll re-raise it later
+ # and forget about if lambda passes
+ exception_thrown = e
+ sleep(period)
+ if exception_thrown:
+ raise exception_thrown
+ else:
+ return False
+
+
+class TimeoutError(Exception):
+ """
+ Timeout exception class for limit_execution_time_to function
+ """
+ pass
+
+
+def limit_execution_time_to(
+ seconds=10, error_message=strerror(errno.ETIME)):
+ """
+ Decorator to limit function execution to specified limit
+ """
+ def decorator(func):
+ def _handle_timeout(signum, frame):
+ raise TimeoutError(error_message)
+
+ def wrapper(*args, **kwargs):
+ signal(SIGALRM, _handle_timeout)
+ alarm(seconds)
+ try:
+ result = func(*args, **kwargs)
+ finally:
+ alarm(0)
+ return result
+
+ return wraps(func)(wrapper)
+
+ return decorator
+
+
+class App(object):
+ """
+ This class does all basic events with the app
+ """
+ def __init__(
+ self, appName, shortcut='<Control><Q>', a11yAppName=None,
+ forceKill=True, parameters='', recordVideo=False):
+ """
+ Initialize object App
+ appName command to run the app
+ shortcut default quit shortcut
+ a11yAppName app's a11y name is different than binary
+ forceKill is the app supposed to be kill before/after test?
+ parameters has the app any params needed to start? (only for startViaCommand)
+ recordVideo start gnome-shell recording while running the app
+ """
+ self.appCommand = appName
+ self.shortcut = shortcut
+ self.forceKill = forceKill
+ self.parameters = parameters
+ self.internCommand = self.appCommand.lower()
+ self.a11yAppName = a11yAppName
+ self.recordVideo = recordVideo
+ self.pid = None
+
+ # a way of overcoming overview autospawn when mouse in 1,1 from start
+ pressKey('Esc')
+ absoluteMotion(100, 100, 2)
+
+ # attempt to make a recording of the test
+ if self.recordVideo:
+ keyCombo('<Control><Alt><Shift>R')
+
+ def isRunning(self):
+ """
+ Is the app running?
+ """
+ if self.a11yAppName is None:
+ self.a11yAppName = self.internCommand
+
+ # Trap weird bus errors
+ for attempt in xrange(0, 30):
+ sleep(1)
+ try:
+ return self.a11yAppName in [x.name for x in root.applications()]
+ except GLib.GError:
+ continue
+ raise Exception("10 at-spi errors, seems that bus is blocked")
+
+ def kill(self):
+ """
+ Kill the app via 'killall'
+ """
+ if self.recordVideo:
+ keyCombo('<Control><Alt><Shift>R')
+
+ try:
+ self.process.kill()
+ except:
+ # Fall back to killall
+ Popen("killall " + self.appCommand, shell=True).wait()
+
+ def startViaCommand(self):
+ """
+ Start the app via command
+ """
+ if self.forceKill and self.isRunning():
+ self.kill()
+ assert not self.isRunning(), "Application cannot be stopped"
+
+ #command = "%s %s" % (self.appCommand, self.parameters)
+ #self.pid = run(command, timeout=5)
+ self.process = Popen(self.appCommand.split() + self.parameters.split(),
+ stdout=PIPE, stderr=PIPE, bufsize=0)
+ self.pid = self.process.pid
+
+ assert self.isRunning(), "Application failed to start"
+ return root.application(self.a11yAppName)
+
+ def closeViaShortcut(self):
+ """
+ Close the app via shortcut
+ """
+ if not self.isRunning():
+ raise Exception("App is not running")
+
+ keyCombo(self.shortcut)
+ assert not self.isRunning(), "Application cannot be stopped"
+
+
+ step(u'Start a new Logs instance')
+def start_new_logs_instance(context):
+ context.app = context.app_class.startViaCommand()
diff --git a/tests/general.feature b/tests/general.feature
new file mode 100644
index 0000000..e7ec4e7
--- /dev/null
+++ b/tests/general.feature
@@ -0,0 +1,9 @@
+Feature: General
+
+ Background:
+ * Make sure that gnome-logs is running
+
+ start_logs
+ Scenario: Start logs directly
+ * Click on search
+ Then all selection toolbar buttons are sensitive
diff --git a/tests/steps/general.py b/tests/steps/general.py
new file mode 100644
index 0000000..afcc8ad
--- /dev/null
+++ b/tests/steps/general.py
@@ -0,0 +1,26 @@
+from behave import step
+
+from os import system
+from pyatspi import STATE_SENSITIVE
+from time import sleep
+from common_steps import App
+
+ step(u'Make sure that gnome-logs is running')
+def make_sure_gnome_logs_is_running(context):
+ system("gnome-logs --force-shutdown 2&> /dev/null")
+ context.execute_steps(u'* Start a new Logs instance')
+
+ step(u'Click on search')
+def click_on_search(context):
+ app.child('Search').click()
+
+ then(u'all selection toolbar buttons are sensitive')
+def all_selection_toolbar_buttons_sensitive(context):
+ sleep(0.5)
+ assert app.child(translate('Important')).sensitive
+ assert app.child(translate('All')).sensitive
+ assert app.child(translate('Applications')).sensitive
+ assert app.child(translate('System')).sensitive
+ assert app.child(translate('Security')).sensitive
+ assert app.child(translate('Hardware')).sensitive
+ sleep(0.5)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]