Fyne测试:如何测试对话框



我想进行GUI测试,但对test包和fyne源的调查让人失望。

有人能告诉我,有没有办法填写创建的对话框的文本字段,然后单击"确定"one_answers"取消"按钮来检查结果?

我看到的所有文档都只建议将表单字段的创建转移到func中,然后单独签出,是的,这是一个选项,但有可能将其作为一个完整的对话框签出吗?

好的,我实现了一种方法,它可以满足我的所有需求。

@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}
}

测试包不包含任何特定于与对话框交互的内容。大多数情况下,您都是专门与每个窗口进行交互的,代码会简单得多。

也许遗漏了什么-请随时在这里提供更多信息或在GitHub项目上打开一个问题。

相关内容

  • 没有找到相关文章

最新更新