Welcome to BhauAutomation!
What is Nightwatch.js?
Nightwatch.js is a JavaScript testing framework used to automate web browsers. It helps testers open websites, click buttons, enter text, and check whether website features are working correctly.
Why Use Nightwatch.js?
- Automates website testing.
- Supports JavaScript.
- Helps find and interact with web elements.
- Checks whether expected results are displayed.
- Supports browser testing and test reports.
Simple JavaScript Example
Imagine we want to open a website and check its page title.
Step 1: Set Up Nightwatch.js
Make sure Node.js is installed, then run:
npm init nightwatch
Complete the setup and select JavaScript and Chrome.
Step 2: Create a Test File
Create exampleTest.js inside your test folder.
module.exports = {
'Check Website Title': function (browser) {
browser
.url('https://example.com')
.waitForElementVisible('body')
.assert.titleContains('Example Domain')
.end();
}
};
Step 3: Run the Test
npx nightwatch
Expected Result (simplified):
Running: Check Website Title
✔ Page title contains Example Domain
1 test passed
Simple Explanation
browser.url()opens the website.waitForElementVisible()waits for the page body.assert.titleContains()checks the page title.end()closes the browser session.
Conclusion
Nightwatch.js makes web automation testing easier with JavaScript. Beginners can start by opening websites, checking page titles, and interacting with web elements.
Keep Learning with BhauAutomation!