# Acceptance test content is present on page

**URL:** https://meta.discourse.org/t/acceptance-test-content-is-present-on-page/57292
**Category:** Development
**Created:** [2017年二月13日 02:48 UTC](https://meta.discourse.org/t/acceptance-test-content-is-present-on-page/57292 "2017-02-13T02:48:10Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![rimian](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rimian/32/120658_2.png) [@rimian](https://meta.discourse.org/u/rimian)
#### Post date: [2017年二月13日 02:48 UTC](https://meta.discourse.org/t/acceptance-test-content-is-present-on-page/57292/1 "2017-02-13T02:48:10Z")

</div>

What’s the best way to (QUnit) assert an element on the page has some content in it?

This passes:

```plaintext
ok($.trim($('.foo').text()) == 'bar', 'content bar renders on page');

```

But isn’t very practical. Is there a better way?

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [2017年二月13日 03:05 UTC](https://meta.discourse.org/t/acceptance-test-content-is-present-on-page/57292/2 "2017-02-13T03:05:28Z")

</div>

I recently wrote some myself. The way I see it, the value hinges on the selector and the conditional.

Perhaps not the best example, but hopefully some help.

> <https://github.com/Mittineague/discourse-newpage/blob/master/test/javascripts/acceptance/discourse-newpage-test.js.es6>

---

<div class="post-metadata">

### Author: ![rimian](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rimian/32/120658_2.png) [@rimian](https://meta.discourse.org/u/rimian)
#### Post date: [2017年二月13日 03:29 UTC](https://meta.discourse.org/t/acceptance-test-content-is-present-on-page/57292/3 "2017-02-13T03:29:55Z")

</div>

Hi @Mittineague! I saw your plugin and I’ve got it running on my local instance. It helped me get my custom page up-and-running quickly.

Are you sure your tests fail correctly? When I run them, I get a pass regardless of the content:

`ok(find("h1.mitt_newpage:contains('something else')"), 'heading displays something else not the heading');`

This passes for me…

---

<div class="post-metadata">

### Author: ![cpradio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cpradio/32/4970_2.png) [@cpradio](https://meta.discourse.org/u/cpradio)
#### Post date: [2017年二月13日 04:41 UTC](https://meta.discourse.org/t/acceptance-test-content-is-present-on-page/57292/4 "2017-02-13T04:41:59Z")

</div>

Ah, yeah, you don’t want to use `find()` when dealing with `:contains`, you will want to use `exists()`

Example of it at  
[https://github.com/discourse/discourse/blob/master/test/javascripts/acceptance/search-full-test.js.es6#L274-L287](https://github.com/discourse/discourse/blob/master/test/javascripts/acceptance/search-full-test.js.es6#L274-L287)

IIRC, that is because `find()` will still return an empty object back and should be used with `assert.equal` so you can compare a specific attribute of what you expected to find to an expectation.

`exists` will handle that object being empty and treat it as “not found” or “false”  
[https://github.com/discourse/discourse/blob/master/test/javascripts/helpers/assertions.js#L3-L5](https://github.com/discourse/discourse/blob/master/test/javascripts/helpers/assertions.js#L3-L5)

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [2017年二月13日 07:34 UTC](https://meta.discourse.org/t/acceptance-test-content-is-present-on-page/57292/5 "2017-02-13T07:34:38Z")

</div>

Thanks. I was planning to write some “should nots” to go along with the “shoulds”, but then got sidetracked trying to get RSpec tests working (still trying) and hadn’t been back to the QUnit tests yet.

I can use a break from the RSpec roadblock so it will be a pleasant change to do the “should nots” and get those up to par.

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [2017年二月14日 02:35 UTC](https://meta.discourse.org/t/acceptance-test-content-is-present-on-page/57292/6 "2017-02-14T02:35:47Z")

</div>

Interesting. I added 10 “not” tests, and sure enough, a lot of the tests that were passing were not passing for the reason I had thought they were.

Changing the "find"s to “exists” fixed most, except for four of them.

I changed the first problem test to an “equals” and discovered that although I specified currentUser as “TestRunner” instead of “Welcome TestRunner” it was “Welcome eviltrout”.

This apparently is coming from a fixture.

I don’t know if having plugins test against Core test fixtures is a great idea, but I guess it could work. Better would be for me to learn how to use my own fixtures that would be part of the plugin.

---

<div class="post-metadata">

### Author: ![rimian](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rimian/32/120658_2.png) [@rimian](https://meta.discourse.org/u/rimian)
#### Post date: [2017年二月14日 05:55 UTC](https://meta.discourse.org/t/acceptance-test-content-is-present-on-page/57292/7 "2017-02-14T05:55:31Z")

</div>

I’ve got RSpec running against my plugin here:

> **[Travis CI](https://app.travis-ci.com/choiceaustralia/discourse-payments)**
>
> Travis CI enables your team to test and ship your apps with confidence. Easily sync your projects with Travis CI and you'll be testing your code in minutes.

[https://github.com/choiceaustralia/discourse-payments](https://github.com/choiceaustralia/discourse-payments)

This is currently in early stages of development. I wouldn’t mind running capybara for feature specs. Much cleaner than running acceptance tests on top of QUnit.
