2017-05-01 20:49:55 -04:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-05-01 20:49:55 -04:00
|
|
|
|
2022-09-02 15:18:23 -04:00
|
|
|
package integration
|
2017-05-01 20:49:55 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-06-17 00:49:45 -04:00
|
|
|
"testing"
|
2017-05-01 20:49:55 -04:00
|
|
|
|
2017-05-07 10:40:31 -04:00
|
|
|
"github.com/PuerkitoBio/goquery"
|
2017-06-17 00:49:45 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 15:42:06 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-05-01 20:49:55 -04:00
|
|
|
)
|
|
|
|
|
2017-06-17 12:29:59 -04:00
|
|
|
// HTMLDoc struct
|
|
|
|
type HTMLDoc struct {
|
2017-05-07 10:40:31 -04:00
|
|
|
doc *goquery.Document
|
2017-05-01 20:49:55 -04:00
|
|
|
}
|
|
|
|
|
2017-06-17 12:29:59 -04:00
|
|
|
// NewHTMLParser parse html file
|
2017-12-03 17:46:01 -05:00
|
|
|
func NewHTMLParser(t testing.TB, body *bytes.Buffer) *HTMLDoc {
|
2019-07-29 00:15:18 -04:00
|
|
|
t.Helper()
|
2017-12-03 17:46:01 -05:00
|
|
|
doc, err := goquery.NewDocumentFromReader(body)
|
2024-07-30 15:42:06 -04:00
|
|
|
require.NoError(t, err)
|
2017-06-17 12:29:59 -04:00
|
|
|
return &HTMLDoc{doc: doc}
|
2017-05-01 20:49:55 -04:00
|
|
|
}
|
|
|
|
|
2017-06-17 12:29:59 -04:00
|
|
|
// GetInputValueByID for get input value by id
|
|
|
|
func (doc *HTMLDoc) GetInputValueByID(id string) string {
|
2017-05-07 10:40:31 -04:00
|
|
|
text, _ := doc.doc.Find("#" + id).Attr("value")
|
|
|
|
return text
|
2017-05-01 20:49:55 -04:00
|
|
|
}
|
|
|
|
|
2017-06-17 12:29:59 -04:00
|
|
|
// GetInputValueByName for get input value by name
|
|
|
|
func (doc *HTMLDoc) GetInputValueByName(name string) string {
|
2017-05-07 10:40:31 -04:00
|
|
|
text, _ := doc.doc.Find("input[name=\"" + name + "\"]").Attr("value")
|
|
|
|
return text
|
2017-05-01 20:49:55 -04:00
|
|
|
}
|
2017-06-17 00:49:45 -04:00
|
|
|
|
2020-10-28 18:33:14 -04: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)
|
|
|
|
}
|
|
|
|
|
2021-10-21 03:37:43 -04:00
|
|
|
// GetCSRF for getting CSRF token value from input
|
2017-06-17 12:29:59 -04:00
|
|
|
func (doc *HTMLDoc) GetCSRF() string {
|
2017-06-17 00:49:45 -04:00
|
|
|
return doc.GetInputValueByName("_csrf")
|
|
|
|
}
|
2017-09-12 02:48:13 -04:00
|
|
|
|
|
|
|
// AssertElement check if element by selector exists or does not exist depending on checkExists
|
|
|
|
func (doc *HTMLDoc) AssertElement(t testing.TB, selector string, checkExists bool) {
|
|
|
|
sel := doc.doc.Find(selector)
|
|
|
|
if checkExists {
|
|
|
|
assert.Equal(t, 1, sel.Length())
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, 0, sel.Length())
|
|
|
|
}
|
|
|
|
}
|