2017-05-01 18:49:55 -06:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package integrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-06-16 22:49:45 -06:00
|
|
|
"testing"
|
2017-05-01 18:49:55 -06:00
|
|
|
|
2017-05-07 08:40:31 -06:00
|
|
|
"github.com/PuerkitoBio/goquery"
|
2017-06-16 22:49:45 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-05-01 18:49:55 -06:00
|
|
|
)
|
|
|
|
|
2017-06-17 10:29:59 -06:00
|
|
|
// HTMLDoc struct
|
|
|
|
type HTMLDoc struct {
|
2017-05-07 08:40:31 -06:00
|
|
|
doc *goquery.Document
|
2017-05-01 18:49:55 -06:00
|
|
|
}
|
|
|
|
|
2017-06-17 10:29:59 -06:00
|
|
|
// NewHTMLParser parse html file
|
2017-12-03 15:46:01 -07:00
|
|
|
func NewHTMLParser(t testing.TB, body *bytes.Buffer) *HTMLDoc {
|
2019-07-28 22:15:18 -06:00
|
|
|
t.Helper()
|
2017-12-03 15:46:01 -07:00
|
|
|
doc, err := goquery.NewDocumentFromReader(body)
|
2017-06-16 22:49:45 -06:00
|
|
|
assert.NoError(t, err)
|
2017-06-17 10:29:59 -06:00
|
|
|
return &HTMLDoc{doc: doc}
|
2017-05-01 18:49:55 -06:00
|
|
|
}
|
|
|
|
|
2017-06-17 10:29:59 -06:00
|
|
|
// GetInputValueByID for get input value by id
|
|
|
|
func (doc *HTMLDoc) GetInputValueByID(id string) string {
|
2017-05-07 08:40:31 -06:00
|
|
|
text, _ := doc.doc.Find("#" + id).Attr("value")
|
|
|
|
return text
|
2017-05-01 18:49:55 -06:00
|
|
|
}
|
|
|
|
|
2017-06-17 10:29:59 -06:00
|
|
|
// GetInputValueByName for get input value by name
|
|
|
|
func (doc *HTMLDoc) GetInputValueByName(name string) string {
|
2017-05-07 08:40:31 -06:00
|
|
|
text, _ := doc.doc.Find("input[name=\"" + name + "\"]").Attr("value")
|
|
|
|
return text
|
2017-05-01 18:49:55 -06:00
|
|
|
}
|
2017-06-16 22:49:45 -06:00
|
|
|
|
2020-10-29 01:52:27 -06: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)
|
|
|
|
}
|
|
|
|
|
2017-06-17 10:29:59 -06:00
|
|
|
// GetCSRF for get CSRC token value from input
|
|
|
|
func (doc *HTMLDoc) GetCSRF() string {
|
2017-06-16 22:49:45 -06:00
|
|
|
return doc.GetInputValueByName("_csrf")
|
|
|
|
}
|
2017-09-12 00:48:13 -06: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())
|
|
|
|
}
|
|
|
|
}
|