javascript - 在 azure 函数中运行剧作家测试

标签 javascript azure playwright

我必须编写一个 Azure javascript 函数(由计时器触发器调用),然后运行在另一个文件中的 playwright 中定义的测试。然后将结果写入应用洞察日志。

我已经编写了azure函数计时器触发器,但我不知道如何调用剧作家测试。请帮助我

/* 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');
  });
});

最佳答案

您应该定义一个导出函数FunctionName(){...}并将代码放入导出函数FunctionName(){...}主体。

然后您可以在计时器触发函数中调用它。

关于javascript - 在 azure 函数中运行剧作家测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66353829/

相关文章:

c# - 发布到 Azure 云服务的 WCF 无法访问

c# - Playwright (.NET) 尝试使用与安装的浏览器版本不同的浏览器版本

javascript - 编剧错误 : Firefox revision is not downloaded. 运行 "npm install"或 "yarn install"

python-3.x - ubuntu python playright headless true 获取错误页面

javascript - jQuery 中 $.remove() 的问题

javascript - 如何将事件 'TabOpen' 添加到 Firefox 扩展中的所有窗口?

javascript - 为什么使用 checked 属性更改复选框时不调用 onchange 函数

javascript - d3 : my code will create 1 chart but not 2

azure - 是否可以将自定义域与 Windows Azure 服务总线一起使用?

azure - 如何重用 Azure Functions 高级计划?