Path: blob/trunk/javascript/selenium-webdriver/test/remote_test.js
4503 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'1819const assert = require('node:assert')20const path = require('node:path')21const io = require('selenium-webdriver/io')22const cmd = require('selenium-webdriver/lib/command')23const remote = require('selenium-webdriver/remote')24const { CancellationError } = require('selenium-webdriver/http/util')2526describe('DriverService', function () {27describe('start()', function () {28var service2930beforeEach(function () {31service = new remote.DriverService(process.execPath, {32port: 1234,33args: ['-e', 'process.exit(1)'],34})35})3637afterEach(function () {38return service.kill()39})4041it('fails if child-process dies', function () {42return service.start(500).then(expectFailure, verifyFailure)43})4445function verifyFailure(e) {46assert.ok(!(e instanceof CancellationError))47assert.strictEqual('Server terminated early with status 1', e.message)48}4950function expectFailure() {51throw Error('expected to fail')52}53})54})5556describe('FileDetector', function () {57class ExplodingDriver {58execute() {59throw Error('unexpected call')60}61}6263it('returns the original path if the file does not exist', function () {64return io.tmpDir().then((dir) => {65let theFile = path.join(dir, 'not-there')66return new remote.FileDetector()67.handleFile(new ExplodingDriver(), theFile)68.then((f) => assert.strictEqual(f, theFile))69})70})7172it('returns the original path if it is a directory', function () {73return io.tmpDir().then((dir) => {74return new remote.FileDetector().handleFile(new ExplodingDriver(), dir).then((f) => assert.strictEqual(f, dir))75})76})7778it('attempts to upload valid files', function () {79return io.tmpFile().then((theFile) => {80return new remote.FileDetector()81.handleFile(82new (class FakeDriver {83execute(command) {84assert.strictEqual(command.getName(), cmd.Name.UPLOAD_FILE)85assert.strictEqual(typeof command.getParameters()['file'], 'string')86return Promise.resolve('success!')87}88})(),89theFile,90)91.then((f) => assert.strictEqual(f, 'success!'))92})93})94})9596describe('fireSessionEvent', function () {97it('sends correct command with eventType only', function () {98const fakeDriver = {99execute(command) {100assert.strictEqual(command.getName(), cmd.Name.FIRE_SESSION_EVENT)101const params = command.getParameters()102assert.strictEqual(params['eventType'], 'test:started')103assert.strictEqual(params['payload'], undefined)104return Promise.resolve({105success: true,106eventType: 'test:started',107timestamp: '2024-01-15T10:30:00Z',108})109},110}111const { WebDriver } = require('selenium-webdriver')112// Directly test the command structure113const command = new cmd.Command(cmd.Name.FIRE_SESSION_EVENT).setParameter('eventType', 'test:started')114return fakeDriver.execute(command).then((result) => {115assert.strictEqual(result.success, true)116assert.strictEqual(result.eventType, 'test:started')117})118})119120it('sends correct command with eventType and payload', function () {121const fakeDriver = {122execute(command) {123assert.strictEqual(command.getName(), cmd.Name.FIRE_SESSION_EVENT)124const params = command.getParameters()125assert.strictEqual(params['eventType'], 'test:failed')126assert.deepStrictEqual(params['payload'], { testName: 'LoginTest', error: 'Element not found' })127return Promise.resolve({128success: true,129eventType: 'test:failed',130timestamp: '2024-01-15T10:30:00Z',131})132},133}134const command = new cmd.Command(cmd.Name.FIRE_SESSION_EVENT)135.setParameter('eventType', 'test:failed')136.setParameter('payload', { testName: 'LoginTest', error: 'Element not found' })137return fakeDriver.execute(command).then((result) => {138assert.strictEqual(result.success, true)139assert.strictEqual(result.eventType, 'test:failed')140})141})142})143144145