I want to make GUI testing, but investigation of test
package and sources of fyne
disappoints a lot.
Could somebody please tell, is there a way to fill text fields of a created dialog and click 'ok' and 'cancel' buttons to check results?
All docs I saw only suggest to move creating of form fields out to a func
and check them out then individually, and, yes, that's an option, but is there a possibility to check it out as a complete dialog?
Ok, I implemented an approach which does everything what I need.
@andy.xyz mb it would be useful to implement this out of the box.
Instead of calling dialog.NewForm
directly I do this:
var newForm = dialog.NewForm
and afterwards I call newForm
with the same arguments, as follows:
name := widget.NewEntry()
eName := widget.NewFormItem("Name", name)
active := widget.NewCheck()
eActive := widget.NewFormItem("Active", active)
d := newForm("A dialog", "OK", "Cancel", []*widget.FormItem{eName, eActive}, func(b bool) {}, w)
A test looks like this:
newForm = testNewForm
assert.Equal(t, "A dialog", lastTestDialog.getTitle())
assert.Equal(t, "OK", lastTestDialog.getConfirm())
assert.Equal(t, "Cancel", lastTestDialog.getDismiss())
lastTestDialog.setText(t, "Name", "some name")
lastTestDialog.setCheck(t, "Active", true)
lastTestDialog.tapOk()
assert.Equal(t, false, lastTestDialog.isValid())
// other checks
And here's the code of the implementation:
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
"testing"
)
type testDialog struct {
title string
confirm string
dismiss string
widgets map[string]fyne.CanvasObject
callback func(bool)
invalid bool
}
func (d *testDialog) Show() {}
func (d *testDialog) Hide() {}
func (d *testDialog) SetDismissText(string) {}
func (d *testDialog) SetOnClosed(func()) {}
func (d *testDialog) Refresh() {}
func (d *testDialog) Resize(fyne.Size) {}
func (d *testDialog) MinSize() fyne.Size {
return fyne.Size{}
}
func (d *testDialog) getTitle() string {
return d.title
}
func (d *testDialog) getConfirm() string {
return d.confirm
}
func (d *testDialog) getDismiss() string {
return d.dismiss
}
func (d *testDialog) isValid() bool {
return !d.invalid
}
func (d *testDialog) tapOk() {
d.invalid = false
for _, wi := range d.widgets {
if w, ok := wi.(fyne.Validatable); ok {
if e := w.Validate(); e != nil {
d.invalid = true
break
}
}
}
if !d.invalid {
d.callback(true)
}
}
func (d *testDialog) tapCancel() {
d.callback(false)
}
func (d *testDialog) setText(t *testing.T, name string, text string) {
wi, ok := d.widgets[name]
if !ok {
t.Fail()
return
}
e, ok := wi.(*widget.Entry)
if !ok {
t.Fail()
return
}
e.SetText(text)
}
func (d *testDialog) setCheck(t *testing.T, name string, check bool) {
wi, ok := d.widgets[name]
if !ok {
t.Fail()
return
}
c, ok := wi.(*widget.Check)
if !ok {
t.Fail()
return
}
c.Checked = check
}
func (d *testDialog) tapButton(t *testing.T, name string) {
t.Helper()
wi, ok := d.widgets[name]
if !ok {
t.Errorf("there's no widget with name %s", name)
return
}
b, ok := wi.(*widget.Button)
if !ok {
t.Errorf("widget '%s' isn't a button", name)
return
}
b.OnTapped()
}
var lastTestDialog *testDialog = nil
func testNewForm(title, confirm, dismiss string, items []*widget.FormItem, callback func(bool), _ fyne.Window) dialog.Dialog {
widgets := make(map[string]fyne.CanvasObject)
for _, i := range items {
widgetsForItem := digWidgets(i.Widget)
l := len(widgetsForItem)
if l < 1 {
continue
}
if l == 1 {
widgets[i.Text] = widgetsForItem[0]
continue
}
for x, wi := range widgetsForItem {
widgets[fmt.Sprintf("%s-%d", i.Text, x)] = wi
}
}
lastTestDialog = &testDialog{title: title, confirm: confirm, dismiss: dismiss, widgets: widgets, callback: callback}
return lastTestDialog
}
func digWidgets(root fyne.CanvasObject) []fyne.CanvasObject {
if cnt, ok := root.(*fyne.Container); ok {
var widgets []fyne.CanvasObject
for _, o := range cnt.Objects {
widgets = append(widgets, digWidgets(o)...)
}
return widgets
}
return []fyne.CanvasObject{root}
}