Api test automation with mocha and chai

Posted on by 0 comment

Mocha is a JavaScript test framework running on Node.js and in the browser, which can be used for unit testing, and api and ui automated testing, here we will use it along with chai to do some api automated testing in nodejs with typescript.

Packages

We will install packages below with yarn add or npm install

  • @types/chai-http
  • @types/mocha
  • @types/node
  • chai
  • chai-http
  • mocha
  • mochawesome
  • ts-mocha
  • ts-node
  • typescript
  • typings

Writing tests

Mocha BDD interface will be used to create the test case. chai-http

describe('test get findByStatus', () => {

    it(`should succeed when findByStatus`, (done) => {
        chai.request(baseUrl)
        .get('/v2/pet/findByStatus')
        .query({status:'available'})
        .end(function(err,res){
            expect(res).to.have.status(200);
            expect(res.body).to.to.have.length(432);
            done();
           });

    }).timeout(30000);

});
describe('test get findByStatus', () => {

    it(`should succeed when findByStatus-async`, async () => {
        const res = await chai.request(baseUrl)
        .get('/v2/pet/findByStatus')
        .query({status:'available'});
        expect(res).to.have.status(200);
        expect(res.body).to.to.have.length(4320);

    }).timeout(30000);

});

Data driven

We have created one test case, but we have some more status to check, so let’s make it data driven.

describe('test get findByStatus', () => {

    var tests : {status:string, expectedCount: number } [] = [
        {status:"available", expectedCount:1000},
        {status:"pending", expectedCount:300},
        {status:"sold", expectedCount:500}
    ];

    tests.forEach(function(test) 
    {
        it(`should succeed when findByStatus - ${test.status}`, async () => {
            const res = await chai.request(baseUrl)
            .get('/v2/pet/findByStatus')
            .query({status:'available'});
            expect(res).to.have.status(200);
            expect(res.body).to.to.have.length(test.expectedCount);
    
        }).timeout(30000);

    });
});

Execution and reporting

//Add content below in the scripts section of package.json
"test": "ts-mocha --file src/test/tw.test.ts",
"test:report": "ts-mocha  --reporter mochawesome --file src/test/tw.test.ts"
Category: javascript

Reading and writing serial port data with PowerShell

Posted on by 0 comment

List com ports

To see a list of available com ports,

Read or write data

To read or write data with given details, such as port, Baudrate etc.

Write unix time

I have a clock which accepts unix time for syncing the time, so I use script below to sync the time if the clock being off for few minutes after some time.

Category: PowerShell, uart

Download UFT script from ALM

Posted on by 0 comment

Usually we use ALM to manage our test cases, test script and resource files, which works very well, but when we consider to host the script in git, we have to download them to local and commit to git.

In this exercise, I try to download UFT scripts and resource files in UFT with using ALM OTA , the OTA api reference can be found here, and here is the code.



		


Category: UFT

Firebase login in docker toolbox

Posted on by 2 comments

The exercise is trying to resolve the Firebase login callback issue in docker; my system setup is VirtualBox with Docker toolbox.
Let’s start with the login command:

Then I copy the long URL, and paste it to a browser window in host machine, allow the FirebaseCLI access, the authentication is working fine, but failed at callback. That is due to the setup is docker with VirtualBox, so localhost is not same as the docker machine, which in my case is 192.168.99.100, and HTTP://localhost:9005 is not accessible. If I try to modified the URL with replacing the localhost with docker machine IP, a 400 OAuth2 error will be returned.

After some trying, and managed to get it working with steps below,

  • Copy the long URL and paste it to a browser window in host machine
  • Follow the steps to complete the authentication
  • The callback failed with URL HTTP://localhost:9005?state=…..
  • Replace localhost with the docker machine IP in the URL and click enter

Now you will receive a successful message in the browser window and docker terminal.

Category: Docker

Add make to Git Bash on Windows

Posted on by 1 comment

Recently I need to use make command which is not packaged with Git Windows by default, and come across this git hub page, which mentioned how to add few commands to Git Bash on windows, this is quite handy and solved my problem.

make

  • Go to ezwinports.
  • Download make-4.1-2-without-guile-w32-bin.zip (get the version without guile).
  • Extract zip.
  • Copy the contents to your Git\mingw64\ merging the folders, but do NOT overwrite/replace any existing files.


Category: git