Path: blob/trunk/javascript/selenium-webdriver/test/select_test.js
2884 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 { Select, By } = require('selenium-webdriver')21const { Pages, suite } = require('../lib/test')22const { escapeQuotes } = require('selenium-webdriver/lib/select')2324let singleSelectValues1 = {25name: 'selectomatic',26values: ['One', 'Two', 'Four', 'Still learning how to count, apparently'],27}2829let disabledSingleSelect = {30name: 'single_disabled',31values: ['Enabled', 'Disabled'],32}33let disabledMultiSelect = {34name: 'multi_disabled',35values: ['Enabled', 'Disabled'],36}3738let multiSelectValues1 = {39name: 'multi',40values: ['Eggs', 'Ham', 'Sausages', 'Onion gravy'],41}42let multiSelectValues2 = {43name: 'select_empty_multiple',44values: ['select_1', 'select_2', 'select_3', 'select_4'],45}4647suite(48function (env) {49let driver5051before(async function () {52driver = await env.builder().build()53})54after(async () => await driver.quit())5556describe('Select by tests', function () {57it('Should be able to select by value', async function () {58await driver.get(Pages.formPage)5960let selector = new Select(driver.findElement(By.name(singleSelectValues1['name'])))61for (let x in singleSelectValues1['values']) {62await selector.selectByValue(singleSelectValues1['values'][x].toLowerCase())63let ele = await selector.getFirstSelectedOption()64assert.deepEqual(await ele.getText(), singleSelectValues1['values'][x])65}66})6768it('Should be able to select by index', async function () {69await driver.get(Pages.formPage)7071let selector = new Select(driver.findElement(By.name(singleSelectValues1['name'])))72for (let x in singleSelectValues1['values']) {73await selector.selectByIndex(x)74let ele = await selector.getFirstSelectedOption()75assert.deepEqual(await ele.getText(), singleSelectValues1['values'][x])76}77})7879it('Should be able to select by visible text', async function () {80await driver.get(Pages.formPage)8182let selector = new Select(driver.findElement(By.name(singleSelectValues1['name'])))83for (let x in singleSelectValues1['values']) {84await selector.selectByVisibleText(singleSelectValues1['values'][x])85let ele = await selector.getFirstSelectedOption()86assert.deepEqual(await ele.getText(), singleSelectValues1['values'][x])87}88})8990it('Should be able to select by visible text with spaces', async function () {91await driver.get(Pages.selectSpacePage)9293const elem = await driver.findElement(By.id('selectWithoutMultiple'))94const select = new Select(elem)95await select.selectByVisibleText(' five')96let selectedElement = await select.getFirstSelectedOption()97selectedElement.getText().then((text) => {98assert.strictEqual(text, ' five')99})100})101102it('Should convert an unquoted string into one with quotes', async function () {103assert.strictEqual(escapeQuotes('abc'), '"abc"')104assert.strictEqual(escapeQuotes('abc aqewqqw'), '"abc aqewqqw"')105assert.strictEqual(escapeQuotes(''), '""')106assert.strictEqual(escapeQuotes(' '), '" "')107assert.strictEqual(escapeQuotes(' abc '), '" abc "')108})109110it('Should add double quotes to a string that contains a single quote', async function () {111assert.strictEqual(escapeQuotes("f'oo"), `"f'oo"`)112})113114it('Should add single quotes to a string that contains a double quotes', async function () {115assert.strictEqual(escapeQuotes('f"oo'), `'f"oo'`)116})117118it('Should provide concatenated strings when string to escape contains both single and double quotes', async function () {119assert.strictEqual(escapeQuotes(`f"o'o`), `concat("f", '"', "o'o")`)120})121122it('Should provide concatenated strings when string ends with quote', async function () {123assert.strictEqual(escapeQuotes(`'"`), `concat("'", '"')`)124})125126it('Should select by multiple index', async function () {127await driver.get(Pages.formPage)128129let selector = new Select(driver.findElement(By.name(multiSelectValues1['name'])))130await selector.deselectAll()131132for (let x in multiSelectValues1['values']) {133await selector.selectByIndex(x)134}135136let ele = await selector.getAllSelectedOptions()137138for (let x in ele) {139assert.deepEqual(await ele[x].getText(), multiSelectValues1['values'][x])140}141})142143it('Should select by multiple value', async function () {144await driver.get(Pages.formPage)145146let selector = new Select(driver.findElement(By.name(multiSelectValues2['name'])))147await selector.deselectAll()148149for (let value of multiSelectValues2['values']) {150await selector.selectByValue(value)151}152153let ele = await selector.getAllSelectedOptions()154155for (let x in ele) {156assert.deepEqual(await ele[x].getText(), multiSelectValues2['values'][x])157}158})159160it('Should select by multiple text', async function () {161await driver.get(Pages.formPage)162163let selector = new Select(driver.findElement(By.name(multiSelectValues2['name'])))164await selector.deselectAll()165166for (let value of multiSelectValues2['values']) {167await selector.selectByVisibleText(value)168}169170let ele = await selector.getAllSelectedOptions()171172for (let x in ele) {173assert.deepEqual(await ele[x].getText(), multiSelectValues2['values'][x])174}175})176177it('Should raise exception select by value single disabled', async function () {178await driver.get(Pages.formPage)179180let selector = new Select(driver.findElement(By.name(disabledSingleSelect['name'])))181182await assert.rejects(183async () => {184await selector.selectByValue(disabledSingleSelect.values[1].toLowerCase())185},186(err) => {187assert.strictEqual(err.name, 'UnsupportedOperationError')188assert.strictEqual(err.message, 'You may not select a disabled option')189return true190},191)192})193194it('Should raise exception select by index single disabled', async function () {195await driver.get(Pages.formPage)196197let selector = new Select(driver.findElement(By.name(disabledSingleSelect['name'])))198199await assert.rejects(200async () => {201await selector.selectByIndex(1)202},203(err) => {204assert.strictEqual(err.name, 'UnsupportedOperationError')205assert.strictEqual(err.message, 'You may not select a disabled option')206return true207},208)209})210211it('Should raise exception select by text single disabled', async function () {212await driver.get(Pages.formPage)213214let selector = new Select(driver.findElement(By.name(disabledSingleSelect['name'])))215216await assert.rejects(217async () => {218await selector.selectByVisibleText(disabledSingleSelect.values[1])219},220(err) => {221assert.strictEqual(err.name, 'UnsupportedOperationError')222assert.strictEqual(err.message, 'You may not select a disabled option')223return true224},225)226})227228it('Should raise exception select by index multiple disabled', async function () {229await driver.get(Pages.formPage)230231let selector = new Select(driver.findElement(By.name(disabledMultiSelect['name'])))232233await assert.rejects(234async () => {235await selector.selectByIndex(1)236},237(err) => {238assert.strictEqual(err.name, 'UnsupportedOperationError')239assert.strictEqual(err.message, 'You may not select a disabled option')240return true241},242)243})244245it('Should raise exception select by value multiple disabled', async function () {246await driver.get(Pages.formPage)247248let selector = new Select(driver.findElement(By.name(disabledMultiSelect['name'])))249250await assert.rejects(251async () => {252await selector.selectByValue(disabledMultiSelect.values[1].toLowerCase())253},254(err) => {255assert.strictEqual(err.name, 'UnsupportedOperationError')256assert.strictEqual(err.message, 'You may not select a disabled option')257return true258},259)260})261262it('Should raise exception select by text multiple disabled', async function () {263await driver.get(Pages.formPage)264265let selector = new Select(driver.findElement(By.name(disabledMultiSelect['name'])))266267await assert.rejects(268async () => {269await selector.selectByVisibleText(disabledMultiSelect.values[1])270},271(err) => {272assert.strictEqual(err.name, 'UnsupportedOperationError')273assert.strictEqual(err.message, 'You may not select a disabled option')274return true275},276)277})278})279},280{ browsers: ['firefox', 'chrome'] },281)282283284