跳至主要内容

从Protractor迁移

迁移原则

速查表

ProtractorPlaywright Test
element(by.buttonText('...'))page.locator('button, input[type="button"], input[type="submit"] >> text="..."')
element(by.css('...'))page.locator('...')
element(by.cssContainingText('..1..', '..2..'))page.locator('..1.. >> text=..2..')
element(by.id('...'))page.locator('#...')
element(by.model('...'))page.locator('[ng-model="..."]')
element(by.repeater('...'))page.locator('[ng-repeat="..."]')
element(by.xpath('...'))page.locator('xpath=...')
element.allpage.locator
browser.get(url)await page.goto(url)
browser.getCurrentUrl()page.url()

示例

Protractor:

describe('angularjs homepage todo list', function() {
it('should add a todo', function() {
browser.get('https://angularjs.org');

element(by.model('todoList.todoText')).sendKeys('first test');
element(by.css('[value="add"]')).click();

const todoList = element.all(by.repeater('todo in todoList.todos'));
expect(todoList.count()).toEqual(3);
expect(todoList.get(2).getText()).toEqual('first test');

// You wrote your first test, cross it off the list
todoList.get(2).element(by.css('input')).click();
const completedAmount = element.all(by.css('.done-true'));
expect(completedAmount.count()).toEqual(2);
});
});

逐行迁移到Playwright Test:

const { test, expect } = require('@playwright/test'); // 1

test.describe('angularjs homepage todo list', () => {
test('should add a todo', async ({ page }) => { // 2, 3
await page.goto('https://angularjs.org'); // 4

await page.locator('[ng-model="todoList.todoText"]').fill('first test');
await page.locator('[value="add"]').click();

const todoList = page.locator('[ng-repeat="todo in todoList.todos"]'); // 5
await expect(todoList).toHaveCount(3);
await expect(todoList.nth(2)).toHaveText('first test', {
useInnerText: true,
});

// You wrote your first test, cross it off the list
await todoList.nth(2).getByRole('textbox').click();
const completedAmount = page.locator('.done-true');
await expect(completedAmount).toHaveCount(2);
});
});

迁移亮点(请参阅Playwright Test代码片段中的内联注释):

  1. 每个Playwright Test文件都明确导入了testexpect函数
  2. 测试函数被标记为async
  3. Playwright Test 提供了一个 page 作为其参数之一。这是 Playwright Test 中众多实用夹具中的一个。
  4. 几乎所有Playwright调用都以await作为前缀
  5. 使用page.locator()创建定位器是少数同步方法之一。

填充waitForAngular的polyfill实现

Playwright Test内置了自动等待功能,使得在一般情况下不再需要protractor的waitForAngular

不过,在某些边缘情况下它可能会派上用场。以下是如何在Playwright Test中填充waitForAngular函数:

  1. 确保你的package.json中已安装protractor

  2. Polyfill function

    async function waitForAngular(page) {
    const clientSideScripts = require('protractor/built/clientsidescripts.js');

    async function executeScriptAsync(page, script, ...scriptArgs) {
    await page.evaluate(`
    new Promise((resolve, reject) => {
    const callback = (errMessage) => {
    if (errMessage)
    reject(new Error(errMessage));
    else
    resolve();
    };
    (function() {${script}}).apply(null, [...${JSON.stringify(scriptArgs)}, callback]);
    })
    `);
    }

    await executeScriptAsync(page, clientSideScripts.waitForAngular, '');
    }

    If you don't want to keep a version protractor around, you can also use this simpler approach using this function (only works for Angular 2+):

    async function waitForAngular(page) {
    await page.evaluate(async () => {
    // @ts-expect-error
    if (window.getAllAngularTestabilities) {
    // @ts-expect-error
    await Promise.all(window.getAllAngularTestabilities().map(whenStable));
    // @ts-expect-error
    async function whenStable(testability) {
    return new Promise(res => testability.whenStable(res));
    }
    }
    });
    }
  3. Polyfill 用法

    const page = await context.newPage();
    await page.goto('https://example.org');
    await waitForAngular(page);

Playwright Test 超级能力

一旦你开始使用Playwright Test,你将获得很多功能!

  • 完全零配置的TypeScript支持
  • 所有主流操作系统(Windows、macOS、Ubuntu)上运行所有网页引擎(Chrome、Firefox、Safari)的测试
  • 全面支持多源站、(i)frames标签页和上下文
  • 在多个浏览器中并行运行测试
  • 内置测试artifact collection

你还将获得这些✨超棒工具✨,它们都随Playwright Test捆绑提供:

延伸阅读

了解更多关于Playwright测试运行器的信息: