0
votes

I have to write a Azure javascript function (called by timer trigger) that then runs the tests that have been defined in playwright in another file. Then write to the app insights log with the results.

I have written the azure function timer trigger, but i don't know how to call the playwright tests. Please help me

/* azure function */

module.exports = async function (context, myTimer) {
    var timeStamp = new Date().toISOString();
    
    /* run the tests below */

    if (myTimer.isPastDue)
    {
        context.log('JavaScript is running late!');
    }
    context.log('JavaScript timer trigger function ran!', timeStamp);   
};

/* playwright tests */

const { it, beforeEach, afterEach, describe, expect } = require('@playwright/test');

describe('site', function() {
  beforeEach(async function login({ page }) {
    await page.setViewportSize({ width: 1920, height: 1080 });

    await page.goto('https://example.com/');

    const {
      TEST_EMAIL = 'email-value',
      TEST_PASSWORD = 'password-value',
    } = process.env;

    const usernameField = await page.waitForSelector('#username', { timeout: 2000 });
    const passwordField = await page.waitForSelector('#password', { timeout: 2000 });
    const button = await page.waitForSelector('button[name="action"]', { timeout: 2000 });

    await usernameField.isVisible();
    await passwordField.isVisible();
    await button.isVisible();

    await usernameField.fill(TEST_EMAIL);
    await passwordField.fill(TEST_PASSWORD);
    await button.click({ timeout: 2000 });
    await page.waitForNavigation();
    await page.waitForSelector('app');
  });

  afterEach(async function({ page }) {
    await page.waitForSelector('#profile-link');
    await page.isVisible('#profile-link');
    await page.click('#profile-link');
    await page.click('#logout');
  });

  it('logs in to the dashboard', async function({ page }) {
    const dashboard = await page.waitForSelector('app > main > dashboard');
    await dashboard.isVisible();
    expect(await page.title()).toEqual('Dashboard | Site');
  });

  it('navigates to sites page', async function({ page }) {
    const dashboard = await page.waitForSelector('app > main > dashboard');
    await dashboard.isVisible();
    const sitesButton = await page.waitForSelector('app mwc-button[label*="Add site" i]');
    await sitesButton.click();
    await page.isVisible('sites');
    expect(await page.title()).toEqual('Add Sites | Site');
  });

  it('navigates to calc page', async function({ page }) {
    const dashboard = await page.waitForSelector('app > main > dashboard');
    await dashboard.isVisible();
    const calcLink = await page.waitForSelector('[href="/calc"]');
    await calcLink.click();
    await page.isVisible('calc');
    expect(await page.title()).toEqual('calc | Site');
  });
});
1

1 Answers

0
votes

You should define an export function FunctionName(){...} and put the code to the export function FunctionName(){...} body.

Then you can call it in your timer trigger function.