Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

html_helper.go 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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, body *bytes.Buffer) *HTMLDoc {
  17. doc, err := goquery.NewDocumentFromReader(body)
  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. }
  35. // AssertElement check if element by selector exists or does not exist depending on checkExists
  36. func (doc *HTMLDoc) AssertElement(t testing.TB, selector string, checkExists bool) {
  37. sel := doc.doc.Find(selector)
  38. if checkExists {
  39. assert.Equal(t, 1, sel.Length())
  40. } else {
  41. assert.Equal(t, 0, sel.Length())
  42. }
  43. }