Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Update readme with splinter / page objects.

  • Loading branch information...
commit 8b7ed9d776845387d076aa6f6d5282e4c7e57fad 1 parent 4663f64
Jonas Baumann authored March 24, 2013

Showing 1 changed file with 151 additions and 0 deletions. Show diff stats Hide diff stats

  1. 151  README.rst
151  README.rst
Source Rendered
@@ -11,6 +11,155 @@ This package provides helpers for writing tests.
11 11
    Certified: 01/2013
12 12
 
13 13
 
  14
+Browser testing with splinter
  15
+-----------------------------
  16
+
  17
+`Splinter`_ is a library which provides a common API for multiple browser.
  18
+It allows to operate zope.testbrowser, PhantomJS or other Selenium brwowsers
  19
+such as Firefox or Chrome with the same API.
  20
+
  21
+The `ftw.testing` package provides integration of `Splinter`_ with Plone
  22
+using Page Objects.
  23
+
  24
+
  25
+Setting a package up for browser tests
  26
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  27
+
  28
+It's easy to setup your package for browser tests:
  29
+
  30
+- Add a test-dependency to `ftw.testing` in your `setup.py`:
  31
+
  32
+.. code:: python
  33
+
  34
+    tests_require = [
  35
+        'ftw.testing',
  36
+        ]
  37
+
  38
+    setup(name='my.package',
  39
+          ...
  40
+          tests_require=tests_require,
  41
+          extras_require=dict(tests=tests_require),
  42
+          )
  43
+
  44
+- In your `testing.py` use the `FunctionalSplinterTesting` layer wrapper:
  45
+
  46
+.. code:: python
  47
+
  48
+    from ftw.testing import FunctionalSplinterTesting
  49
+    from plone.app.testing import PLONE_FIXTURE
  50
+    from plone.app.testing import PloneSandboxLayer
  51
+    from plone.app.testing import applyProfile
  52
+    from zope.configuration import xmlconfig
  53
+
  54
+
  55
+    class MyPackageLayer(PloneSandboxLayer):
  56
+
  57
+        defaultBases = (PLONE_FIXTURE,)
  58
+
  59
+        def setUpZope(self, app, configurationContext):
  60
+            import my.package
  61
+            xmlconfig.file('configure.zcml', my.package)
  62
+
  63
+        def setUpPloneSite(self, portal):
  64
+            applyProfile(portal, 'my.package:default')
  65
+
  66
+
  67
+    MY_PACKAGE_FIXTURE = MyPackageLayer()
  68
+    MY_PACKAGE_FUNCTIONAL_TESTING = FunctionalSplinterTesting(
  69
+        bases=(MY_PACKAGE_FIXTURE, ),
  70
+        name="my.package:functional")
  71
+
  72
+- Write tests using the Plone Page Objects:
  73
+
  74
+.. code:: python
  75
+
  76
+    from ftw.testing import browser
  77
+    from ftw.testing import browser
  78
+    from ftw.testing.pages import Plone
  79
+    from my.package.testing import MY_PACKAGE_FUNCTIONAL_TESTING
  80
+    from plone.app.testing import SITE_OWNER_NAME
  81
+    from plone.app.testing import SITE_OWNER_PASSWORD
  82
+    from unittest2 import TestCase
  83
+
  84
+
  85
+    class TestDocument(TestCase):
  86
+
  87
+        layer = MY_PACKAGE_FUNCTIONAL_TESTING
  88
+
  89
+        def test_add_document(self):
  90
+            Plone().login(SITE_OWNER_NAME, SITE_OWNER_PASSWORD)
  91
+            Plone().visit_portal()
  92
+            Plone().create_object('Page', {'Title': 'Foo',
  93
+                                           'Body Text': '<b>Hello World</b>'})
  94
+            self.assertTrue(browser().is_text_present('Hello World'))
  95
+
  96
+
  97
+Writing Page Objects
  98
+~~~~~~~~~~~~~~~~~~~~
  99
+
  100
+Write your own Page Objects for your views and content types.
  101
+Put a module `pages.py` in your tests folder:
  102
+
  103
+.. code:: python
  104
+
  105
+    from ftw.testing.pages import Plone
  106
+
  107
+
  108
+    class MyContentType(Plone):
  109
+
  110
+        def create_my_content(self, title, text):
  111
+            self.create_object('MyContent', {'Title': title,
  112
+                                             'Body Text: text})
  113
+            return self
  114
+
  115
+The Page Object should have methods for all features of your view.
  116
+
  117
+
  118
+Switching to a JavaScript browser
  119
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  120
+
  121
+The default browser for JavaScript enabled tests is `PhantomJS`_.
  122
+PhantomJS is fast, headless but runs JS by using Gecko.
  123
+You should write JavaScript tests as few as possible but as much as
  124
+necessary, because it will get really slow when you have lots of them.
  125
+
  126
+Switching to the PhantomJS is done by just marking your test with the
  127
+`javascript` decorator:
  128
+
  129
+.. code:: python
  130
+
  131
+    from ftw.testing import javascript
  132
+
  133
+
  134
+    class TestDocument(TestCase):
  135
+
  136
+        @javascript
  137
+        def test_add_document(self):
  138
+            Plone().login(SITE_OWNER_NAME, SITE_OWNER_PASSWORD)
  139
+            Plone().visit_portal()
  140
+            Plone().create_object('Page', {'Title': 'Foo',
  141
+                                           'Body Text': '<b>Hello World</b>'})
  142
+            self.assertTrue(browser().is_text_present('Hello World'))
  143
+
  144
+
  145
+Using the Plone Page Objects
  146
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  147
+
  148
+The Plone page object provided by `ftw.testing` already has the most
  149
+important features built in, such as:
  150
+
  151
+- portal_url handling (the zope.testbrowser URL is different than the
  152
+  PhantomJS url)
  153
+- Login
  154
+- Accessing Headings, <body>-CSS-classes, status messages
  155
+- Adding content
  156
+- TinyMCE handling
  157
+
  158
+Currently it's best to just look in the
  159
+`page object code <https://github.com/4teamwork/ftw.testing/blob/jone-splinter/ftw/testing/pages.py>`.
  160
+
  161
+
  162
+
14 163
 MockTestCase
15 164
 ------------
16 165
 
@@ -171,3 +320,5 @@ This package is copyright by `4teamwork <http://www.4teamwork.ch/>`_.
171 320
 
172 321
 .. _plone.mocktestcase: http://pypi.python.org/pypi/plone.mocktestcase
173 322
 .. _plone.act: https://github.com/plone/plone.act
  323
+.. _Splinter: https://pypi.python.org/pypi/splinter
  324
+.. _PhantomJS: http://phantomjs.org/

0 notes on commit 8b7ed9d

Please sign in to comment.
Something went wrong with that request. Please try again.