我将一些剧作家测试从 js 中的 Visual Studio 代码移植到使用 xuint 的 Visual Studio C#。
我似乎无法让 Expect 命令在 Visual Studio 中工作。
在 javascript 项目中,我将执行以下操作来检查元素是否包含一些文本:
await expect(page.locator('elementtofind')).toContainText('myText');
Run Code Online (Sandbox Code Playgroud)
在 C# 中,如果我写同样的东西,我会在expect命令上得到“名称‘标识符’在当前上下文中不存在”错误
在 JS 项目中我的导入语句是
import { test, expect } from '@playwright/test';
Run Code Online (Sandbox Code Playgroud)
在 C# 项目中我使用:
using Microsoft.Playwright;
Run Code Online (Sandbox Code Playgroud)
其中似乎不包含期望
作为临时解决方法,我在 C# 中执行以下操作以获得相同的结果
var item1 = page.Locator("elementtofind");
Assert.Equal("myText", await item1.InnerTextAsync());
Run Code Online (Sandbox Code Playgroud)
但想在我的测试中使用预期功能。
有谁知道我如何在我的项目中实现这一点?
dar*_*iom 10
不幸的是,如果您使用 xUnit,则无法直接使用 Expect。对于 NUnit 和 MSTest,您可以从提供此方法的基类中PageTest派生测试。xUnit 的未来版本可能可以创建类似的基类,但目前我还没有找到实现等效功能的好方法。
作为 xUnit 中的解决方法,我在测试中直接使用 PlaywrightAssertions类,例如:
await Assertions.Expect(Page.Locator("h1")).ToHaveTextAsync("Foo");
Run Code Online (Sandbox Code Playgroud)