2017-05-02 08:49:55 +08: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-17 12:49:45 +08:00
|
|
|
"testing"
|
2017-05-02 08:49:55 +08:00
|
|
|
|
2017-05-07 22:40:31 +08:00
|
|
|
"github.com/PuerkitoBio/goquery"
|
2017-06-17 12:49:45 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-05-02 08:49:55 +08:00
|
|
|
)
|
|
|
|
|
2017-06-18 00:29:59 +08:00
|
|
|
// HTMLDoc struct
|
|
|
|
type HTMLDoc struct {
|
2017-05-07 22:40:31 +08:00
|
|
|
doc *goquery.Document
|
2017-05-02 08:49:55 +08:00
|
|
|
}
|
|
|
|
|
2017-06-18 00:29:59 +08:00
|
|
|
// NewHTMLParser parse html file
|
|
|
|
func NewHTMLParser(t testing.TB, content []byte) *HTMLDoc {
|
2017-05-07 22:40:31 +08:00
|
|
|
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(content))
|
2017-06-17 12:49:45 +08:00
|
|
|
assert.NoError(t, err)
|
2017-06-18 00:29:59 +08:00
|
|
|
return &HTMLDoc{doc: doc}
|
2017-05-02 08:49:55 +08:00
|
|
|
}
|
|
|
|
|
2017-06-18 00:29:59 +08:00
|
|
|
// GetInputValueByID for get input value by id
|
|
|
|
func (doc *HTMLDoc) GetInputValueByID(id string) string {
|
2017-05-07 22:40:31 +08:00
|
|
|
text, _ := doc.doc.Find("#" + id).Attr("value")
|
|
|
|
return text
|
2017-05-02 08:49:55 +08:00
|
|
|
}
|
|
|
|
|
2017-06-18 00:29:59 +08:00
|
|
|
// GetInputValueByName for get input value by name
|
|
|
|
func (doc *HTMLDoc) GetInputValueByName(name string) string {
|
2017-05-07 22:40:31 +08:00
|
|
|
text, _ := doc.doc.Find("input[name=\"" + name + "\"]").Attr("value")
|
|
|
|
return text
|
2017-05-02 08:49:55 +08:00
|
|
|
}
|
2017-06-17 12:49:45 +08:00
|
|
|
|
2017-06-18 00:29:59 +08:00
|
|
|
// GetCSRF for get CSRC token value from input
|
|
|
|
func (doc *HTMLDoc) GetCSRF() string {
|
2017-06-17 12:49:45 +08:00
|
|
|
return doc.GetInputValueByName("_csrf")
|
|
|
|
}
|