Fyne测试:我如何测试对话框?

huangapple go评论92阅读模式
英文:

Fyne testing: how do I test dialogs?

问题

我想进行GUI测试,但是对于test包和fyne的源代码的调查让我很失望。

请问是否有一种方法可以填充创建的对话框中的文本字段,并点击“确定”和“取消”按钮来检查结果?

我看到的所有文档都只建议将表单字段的创建移到一个func中,然后逐个检查它们,是的,这是一个选择,但是否有可能作为一个完整的对话框进行检查?

英文:

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?

答案1

得分: 1

好的,我已经实现了一个满足我需求的方法。

@andy.xyz,也许将其作为默认实现会很有用。

我不直接调用dialog.NewForm,而是这样做:

var newForm = dialog.NewForm

然后,我使用相同的参数调用newForm,如下所示:

	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)

测试代码如下:

	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())
    // 其他检查

以下是实现的代码:

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}
}
英文:

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(&quot;Name&quot;, name)
	active := widget.NewCheck()
	eActive := widget.NewFormItem(&quot;Active&quot;, active)
	d := newForm(&quot;A dialog&quot;, &quot;OK&quot;, &quot;Cancel&quot;, []*widget.FormItem{eName, eActive}, func(b bool) {}, w)

A test looks like this:

	newForm = testNewForm

	assert.Equal(t, &quot;A dialog&quot;, lastTestDialog.getTitle())
	assert.Equal(t, &quot;OK&quot;, lastTestDialog.getConfirm())
	assert.Equal(t, &quot;Cancel&quot;, lastTestDialog.getDismiss())

	lastTestDialog.setText(t, &quot;Name&quot;, &quot;some name&quot;)
	lastTestDialog.setCheck(t, &quot;Active&quot;, true)
	lastTestDialog.tapOk()
	assert.Equal(t, false, lastTestDialog.isValid())
    // other checks

And here's the code of the implementation:

package main

import (
	&quot;fmt&quot;
	&quot;fyne.io/fyne/v2&quot;
	&quot;fyne.io/fyne/v2/dialog&quot;
	&quot;fyne.io/fyne/v2/widget&quot;
	&quot;testing&quot;
)

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(&quot;there&#39;s no widget with name %s&quot;, name)
		return
	}
	b, ok := wi.(*widget.Button)
	if !ok {
		t.Errorf(&quot;widget &#39;%s&#39; isn&#39;t a button&quot;, 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 &lt; 1 {
			continue
		}
		if l == 1 {
			widgets[i.Text] = widgetsForItem[0]
			continue
		}
		for x, wi := range widgetsForItem {
			widgets[fmt.Sprintf(&quot;%s-%d&quot;, i.Text, x)] = wi
		}
	}
	lastTestDialog = &amp;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}
}

答案2

得分: 0

测试包中没有包含与对话交互相关的特定内容。大多数情况下,您需要与每个窗口进行特定的交互,代码会变得更简单。

也许有些东西被忽略了 - 欢迎在此提供更多信息或在GitHub项目上提出问题。

英文:

The test package does not include anything specific for interacting with dialogs. Mostly you interact with each window specifically, the code works out much simpler.

Maybe something has been missed - feel free to provide more info here or open an issue on the GitHub project.

huangapple
  • 本文由 发表于 2022年2月2日 08:23:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/70949252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定