expect-playwright
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -22,3 +22,3 @@ // copied into our codebase for autocompletion purposes | ||
*/ | ||
toHaveText(value: string, options?: PageWaitForSelectorOptions): Promise<R>; | ||
toHaveText(value: string): Promise<R>; | ||
/** | ||
@@ -25,0 +25,0 @@ * Will compare the element's textContent on the page determined by the selector with the given text. |
@@ -10,2 +10,3 @@ "use strict"; | ||
const toEqualValue_1 = __importDefault(require("./toEqualValue")); | ||
const toHaveSelectorCount_1 = __importDefault(require("./toHaveSelectorCount")); | ||
exports.default = { | ||
@@ -16,2 +17,3 @@ toHaveText: toHaveText_1.default, | ||
toEqualValue: toEqualValue_1.default, | ||
toHaveSelectorCount: toHaveSelectorCount_1.default, | ||
}; |
@@ -8,5 +8,2 @@ "use strict"; | ||
const actualCount = (await page.$$(selector)).length; | ||
if (actualCount === 0) { | ||
throw new Error('Element not found'); | ||
} | ||
if (actualCount === expectedValue) { | ||
@@ -13,0 +10,0 @@ return { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const utils_1 = require("../utils"); | ||
const MAX_LENGTH = 100; | ||
const getResultText = (actualTextContent, type, selector) => { | ||
if (type === "Page") { | ||
return selector ? | ||
`${actualTextContent.length < MAX_LENGTH ? | ||
utils_1.quote(actualTextContent) : 'content'} of ${utils_1.quote(selector)} selector` : 'page content'; | ||
} | ||
if (type === "ElementHandle") { | ||
return selector && actualTextContent.length < MAX_LENGTH ? utils_1.quote(actualTextContent) : 'element content'; | ||
} | ||
return ''; | ||
}; | ||
const toHaveText = async (...args) => { | ||
try { | ||
const { elementHandle, selector, expectedValue } = await utils_1.getElementText(...args); | ||
const { elementHandle, selector, type, expectedValue } = await utils_1.getElementText(...args); | ||
/* istanbul ignore next */ | ||
const actualTextContent = await elementHandle.evaluate((el) => el.textContent); | ||
const actualTextContent = await elementHandle.evaluate((el) => el.textContent) || ''; | ||
const resultTextText = getResultText(actualTextContent, type, selector); | ||
if (actualTextContent === null || actualTextContent === void 0 ? void 0 : actualTextContent.includes(expectedValue)) { | ||
return { | ||
pass: true, | ||
message: () => `${utils_1.quote(expectedValue)} is included in ${utils_1.quote(actualTextContent)}.` | ||
message: () => `${utils_1.quote(expectedValue)} is included in ${resultTextText}.` | ||
}; | ||
@@ -17,3 +30,3 @@ } | ||
pass: false, | ||
message: () => `${utils_1.quote(expectedValue)} is not included in ${utils_1.quote(actualTextContent)}${selector ? ' of ' + utils_1.quote(selector) + "." : '.'}` | ||
message: () => `${utils_1.quote(expectedValue)} is not included in ${resultTextText}` | ||
}; | ||
@@ -20,0 +33,0 @@ } |
@@ -20,39 +20,62 @@ "use strict"; | ||
const getElementText = async (...args) => { | ||
/** | ||
* Handle the following cases: | ||
* - expect(page).foo("bar") | ||
* - expect(element).foo("bar") | ||
*/ | ||
if (args.length === 2) { | ||
if (args.length > 1) { | ||
const type = exports.detectExpectType(args[0]); | ||
if (type === ExpectTypeElementHandle) { | ||
/** | ||
* Handle the following cases: | ||
* - expect(page).foo("bar") | ||
* - expect(element).foo("bar") | ||
*/ | ||
if (args.length === 2) { | ||
if (type === ExpectTypeElementHandle) { | ||
return { | ||
elementHandle: args[0], | ||
expectedValue: args[1], | ||
type | ||
}; | ||
} | ||
const page = args[0]; | ||
return { | ||
elementHandle: args[0], | ||
expectedValue: args[1] | ||
elementHandle: await page.$("body"), | ||
expectedValue: args[1], | ||
type | ||
}; | ||
} | ||
const page = args[0]; | ||
return { | ||
elementHandle: await page.$("body"), | ||
expectedValue: args[1] | ||
}; | ||
} | ||
/** | ||
* Handle the following case: | ||
* - expect(page).foo("#foo", "bar") | ||
*/ | ||
if (args.length === 3 && lastElementHasType(args, "string") || args.length === 4 && lastElementHasType(args, "object")) { | ||
const selector = args[1]; | ||
const page = args[0]; | ||
try { | ||
await page.waitForSelector(selector, args[3]); | ||
/** | ||
* Handle the following case: | ||
* - expect(page).foo("#foo", "bar") | ||
*/ | ||
if (type === ExpectTypePage) { | ||
const page = args[0]; | ||
const selector = args[1]; | ||
if (args.length === 3) { | ||
if (lastElementHasType(args, "string")) { | ||
try { | ||
await page.waitForSelector(selector); | ||
} | ||
catch (err) { | ||
throw new Error(`Timeout exceed for element ${exports.quote(selector)}`); | ||
} | ||
return { | ||
elementHandle: await page.$(selector), | ||
expectedValue: args[2], | ||
selector, | ||
type | ||
}; | ||
} | ||
} | ||
if (args.length === 4 && lastElementHasType(args, "object")) { | ||
try { | ||
await page.waitForSelector(selector, args[3]); | ||
} | ||
catch (err) { | ||
throw new Error(`Timeout exceed for element ${exports.quote(selector)}`); | ||
} | ||
return { | ||
elementHandle: await page.$(selector), | ||
expectedValue: args[2], | ||
selector, | ||
type | ||
}; | ||
} | ||
} | ||
catch (err) { | ||
throw new Error(`Timeout exceed for element ${exports.quote(selector)}`); | ||
} | ||
return { | ||
elementHandle: await page.$(selector), | ||
expectedValue: args[2], | ||
selector | ||
}; | ||
} | ||
@@ -59,0 +82,0 @@ throw new Error(`Invalid input length: ${args.length}`); |
{ | ||
"name": "expect-playwright", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "types": "./global.d.ts", |
@@ -54,2 +54,3 @@ # expect-playwright | ||
- [toHaveSelector](#toHaveSelector) | ||
- [toHaveSelectorCount](#toHaveSelectorCount) | ||
- [toHaveText](#toHaveText) | ||
@@ -61,3 +62,3 @@ - [toEqualText](#toEqualText) | ||
**expect(page: [Page]).toHaveSelector(selector: string, options?: [PageWaitForSelectorOptions](https://github.com/microsoft/playwright/blob/master/docs/api.md#pagewaitforselectorselector-options))** | ||
**expect(page: [Page]).toHaveSelector(selector: string, options?: [PageWaitForSelectorOptions](https://playwright.dev/docs/api/class-page/#pagewaitforselectorselector-options))** | ||
@@ -74,9 +75,20 @@ This function waits as a maximum as the timeout exceeds for a given selector once it appears on the page. | ||
### toHaveSelectorCount | ||
**expect(page: [Page]).toHaveSelector(selector: string, value: number, options?: [PageWaitForSelectorOptions](https://playwright.dev/docs/api/class-page/#pagewaitforselectorselector-options))** | ||
This function checks if the count of a given selector is the same as the provided value. | ||
```javascript | ||
await expect(page).toHaveSelectorCount(".my-element", 3) | ||
``` | ||
### toHaveText | ||
This function checks if the the `textContent` of a given element contains the provided value. | ||
This function checks if the `textContent` of a given element contains the provided value. | ||
You can do this via a selector on the whole page: | ||
**expect(page: [Page]).toHaveText(selector: string, value: string, options?: [PageWaitForSelectorOptions](https://github.com/microsoft/playwright/blob/master/docs/api.md#pagewaitforselectorselector-options))** | ||
**expect(page: [Page]).toHaveText(selector: string, value: string, options?: [PageWaitForSelectorOptions](https://playwright.dev/docs/api/class-page/#pagewaitforselectorselector-options))** | ||
@@ -89,3 +101,3 @@ ```javascript | ||
**expect(page: [Page]).toHaveText(value: string, options?: [PageWaitForSelectorOptions](https://github.com/microsoft/playwright/blob/master/docs/api.md#pagewaitforselectorselector-options))** | ||
**expect(page: [Page]).toHaveText(value: string)** | ||
@@ -98,3 +110,3 @@ ```javascript | ||
**expect(page: [ElementHandle]).toHaveText(value: string, options?: [PageWaitForSelectorOptions](https://github.com/microsoft/playwright/blob/master/docs/api.md#pagewaitforselectorselector-options))** | ||
**expect(page: [ElementHandle]).toHaveText(value: string)** | ||
@@ -114,3 +126,3 @@ ```javascript | ||
**expect(page: [Page]).toEqualText(selector: string, value: string, options?: [PageWaitForSelectorOptions](https://github.com/microsoft/playwright/blob/master/docs/api.md#pagewaitforselectorselector-options))** | ||
**expect(page: [Page]).toEqualText(selector: string, value: string, options?: [PageWaitForSelectorOptions](https://playwright.dev/docs/api/class-page/#pagewaitforselectorselector-options))** | ||
@@ -123,3 +135,3 @@ ```javascript | ||
**expect(page: [Page]).toEqualText(value: string, options?: [PageWaitForSelectorOptions](https://github.com/microsoft/playwright/blob/master/docs/api.md#pagewaitforselectorselector-options))** | ||
**expect(page: [Page]).toEqualText(value: string, options?: [PageWaitForSelectorOptions](https://playwright.dev/docs/api/class-page/#pagewaitforselectorselector-options))** | ||
@@ -132,3 +144,3 @@ ```javascript | ||
**expect(page: [ElementHandle]).toEqualText(value: string, options?: [PageWaitForSelectorOptions](https://github.com/microsoft/playwright/blob/master/docs/api.md#pagewaitforselectorselector-options))** | ||
**expect(page: [ElementHandle]).toEqualText(value: string, options?: [PageWaitForSelectorOptions](https://playwright.dev/docs/api/class-page/#pagewaitforselectorselector-options))** | ||
@@ -148,3 +160,3 @@ ```javascript | ||
**expect(page: [Page]).toEqualValue(selector: string, value: string, options?: [PageWaitForSelectorOptions](https://github.com/microsoft/playwright/blob/master/docs/api.md#pagewaitforselectorselector-options))** | ||
**expect(page: [Page]).toEqualValue(selector: string, value: string, options?: [PageWaitForSelectorOptions](https://playwright.dev/docs/api/class-page/#pagewaitforselectorselector-options))** | ||
@@ -157,3 +169,3 @@ ```javascript | ||
**expect(page: [ElementHandle]).toEqualValue(value: string, options?: [PageWaitForSelectorOptions](https://github.com/microsoft/playwright/blob/master/docs/api.md#pagewaitforselectorselector-options))** | ||
**expect(page: [ElementHandle]).toEqualValue(value: string, options?: [PageWaitForSelectorOptions](https://playwright.dev/docs/api/class-page/#pagewaitforselectorselector-options))** | ||
@@ -160,0 +172,0 @@ ```javascript |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29035
18
321
204
1