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"
|
2024-07-16 14:42:35 +00:00
|
|
|
"fmt"
|
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"
|
2024-07-30 19:41:10 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
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)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2017-06-17 11:29:59 -05:00
|
|
|
return &HTMLDoc{doc: doc}
|
2017-05-02 03:49:55 +03:00
|
|
|
}
|
|
|
|
|
2025-09-05 06:55:32 +02:00
|
|
|
func (doc *HTMLDoc) AssertAttrPredicate(t testing.TB, selector, attr string, predicate func(attrValue string) bool) bool {
|
2025-08-25 19:33:40 +02:00
|
|
|
t.Helper()
|
|
|
|
selection := doc.doc.Find(selector)
|
|
|
|
require.NotEmpty(t, selection, selector)
|
|
|
|
|
|
|
|
actual, exists := selection.Attr(attr)
|
|
|
|
require.True(t, exists, "%s not found in %s", attr, selection.Text())
|
|
|
|
|
2025-09-05 06:55:32 +02:00
|
|
|
return predicate(actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (doc *HTMLDoc) AssertAttrEqual(t testing.TB, selector, attr, expected string) bool {
|
|
|
|
t.Helper()
|
|
|
|
return doc.AssertAttrPredicate(t, selector, attr, func(actual string) bool {
|
|
|
|
return assert.Equal(t, expected, actual)
|
|
|
|
})
|
2025-08-25 19:33:40 +02:00
|
|
|
}
|
|
|
|
|
2017-06-17 11:29:59 -05:00
|
|
|
// GetInputValueByID for get input value by id
|
|
|
|
func (doc *HTMLDoc) GetInputValueByID(id string) string {
|
2017-05-07 17:40:31 +03:00
|
|
|
text, _ := doc.doc.Find("#" + id).Attr("value")
|
|
|
|
return text
|
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 {
|
2017-05-07 17:40:31 +03:00
|
|
|
text, _ := doc.doc.Find("input[name=\"" + name + "\"]").Attr("value")
|
|
|
|
return text
|
2017-05-02 03:49:55 +03:00
|
|
|
}
|
2017-06-17 00:49:45 -04:00
|
|
|
|
2024-07-16 14:42:35 +00:00
|
|
|
func (doc *HTMLDoc) AssertDropdown(t testing.TB, name string) *goquery.Selection {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
dropdownGroup := doc.Find(fmt.Sprintf(".dropdown:has(input[name='%s'])", name))
|
2024-08-14 11:43:42 +02:00
|
|
|
assert.Equal(t, 1, dropdownGroup.Length(), "%s dropdown does not exist", name)
|
2024-07-16 14:42:35 +00:00
|
|
|
return dropdownGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that a dropdown has at least one non-empty option
|
|
|
|
func (doc *HTMLDoc) AssertDropdownHasOptions(t testing.TB, dropdownName string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
options := doc.AssertDropdown(t, dropdownName).Find(".menu [data-value]:not([data-value=''])")
|
2025-03-28 22:22:21 +00:00
|
|
|
assert.Positive(t, options.Length(), "%s dropdown has no options", dropdownName)
|
2024-07-16 14:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (doc *HTMLDoc) AssertDropdownHasSelectedOption(t testing.TB, dropdownName, expectedValue string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
dropdownGroup := doc.AssertDropdown(t, dropdownName)
|
|
|
|
|
|
|
|
selectedValue, _ := dropdownGroup.Find(fmt.Sprintf("input[name='%s']", dropdownName)).Attr("value")
|
2024-08-14 11:43:42 +02:00
|
|
|
assert.Equal(t, expectedValue, selectedValue, "%s dropdown doesn't have expected value selected", dropdownName)
|
2024-07-16 14:42:35 +00:00
|
|
|
|
|
|
|
dropdownValues := dropdownGroup.Find(".menu [data-value]").Map(func(i int, s *goquery.Selection) string {
|
|
|
|
value, _ := s.Attr("data-value")
|
|
|
|
return value
|
|
|
|
})
|
2024-08-14 11:43:42 +02:00
|
|
|
assert.Contains(t, dropdownValues, expectedValue, "%s dropdown doesn't have an option with expected value", dropdownName)
|
2024-07-16 14:42:35 +00: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-08-15 10:56:45 +02:00
|
|
|
// FindByText gets all elements by selector that also has the given text
|
|
|
|
func (doc *HTMLDoc) FindByText(selector, text string) *goquery.Selection {
|
|
|
|
return doc.doc.Find(selector).FilterFunction(func(i int, s *goquery.Selection) bool {
|
|
|
|
return s.Text() == text
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-21 15:37:43 +08:00
|
|
|
// GetCSRF for getting CSRF token value from input
|
2017-06-17 11:29:59 -05:00
|
|
|
func (doc *HTMLDoc) GetCSRF() string {
|
2017-06-17 00:49:45 -04:00
|
|
|
return doc.GetInputValueByName("_csrf")
|
|
|
|
}
|
2017-09-12 08:48:13 +02:00
|
|
|
|
2025-08-15 10:56:45 +02:00
|
|
|
// AssertSelection check if selection exists or does not exist depending on checkExists
|
|
|
|
func (doc *HTMLDoc) AssertSelection(t testing.TB, selection *goquery.Selection, checkExists bool) {
|
2017-09-12 08:48:13 +02:00
|
|
|
if checkExists {
|
2025-08-15 10:56:45 +02:00
|
|
|
assert.Equal(t, 1, selection.Length())
|
2017-09-12 08:48:13 +02:00
|
|
|
} else {
|
2025-08-15 10:56:45 +02:00
|
|
|
assert.Equal(t, 0, selection.Length())
|
2017-09-12 08:48:13 +02:00
|
|
|
}
|
|
|
|
}
|
2025-08-15 10:56:45 +02: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) {
|
|
|
|
doc.AssertSelection(t, doc.doc.Find(selector), checkExists)
|
|
|
|
}
|