Showing posts with label Mocha. Show all posts
Showing posts with label Mocha. Show all posts

1/16/2018

Visual Studio Code, Selenium, Firefox, JavaScript and Mocha with Chai

Let's leave Netbeans IDE to Java and start using lightweight Visual Studio Code for JavaScript test automation.

Preconditions:
  • Node.js installed
  • Gecko driver downloaded
  • path to Gecko driver added to PATH
Download and install Visual Studio Code

Assign some hot-keys to run it without a mouse, for example:


We will try not to use a mouse.

Open Visual Studio Code using the shortcut keys and open Terminal in the editor using the following hot-keys: Ctrl + `  and change the directory, for example cd c:/devel



Visual Studio Code works primarily with folders, so to start new project you need to just create new folder, for example: mkdir selenium_vsc , then change directory to the folder: cd selenium_vsc:



Init node.js project in the folder by the following : npm init -y



Install the libraries:

Mocha:

npm install mocha --save-dev




npm install chai --save-dev



Selenium Web Driver:

npm install selenium-webdriver --save-dev



Create test folder:  mkdir test

Change current directory to the folder: cd test

You may use Tab button to autocomplete the folder/file name

Create e2e folder for our end-to-end Selenium tests:  mkdir e2e

Change current directory to the folder: cd e2e

Create new empty file: break > first.test.js



Happy Linux and Mac OS users would just type: touch first.test.js

In the future you most likely will create new files by closing the terminal Ctrl + ` and then Ctrl + N , but now we just learn the Windows console commands.

Open the new file by typing the following: code -r first.test.js




Copy/paste our example code:

const {assert} = require('chai');
const webdriver = require('selenium-webdriver');
const Capabilities = require('selenium-webdriver/lib/capabilities')
        .Capabilities;
const capabilities = Capabilities.firefox();
capabilities.set('marionette', true);

let driver;

describe('Automation Playground tests', async () => {
    describe('When verifying the blog title', () => {
        it('should be equil to Automation Playground', async() => {
            const title = await driver.getTitle();
            assert.equal(title, 'Automation Playground');
        });
    });
});
before(async() => {
    driver = await new webdriver.Builder()
            .withCapabilities(capabilities).build();
    driver.get("https://automation-playground.blogspot.com");
});
after(async() => {
    await driver.quit();
});


Save Ctrl  + S

Go to the project root directory: cd ../..



Open package.json file by typing the following: code -r package.json



Copy/paste the following as script for test:

mocha ./test/e2e/*.test.js --timeout 30000



Save Ctrl + S

Go to the terminal by clicking twice Ctrl + `

Enter to run the test: npm test

The following test scenario will be executed:

  • run Firefox browser 
  • go to https://automation-playground.blogspot.com 
  • verify the blog's title
  • close Firefox browser


Test passed !

1/04/2018

NetBeans IDE, Selenium, Firefox, JavaScript and Mocha with Chai

Now we will use Mocha testing framework and Chai assertion library in NetBeans IDE to write and execute tests.

We will use Mocha hooks (before, after) as well.

Also we will use console in Windows to set up the project and execute tests.

Skip the steps that you have already done:

1. Install the actual version of  Node.js

2. Install and set Git for Windows to use it as terminal.

3. Download Gecko driver from: https://github.com/mozilla/geckodriver  and store it in some folder, for example: C:\tools\web-drivers\ .

4. Add the driver location to the PATH:



5. Run NetBeans IDE

6. File > New Project



7. Select Node.js Application > Next and then enter the project name and location:



8. Click Next and select Create package.json:



9.  Git-bash and go to the project directory:



10. Install the libraries:

 Mocha:

npm install mocha --save-dev

Chai:

npm install chai --save-dev

Selenium Web Driver:

npm install selenium-webdriver --save-dev


Open package.json and see the dependencies have been added:



11. Create 'test' folder with unit and 'e2e' subfolders:




12.Open project Properties and select the subfolders as Unit Tests Folder and Selenium Tests Folder:



13. Click Selenium Testing and select Mocha test runner:




14. Set ES7 for the project in Settings:



15. Create 'blog.test.js' file in 'e2e' folder and copy/paste the following code:


const {assert} = require('chai');
const webdriver = require('selenium-webdriver');
const Capabilities = require('selenium-webdriver/lib/capabilities')
        .Capabilities;
const capabilities = Capabilities.firefox();
capabilities.set('marionette', true);

let driver;

describe('Automation Playground tests', async () => {
    describe('When verifying the blog title', () => {
        it('should be equil to Automation Playground', async() => {
            const title = await driver.getTitle();
            assert.equal(title, 'Automation Playground');
        });
    });
});
before(async() => {
    driver = await new webdriver.Builder()
            .withCapabilities(capabilities).build();
    driver.get("https://automation-playground.blogspot.com");
});
after(async() => {
    await driver.quit();
});


16. Click Run Selenium Tests:



The following test scenario will be executed:
  • run Firefox browser 
  • go to https://automation-playground.blogspot.com 
  • verify the blog's title
  • close Firefox browser


Test passed!

17. Lets run the test via command line in Git Bash, enter the following command in the project root directory:



mocha ./test/e2e/*.test.js --timeout 20000


Test will run again and...


Test passed again!

Visual Studio Code, WebdriverIO, JavaScript and Chrome - cucumber html test report

 Apart Allure test report  we can use Cucumber test report in html format. We will follow this instructions . 1. Install  wdio-cucumberjs-js...