0

我想做 GUI 测试,但是对test软件包和来源的调查fyne令人失望。

有人可以告诉我,有没有办法填充已创建对话框的文本字段并单击“确定”和“取消”按钮来检查结果?

我看到的所有文档只建议将表单字段的创建移到 afunc并单独检查它们,是的,这是一个选项,但是有可能将它作为一个完整的对话框检查吗?

4

2 回答 2

0

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

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

于 2022-02-02T05:18:57.413 回答
0

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

@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}
}
于 2022-02-06T00:34:31.913 回答