# Macro XML to python

**URL:** <https://discourse.slicer.org/t/macro-xml-to-python/15803>\
**Category:** Development\
**Created:** [February 3, 2021, 1:10am UTC](https://discourse.slicer.org/t/macro-xml-to-python/15803 "2021-02-03T01:10:58Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [February 3, 2021, 8:22pm UTC](https://discourse.slicer.org/t/macro-xml-to-python/15803/2 "2021-02-03T20:22:07Z")

</div>

There are currently no way of recording macro as python script. As described below, there is a way to record python code but this would be very similar to the generated XML and challenging to maintain.

**Question** : What are you trying to achieve ? By understanding this, we may be able to better answer your question.

For now, I instead suggest to write [self test](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Tutorials/SelfTestModule) by hand.

There are a lot of python snippets available [here](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository), and the extension wizard will create a skeleton of module including the relevant infrastructure to run the test.

### Enabling macro recording

Answers in this post assumes you are talking about using [QtTesting](https://www.slicer.org/wiki/Documentation/Nightly/SlicerApplication/QtTesting) functionality that can be enabled through the settings:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/7/f713798341e9e2d4d146b129c41af929913f87a3.png)

### QtTesting and python

All of that said, we could look into generating python code similar to what is done in Paraview (see [here](https://www.paraview.org/ParaView/index.php/Testing_Guide#Python_based_testing)). The code would be similar to the one posted below:

```python
import QtTesting
import QtTestingImage
object1 = 'MainWindow/menubar/menuFile'
QtTesting.playCommand(object1, 'activate', 'actionFileLoadServerState')
object2 = 'MainWindow/ServerStartupBrowser/connect'
QtTesting.playCommand(object2, 'activate', )
object3 = 'MainWindow/FileLoadServerStateDialog'
QtTesting.playCommand(object3, 'filesSelected', '$PARAVIEW_DATA_ROOT/Data/LoadStateMultiView.pvsm')
snapshotWidget = 'MainWindow/1pqRenderWindowManager0/SplitterFrame/MultiViewSplitter/0/Viewport'
QtTestingImage.compareImage(snapshotWidget, 'LoadStateMultiView.png', 200, 200);

```

---

_[View the full topic](https://discourse.slicer.org/t/macro-xml-to-python/15803)._
