Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
262 changes: 125 additions & 137 deletions test/integration/standalone/standalone-save-state.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from "fs";
import { strict as assert } from "assert";
import { launchBrowser } from "../../../src/browser/standalone";
import { BROWSER_CONFIG, BROWSER_NAME } from "./constants";
import { BROWSER_CONFIG } from "./constants";
import { SaveStateData } from "../../../src";
import { DEVTOOLS_PROTOCOL, WEBDRIVER_PROTOCOL } from "../../../src/constants/config";
import { WEBDRIVER_PROTOCOL } from "../../../src/constants/config";

import { AuthServer } from "./mock-auth-page/server";
import process from "node:process";
Expand All @@ -19,182 +19,170 @@ const removeDomainFromCookies = (loginState: SaveStateData): void => {
}
};

type AutomationProtocol = typeof DEVTOOLS_PROTOCOL | typeof WEBDRIVER_PROTOCOL;
[true, false].forEach(webSocketUrl => {
[true, false].forEach(isolation => {
describe(`saveState and restoreState tests, isolation: ${isolation}, webSocketUrl: ${webSocketUrl}`, function () {
this.timeout(TIMEOUT);

[DEVTOOLS_PROTOCOL, WEBDRIVER_PROTOCOL].forEach((automationProtocol): void => {
if (automationProtocol === DEVTOOLS_PROTOCOL && BROWSER_NAME === "firefox") {
return;
}

[true, false].forEach(webSocketUrl => {
[true, false].forEach(isolation => {
describe(`saveState and restoreState tests, isolation: ${isolation}, webSocketUrl: ${webSocketUrl}, automationProtocol: ${automationProtocol}`, function () {
this.timeout(TIMEOUT);
setTimeout(() => {
console.error(
`ERROR! Standalone test failed to complete in ${TIMEOUT / 1000} seconds.\n` +
"If all tests have passed, most likely this is caused by a bug in browser cleanup logic, e.g. deleteSession() command.",
);
process.exit(1);
}, TIMEOUT).unref();

setTimeout(() => {
console.error(
`ERROR! Standalone test failed to complete in ${TIMEOUT / 1000} seconds.\n` +
"If all tests have passed, most likely this is caused by a bug in browser cleanup logic, e.g. deleteSession() command.",
);
process.exit(1);
}, TIMEOUT).unref();
let browser: WebdriverIO.Browser & { getDriverPid?: () => number | undefined };

let browser: WebdriverIO.Browser & { getDriverPid?: () => number | undefined };
let loginState: SaveStateData;
let status: WebdriverIO.Element;
const mockAuthServer = new AuthServer();

let loginState: SaveStateData;
let status: WebdriverIO.Element;
const mockAuthServer = new AuthServer();
before(async () => {
console.log("Start mock server");
mockAuthServer.start();
});

before(async () => {
console.log("Start mock server");
mockAuthServer.start();
beforeEach(async () => {
browser = await launchBrowser({
...BROWSER_CONFIG,
desiredCapabilities: {
...BROWSER_CONFIG.desiredCapabilities,
webSocketUrl,
},
isolation,
automationProtocol: WEBDRIVER_PROTOCOL,
});

beforeEach(async () => {
browser = await launchBrowser({
...BROWSER_CONFIG,
desiredCapabilities: {
...BROWSER_CONFIG.desiredCapabilities,
webSocketUrl,
},
isolation,
automationProtocol: automationProtocol as AutomationProtocol,
});

assert.ok(browser, "Browser should be initialized");
assert.ok(browser.sessionId, "Browser should have a valid session ID");
assert.ok(browser, "Browser should be initialized");
assert.ok(browser.sessionId, "Browser should have a valid session ID");

// go to mock page
await browser.url("http://localhost:3000/");
// go to mock page
await browser.url("http://localhost:3000/");

status = await browser.$("#status");
status = await browser.$("#status");

// check that we are not logged in
assert.strictEqual(await status.getText(), "You are not logged in");
});
// check that we are not logged in
assert.strictEqual(await status.getText(), "You are not logged in");
});

it("saveState", async function () {
// input login
const emailInput = await browser.$("#login");
await emailInput.setValue("admin");
it("saveState", async function () {
// input login
const emailInput = await browser.$("#login");
await emailInput.setValue("admin");

// input password
const passwordInput = await browser.$("#password");
await passwordInput.setValue("admin123");
// input password
const passwordInput = await browser.$("#password");
await passwordInput.setValue("admin123");

// click to login
const logInButton = await browser.$('[type="submit"]');
await logInButton.click();
// click to login
const logInButton = await browser.$('[type="submit"]');
await logInButton.click();

// save state
loginState = await browser.saveState();
// save state
loginState = await browser.saveState();

if (automationProtocol === DEVTOOLS_PROTOCOL) {
await browser.pause(500);
}
// check that now we logged in
assert.strictEqual(await status.getText(), "You are logged in");
});

// check that now we logged in
assert.strictEqual(await status.getText(), "You are logged in");
it("saveState: {keepFile: true}", async function () {
await browser.saveState({
keepFile: true,
path: "./state.json",
});

it("saveState: {keepFile: true}", async function () {
await browser.saveState({
keepFile: true,
path: "./state.json",
});
await browser.deleteSession();

await browser.deleteSession();
const fileExist = fs.existsSync("./state.json");
assert.strictEqual(fileExist, true);
fs.rmSync("./state.json");
});

const fileExist = fs.existsSync("./state.json");
assert.strictEqual(fileExist, true);
fs.rmSync("./state.json");
it("saveState: {keepFile: false}", async function () {
await browser.saveState({
keepFile: false,
path: "./state.json",
});

it("saveState: {keepFile: false}", async function () {
await browser.saveState({
keepFile: false,
path: "./state.json",
});
await browser.deleteSession();

await browser.deleteSession();
const fileExist = fs.existsSync("./state.json");
assert.strictEqual(fileExist, false);
});

const fileExist = fs.existsSync("./state.json");
assert.strictEqual(fileExist, false);
it("saveState: emptyState", async function () {
await browser.saveState({
keepFile: true,
cookieFilter: () => false,
path: "./state.json",
localStorage: false,
sessionStorage: false,
});

it("saveState: emptyState", async function () {
await browser.saveState({
keepFile: true,
cookieFilter: () => false,
path: "./state.json",
localStorage: false,
sessionStorage: false,
});
await browser.deleteSession();

await browser.deleteSession();
const fileExist = fs.existsSync("./state.json");
assert.strictEqual(fileExist, false);
});

const fileExist = fs.existsSync("./state.json");
assert.strictEqual(fileExist, false);
});
it("getState", async function () {
const options = {
path: "./state.json",
};

it("getState", async function () {
const options = {
path: "./state.json",
};
const saveResult = await browser.saveState(options);
const getResult = await browser.getState(options);

const saveResult = await browser.saveState(options);
const getResult = await browser.getState(options);
assert.deepEqual(saveResult, getResult);
});

assert.deepEqual(saveResult, getResult);
});
it("restoreState", async function () {
if (loginState) {
removeDomainFromCookies(loginState);

it("restoreState", async function () {
if (loginState) {
removeDomainFromCookies(loginState);
await browser.restoreState({
data: loginState,
});
}

await browser.restoreState({
data: loginState,
});
}
// check that now we logged in
assert.strictEqual(await status.getText(), "You are logged in");
});

// check that now we logged in
assert.strictEqual(await status.getText(), "You are logged in");
});
it("cookieFilter: restoreState", async function () {
// restore state
if (loginState) {
removeDomainFromCookies(loginState);

it("cookieFilter: restoreState", async function () {
// restore state
if (loginState) {
removeDomainFromCookies(loginState);
await browser.restoreState({
data: loginState,
cookieFilter: ({ name }) => name !== "sessionId",
});
}

await browser.restoreState({
data: loginState,
cookieFilter: ({ name }) => name !== "sessionId",
});
}
// check that still we are not logged in
assert.strictEqual(await status.getText(), "You are not logged in");
});

// check that still we are not logged in
assert.strictEqual(await status.getText(), "You are not logged in");
it("cookieFilter: saveState", async function () {
const state = await browser.saveState({
cookieFilter: () => false,
});

it("cookieFilter: saveState", async function () {
const state = await browser.saveState({
cookieFilter: () => false,
});

// now we don't have cookie in save data object
assert.ok(state.cookies?.length === 0);
});
// now we don't have cookie in save data object
assert.ok(state.cookies?.length === 0);
});

afterEach(async () => {
if (browser) {
await browser.deleteSession();
}
});
afterEach(async () => {
if (browser) {
await browser.deleteSession();
}
});

after(async () => {
console.log("Stop mock server");
mockAuthServer.stop();
});
after(async () => {
console.log("Stop mock server");
mockAuthServer.stop();
});
});
});
Expand Down
Loading