0

I am new to playwright (Automation tool).

Scenario: Need to connect to Vertica DB, fetch required record and compare the result with UI values.

I am using following Javascript through which I am able to connect to DB successfully.

console.log("Hello")
console.log("Entering the test")

const {Client} = require('vertica-nodejs')
const connectionString = 'vertica://'
                        + 'xxx' + ':'
                        + 'xyz' + '@'
                        + 'abcdefg' + ':'
                        + 5433 + '/'
                        + 'abc';
const client = new Client({
    connectionString,
});
client.connect()
client.query("SELECT 'success' as connectionWithString", (err, res) => {
    console.log(err || res.rows[0])
    client.end()
})

OUTPUT:

PS C:\Stab\Automation\ConnectVertica> node .\temp.js
Hello
Entering the test
{ connectionWithString: 'success' }
PS C:\Stab\Automation\ConnectVertica>  

Using same code when I am trying to connect to DB from Playwright script, not seeing console log which says connectionWithString: 'success'.

import { test, expect } from '@playwright/test';

const {Client} = require('vertica-nodejs')
const connectionString = 'vertica://'
                        + 'xxx' + ':'
                        + 'xyz' + '@'
                        + 'abcdefg' + ':'
                        + 5433 + '/'
                        + 'abc';
const client = new Client({
    connectionString,
});

async function connectAndQuery() {
 try {
 console.log("Connecting to Vertica DB")
 await client.connect();
 const res = await client.query("SELECT 'Connection successful' as message");
 console.log(res.rows[0]);
 } catch (err) {
   console.error(err);
 } finally {
 await client.end();
 }
 } 
 test('test db connection', async () => {
 connectAndQuery();
 });

OUTPUT:

Running 1 test using 1 worker
[chromium] › tests\testVertica.spec.js:25:5 › test db connection
Connecting to Vertica DB
  1 passed (3.9s)

Can anyone help me here to understand what I am missing here.

4
  • 1
    Can you share how you're calling to that function? Commented Feb 26 at 13:02
  • @hardkoded, In 1st script, I have created a javascript file "temp.js" and executing using command: node .\temp.js Whereas for playwright script, on the test method (test('test db connection', async () => {), executing using run button. Its the same way how we execute any playwright test methods. Commented Feb 26 at 14:55
  • 1
    This code is missing an await connectAndQuery(); in the last block, but I'm not sure why you'd want to connect to a database in a Playwright test in the first place. Since it's an E2E test, you generally want to manipulate everything as a user would, from the UI. Also not sure why this is tagged playwright-java. Commented Feb 26 at 15:17
  • @ggorlen, Thanks a lot for quick response. It worked. I am working on data validation and can't go with API testing. Thats the reason I am going with this approach. I understand your point and agree with you. But here its a special need where I have to validate UI values with DB. Commented Feb 26 at 15:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.