Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions axe_selenium_python/axe.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,8 @@ def write_results(self, data, name="results.json"):
:param name: Name of file to be written to.
:param output: JSON object.
"""
with open(name, "r", encoding="utf8") as f:
f.write(json.dumps(data, indent=4))
with open(name, "w", encoding="utf8") as f:
try:
f.write(unicode(json.dumps(data, indent=4)))
except NameError:
f.write(json.dumps(data, indent=4))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using json.dumps try json.dump as this returns a file-like object and shouldn't require the try/except.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm running into an error with py27 when using json.dump

TypeError: write() argument 1 must be unicode, not str

def test_write_results_to_file(tmpdir, mocker):
    axe = Axe(mocker.MagicMock())
    data = {"testKey": "testValue"}
    filename = path.join(str(tmpdir), "results.json")
    axe.write_results(data, filename)
def write_results(self, data, name="results.json"):
    """
    Write JSON to file with the specified name.
    :param name: Name of file to be written to.
    :param output: JSON object.
    """
    with open(name, "w", encoding="utf8") as f:
        json.dump(data, f)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, sorry - that sucks. You could consider using futures or six to make this code look a little nicer, but I'm not sure if it's worth the additional dependency.

1 change: 1 addition & 0 deletions axe_selenium_python/tests/requirements/tests.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pytest==3.8.2
selenium==3.14.1
pytest-html==1.19.0
pytest-mock==1.10.0
16 changes: 15 additions & 1 deletion axe_selenium_python/tests/test_axe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

from os import path, getenv
import json
from os import getenv, path

import pytest
from selenium import webdriver
Expand Down Expand Up @@ -62,3 +63,16 @@ def _perform_axe_run(driver):
axe.inject()
data = axe.execute()
return data


def test_write_results_to_file(tmpdir, mocker):
axe = Axe(mocker.MagicMock())
data = {"testKey": "testValue"}
filename = path.join(str(tmpdir), "results.json")

axe.write_results(data, filename)

with open(filename) as f:
actual_file_contents = json.loads(f.read())

assert data == actual_file_contents
12 changes: 3 additions & 9 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@ envlist = py27, py36, flake8
skipsdist = true

[testenv]
passenv = HOME DISPLAY MOZ_HEADLESS PYTEST_ADDOPTS PYTEST_BASE_URL \
SAUCELABS_USR SAUCELABS_PSW JENKINS_URL JOB_NAME BUILD_NUMBER
setenv =
MOZ_HEADLESS = 1
deps = -raxe_selenium_python/tests/requirements/tests.txt
commands = pytest

[testenv:py27]
commands = pytest

[testenv:py36]
commands = pytest
commands = pytest -rsx --verbose

[testenv:flake8]
deps = -raxe_selenium_python/tests/requirements/flake8.txt
Expand Down