Mozilla Internship: Translate Java to Python, Pt. 1

Translating a Java package into Python

This is a pretty interesting task for me, to translate Java to Python, and is currently my focus in the internship.

What I have found so far is that most of the methods will be much easier to implement in Python than in Java.

I’m glad I’m interpreting Java into Python…and not the reverse!

Take, for example, the get_contents() function:

private static String getContents(final URL script) {
  final StringBuilder sb = new StringBuilder();
  BufferedReader reader = null;

  try {
    URLConnection connection = script.openConnection();
    reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String line;

    while ((line = reader.readLine()) != null) {
  } catch (Exception e) {
    throw new RuntimeException(e);
  } finally {
    if (reader != null) {
      try { reader.close(); }
              catch (IOException ignored) {}

  return sb.toString();

def get_contents(self, script_url):
    with open(script_url, 'r') as file:
        script ='\n', '')
    return script

I’ve also been familiarizing myself with object oriented programming again.

My first programming language was Java, but I haven’t touched it in several years.

Being fairly new to python, and very new to python package development, I have been following a tutorial on writing & distributing python packages.

I’ve also been able to reach out to several members of the Firefox Test Engineering team who have experience in package development, who have all been very helpful.

Drafting the Methods and Docstrings

After setting up the core files and structure, the first step was to create a skeleton of based off of in axe-selenium-java.

This basically consisted of the class names, method names, and docstrings describing the behavior of each method.

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at

class Axe:

    def get_contents(self, script_url):
        Return contents of the axe.js or axe.min.js script.

        :param script_url: URL of the axe-core script.
        :type script_url: string
        :return: contents of script.
        :rtype: string

    def inject(driver, script_url):
        Recursively inject aXe into all iframes and the top level document.

        :param driver: WebDriver instance to inject script into.
        :param script_url: URL of the axe-core script.
        :type driver: WebDriver
        :type script_url: string

. . .

Implement a Method and Test It!

The next step was to implement the most simple method (which was actually quite simple!)

This is the first method in, the get_contents() method.

This method takes one parameter, an address of a script (the axe-core script), opens the file, and returns its contents as a string.

    def get_contents(self, script_url):
        with open(script_url, 'r') as file:
            script ='\n', '')
        return script

After implemented it, I wrote a very simple test case for it, just to verify that the object returned is indeed a string.

from unittest import TestCase

from axe_selenium_python import Axe

class TestGetScriptContents(TestCase):

    def test_is_string(self):
        a = Axe()
        script = a.get_contents('axe_selenium_python/src/test/resources/axe.min.js')
        self.assertTrue(isinstance(script, basestring))

There is still a long way to go, but I find the learning to be a very exciting process.

I later decided that the get_contents() method is superfluous in Python, and took it out altogether.

I was able to implement the function of both the get_contents() method and the inject() method in a single line of code.

I enjoy seeing both my knowledge and my progress grow quickly in a short amount of time.

Especially when I ask questions that my mentor can’t answer!

Continued in Part 2

Syntax Highlighting by EnlighterJS

About the Author

Kimberly is currently a Test Automation Engineer for Mozilla, with a focus on automating web accessibility testing.

While she always enjoys learning new technologies, her current focus is python, and when she has free time (she doesn't), she enjoys JavaScript programming and learning about Data Analysis.

When not coding, Kimberly spends time with her three young boys, brand new baby girl, and her partner, in Durango, CO.