# Access widget class from the test class in a Slicer plugin

**URL:** <https://discourse.slicer.org/t/access-widget-class-from-the-test-class-in-a-slicer-plugin/11585>\
**Category:** Support\
**Created:** [May 17, 2020, 9:45am UTC](https://discourse.slicer.org/t/access-widget-class-from-the-test-class-in-a-slicer-plugin/11585 "2020-05-17T09:45:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)\
**Post date:** [May 17, 2020, 9:45am UTC](https://discourse.slicer.org/t/access-widget-class-from-the-test-class-in-a-slicer-plugin/11585/1 "2020-05-17T09:45:13Z")

</div>

Dear all,

Is it possible to access a widget class element from the test class in a slicer plugin? I know this is not how the test class works but I am controlling the logic functions with an extra variable taht indicates if the call from the widget class or from the test class.

When I try this:

```
class myClassTest(ScriptedLoadableModuleTest):
     def setUp(self):
           slicer.mrmlScene.Clear(0)   
           self.logic = myClassLogic()
           self.widg = myClassWidget()

```

It creates a new instance of the widget instead of accessing the current widget.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [May 17, 2020, 1:22pm UTC](https://discourse.slicer.org/t/access-widget-class-from-the-test-class-in-a-slicer-plugin/11585/2 "2020-05-17T13:22:36Z")

</div>

I don’t believe there is a direct way to access it from the test class but you can get it through the slicer module:

`volumeRenderingWidget = slicer.modules.volumerendering.widgetRepresentation()`

---

<div class="post-metadata">

**Author:** ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)\
**Post date:** [May 18, 2020, 4:31pm UTC](https://discourse.slicer.org/t/access-widget-class-from-the-test-class-in-a-slicer-plugin/11585/3 "2020-05-18T16:31:09Z")

</div>

Thanks for your quick answer.

---

<div class="post-metadata">

**Author:** ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)\
**Post date:** [May 19, 2020, 1:18pm UTC](https://discourse.slicer.org/t/access-widget-class-from-the-test-class-in-a-slicer-plugin/11585/4 "2020-05-19T13:18:49Z")

</div>

It seems the widget get new instance every time one click reload and test without destroying the old instance. This code gets the last instance:

In the widget class:

```
    self.mainCollapsibleBtn = ctk.ctkCollapsibleButton()
    self.mainCollapsibleBtn.setWindowTitle("mainCollapsibleBtn")

```

In the test class:

```
  widg = slicer.modules.myModule.widgetRepresentation()
  wObjs = [x for x in widg.children()[1:] if x.windowTitle =="mainCollapsibleBtn"][-1].children()
  #getting the classes information
  for x in wObjs[1:]:
      print(x.className())
  #endfor x

```

By getting the classes information one can access the desirable class e.g.

```
   myButton = wObjs[3]
```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [May 19, 2020, 11:41pm UTC](https://discourse.slicer.org/t/access-widget-class-from-the-test-class-in-a-slicer-plugin/11585/5 "2020-05-19T23:41:11Z")

</div>

> [@brhoom](#):
>
> It seems the widget get new instance every time one click reload and test without destroying the old instance.

Slicer releases all references to the old widget, but if you keep references to it then it will not be destroyed. It is a debug/development feature anyway, so you don’t need worry about old widgets hanging around (if it becomes to confusing then just restart the application), but it makes sense to at least [remove all observers in the widget’s `cleanup()` method](https://github.com/Slicer/Slicer/blob/a18612bb2584018822347ff4db16439b5c578e00/Utilities/Templates/Modules/Scripted/TemplateKey.py#L104-L108).

By the way, you should never ever need to use `children()` or `findWidget` on your own module’s GUI. You have direct access to all widgets (something like `module.xyz.widgetRepresentation().self().ui.abcWidget`, if you follow the current Python scripted module template).
