# Page
The page module provides a way to control the Weixin Mini Program page.
# attribute
# page.path
Page path.
page.path: string
# page.query
Page parameters.
page.query: Object
# method
# page.$
Get the page elements.
page.$(selector: string): Promise<Element>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| selector | string | yes | - | Selector |
As with WXSS, only some CSS selectors are supported, click here for details.
Example code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const element = await page.$('.index-desc')
console.log(element.tagName) // -> 'view'
})
# page.$$
Get an array of page elements.
page.$$(selector: string): Promise<Element[]>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| selector | string | yes | - | Selector |
This method, like $, cannot select elements within a custom component. Use the element. $ .
Example code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const elements = await page.$$('.kind-list-text')
console.log(elements.length)
})
# page.waitFor
Wait until the specified conditions are established.
page.waitFor(condition: string | number | Function): Promise<void>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| condition | string number Function | yes | - | Waiting Conditions |
If the condition is a string type, then the parameter is treated as a selector, and when the selector selects a number of elements that are not zero, the wait ends.
If the condition is of type number, then this parameter is treated as a timeout, and when the specified time has elapsed, the wait ends.
If the condition is of type Function, then the argument is treated as an assertion function, and when the function returns the true value, the wait ends.
Example code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
await page.waitFor(5000) // 等待 5 秒
await page.waitFor('picker') // 等待页面中出现 picker 元素
await page.waitFor(async () => {
return (await page.$$('picker')).length > 5
}) // 等待页面中 picker 元素数量大于 5
})
# page.data
Transfer data path automator 0. 6. 0, base library 2. 9. 0 started support.
Get page rendering data.
page.data(path?: string): Promise<Object>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| path | string | no | - | Data Path |
Example code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
console.log(await page.data('list'))
})
# page.setData
Set the page rendering data.
page.setData(data: Object): Promise<void>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| data | Object | yes | - | The data to change |
Example code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
await page.setData({
text: 'changed data'
})
})
# page.size
Get the page size.
page.size(): Promise<Object>
Return value explaination
| field | type | Introductions |
|---|---|---|
| width | number | Page scrolling width |
| height | number | The page can scroll at a height |
Example code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
const { width, height } = await page.size()
console.log(width, height)
})
# page.scrollTop
Automator 0.7.0 is supported.
Get the page scrolling location.
page.scrollTop(): Promise<number>
Example code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
await miniProgram.pageScrollTo(20)
console.log(await page.scrollTop())
})
# page.callMethod
Call the page to specify the method.
page.callMethod(method: string, ...args: any[]): Promise<any>
Parameter explaination
| field | type | Required to fill in | Default values | Introductions |
|---|---|---|---|---|
| method | string | yes | - | Method name that needs to be called |
| ...args | array|no | - | Method parameters |
Example code:
automator.launch().then(async miniProgram => {
const page = await miniProgram.currentPage()
await page.callMethod('onShareAppMessage')
})