2017-05-02 00:49:55 +00:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-05-02 00:49:55 +00:00
|
|
|
|
2022-09-02 19:18:23 +00:00
|
|
|
package integration
|
2017-05-02 00:49:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-07-16 14:42:35 +00:00
|
|
|
"fmt"
|
2017-06-17 04:49:45 +00:00
|
|
|
"testing"
|
2017-05-02 00:49:55 +00:00
|
|
|
|
2017-05-07 14:40:31 +00:00
|
|
|
"github.com/PuerkitoBio/goquery"
|
2017-06-17 04:49:45 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-05-02 00:49:55 +00:00
|
|
|
)
|
|
|
|
|
2017-06-17 16:29:59 +00:00
|
|
|
// HTMLDoc struct
|
|
|
|
type HTMLDoc struct {
|
2017-05-07 14:40:31 +00:00
|
|
|
doc *goquery.Document
|
2017-05-02 00:49:55 +00:00
|
|
|
}
|
|
|
|
|
2017-06-17 16:29:59 +00:00
|
|
|
// NewHTMLParser parse html file
|
2017-12-03 22:46:01 +00:00
|
|
|
func NewHTMLParser(t testing.TB, body *bytes.Buffer) *HTMLDoc {
|
2019-07-29 04:15:18 +00:00
|
|
|
t.Helper()
|
2017-12-03 22:46:01 +00:00
|
|
|
doc, err := goquery.NewDocumentFromReader(body)
|
2017-06-17 04:49:45 +00:00
|
|
|
assert.NoError(t, err)
|
2017-06-17 16:29:59 +00:00
|
|
|
return &HTMLDoc{doc: doc}
|
2017-05-02 00:49:55 +00:00
|
|
|
}
|
|
|
|
|
2017-06-17 16:29:59 +00:00
|
|
|
// GetInputValueByID for get input value by id
|
|
|
|
func (doc *HTMLDoc) GetInputValueByID(id string) string {
|
2017-05-07 14:40:31 +00:00
|
|
|
text, _ := doc.doc.Find("#" + id).Attr("value")
|
|
|
|
return text
|
2017-05-02 00:49:55 +00:00
|
|
|
}
|
|
|
|
|
2017-06-17 16:29:59 +00:00
|
|
|
// GetInputValueByName for get input value by name
|
|
|
|
func (doc *HTMLDoc) GetInputValueByName(name string) string {
|
2017-05-07 14:40:31 +00:00
|
|
|
text, _ := doc.doc.Find("input[name=\"" + name + "\"]").Attr("value")
|
|
|
|
return text
|
2017-05-02 00:49:55 +00:00
|
|
|
}
|
2017-06-17 04:49:45 +00: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))
|
|
|
|
assert.Equal(t, dropdownGroup.Length(), 1, fmt.Sprintf("%s dropdown does not exist", name))
|
|
|
|
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=''])")
|
|
|
|
assert.Greater(t, options.Length(), 0, fmt.Sprintf("%s dropdown has no options", dropdownName))
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
assert.Equal(t, expectedValue, selectedValue, fmt.Sprintf("%s dropdown doesn't have expected value selected", dropdownName))
|
|
|
|
|
|
|
|
dropdownValues := dropdownGroup.Find(".menu [data-value]").Map(func(i int, s *goquery.Selection) string {
|
|
|
|
value, _ := s.Attr("data-value")
|
|
|
|
return value
|
|
|
|
})
|
|
|
|
assert.Contains(t, dropdownValues, expectedValue, fmt.Sprintf("%s dropdown doesn't have an option with expected value", dropdownName))
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-10-21 07:37:43 +00:00
|
|
|
// GetCSRF for getting CSRF token value from input
|
2017-06-17 16:29:59 +00:00
|
|
|
func (doc *HTMLDoc) GetCSRF() string {
|
2017-06-17 04:49:45 +00:00
|
|
|
return doc.GetInputValueByName("_csrf")
|
|
|
|
}
|
2017-09-12 06:48:13 +00: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())
|
|
|
|
}
|
|
|
|
}
|