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:

AXE.java

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) {
      sb.append(line);
      sb.append(lineSeparator);
    }
  } catch (Exception e) {
    throw new RuntimeException(e);
  } finally {
    if (reader != null) {
      try { reader.close(); }
              catch (IOException ignored) {}
    }
  }

  return sb.toString();
}

axe.py

def get_contents(self, script_url):
    with open(script_url, 'r') as file:
        script = file.read().replace('\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 axe.py based off of AXE.java in axe-selenium-java.

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

axe.py

# 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 http://mozilla.org/MPL/2.0/.


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 axe.py, 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.

axe.py

    def get_contents(self, script_url):
        with open(script_url, 'r') as file:
            script = file.read().replace('\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.

test_get_script_contents.py

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 a software engineer, and currently works as a Test Engineering Intern for Mozilla.

While she always enjoys learning new technologies, her current focus is python, and when she has free time (she doesn't), Angular & Node.

When not coding, Kimberly spends time with her three young boys in Durango, CO.