Fyne 测试:如何测试对话?
Fyne testing: how do I test dialogs?
我想进行 GUI 测试,但是对 test
包和 fyne
源的调查让我很失望。
有人能告诉我,有没有办法填充创建的对话框的文本字段并单击 'ok' 和 'cancel' 按钮来检查结果?
我看到的所有文档只建议将表单字段的创建移到 func
中,然后单独检查它们,是的,这是一个选项,但是否有可能将其检查为一个完整的对话框?
测试包不包含任何特定于与对话框交互的内容。大多数情况下,您会具体地与每个 window 进行交互,代码会简单得多。
也许遗漏了什么 - 请随时在此处提供更多信息或在 GitHub 项目上提出问题。
好的,我实施了一种方法,可以满足我的所有需求。
@andy.xyz mb 开箱即用会很有用。
我没有直接调用 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())
// other checks
下面是实现代码:
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}
}
我想进行 GUI 测试,但是对 test
包和 fyne
源的调查让我很失望。
有人能告诉我,有没有办法填充创建的对话框的文本字段并单击 'ok' 和 'cancel' 按钮来检查结果?
我看到的所有文档只建议将表单字段的创建移到 func
中,然后单独检查它们,是的,这是一个选项,但是否有可能将其检查为一个完整的对话框?
测试包不包含任何特定于与对话框交互的内容。大多数情况下,您会具体地与每个 window 进行交互,代码会简单得多。
也许遗漏了什么 - 请随时在此处提供更多信息或在 GitHub 项目上提出问题。
好的,我实施了一种方法,可以满足我的所有需求。
@andy.xyz mb 开箱即用会很有用。
我没有直接调用 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())
// other checks
下面是实现代码:
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}
}