-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(roll): roll to ToT Playwright (17-10-24) (#1565)
- Loading branch information
1 parent
c31dbcf
commit 74ee03e
Showing
3 changed files
with
69 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,11 +22,11 @@ These are the recommended built-in locators. | |
- [Page.GetByTestId()](#locate-by-test-id) to locate an element based on its `data-testid` attribute (other attributes can be configured). | ||
|
||
```csharp | ||
await page.GetByLabel("User Name").FillAsync("John"); | ||
await Page.GetByLabel("User Name").FillAsync("John"); | ||
|
||
await page.GetByLabel("Password").FillAsync("secret-password"); | ||
await Page.GetByLabel("Password").FillAsync("secret-password"); | ||
|
||
await page.GetByRole(AriaRole.Button, new() { Name = "Sign in" }).ClickAsync(); | ||
await Page.GetByRole(AriaRole.Button, new() { Name = "Sign in" }).ClickAsync(); | ||
|
||
await Expect(Page.GetByText("Welcome, John!")).ToBeVisibleAsync(); | ||
``` | ||
|
@@ -50,7 +50,7 @@ For example, consider the following DOM structure. | |
Locate the element by its role of `button` with name "Sign in". | ||
|
||
```csharp | ||
await page.GetByRole(AriaRole.Button, new() { Name = "Sign in" }).ClickAsync(); | ||
await Page.GetByRole(AriaRole.Button, new() { Name = "Sign in" }).ClickAsync(); | ||
``` | ||
|
||
:::note | ||
|
@@ -60,7 +60,7 @@ Use the [code generator](./codegen.mdx) to generate a locator, and then edit it | |
Every time a locator is used for an action, an up-to-date DOM element is located in the page. In the snippet below, the underlying DOM element will be located twice, once prior to every action. This means that if the DOM changes in between the calls due to re-render, the new element corresponding to the locator will be used. | ||
|
||
```csharp | ||
var locator = page.GetByRole(AriaRole.Button, new() { Name = "Sign in" }); | ||
var locator = Page.GetByRole(AriaRole.Button, new() { Name = "Sign in" }); | ||
|
||
await locator.HoverAsync(); | ||
await locator.ClickAsync(); | ||
|
@@ -69,7 +69,7 @@ await locator.ClickAsync(); | |
Note that all methods that create a locator, such as [Page.GetByLabel()](/api/class-page.mdx#page-get-by-label), are also available on the [Locator] and [FrameLocator] classes, so you can chain them and iteratively narrow down your locator. | ||
|
||
```csharp | ||
var locator = page | ||
var locator = Page | ||
.FrameLocator("#my-frame") | ||
.GetByRole(AriaRole.Button, new() { Name = "Sign in" }); | ||
|
||
|
@@ -109,11 +109,11 @@ await Expect(Page | |
.GetByRole(AriaRole.Heading, new() { Name = "Sign up" })) | ||
.ToBeVisibleAsync(); | ||
|
||
await page | ||
await Page | ||
.GetByRole(AriaRole.Checkbox, new() { Name = "Subscribe" }) | ||
.CheckAsync(); | ||
|
||
await page | ||
await Page | ||
.GetByRole(AriaRole.Button, new() { | ||
NameRegex = new Regex("submit", RegexOptions.IgnoreCase) | ||
}) | ||
|
@@ -150,7 +150,7 @@ For example, consider the following DOM structure. | |
You can fill the input after locating it by the label text: | ||
|
||
```csharp | ||
await page.GetByLabel("Password").FillAsync("secret"); | ||
await Page.GetByLabel("Password").FillAsync("secret"); | ||
``` | ||
|
||
:::note[When to use label locators] | ||
|
@@ -176,7 +176,7 @@ For example, consider the following DOM structure. | |
You can fill the input after locating it by the placeholder text: | ||
|
||
```csharp | ||
await page | ||
await Page | ||
.GetByPlaceholder("[email protected]") | ||
.FillAsync("[email protected]"); | ||
``` | ||
|
@@ -252,7 +252,7 @@ For example, consider the following DOM structure. | |
You can click on the image after locating it by the text alternative: | ||
|
||
```csharp | ||
await page.GetByAltText("playwright logo").ClickAsync(); | ||
await Page.GetByAltText("playwright logo").ClickAsync(); | ||
``` | ||
|
||
:::note[When to use alt locators] | ||
|
@@ -304,7 +304,7 @@ For example, consider the following DOM structure. | |
You can locate the element by its test id: | ||
|
||
```csharp | ||
await page.GetByTestId("directions").ClickAsync(); | ||
await Page.GetByTestId("directions").ClickAsync(); | ||
``` | ||
|
||
:::note[When to use testid locators] | ||
|
@@ -336,27 +336,27 @@ In your html you can now use `data-pw` as your test id instead of the default `d | |
And then locate the element as you would normally do: | ||
|
||
```csharp | ||
await page.GetByTestId("directions").ClickAsync(); | ||
await Page.GetByTestId("directions").ClickAsync(); | ||
``` | ||
|
||
### Locate by CSS or XPath | ||
|
||
If you absolutely must use CSS or XPath locators, you can use [Page.Locator()](/api/class-page.mdx#page-locator) to create a locator that takes a selector describing how to find an element in the page. Playwright supports CSS and XPath selectors, and auto-detects them if you omit `css=` or `xpath=` prefix. | ||
|
||
```csharp | ||
await page.Locator("css=button").ClickAsync(); | ||
await page.Locator("xpath=//button").ClickAsync(); | ||
await Page.Locator("css=button").ClickAsync(); | ||
await Page.Locator("xpath=//button").ClickAsync(); | ||
|
||
await page.Locator("button").ClickAsync(); | ||
await page.Locator("//button").ClickAsync(); | ||
await Page.Locator("button").ClickAsync(); | ||
await Page.Locator("//button").ClickAsync(); | ||
``` | ||
|
||
XPath and CSS selectors can be tied to the DOM structure or implementation. These selectors can break when the DOM structure changes. Long CSS or XPath chains below are an example of a **bad practice** that leads to unstable tests: | ||
|
||
```csharp | ||
await page.Locator("#tsf > div:nth-child(2) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input").ClickAsync(); | ||
await Page.Locator("#tsf > div:nth-child(2) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input").ClickAsync(); | ||
|
||
await page.Locator("//*[@id='tsf']/div[2]/div[1]/div[1]/div/div[2]/input").ClickAsync(); | ||
await Page.Locator("//*[@id='tsf']/div[2]/div[1]/div[1]/div/div[2]/input").ClickAsync(); | ||
``` | ||
|
||
:::note[When to use this] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters