mozilla

Revision 53841 of Marionette for Interactive Python

  • Revision slug: Mozilla/Boot_to_Gecko/Marionette_for_Interactive_Python
  • Revision title: Marionette for Interactive Python
  • Revision id: 53841
  • Created:
  • Creator: jhammink
  • Is current revision? No
  • Comment 221 words added

Revision Content

This tutorial assumes you've run the previous one.

In addition to the port-forwarding terminal you've opened, open a second terminal and launch Python to get to the interactive prompt:

$ python

From the interactive prompt, run the commands necessary to invoke a Marionette session interactively:

>>> from marionette import Marionette
>>> marionette = Marionette('localhost', 2828)
>>> marionette.start_session()
 u'session-b2g'

Here, we see that the system returns that a Marionette session is running.

The command "marionette.execute_script()" can embed JavaScript commands, which can then run on B2G's Gecko platform.  Using this, you can see which DOM elements return HTMLElement objects and available attributes and methods:

>>> marionette.execute_script("return navigator.mozBattery;")
{u'onlevelchange': None, u'level': 0.91, u'dischargingTime': None, u'onchargingchange': None, u'ondischargingtimechange': None, u'onchargingtimechange': None, u'chargingTime': None, u'charging': True}
>>> marionette.execute_script("return navigator.mozBattery.level;")
0.91
>>> marionette.execute_script("return navigator.geolocation;")
{}
>>> marionette.execute_script("return navigator.mozSms;")
{u'onreceived': None, u'ondelivered': None, u'onsent': None}

You can traverse the DOM tree using this technique to evaluate which objects, methods, and attributes are available.

Testing Basic Telephony interactively

you can test out basic telephony interactively with marionette.  The following example requires two working phones, each with simcards.  One is your Galaxy SII, with B2G running.

Start an interactive Marionette session and forward the port:

$ adb forward tcp:2828 tcp:2828
$ python
>>> from marionette import Marionette
>>> marionette = Marionette('localhost', 2828)
>>> marionette.start_session()
u'5-b2g

Now, you have two possible approaches with marionette.  One is a little more Pythonic:

>>> marionette.set_context("chrome")
True
>>> marionette.execute_script("return navigator.mozTelephony;")
>>> num =
>>> marionette.execute_script("return navigator.mozTelephony.dial('%d');" % num)

Or else, this approach, which relies more on JS (embedded within marionette.execute_script() ) Notice the quotes around the JS number variable:

>>> marionette.set_context("chrome")
True
>>> marionette.execute_script("""
... var num = ""
... return navigator.mozTelephony.dial(num);
... """)
{}

We'll try the first approach:

This kicks off a phone call, the output of which can be monitored in $adb logcat

Revision Source

