@bddr/selenium 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +104 -2
- package/dist/index.d.ts +72 -0
- package/dist/index.js +179 -0
- package/package.json +62 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 afsarali273
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,105 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @bddr/selenium
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Selenium WebDriver for [bddr](../../README.md), as a plugin. bddr has no dependency on Selenium: your project installs
|
|
4
|
+
both, and this plugin connects them through bddr fixtures. It works the same way as [`@bddr/playwright`](../playwright/README.md).
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
npm i -D @bddr/selenium selenium-webdriver
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
// bddr.config.ts
|
|
12
|
+
import { defineConfig } from '@bddr/cli';
|
|
13
|
+
import { selenium } from '@bddr/selenium';
|
|
14
|
+
|
|
15
|
+
export default defineConfig({
|
|
16
|
+
plugins: [selenium({ browser: 'chrome', baseURL: 'https://shop.example.com', screenshot: 'only-on-failure' })],
|
|
17
|
+
// or untyped: plugins: [['@bddr/selenium', { browser: 'firefox' }]]
|
|
18
|
+
});
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
// steps: ask for what you need
|
|
23
|
+
Given('I open {string}', ({ open }, path: string) => open(path));
|
|
24
|
+
When('I log in', async ({ driver, selenium: { By } }) => {
|
|
25
|
+
await driver.findElement(By.css('#user')).sendKeys('ana');
|
|
26
|
+
});
|
|
27
|
+
Then('I see the heading', async ({ wait, selenium: { By, until } }) => {
|
|
28
|
+
await wait(until.elementLocated(By.css('h1')));
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// page objects and step classes: inject()
|
|
32
|
+
class LoginPage { private driver = inject('driver'); open() { return this.driver.get('/login'); } }
|
|
33
|
+
@Steps() class LoginSteps { private login = inject(LoginPage); /* @Given(...) methods */ }
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Add `import '@bddr/selenium';` (or `"types": ["@bddr/selenium"]` in tsconfig) in one support file so `ctx.driver`,
|
|
37
|
+
`({ driver })` and `inject('driver')` are typed.
|
|
38
|
+
|
|
39
|
+
bddr starts a fixture only when a step names it (`({ driver })`, `ctx.driver`, `const { driver } = ctx`, `inject('driver')`).
|
|
40
|
+
A helper that receives the whole `ctx` and reads `ctx.driver` inside is not seen; destructure it in the step and pass it on.
|
|
41
|
+
|
|
42
|
+
## Fixtures
|
|
43
|
+
|
|
44
|
+
| name | scope | what |
|
|
45
|
+
|---|---|---|
|
|
46
|
+
| `selenium` | worker | the `selenium-webdriver` module: `By`, `Key`, `until`, `Capabilities` ... |
|
|
47
|
+
| `driver` | scenario | a fresh `WebDriver` (one browser session) per scenario attempt; screenshot at the end; quit after |
|
|
48
|
+
| `open` | scenario | `await open('/login')`: `driver.get()` relative to `baseURL` |
|
|
49
|
+
| `wait` | scenario | `await wait(until...)`: `driver.wait()` with `waitTimeoutMs` as the default timeout |
|
|
50
|
+
| `newDriver` | scenario | `await newDriver({ browser: 'firefox' })`: extra sessions (second user, other browser); quit after the scenario |
|
|
51
|
+
|
|
52
|
+
Selenium has no browser/context split, so every session is a real browser process. A scenario that uses none of these
|
|
53
|
+
starts nothing. Groups, dependencies, locks, `--pool process`, `--shard` and `--watch` work as usual.
|
|
54
|
+
|
|
55
|
+
## Options
|
|
56
|
+
|
|
57
|
+
| option | default | |
|
|
58
|
+
|---|---|---|
|
|
59
|
+
| `browser` | `'chrome'` | `'chrome' \| 'firefox' \| 'edge' \| 'safari'`; `-D browser=firefox` overrides |
|
|
60
|
+
| `headless` | `true` | `-D headed=true` overrides (safari has no headless mode) |
|
|
61
|
+
| `args` | | extra browser command line arguments (`'--lang=de'`, `'--incognito'` ...) |
|
|
62
|
+
| `capabilities` | | extra W3C capabilities; vendor `args` are combined with the ones above |
|
|
63
|
+
| `server` | | remote endpoint (Selenium Grid, a cloud provider): `'http://grid:4444'`; `-D server=` overrides |
|
|
64
|
+
| `baseURL` | | base of `open('/path')`; `-D baseURL=` overrides |
|
|
65
|
+
| `windowSize` | | `{ width, height }` |
|
|
66
|
+
| `implicitWaitMs`, `pageLoadTimeoutMs`, `scriptTimeoutMs` | Selenium's | session timeouts |
|
|
67
|
+
| `waitTimeoutMs` | `10000` | default timeout of `wait(...)` |
|
|
68
|
+
| `screenshot` | `'only-on-failure'` | `'off' \| 'on' \| 'only-on-failure'`; attached to the report |
|
|
69
|
+
| `reuseSession` | `'off'` | `'serial'`: every `@serial` feature shares one browser session |
|
|
70
|
+
|
|
71
|
+
The plugin uses the `selenium-webdriver` of your project (so your own `import { until } from 'selenium-webdriver'` and the driver
|
|
72
|
+
share one copy; Selenium checks conditions with `instanceof`). Selenium Manager (bundled with selenium-webdriver) finds or downloads the browser driver; nothing to install by hand
|
|
73
|
+
for chrome, firefox and edge.
|
|
74
|
+
|
|
75
|
+
## One browser session per feature
|
|
76
|
+
|
|
77
|
+
```gherkin
|
|
78
|
+
@serial @reuseSession
|
|
79
|
+
Feature: Checkout journey
|
|
80
|
+
Scenario: log in # starts the browser
|
|
81
|
+
Scenario: add to basket # same session, still logged in
|
|
82
|
+
Scenario: pay # same session
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
- Needs `@serial` on the Feature (a shared session only works one scenario at a time); `@reuseSession` without it is an
|
|
86
|
+
error. `reuseSession: 'serial'` turns it on for every serial feature; `@freshSession` opts one out.
|
|
87
|
+
- The browser is quit when the feature ends. In `--pool process` the whole feature runs in one worker process.
|
|
88
|
+
- Screenshots are still per failed scenario. A retried scenario continues in whatever state the failed attempt left.
|
|
89
|
+
- Selecting one scenario of the feature (`-t`, `file:line`) runs it alone on a new session: it does not replay the
|
|
90
|
+
earlier scenarios.
|
|
91
|
+
|
|
92
|
+
## Example
|
|
93
|
+
|
|
94
|
+
`example/` is a complete project that runs offline (a local HTTP server is a worker fixture): login scenarios with a
|
|
95
|
+
page object, and a `@serial @reuseSession` journey.
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
cd plugins/selenium && npm install
|
|
99
|
+
cd example && npx bddr # or: -D browser=firefox, -D headed=true, --pool process -w 2
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Developing
|
|
103
|
+
|
|
104
|
+
`npm run verify` (typecheck, build, tests). Tests use a fake `selenium-webdriver`, so they need no browsers; the fake
|
|
105
|
+
behaves like the real `Builder` (for example `withCapabilities` replaces the browser name), which a real run caught once.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { Condition, WebDriver, WebElement, WebElementCondition } from 'selenium-webdriver';
|
|
2
|
+
import { type BddrPlugin } from '@bddr/runtime';
|
|
3
|
+
type SeleniumModule = typeof import('selenium-webdriver');
|
|
4
|
+
declare module '@bddr/runtime' {
|
|
5
|
+
interface Fixtures {
|
|
6
|
+
selenium: SeleniumModule;
|
|
7
|
+
driver: WebDriver;
|
|
8
|
+
/** driver.get() relative to the plugin's `baseURL`. */
|
|
9
|
+
open: (path?: string) => Promise<void>;
|
|
10
|
+
/** driver.wait() with `waitTimeoutMs` as the default timeout. */
|
|
11
|
+
wait: {
|
|
12
|
+
/** `until.elementLocated(...)` and friends resolve to the element. */
|
|
13
|
+
(condition: WebElementCondition, timeoutMs?: number, message?: string): Promise<WebElement>;
|
|
14
|
+
<T>(condition: Condition<T> | ((driver: WebDriver) => T | Promise<T>), timeoutMs?: number, message?: string): Promise<T>;
|
|
15
|
+
};
|
|
16
|
+
/** Start an extra browser session (same options as `driver`, plus your overrides); quit after the scenario. */
|
|
17
|
+
newDriver: (options?: Partial<DriverOptions>) => Promise<WebDriver>;
|
|
18
|
+
/** The driver shared by a feature's scenarios when it reuses one (see `reuseSession`). */
|
|
19
|
+
sharedDriver: WebDriver;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export type BrowserName = 'chrome' | 'firefox' | 'edge' | 'safari';
|
|
23
|
+
/** `only-on-failure`: attached only when the scenario failed. */
|
|
24
|
+
export type ArtifactMode = 'off' | 'on' | 'only-on-failure';
|
|
25
|
+
/** What identifies one browser session; the plugin options minus the plugin-wide settings. */
|
|
26
|
+
export interface DriverOptions {
|
|
27
|
+
/** Default 'chrome'. `-D browser=firefox` overrides. */
|
|
28
|
+
browser: BrowserName;
|
|
29
|
+
/** Default true (not supported by safari). `-D headed=true` overrides. */
|
|
30
|
+
headless: boolean;
|
|
31
|
+
/** Extra command line arguments for the browser ('--lang=de', '--incognito' ...). */
|
|
32
|
+
args: string[];
|
|
33
|
+
/** Extra W3C capabilities (merged over the plugin's; `goog:chromeOptions` / `moz:firefoxOptions` args are combined). */
|
|
34
|
+
capabilities: Record<string, unknown>;
|
|
35
|
+
/** Remote WebDriver endpoint: a Selenium Grid or cloud provider, 'http://grid:4444'. `-D server=` overrides. */
|
|
36
|
+
server: string;
|
|
37
|
+
/** Window size. */
|
|
38
|
+
windowSize: {
|
|
39
|
+
width: number;
|
|
40
|
+
height: number;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export interface SeleniumPluginOptions extends Partial<DriverOptions> {
|
|
44
|
+
/** Base of relative `open('/path')` calls. `-D baseURL=` overrides. */
|
|
45
|
+
baseURL?: string;
|
|
46
|
+
/** Implicit wait (ms) of every findElement. Default: Selenium's 0; prefer explicit `wait(until...)`. */
|
|
47
|
+
implicitWaitMs?: number;
|
|
48
|
+
/** Page load timeout (ms). Selenium's default is 300000. */
|
|
49
|
+
pageLoadTimeoutMs?: number;
|
|
50
|
+
/** Script timeout (ms). */
|
|
51
|
+
scriptTimeoutMs?: number;
|
|
52
|
+
/** Default timeout of the `wait` fixture (ms). Default 10000. */
|
|
53
|
+
waitTimeoutMs?: number;
|
|
54
|
+
/** Screenshot at the end of the scenario, attached to the report. Default 'only-on-failure'. */
|
|
55
|
+
screenshot?: ArtifactMode;
|
|
56
|
+
/**
|
|
57
|
+
* Reuse one browser session for every scenario of a @serial feature (cookies, storage and the open page carry over;
|
|
58
|
+
* the browser is quit when the feature ends). 'off' (default): a fresh session per scenario.
|
|
59
|
+
* 'serial': every @serial feature reuses. Per feature: `@reuseSession` (needs @serial) / `@freshSession`.
|
|
60
|
+
*/
|
|
61
|
+
reuseSession?: 'off' | 'serial';
|
|
62
|
+
}
|
|
63
|
+
/** Build the plugin. `load` is how the `selenium-webdriver` module is imported (tests pass a fake). */
|
|
64
|
+
export declare function createSeleniumPlugin(load?: () => Promise<SeleniumModule>): BddrPlugin<SeleniumPluginOptions>;
|
|
65
|
+
/** The plugin with Selenium from your project. Use as `plugins: ['@bddr/selenium']` or `plugins: [selenium({ ... })]`. */
|
|
66
|
+
declare const plugin: BddrPlugin<SeleniumPluginOptions>;
|
|
67
|
+
export default plugin;
|
|
68
|
+
/** Typed form for bddr.config.ts: `plugins: [selenium({ browser: 'firefox', screenshot: 'on' })]`. */
|
|
69
|
+
export declare function selenium(options?: SeleniumPluginOptions): {
|
|
70
|
+
name: string;
|
|
71
|
+
setup(): void | Promise<void>;
|
|
72
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @bddr/selenium: Selenium WebDriver for bddr, as a plugin. bddr does not depend on Selenium; your project does.
|
|
3
|
+
*
|
|
4
|
+
* // bddr.config.ts
|
|
5
|
+
* import { selenium } from '@bddr/selenium';
|
|
6
|
+
* export default defineConfig({ plugins: [selenium({ browser: 'chrome', baseURL: 'https://shop.example.com' })] });
|
|
7
|
+
*
|
|
8
|
+
* // steps
|
|
9
|
+
* Given('I open {string}', ({ open }, path: string) => open(path));
|
|
10
|
+
* When('I log in', async ({ driver, selenium: { By } }) => driver.findElement(By.css('#login')).click());
|
|
11
|
+
* class LoginPage { private driver = inject('driver'); }
|
|
12
|
+
*
|
|
13
|
+
* Fixtures:
|
|
14
|
+
* selenium worker the `selenium-webdriver` module (By, Key, until, Capabilities ...)
|
|
15
|
+
* driver scenario a fresh WebDriver (= one browser session) per scenario attempt; screenshot at the end
|
|
16
|
+
* With `reuseSession` a @serial feature gets ONE driver for all its scenarios instead
|
|
17
|
+
* (sharedDriver, feature scoped): log in once, keep cookies and the open page.
|
|
18
|
+
* open scenario `await open('/login')`: driver.get() relative to `baseURL`
|
|
19
|
+
* wait scenario `await wait(until.elementLocated(By.css('h1')))`: driver.wait() with the default timeout
|
|
20
|
+
* newDriver scenario `await newDriver({ browser: 'firefox' })`: extra sessions (a second user, another browser),
|
|
21
|
+
* quit after the scenario
|
|
22
|
+
*
|
|
23
|
+
* Run-time overrides with -D: `-D browser=firefox`, `-D headed=true`, `-D baseURL=https://staging.example.com`,
|
|
24
|
+
* `-D server=http://grid:4444` (a Selenium Grid / cloud endpoint).
|
|
25
|
+
*/
|
|
26
|
+
import { createRequire } from 'node:module';
|
|
27
|
+
import { join } from 'node:path';
|
|
28
|
+
import { defineFixture, definePlugin } from '@bddr/runtime';
|
|
29
|
+
const keep = (mode, status) => mode === 'on' || (mode === 'only-on-failure' && status === 'failed');
|
|
30
|
+
const bool = (v) => (v === undefined ? undefined : /^(1|true|yes|on)$/i.test(v));
|
|
31
|
+
const BROWSERS = ['chrome', 'firefox', 'edge', 'safari'];
|
|
32
|
+
/** Vendor capability that carries the command line arguments, and the flag that makes the browser headless. */
|
|
33
|
+
const VENDOR = {
|
|
34
|
+
chrome: { key: 'goog:chromeOptions', headless: '--headless=new' },
|
|
35
|
+
firefox: { key: 'moz:firefoxOptions', headless: '-headless' },
|
|
36
|
+
edge: { key: 'ms:edgeOptions', headless: '--headless=new' },
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Load `selenium-webdriver` from the PROJECT (the working directory), not from wherever this plugin is installed.
|
|
40
|
+
* Selenium checks `until.*` conditions with `instanceof`, so the driver and your own `import { until } from
|
|
41
|
+
* 'selenium-webdriver'` must come from the same copy; with a linked or duplicated install they would not.
|
|
42
|
+
*/
|
|
43
|
+
async function loadProjectSelenium() {
|
|
44
|
+
try {
|
|
45
|
+
return createRequire(join(process.cwd(), 'package.json'))('selenium-webdriver');
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
const mod = (await import('selenium-webdriver')); // CommonJS package
|
|
49
|
+
return mod.Builder ? mod : mod.default;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** Build the plugin. `load` is how the `selenium-webdriver` module is imported (tests pass a fake). */
|
|
53
|
+
export function createSeleniumPlugin(load = loadProjectSelenium) {
|
|
54
|
+
return definePlugin({
|
|
55
|
+
name: 'selenium',
|
|
56
|
+
setup(o = {}) {
|
|
57
|
+
const screenshot = o.screenshot ?? 'only-on-failure';
|
|
58
|
+
const waitTimeout = o.waitTimeoutMs ?? 10_000;
|
|
59
|
+
const baseURLOf = (ctx) => ctx.params['baseURL'] ?? o.baseURL;
|
|
60
|
+
defineFixture('selenium', () => load(), { scope: 'worker' });
|
|
61
|
+
/** Start one browser session. `extra` overrides the plugin options (newDriver). */
|
|
62
|
+
const start = async (ctx, extra = {}) => {
|
|
63
|
+
const selenium = await ctx.fixture('selenium');
|
|
64
|
+
const name = (extra.browser ?? ctx.params['browser'] ?? o.browser ?? 'chrome');
|
|
65
|
+
if (!BROWSERS.includes(name))
|
|
66
|
+
throw new Error(`@bddr/selenium: browser must be ${BROWSERS.join(', ')} (got "${name}")`);
|
|
67
|
+
const headed = bool(ctx.params['headed']);
|
|
68
|
+
const headless = extra.headless ?? (headed !== undefined ? !headed : (o.headless ?? true));
|
|
69
|
+
const server = extra.server ?? ctx.params['server'] ?? o.server;
|
|
70
|
+
const capabilities = { ...o.capabilities, ...extra.capabilities };
|
|
71
|
+
if (name !== 'safari') {
|
|
72
|
+
const vendor = VENDOR[name];
|
|
73
|
+
const own = (capabilities[vendor.key] ?? {});
|
|
74
|
+
capabilities[vendor.key] = {
|
|
75
|
+
...own,
|
|
76
|
+
args: [...(own.args ?? []), ...(o.args ?? []), ...(extra.args ?? []), ...(headless ? [vendor.headless] : [])],
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
// withCapabilities() replaces the builder's capabilities (browserName included), so it goes first.
|
|
80
|
+
let builder = new selenium.Builder();
|
|
81
|
+
if (Object.keys(capabilities).length > 0)
|
|
82
|
+
builder = builder.withCapabilities(capabilities);
|
|
83
|
+
builder = builder.forBrowser(name);
|
|
84
|
+
if (server)
|
|
85
|
+
builder = builder.usingServer(server);
|
|
86
|
+
const driver = await builder.build();
|
|
87
|
+
try {
|
|
88
|
+
const timeouts = {
|
|
89
|
+
...(o.implicitWaitMs !== undefined ? { implicit: o.implicitWaitMs } : {}),
|
|
90
|
+
...(o.pageLoadTimeoutMs !== undefined ? { pageLoad: o.pageLoadTimeoutMs } : {}),
|
|
91
|
+
...(o.scriptTimeoutMs !== undefined ? { script: o.scriptTimeoutMs } : {}),
|
|
92
|
+
};
|
|
93
|
+
if (Object.keys(timeouts).length > 0)
|
|
94
|
+
await driver.manage().setTimeouts(timeouts);
|
|
95
|
+
const size = extra.windowSize ?? o.windowSize;
|
|
96
|
+
if (size)
|
|
97
|
+
await driver.manage().window().setRect({ width: size.width, height: size.height });
|
|
98
|
+
}
|
|
99
|
+
catch (e) {
|
|
100
|
+
await driver.quit().catch(() => undefined);
|
|
101
|
+
throw e;
|
|
102
|
+
}
|
|
103
|
+
return driver;
|
|
104
|
+
};
|
|
105
|
+
const quit = (driver) => driver.quit().catch(() => undefined);
|
|
106
|
+
const screenshotTo = async (driver, status, ctx) => {
|
|
107
|
+
if (!keep(screenshot, status))
|
|
108
|
+
return;
|
|
109
|
+
try {
|
|
110
|
+
ctx.attach(Buffer.from(await driver.takeScreenshot(), 'base64'), 'image/png', `screenshot ${status ?? ''}`.trim());
|
|
111
|
+
}
|
|
112
|
+
catch (e) {
|
|
113
|
+
ctx.log.warn(`@bddr/selenium: could not take a screenshot: ${e instanceof Error ? e.message : String(e)}`);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
const shared = new WeakSet();
|
|
117
|
+
const featureFailed = new WeakMap();
|
|
118
|
+
const opened = new WeakMap();
|
|
119
|
+
/** Does this scenario use the feature's shared driver? */
|
|
120
|
+
const reuses = (ctx) => {
|
|
121
|
+
const tags = ctx.featureInfo.tags.map((t) => t.name);
|
|
122
|
+
if (tags.includes('@freshSession'))
|
|
123
|
+
return false;
|
|
124
|
+
const asked = tags.includes('@reuseSession');
|
|
125
|
+
if (asked && ctx.scenario.serial !== 'feature') {
|
|
126
|
+
throw new Error(`@reuseSession on feature "${ctx.featureInfo.name}" needs @serial on the Feature: scenarios sharing one browser session must run one at a time, in order`);
|
|
127
|
+
}
|
|
128
|
+
return asked || (o.reuseSession === 'serial' && ctx.scenario.serial === 'feature');
|
|
129
|
+
};
|
|
130
|
+
defineFixture('sharedDriver', async (ctx) => {
|
|
131
|
+
const driver = await start(ctx);
|
|
132
|
+
shared.add(driver);
|
|
133
|
+
return driver;
|
|
134
|
+
}, {
|
|
135
|
+
scope: 'feature',
|
|
136
|
+
afterScenario: async (driver, { status, ctx }) => {
|
|
137
|
+
if (status === 'failed')
|
|
138
|
+
featureFailed.set(driver, true);
|
|
139
|
+
await screenshotTo(driver, status, ctx);
|
|
140
|
+
},
|
|
141
|
+
teardown: (driver) => quit(driver),
|
|
142
|
+
});
|
|
143
|
+
defineFixture('driver', async (ctx) => (reuses(ctx) ? ctx.fixture('sharedDriver') : start(ctx)), {
|
|
144
|
+
teardown: async (driver, { status, ctx }) => {
|
|
145
|
+
if (shared.has(driver))
|
|
146
|
+
return;
|
|
147
|
+
await screenshotTo(driver, status, ctx);
|
|
148
|
+
await quit(driver);
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
defineFixture('open', async (ctx) => {
|
|
152
|
+
const driver = await ctx.fixture('driver');
|
|
153
|
+
const base = baseURLOf(ctx);
|
|
154
|
+
return (path = '/') => driver.get(base ? new URL(path, base).href : path); // same rule as Playwright's baseURL
|
|
155
|
+
});
|
|
156
|
+
defineFixture('wait', async (ctx) => {
|
|
157
|
+
const driver = await ctx.fixture('driver');
|
|
158
|
+
return ((condition, timeoutMs, message) => driver.wait(condition, timeoutMs ?? waitTimeout, message));
|
|
159
|
+
});
|
|
160
|
+
defineFixture('newDriver', async (ctx) => {
|
|
161
|
+
const list = [];
|
|
162
|
+
const make = async (options = {}) => {
|
|
163
|
+
const driver = await start(ctx, options);
|
|
164
|
+
list.push(driver);
|
|
165
|
+
return driver;
|
|
166
|
+
};
|
|
167
|
+
opened.set(make, list);
|
|
168
|
+
return make;
|
|
169
|
+
}, { teardown: async (make) => void (await Promise.all((opened.get(make) ?? []).map(quit))) });
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
/** The plugin with Selenium from your project. Use as `plugins: ['@bddr/selenium']` or `plugins: [selenium({ ... })]`. */
|
|
174
|
+
const plugin = createSeleniumPlugin();
|
|
175
|
+
export default plugin;
|
|
176
|
+
/** Typed form for bddr.config.ts: `plugins: [selenium({ browser: 'firefox', screenshot: 'on' })]`. */
|
|
177
|
+
export function selenium(options = {}) {
|
|
178
|
+
return { name: plugin.name, setup: () => plugin.setup(options) };
|
|
179
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,64 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bddr/selenium",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Selenium WebDriver plugin for bddr: a driver per scenario, shared sessions for serial features, remote grids, screenshots",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/afsarali273/bddr.git",
|
|
9
|
+
"directory": "plugins/selenium"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://afsarali273.github.io/bddr/",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/afsarali273/bddr/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"bdd",
|
|
17
|
+
"gherkin",
|
|
18
|
+
"test-runner",
|
|
19
|
+
"automation",
|
|
20
|
+
"cucumber",
|
|
21
|
+
"playwright",
|
|
22
|
+
"selenium",
|
|
23
|
+
"testing"
|
|
24
|
+
],
|
|
25
|
+
"type": "module",
|
|
26
|
+
"main": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"import": "./dist/index.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"README.md",
|
|
37
|
+
"LICENSE"
|
|
38
|
+
],
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc -p tsconfig.json",
|
|
41
|
+
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.test.json",
|
|
42
|
+
"test": "node --import tsx --test test/*.test.ts",
|
|
43
|
+
"verify": "npm run typecheck && npm run build && npm test"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@bddr/runtime": ">=0.1.0",
|
|
47
|
+
"selenium-webdriver": ">=4.20.0"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@bddr/core": "file:../../packages/core",
|
|
51
|
+
"@bddr/runtime": "file:../../packages/runtime",
|
|
52
|
+
"@types/node": "^22.20.5",
|
|
53
|
+
"@types/selenium-webdriver": "^4.1.28",
|
|
54
|
+
"selenium-webdriver": "^4.27.0",
|
|
55
|
+
"tsx": "^4.20.0",
|
|
56
|
+
"typescript": "~5.9.3"
|
|
57
|
+
},
|
|
58
|
+
"engines": {
|
|
59
|
+
"node": ">=20.10"
|
|
60
|
+
},
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"access": "public"
|
|
63
|
+
}
|
|
64
|
+
}
|