Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update adding-custom-assertions.md #232

Open
wants to merge 2 commits into
base: versions/2.0
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions guide/extending-nightwatch/adding-custom-assertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,51 @@ Assertions implement a simple interface which is shared between built-in asserti

};</code></pre></div>


Here is another example, potentially showing a simpler design. It polls the browser for a specific object made by another program.
jpratt2 marked this conversation as resolved.
Show resolved Hide resolved

<div class="sample-test"><i>globalObjectReady.js</i>
<pre class="line-numbers" data-language="javascript"><code class=" language-javascript">{
//example of use: browser.assert.globalObjectReady("info");

GlobalObjectReady = function (info) {
this.message = `Testing if specificObject is on this page`;

this.expected = () => {
return true;
};

this.evaluate = (value) => {
return value;
};

this.value = (result) => {
return result.value;
};

this.command = (callback) => {
return this.api.execute(globalObjectCheck, [info], callback);
};

async function globalObjectCheck(info){
//for illustrative purposes only
console.log(info);

//check every 250 ms for the existence of a specific global object
for(let i=0; i<20; i++){
if(typeof specificObject === 'undefined'){
await new Promise(resolve => setTimeout(resolve, 250));
}else{
return true;
}
}
return false;
}
};

module.exports.assertion = GlobalObjectReady;
</code></pre></div>

### Recommended content
- [Define custom commands](https://nightwatchjs.org/guide/extending-nightwatch/adding-custom-commands.html)

Expand Down