<p>This tutorial assumes you've run the <a href="/en/Mozilla/Boot_to_Gecko/Setting_Up_Marionette_for_B2G" title="https://developer.mozilla.org/en/Mozilla/Boot_to_Gecko/Setting_Up_Marionette_for_B2G">previous</a> one.</p>
<p>In addition to the port-forwarding terminal you've opened, open a second terminal and launch Python to get to the interactive prompt:</p>
<p><code>$ python</code></p>
<p>From the interactive prompt, run the commands necessary to invoke a Marionette session interactively:</p>
<p><span style='font-family: "Courier New",Courier,monospace;'>&gt;&gt;&gt; from marionette import Marionette<br> &gt;&gt;&gt; marionette = Marionette('localhost', 2828)<br> &gt;&gt;&gt; marionette.start_session()<br>  u'session-b2g'</span></p>
<p>Here, we see that the system returns that a Marionette session is running.</p>
<p><a class="external" href="http://4.bp.blogspot.com/-wdjTWI_UrH0/Tys1t-VWTMI/AAAAAAAAANM/Hb3pLdPOoMc/s640/Starting+Marionette+Session+Interactively.tiff"><img alt="" class="default" src="http://4.bp.blogspot.com/-wdjTWI_UrH0/Tys1t-VWTMI/AAAAAAAAANM/Hb3pLdPOoMc/s640/Starting+Marionette+Session+Interactively.tiff"></a></p>
<p>The command "<code>marionette.execute_script()</code>" can embed JavaScript commands, which can then run on B2G's Gecko platform.  Using this, you can see which DOM elements return HTMLElement objects and available attributes and methods:</p>
<p><span style='font-family: "Courier New",Courier,monospace;'>&gt;&gt;&gt; marionette.execute_script("return navigator.mozBattery;")<br> {u'onlevelchange': None, u'level': 0.91, u'dischargingTime': None, u'onchargingchange': None, u'ondischargingtimechange': None, u'onchargingtimechange': None, u'chargingTime': None, u'charging': True}<br> &gt;&gt;&gt; marionette.execute_script("return navigator.mozBattery.level;")<br> 0.91<br> &gt;&gt;&gt; marionette.execute_script("return navigator.geolocation;")<br> {}<br> &gt;&gt;&gt; marionette.execute_script("return navigator.mozSms;")<br> {u'onreceived': None, u'ondelivered': None, u'onsent': None}</span></p>
<p><a class="external" href="http://2.bp.blogspot.com/-WaSbVYa85K0/Tys2-IJC3ZI/AAAAAAAAANU/s7wvpanwmNY/s640/Getting+DOMHTMLelements.tiff"><img alt="" class="default" src="http://2.bp.blogspot.com/-WaSbVYa85K0/Tys2-IJC3ZI/AAAAAAAAANU/s7wvpanwmNY/s640/Getting+DOMHTMLelements.tiff"></a></p>
<p>You can traverse the DOM tree using this technique to evaluate which objects, methods, and attributes are available.</p>
<h2 id="Testing_Basic_Telephony_interactively">Testing Basic Telephony interactively</h2>
<p>you can test out basic telephony interactively with marionette.  The following example requires two working phones, each with simcards.  One is your Galaxy SII, with B2G running.</p>
<p>Start an interactive Marionette session and forward the port:</p>
<p><code>$ adb forward tcp:2828 tcp:2828<br> $ python<br> &gt;&gt;&gt; from marionette import Marionette<br> &gt;&gt;&gt; marionette = Marionette('localhost', 2828)<br> &gt;&gt;&gt; marionette.start_session()<br> u'5-b2g</code></p>
<p>Now, you have two possible approaches with marionette.  One is a little more Pythonic:</p>
<p><code>&gt;&gt;&gt; marionette.set_context("chrome")<br> True<br> &gt;&gt;&gt; marionette.execute_script("return navigator.mozTelephony;")<br> &gt;&gt;&gt; num =<br> &gt;&gt;&gt; marionette.execute_script("return navigator.mozTelephony.dial('%d');" % num)</code></p>
<p>Or else, this approach, which relies more on JS (embedded within <code>marionette.execute_script()</code> ) Notice the quotes around the JS number variable:</p>
<p><code>&gt;&gt;&gt; marionette.set_context("chrome")<br> True<br> &gt;&gt;&gt; marionette.execute_script("""<br> ... var num = ""<br> ... return navigator.mozTelephony.dial(num);<br> ... """)<br> {} </code></p>
<p>We'll try the first approach:</p>
<p><a class=" external" href="http://3.bp.blogspot.com/-cAsP3Beif4g/Tyxr92r6baI/AAAAAAAAANk/GiMlZsXfFYc/s640/Marionette_interactive_telephony.tiff"><img alt="" class="default" src="http://3.bp.blogspot.com/-cAsP3Beif4g/Tyxr92r6baI/AAAAAAAAANk/GiMlZsXfFYc/s640/Marionette_interactive_telephony.tiff"></a></p>
<p>This kicks off a phone call, the output of which can be monitored in <code>$adb logcat</code></p>
<p><a class=" external" href="http://4.bp.blogspot.com/-Np12ZkqpRfM/Tyxs3zAf0EI/AAAAAAAAANs/xhr58eU_s00/s640/ADB+LOGCAT+interactive+telephony.tiff"><img alt="" class="default" src="http://4.bp.blogspot.com/-Np12ZkqpRfM/Tyxs3zAf0EI/AAAAAAAAANs/xhr58eU_s00/s640/ADB+LOGCAT+interactive+telephony.tiff"></a></p>
Revert to this revision