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.

html_helper.go 1.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "bytes"
  7. "testing"
  8. "github.com/PuerkitoBio/goquery"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. // HTMLDoc struct
  12. type HTMLDoc struct {
  13. doc *goquery.Document
  14. }
  15. // NewHTMLParser parse html file
  16. func NewHTMLParser(t testing.TB, content []byte) *HTMLDoc {
  17. doc, err := goquery.NewDocumentFromReader(bytes.NewReader(content))
  18. assert.NoError(t, err)
  19. return &HTMLDoc{doc: doc}
  20. }
  21. // GetInputValueByID for get input value by id
  22. func (doc *HTMLDoc) GetInputValueByID(id string) string {
  23. text, _ := doc.doc.Find("#" + id).Attr("value")
  24. return text
  25. }
  26. // GetInputValueByName for get input value by name
  27. func (doc *HTMLDoc) GetInputValueByName(name string) string {
  28. text, _ := doc.doc.Find("input[name=\"" + name + "\"]").Attr("value")
  29. return text
  30. }
  31. // GetCSRF for get CSRC token value from input
  32. func (doc *HTMLDoc) GetCSRF() string {
  33. return doc.GetInputValueByName("_csrf")
  34. }