英文:
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("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}
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论