mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.6 KiB
61 lines
1.6 KiB
8 years ago
|
// 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.
|
||
|
|
||
2 years ago
|
package integration
|
||
8 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
8 years ago
|
"testing"
|
||
8 years ago
|
|
||
8 years ago
|
"github.com/PuerkitoBio/goquery"
|
||
8 years ago
|
"github.com/stretchr/testify/assert"
|
||
8 years ago
|
)
|
||
|
|
||
8 years ago
|
// HTMLDoc struct
|
||
|
type HTMLDoc struct {
|
||
8 years ago
|
doc *goquery.Document
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// NewHTMLParser parse html file
|
||
7 years ago
|
func NewHTMLParser(t testing.TB, body *bytes.Buffer) *HTMLDoc {
|
||
5 years ago
|
t.Helper()
|
||
7 years ago
|
doc, err := goquery.NewDocumentFromReader(body)
|
||
8 years ago
|
assert.NoError(t, err)
|
||
8 years ago
|
return &HTMLDoc{doc: doc}
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// GetInputValueByID for get input value by id
|
||
|
func (doc *HTMLDoc) GetInputValueByID(id string) string {
|
||
8 years ago
|
text, _ := doc.doc.Find("#" + id).Attr("value")
|
||
|
return text
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// GetInputValueByName for get input value by name
|
||
|
func (doc *HTMLDoc) GetInputValueByName(name string) string {
|
||
8 years ago
|
text, _ := doc.doc.Find("input[name=\"" + name + "\"]").Attr("value")
|
||
|
return text
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
4 years ago
|
// 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)
|
||
|
}
|
||
|
|
||
3 years ago
|
// GetCSRF for getting CSRF token value from input
|
||
8 years ago
|
func (doc *HTMLDoc) GetCSRF() string {
|
||
8 years ago
|
return doc.GetInputValueByName("_csrf")
|
||
|
}
|
||
7 years ago
|
|
||
|
// 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())
|
||
|
}
|
||
|
}
|