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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "bytes"
  6. "testing"
  7. "github.com/PuerkitoBio/goquery"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. // HTMLDoc struct
  11. type HTMLDoc struct {
  12. doc *goquery.Document
  13. }
  14. // NewHTMLParser parse html file
  15. func NewHTMLParser(t testing.TB, body *bytes.Buffer) *HTMLDoc {
  16. t.Helper()
  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. // Find gets the descendants of each element in the current set of
  32. // matched elements, filtered by a selector. It returns a new Selection
  33. // object containing these matched elements.
  34. func (doc *HTMLDoc) Find(selector string) *goquery.Selection {
  35. return doc.doc.Find(selector)
  36. }
  37. // GetCSRF for getting CSRF token value from input
  38. func (doc *HTMLDoc) GetCSRF() string {
  39. return doc.GetInputValueByName("_csrf")
  40. }
  41. // AssertElement check if element by selector exists or does not exist depending on checkExists
  42. func (doc *HTMLDoc) AssertElement(t testing.TB, selector string, checkExists bool) {
  43. sel := doc.doc.Find(selector)
  44. if checkExists {
  45. assert.Equal(t, 1, sel.Length())
  46. } else {
  47. assert.Equal(t, 0, sel.Length())
  48. }
  49. }