2017-05-02 03:49:55 +03:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-05-02 03:49:55 +03:00
|
|
|
|
2022-09-02 15:18:23 -04:00
|
|
|
package integration
|
2017-05-02 03:49:55 +03:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2017-06-17 00:49:45 -04:00
|
|
|
"testing"
|
2017-05-02 03:49:55 +03:00
|
|
|
|
2017-05-07 17:40:31 +03:00
|
|
|
"github.com/PuerkitoBio/goquery"
|
2017-06-17 00:49:45 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-05-02 03:49:55 +03:00
|
|
|
)
|
|
|
|
|
|
2017-06-17 11:29:59 -05:00
|
|
|
// HTMLDoc struct
|
|
|
|
|
type HTMLDoc struct {
|
2017-05-07 17:40:31 +03:00
|
|
|
doc *goquery.Document
|
2017-05-02 03:49:55 +03:00
|
|
|
}
|
|
|
|
|
|
2017-06-17 11:29:59 -05:00
|
|
|
// NewHTMLParser parse html file
|
2017-12-03 14:46:01 -08:00
|
|
|
func NewHTMLParser(t testing.TB, body *bytes.Buffer) *HTMLDoc {
|
2019-07-29 06:15:18 +02:00
|
|
|
t.Helper()
|
2017-12-03 14:46:01 -08:00
|
|
|
doc, err := goquery.NewDocumentFromReader(body)
|
2017-06-17 00:49:45 -04:00
|
|
|
assert.NoError(t, err)
|
2017-06-17 11:29:59 -05:00
|
|
|
return &HTMLDoc{doc: doc}
|
2017-05-02 03:49:55 +03:00
|
|
|
}
|
|
|
|
|
|
2017-06-17 11:29:59 -05:00
|
|
|
// GetInputValueByName for get input value by name
|
|
|
|
|
func (doc *HTMLDoc) GetInputValueByName(name string) string {
|
2024-12-05 15:07:53 +02:00
|
|
|
text, _ := doc.doc.Find(`input[name="` + name + `"]`).Attr("value")
|
2017-05-07 17:40:31 +03:00
|
|
|
return text
|
2017-05-02 03:49:55 +03:00
|
|
|
}
|
2017-06-17 00:49:45 -04:00
|
|
|
|
2020-10-28 22:33:14 +00:00
|
|
|
// Find gets the descendants of each element in the current set of
|
|
|
|
|
// matched elements, filtered by a selector. It returns a new Selection
|
|
|
|
|
// object containing these matched elements.
|
|
|
|
|
func (doc *HTMLDoc) Find(selector string) *goquery.Selection {
|
|
|
|
|
return doc.doc.Find(selector)
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-22 14:43:43 +02:00
|
|
|
// AssertHTMLElement check if the element by selector exists or does not exist depending on checkExists
|
2025-03-14 15:45:11 +08:00
|
|
|
func AssertHTMLElement[T int | bool](t testing.TB, doc *HTMLDoc, selector string, checkExists T) {
|
2017-09-12 08:48:13 +02:00
|
|
|
sel := doc.doc.Find(selector)
|
2025-03-14 15:45:11 +08:00
|
|
|
switch v := any(checkExists).(type) {
|
|
|
|
|
case bool:
|
|
|
|
|
assert.Equal(t, v, sel.Length() > 0)
|
|
|
|
|
case int:
|
|
|
|
|
assert.Equal(t, v, sel.Length())
|
2017-09-12 08:48:13 +02:00
|
|
|
}
|
|
|
|
|
}
|