some part of code is modified by juilen https://github.com/mozilla-b2g/js-test-agent/pull/53/files#diff-d3b883f9359d7d76f54e32e485240a1dR56. I add additional change at L117-L131