You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> [Playwright.js](https://playwright.com/) is an open-source automation library developed by Microsoft, primarily designed for end-to-end testing and web scraping of web applications.
27
27
28
+
We assume you already have a project with Puppeteer.
29
+
30
+
### add the dependency
31
+
32
+
```bash
33
+
npm install @midscene/web --save-dev
34
+
```
35
+
28
36
### Step 1. update playwright.config.ts
29
37
30
38
```diff
@@ -49,60 +57,99 @@ export const test = base.extend<PlayWrightAiFixtureType>(PlaywrightAiFixture());
49
57
50
58
### Step 3. write the test case
51
59
52
-
Save the following code as `./e2e/ebay.spec.ts`;
60
+
Save the following code as `./e2e/ebay-search.spec.ts`
awaitai('type "Headphones" in search box, hit Enter');
75
+
76
+
// 👀 find the items
77
+
const items =awaitaiQuery(
78
+
"{itemTitle: string, price: Number}[], find item in list and corresponding price"
79
+
);
80
+
81
+
console.log("headphones in stock", items);
82
+
expect(items?.length).toBeGreaterThan(1);
83
+
});
84
+
56
85
```
57
86
58
87
### Step 4. run the test case
59
88
60
89
```bash
61
-
npx playwright test ./test/ebay.spec.ts
90
+
npx playwright test ./e2e/ebay-search.spec.ts
62
91
```
63
92
64
93
### Step 5. view test report after running
65
94
95
+
Follow the instructions in the command line to server the report
96
+
66
97
```bash
67
98
68
99
```
69
100
70
-
71
101
## Integrate with Puppeteer
72
102
73
103
> [Puppeteer](https://pptr.dev/) is a Node.js library which provides a high-level API to control Chrome or Firefox over the DevTools Protocol or WebDriver BiDi. Puppeteer runs in the headless (no visible UI) by default but can be configured to run in a visible ("headful") browser.
headless: false, // here we use headed mode to help debug
125
+
});
126
+
127
+
const page =awaitbrowser.newPage();
128
+
awaitpage.setViewport({
129
+
width: 1280,
130
+
height: 800,
131
+
deviceScaleFactor: 1,
132
+
});
133
+
134
+
awaitpage.goto("https://www.ebay.com");
135
+
awaitsleep(5000);
136
+
137
+
// 👀 init MidScene agent
138
+
const mid =newPuppeteerAgent(page);
139
+
140
+
// 👀 type keywords, perform a search
141
+
awaitmid.aiAction('type "Headphones" in search box, hit Enter');
142
+
awaitsleep(5000);
143
+
144
+
// 👀 understand the page content, find the items
145
+
const items =awaitmid.aiQuery(
146
+
"{itemTitle: string, price: Number}[], find item in list and corresponding price"
147
+
);
148
+
console.log("headphones in stock", items);
149
+
150
+
awaitbrowser.close();
151
+
})()
104
152
);
105
-
console.log('headphones in stock', items);
106
153
```
107
154
108
155
:::tip
@@ -116,6 +163,8 @@ await mid.aiQuery(
116
163
```
117
164
:::
118
165
166
+
### Step 3. run
167
+
119
168
Using ts-node to run, you will get the data of Headphones on ebay:
120
169
121
170
```bash
@@ -135,6 +184,13 @@ npx ts-node demo.ts
135
184
# ]
136
185
```
137
186
138
-
After running, MidScene will generate a log dump, which is placed in `./midscene_run/latest.web-dump.json` by default. Then put this file into [Visualization Tool](/visualization/), and you will have a clearer understanding of the process.
187
+
### Step 4. view test report after running
188
+
189
+
After running, MidScene will generate a log dump, which is placed in `./midscene_run/report/latest.web-dump.json` by default. Then put this file into [Visualization Tool](/visualization/), and you will have a clearer understanding of the process.
190
+
191
+
Click the 'Load Demo' button in the [Visualization Tool](/visualization/), you will be able to see the results of the previous code as well as some other samples.
192
+
193
+
194
+
## Demo Projects
139
195
140
-
Click the 'Load Demo' button in the [Visualization Tool](/visualization/), you will be able to see the results of the previous code as well as some other samples.
196
+
You can clone a complete demo project in this repo: https://github.com/web-infra-dev/midscene-example/
0 commit comments