spec/screenplay/interactions/execute-script/ExecuteScriptFromUrl.spec.ts
import 'mocha';
import { expect } from '@integration/testing-tools';
import { containAtLeastOneItemThat, Ensure, equals, includes, property } from '@serenity-js/assertions';
import { actorCalled, engage, LogicError } from '@serenity-js/core';
import { by } from 'protractor';
import { Browser, ExecuteScript, Navigate, Target, Text } from '../../../../src';
import { UIActors } from '../../../UIActors';
describe('ExecuteScriptFromUrl', function () {
this.timeout(10 * 1000);
const
pathToScript = fileUrl(require.resolve('./resources/execute-script-sample.js')),
pathToPage = fileUrl(require.resolve('./resources/execute-script-sandbox.html'));
class Sandbox {
static Result = Target.the('sandbox result').located(by.id('result'));
}
beforeEach(() => engage(new UIActors()));
it('allows the actor to execute a script stored at a specific location', () => actorCalled('Joe').attemptsTo(
Navigate.to(pathToPage),
ExecuteScript.from(pathToScript),
Ensure.that(Text.of(Sandbox.Result), equals('Script loaded successfully')),
));
it('complains if the script could not be loaded', () => expect(actorCalled('Joe').attemptsTo(
Navigate.to(pathToPage),
ExecuteScript.from(pathToScript + '.invalid'),
)).to.be.rejected
.then(error => {
expect(error).to.be.instanceOf(LogicError);
expect(error.message).to.match(new RegExp(`Couldn't load script from.*?${ pathToScript }.invalid`))
})
.then(() => actorCalled('Joe').attemptsTo(
Ensure.that(Browser.log(), containAtLeastOneItemThat(property('message', includes('execute-script-sample.js.invalid - Failed to load resource')))),
)));
it('complains if the script has already been loaded', () => expect(actorCalled('Joe').attemptsTo(
Navigate.to(pathToPage),
ExecuteScript.from(pathToScript),
ExecuteScript.from(pathToScript),
)).to.be.rejectedWith(LogicError, `Script from ${ pathToScript } has already been loaded`));
it('provides a sensible description of the interaction being performed', () => {
expect(ExecuteScript.from(pathToScript).toString())
.to.equal(`#actor executes a script from ${ pathToScript }`);
});
function fileUrl(filePath: string) {
let pathName = filePath.replace(/\\/g, '/');
if (pathName[0] !== '/') {
pathName = `/${pathName}`;
}
return encodeURI(`file://${pathName}`).replace(/[?#]/g, encodeURIComponent);
}
});