diff --git a/.travis.yml b/.travis.yml index b14a6b4a7..3cb04d5e6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -74,7 +74,13 @@ before_script: - echo $INTERPRETER - $INTERPRETER --version - $INTERPRETER -m pip install . - - $INTERPRETER -m pip install -r requirements-dev.txt + - if [ "$JYTHON" == "true" ]; then + $INTERPRETER -m pip install mockito; + $INTERPRETER -m pip install robotstatuschecker; + $INTERPRETER -m pip install -r requirements.txt; + else + $INTERPRETER -m pip install -r requirements-dev.txt; + fi - $INTERPRETER -m pip install selenium==$SELENIUM - $INTERPRETER -m pip install robotframework==$ROBOTFRAMEWORK script: diff --git a/requirements-dev.txt b/requirements-dev.txt index bddcaaa75..21f70296d 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -3,6 +3,7 @@ mockito >= 1.0.0 robotstatuschecker +approvaltests >= 0.2.3 # Include normal dependencies from requirements.txt. Makes it possible to use # requirements-dev.txt as a single requirement file in PyCharm and other IDEs. diff --git a/src/SeleniumLibrary/utils/platform.py b/src/SeleniumLibrary/utils/platform.py new file mode 100644 index 000000000..6a6dbe981 --- /dev/null +++ b/src/SeleniumLibrary/utils/platform.py @@ -0,0 +1,4 @@ +import sys + +# Can be removed when Robot Framework 2.8.4 is not supported. +JYTHON = sys.platform.startswith('java') diff --git a/utest/README.rst b/utest/README.rst index c40c482bb..fdc46b683 100644 --- a/utest/README.rst +++ b/utest/README.rst @@ -15,9 +15,25 @@ Unit test can be executed by running:: The utest directory contains everything needed to run SeleniumLibrary unit tests. This includes: - - Unit test in `test` folder. - - Unit test runner: `run.py` + +- Unit test in `test` folder. +- Unit test runner: `run.py` Unit test are executed using the interpreter which starts the `run.py` script. +ApprovalTests +------------- +For unit test, it is possible to use `ApprovalTests`_ framework. ApprovalTests +provides an easy and visual way to compare strings in unit tests. For more +details, please read `ApprovalTests`_ documentation and `ApprovalTests blog post`_. +The downside of ApprovalTests is that it does not work when using `Jython`_ +as an interpreter. Therefore all unit tests using ApprovalTests imports +must handled with `try/except ImportError:` and skipped with: +`@unittest.skipIf(JYTHON, 'ApprovalTest does not work with Jython')`. The `JYTHON` is +imported from `from robot.utils import JYTHON` + + .. _unittest: https://docs.python.org/3/library/unittest.html +.. _ApprovalTests: https://github.com/approvals/ApprovalTests.Python +.. _ApprovalTests blog post: http://blog.approvaltests.com/ +.. _Jython: http://www.jython.org/ diff --git a/utest/test/approvals_reporters.json b/utest/test/approvals_reporters.json new file mode 100644 index 000000000..3ac5beb63 --- /dev/null +++ b/utest/test/approvals_reporters.json @@ -0,0 +1,6 @@ +[ + [ + "Diffuse", + "/usr/bin/diffuse" + ] +] \ No newline at end of file diff --git a/utest/test/keywords/approvaltests_config.json b/utest/test/keywords/approvaltests_config.json new file mode 100644 index 000000000..1ceea7eb0 --- /dev/null +++ b/utest/test/keywords/approvaltests_config.json @@ -0,0 +1,3 @@ +{ + "subdirectory": "approved_files" +} \ No newline at end of file diff --git a/utest/test/keywords/approved_files/JavaScriptKeywordsTest.test_get_javascript.approved.txt b/utest/test/keywords/approved_files/JavaScriptKeywordsTest.test_get_javascript.approved.txt new file mode 100644 index 000000000..140a65a11 --- /dev/null +++ b/utest/test/keywords/approved_files/JavaScriptKeywordsTest.test_get_javascript.approved.txt @@ -0,0 +1 @@ +code here \ No newline at end of file diff --git a/utest/test/keywords/test_javascript.py b/utest/test/keywords/test_javascript.py new file mode 100644 index 000000000..32bac4ed4 --- /dev/null +++ b/utest/test/keywords/test_javascript.py @@ -0,0 +1,33 @@ +import os +import unittest + +from SeleniumLibrary.utils.platform import JYTHON +try: + from approvaltests.approvals import verify + from approvaltests.reporters.generic_diff_reporter_factory import GenericDiffReporterFactory +except ImportError: + if JYTHON: + verify = None + GenericDiffReporterFactory = None + else: + raise + +from SeleniumLibrary.keywords import JavaScriptKeywords + + +class JavaScriptKeywordsTest(unittest.TestCase): + + @classmethod + @unittest.skipIf(JYTHON, 'ApprovalTest does not work with Jython') + def setUpClass(cls): + cls.javascript = JavaScriptKeywords(None) + path = os.path.dirname(__file__) + reporter_json = os.path.abspath(os.path.join(path, '..', 'approvals_reporters.json')) + factory = GenericDiffReporterFactory() + factory.load(reporter_json) + cls.reporter = factory.get_first_working() + + @unittest.skipIf(JYTHON, 'ApprovalTest does not work with Jython') + def test_get_javascript(self): + code = self.javascript._get_javascript_to_execute('code here') + verify(code, self.reporter) \ No newline at end of file