2016-01-25 14:30:35 -08:00
|
|
|
// Licensed to the Software Freedom Conservancy (SFC) under one
|
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
|
// distributed with this work for additional information
|
|
|
|
|
// regarding copyright ownership. The SFC licenses this file
|
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
|
// "License"); you may not use this file except in compliance
|
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing,
|
|
|
|
|
// software distributed under the License is distributed on an
|
|
|
|
|
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
|
// KIND, either express or implied. See the License for the
|
|
|
|
|
// specific language governing permissions and limitations
|
|
|
|
|
// under the License.
|
|
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
'use strict'
|
2016-01-25 14:30:35 -08:00
|
|
|
|
2024-04-16 01:50:58 +05:30
|
|
|
const assert = require('node:assert')
|
2016-01-25 14:30:35 -08:00
|
|
|
|
2024-06-12 13:34:19 +01:00
|
|
|
const By = require('selenium-webdriver/lib/by').By
|
|
|
|
|
const CommandName = require('selenium-webdriver/lib/command').Name
|
|
|
|
|
const error = require('selenium-webdriver/lib/error')
|
|
|
|
|
const until = require('selenium-webdriver/lib/until')
|
|
|
|
|
const webdriver = require('selenium-webdriver/lib/webdriver')
|
2024-04-16 01:50:58 +05:30
|
|
|
const WebElement = webdriver.WebElement
|
2016-01-25 14:30:35 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
describe('until', function () {
|
|
|
|
|
let driver, executor
|
2016-01-25 14:30:35 -08:00
|
|
|
|
|
|
|
|
class TestExecutor {
|
|
|
|
|
constructor() {
|
2020-08-03 17:56:31 +03:00
|
|
|
this.handlers_ = {}
|
2016-01-25 14:30:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
on(cmd, handler) {
|
2020-08-03 17:56:31 +03:00
|
|
|
this.handlers_[cmd] = handler
|
|
|
|
|
return this
|
2016-01-25 14:30:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
execute(cmd) {
|
2020-08-03 17:56:31 +03:00
|
|
|
let self = this
|
|
|
|
|
return Promise.resolve().then(function () {
|
2016-01-25 14:30:35 -08:00
|
|
|
if (!self.handlers_[cmd.getName()]) {
|
2020-08-03 17:56:31 +03:00
|
|
|
throw new error.UnknownCommandError(cmd.getName())
|
2016-01-25 14:30:35 -08:00
|
|
|
}
|
2020-08-03 17:56:31 +03:00
|
|
|
return self.handlers_[cmd.getName()](cmd)
|
|
|
|
|
})
|
2016-01-25 14:30:35 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function fail(opt_msg) {
|
2020-08-03 17:56:31 +03:00
|
|
|
throw new assert.AssertionError({ message: opt_msg })
|
2016-01-25 14:30:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
beforeEach(function setUp() {
|
2020-08-03 17:56:31 +03:00
|
|
|
executor = new TestExecutor()
|
|
|
|
|
driver = new webdriver.WebDriver('session-id', executor)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('ableToSwitchToFrame', function () {
|
|
|
|
|
it('failsFastForNonSwitchErrors', function () {
|
|
|
|
|
let e = Error('boom')
|
|
|
|
|
executor.on(CommandName.SWITCH_TO_FRAME, function () {
|
|
|
|
|
throw e
|
|
|
|
|
})
|
2024-02-07 16:07:24 +00:00
|
|
|
return driver.wait(until.ableToSwitchToFrame(0), 100).then(fail, (e2) => assert.strictEqual(e2, e))
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const ELEMENT_ID = 'some-element-id'
|
|
|
|
|
const ELEMENT_INDEX = 1234
|
2017-02-03 20:57:19 -08:00
|
|
|
|
|
|
|
|
function onSwitchFrame(expectedId) {
|
|
|
|
|
if (typeof expectedId === 'string') {
|
2020-08-03 17:56:31 +03:00
|
|
|
expectedId = WebElement.buildId(expectedId)
|
2017-02-03 20:57:19 -08:00
|
|
|
} else {
|
2024-02-07 16:07:24 +00:00
|
|
|
assert.strictEqual(typeof expectedId, 'number', 'must be string or number')
|
2017-02-03 20:57:19 -08:00
|
|
|
}
|
2020-08-03 17:56:31 +03:00
|
|
|
return (cmd) => {
|
2024-02-07 16:07:24 +00:00
|
|
|
assert.deepStrictEqual(cmd.getParameter('id'), expectedId, 'frame ID not specified')
|
2020-08-03 17:56:31 +03:00
|
|
|
return true
|
|
|
|
|
}
|
2017-02-03 20:57:19 -08:00
|
|
|
}
|
|
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
it('byIndex', function () {
|
|
|
|
|
executor.on(CommandName.SWITCH_TO_FRAME, onSwitchFrame(ELEMENT_INDEX))
|
|
|
|
|
return driver.wait(until.ableToSwitchToFrame(ELEMENT_INDEX), 100)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('byWebElement', function () {
|
|
|
|
|
executor.on(CommandName.SWITCH_TO_FRAME, onSwitchFrame(ELEMENT_ID))
|
|
|
|
|
|
|
|
|
|
var el = new webdriver.WebElement(driver, ELEMENT_ID)
|
|
|
|
|
return driver.wait(until.ableToSwitchToFrame(el), 100)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('byWebElementPromise', function () {
|
|
|
|
|
executor.on(CommandName.SWITCH_TO_FRAME, onSwitchFrame(ELEMENT_ID))
|
2024-02-07 16:07:24 +00:00
|
|
|
var el = new webdriver.WebElementPromise(driver, Promise.resolve(new webdriver.WebElement(driver, ELEMENT_ID)))
|
2020-08-03 17:56:31 +03:00
|
|
|
return driver.wait(until.ableToSwitchToFrame(el), 100)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('byLocator', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
executor.on(CommandName.FIND_ELEMENTS, () => [WebElement.buildId(ELEMENT_ID)])
|
2020-08-03 17:56:31 +03:00
|
|
|
executor.on(CommandName.SWITCH_TO_FRAME, onSwitchFrame(ELEMENT_ID))
|
|
|
|
|
return driver.wait(until.ableToSwitchToFrame(By.id('foo')), 100)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('byLocator_elementNotInitiallyFound', function () {
|
|
|
|
|
let foundResponses = [[], [], [WebElement.buildId(ELEMENT_ID)]]
|
|
|
|
|
executor.on(CommandName.FIND_ELEMENTS, () => foundResponses.shift())
|
|
|
|
|
executor.on(CommandName.SWITCH_TO_FRAME, onSwitchFrame(ELEMENT_ID))
|
|
|
|
|
|
|
|
|
|
return driver
|
|
|
|
|
.wait(until.ableToSwitchToFrame(By.id('foo')), 2000)
|
2021-03-23 21:25:55 +05:30
|
|
|
.then(() => assert.deepStrictEqual(foundResponses, []))
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('timesOutIfNeverAbletoSwitchFrames', function () {
|
|
|
|
|
var count = 0
|
|
|
|
|
executor.on(CommandName.SWITCH_TO_FRAME, function () {
|
|
|
|
|
count += 1
|
|
|
|
|
throw new error.NoSuchFrameError()
|
|
|
|
|
})
|
2016-01-25 14:30:35 -08:00
|
|
|
|
2024-02-07 16:07:24 +00:00
|
|
|
return driver.wait(until.ableToSwitchToFrame(0), 100).then(fail, function (e) {
|
|
|
|
|
assert.ok(count > 0)
|
|
|
|
|
assert.ok(e.message.startsWith('Waiting to be able to switch to frame'), 'Wrong message: ' + e.message)
|
|
|
|
|
})
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('alertIsPresent', function () {
|
|
|
|
|
it('failsFastForNonAlertSwitchErrors', function () {
|
|
|
|
|
return driver.wait(until.alertIsPresent(), 100).then(fail, function (e) {
|
|
|
|
|
assert.ok(e instanceof error.UnknownCommandError)
|
2021-03-23 21:25:55 +05:30
|
|
|
assert.strictEqual(e.message, CommandName.GET_ALERT_TEXT)
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
})
|
2016-05-23 20:51:52 +02:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
it('waitsForAlert', function () {
|
|
|
|
|
var count = 0
|
|
|
|
|
executor
|
|
|
|
|
.on(CommandName.GET_ALERT_TEXT, function () {
|
2016-05-23 20:51:52 +02:00
|
|
|
if (count++ < 3) {
|
2020-08-03 17:56:31 +03:00
|
|
|
throw new error.NoSuchAlertError()
|
2016-05-23 20:51:52 +02:00
|
|
|
} else {
|
2020-08-03 17:56:31 +03:00
|
|
|
return true
|
2016-05-23 20:51:52 +02:00
|
|
|
}
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
.on(CommandName.DISMISS_ALERT, () => true)
|
|
|
|
|
|
|
|
|
|
return driver.wait(until.alertIsPresent(), 1000).then(function (alert) {
|
2021-03-23 21:25:55 +05:30
|
|
|
assert.strictEqual(count, 4)
|
2020-08-03 17:56:31 +03:00
|
|
|
return alert.dismiss()
|
2016-05-23 20:51:52 +02:00
|
|
|
})
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// TODO: Remove once GeckoDriver doesn't throw this unwanted error.
|
|
|
|
|
// See https://github.com/SeleniumHQ/selenium/pull/2137
|
|
|
|
|
describe('workaround for GeckoDriver', function () {
|
|
|
|
|
it('doesNotFailWhenCannotConvertNullToObject', function () {
|
|
|
|
|
var count = 0
|
|
|
|
|
executor
|
|
|
|
|
.on(CommandName.GET_ALERT_TEXT, function () {
|
|
|
|
|
if (count++ < 3) {
|
|
|
|
|
throw new error.WebDriverError(`can't convert null to object`)
|
|
|
|
|
} else {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.on(CommandName.DISMISS_ALERT, () => true)
|
|
|
|
|
|
|
|
|
|
return driver.wait(until.alertIsPresent(), 1000).then(function (alert) {
|
2021-03-23 21:25:55 +05:30
|
|
|
assert.strictEqual(count, 4)
|
2020-08-03 17:56:31 +03:00
|
|
|
return alert.dismiss()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('keepsRaisingRegularWebdriverError', function () {
|
|
|
|
|
var webDriverError = new error.WebDriverError()
|
|
|
|
|
|
|
|
|
|
executor.on(CommandName.GET_ALERT_TEXT, function () {
|
|
|
|
|
throw webDriverError
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return driver.wait(until.alertIsPresent(), 1000).then(
|
|
|
|
|
function () {
|
|
|
|
|
throw new Error('driver did not fail against WebDriverError')
|
|
|
|
|
},
|
|
|
|
|
function (error) {
|
2021-03-23 21:25:55 +05:30
|
|
|
assert.strictEqual(error, webDriverError)
|
2024-02-07 16:07:24 +00:00
|
|
|
},
|
2020-08-03 17:56:31 +03:00
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('testUntilTitleIs', function () {
|
|
|
|
|
var titles = ['foo', 'bar', 'baz']
|
|
|
|
|
executor.on(CommandName.GET_TITLE, () => titles.shift())
|
|
|
|
|
|
|
|
|
|
return driver.wait(until.titleIs('bar'), 3000).then(function () {
|
|
|
|
|
assert.deepStrictEqual(titles, ['baz'])
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('testUntilTitleContains', function () {
|
|
|
|
|
var titles = ['foo', 'froogle', 'google']
|
|
|
|
|
executor.on(CommandName.GET_TITLE, () => titles.shift())
|
|
|
|
|
|
|
|
|
|
return driver.wait(until.titleContains('oogle'), 3000).then(function () {
|
|
|
|
|
assert.deepStrictEqual(titles, ['google'])
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('testUntilTitleMatches', function () {
|
|
|
|
|
var titles = ['foo', 'froogle', 'aaaabc', 'aabbbc', 'google']
|
|
|
|
|
executor.on(CommandName.GET_TITLE, () => titles.shift())
|
|
|
|
|
|
2024-02-07 16:07:24 +00:00
|
|
|
return driver.wait(until.titleMatches(/^a{2,3}b+c$/), 3000).then(function () {
|
|
|
|
|
assert.deepStrictEqual(titles, ['google'])
|
|
|
|
|
})
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('testUntilUrlIs', function () {
|
|
|
|
|
var urls = ['http://www.foo.com', 'https://boo.com', 'http://docs.yes.com']
|
|
|
|
|
executor.on(CommandName.GET_CURRENT_URL, () => urls.shift())
|
|
|
|
|
|
|
|
|
|
return driver.wait(until.urlIs('https://boo.com'), 3000).then(function () {
|
|
|
|
|
assert.deepStrictEqual(urls, ['http://docs.yes.com'])
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('testUntilUrlContains', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
var urls = ['http://foo.com', 'https://groups.froogle.com', 'http://google.com']
|
2020-08-03 17:56:31 +03:00
|
|
|
executor.on(CommandName.GET_CURRENT_URL, () => urls.shift())
|
|
|
|
|
|
|
|
|
|
return driver.wait(until.urlContains('oogle.com'), 3000).then(function () {
|
|
|
|
|
assert.deepStrictEqual(urls, ['http://google.com'])
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('testUntilUrlMatches', function () {
|
|
|
|
|
var urls = ['foo', 'froogle', 'aaaabc', 'aabbbc', 'google']
|
|
|
|
|
executor.on(CommandName.GET_CURRENT_URL, () => urls.shift())
|
|
|
|
|
|
|
|
|
|
return driver.wait(until.urlMatches(/^a{2,3}b+c$/), 3000).then(function () {
|
|
|
|
|
assert.deepStrictEqual(urls, ['google'])
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('testUntilElementLocated', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
var responses = [[], [WebElement.buildId('abc123'), WebElement.buildId('foo')], ['end']]
|
2020-08-03 17:56:31 +03:00
|
|
|
executor.on(CommandName.FIND_ELEMENTS, () => responses.shift())
|
|
|
|
|
|
|
|
|
|
let element = driver.wait(until.elementLocated(By.id('quux')), 2000)
|
|
|
|
|
assert.ok(element instanceof webdriver.WebElementPromise)
|
|
|
|
|
return element.getId().then(function (id) {
|
|
|
|
|
assert.deepStrictEqual(responses, [['end']])
|
2021-03-23 21:25:55 +05:30
|
|
|
assert.strictEqual(id, 'abc123')
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('untilElementLocated, elementNeverFound', function () {
|
2016-01-25 14:30:35 -08:00
|
|
|
function runNoElementFoundTest(locator, locatorStr) {
|
2020-08-03 17:56:31 +03:00
|
|
|
executor.on(CommandName.FIND_ELEMENTS, () => [])
|
2016-01-25 14:30:35 -08:00
|
|
|
|
|
|
|
|
function expectedFailure() {
|
2020-08-03 17:56:31 +03:00
|
|
|
fail('expected condition to timeout')
|
2016-01-25 14:30:35 -08:00
|
|
|
}
|
|
|
|
|
|
2024-02-07 16:07:24 +00:00
|
|
|
return driver.wait(until.elementLocated(locator), 100).then(expectedFailure, function (error) {
|
|
|
|
|
var expected = 'Waiting for element to be located ' + locatorStr
|
|
|
|
|
var lines = error.message.split(/\n/, 2)
|
|
|
|
|
assert.strictEqual(lines[0], expected)
|
|
|
|
|
|
|
|
|
|
let regex = /^Wait timed out after \d+ms$/
|
|
|
|
|
assert.ok(regex.test(lines[1]), `Lines <${lines[1]}> does not match ${regex}`)
|
|
|
|
|
})
|
2016-01-25 14:30:35 -08:00
|
|
|
}
|
|
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
it('byLocator', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
return runNoElementFoundTest(By.id('quux'), 'By(css selector, *[id="quux"])')
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
2016-01-25 14:30:35 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
it('byHash', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
return runNoElementFoundTest({ id: 'quux' }, 'By(css selector, *[id="quux"])')
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
2016-01-25 14:30:35 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
it('byFunction', function () {
|
|
|
|
|
return runNoElementFoundTest(function () {}, 'by function()')
|
|
|
|
|
})
|
|
|
|
|
})
|
2016-01-25 14:30:35 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
it('testUntilElementsLocated', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
var responses = [[], [WebElement.buildId('abc123'), WebElement.buildId('foo')], ['end']]
|
2020-08-03 17:56:31 +03:00
|
|
|
executor.on(CommandName.FIND_ELEMENTS, () => responses.shift())
|
|
|
|
|
|
|
|
|
|
return driver
|
|
|
|
|
.wait(until.elementsLocated(By.id('quux')), 2000)
|
|
|
|
|
.then(function (els) {
|
|
|
|
|
return Promise.all(els.map((e) => e.getId()))
|
|
|
|
|
})
|
|
|
|
|
.then(function (ids) {
|
|
|
|
|
assert.deepStrictEqual(responses, [['end']])
|
2021-03-23 21:25:55 +05:30
|
|
|
assert.strictEqual(ids.length, 2)
|
|
|
|
|
assert.strictEqual(ids[0], 'abc123')
|
|
|
|
|
assert.strictEqual(ids[1], 'foo')
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('untilElementsLocated, noElementsFound', function () {
|
2016-01-25 14:30:35 -08:00
|
|
|
function runNoElementsFoundTest(locator, locatorStr) {
|
2020-08-03 17:56:31 +03:00
|
|
|
executor.on(CommandName.FIND_ELEMENTS, () => [])
|
2016-01-25 14:30:35 -08:00
|
|
|
|
|
|
|
|
function expectedFailure() {
|
2020-08-03 17:56:31 +03:00
|
|
|
fail('expected condition to timeout')
|
2016-01-25 14:30:35 -08:00
|
|
|
}
|
|
|
|
|
|
2024-02-07 16:07:24 +00:00
|
|
|
return driver.wait(until.elementsLocated(locator), 100).then(expectedFailure, function (error) {
|
|
|
|
|
var expected = 'Waiting for at least one element to be located ' + locatorStr
|
|
|
|
|
var lines = error.message.split(/\n/, 2)
|
|
|
|
|
assert.strictEqual(lines[0], expected)
|
|
|
|
|
|
|
|
|
|
let regex = /^Wait timed out after \d+ms$/
|
|
|
|
|
assert.ok(regex.test(lines[1]), `Lines <${lines[1]}> does not match ${regex}`)
|
|
|
|
|
})
|
2016-01-25 14:30:35 -08:00
|
|
|
}
|
|
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
it('byLocator', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
return runNoElementsFoundTest(By.id('quux'), 'By(css selector, *[id="quux"])')
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
2016-01-25 14:30:35 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
it('byHash', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
return runNoElementsFoundTest({ id: 'quux' }, 'By(css selector, *[id="quux"])')
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('byFunction', function () {
|
|
|
|
|
return runNoElementsFoundTest(function () {}, 'by function()')
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('testUntilStalenessOf', function () {
|
|
|
|
|
let count = 0
|
|
|
|
|
executor.on(CommandName.GET_ELEMENT_TAG_NAME, function () {
|
2016-02-20 12:03:40 -08:00
|
|
|
while (count < 3) {
|
2020-08-03 17:56:31 +03:00
|
|
|
count += 1
|
|
|
|
|
return 'body'
|
2016-02-20 12:03:40 -08:00
|
|
|
}
|
2020-08-03 17:56:31 +03:00
|
|
|
throw new error.StaleElementReferenceError('now stale')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
var el = new webdriver.WebElement(driver, { ELEMENT: 'foo' })
|
2024-02-07 16:07:24 +00:00
|
|
|
return driver.wait(until.stalenessOf(el), 2000).then(() => assert.strictEqual(count, 3))
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('element state conditions', function () {
|
|
|
|
|
function runElementStateTest(predicate, command, responses, _var_args) {
|
|
|
|
|
let original = new webdriver.WebElement(driver, 'foo')
|
|
|
|
|
let predicateArgs = [original]
|
2016-01-31 11:58:32 -08:00
|
|
|
if (arguments.length > 3) {
|
2020-08-03 17:56:31 +03:00
|
|
|
predicateArgs = predicateArgs.concat(arguments[1])
|
|
|
|
|
command = arguments[2]
|
|
|
|
|
responses = arguments[3]
|
2016-01-31 11:58:32 -08:00
|
|
|
}
|
|
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
assert.ok(responses.length > 1)
|
|
|
|
|
|
|
|
|
|
responses = responses.concat(['end'])
|
|
|
|
|
executor.on(command, () => responses.shift())
|
|
|
|
|
|
|
|
|
|
let result = driver.wait(predicate.apply(null, predicateArgs), 2000)
|
|
|
|
|
assert.ok(result instanceof webdriver.WebElementPromise)
|
|
|
|
|
return result
|
|
|
|
|
.then(function (value) {
|
|
|
|
|
assert.ok(value instanceof webdriver.WebElement)
|
|
|
|
|
assert.ok(!(value instanceof webdriver.WebElementPromise))
|
|
|
|
|
return value.getId()
|
|
|
|
|
})
|
|
|
|
|
.then(function (id) {
|
2021-03-23 21:25:55 +05:30
|
|
|
assert.strictEqual('foo', id)
|
2020-08-03 17:56:31 +03:00
|
|
|
assert.deepStrictEqual(responses, ['end'])
|
|
|
|
|
})
|
2016-01-25 14:30:35 -08:00
|
|
|
}
|
|
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
it('elementIsVisible', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
return runElementStateTest(until.elementIsVisible, CommandName.IS_ELEMENT_DISPLAYED, [false, false, true])
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
2016-01-25 14:30:35 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
it('elementIsNotVisible', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
return runElementStateTest(until.elementIsNotVisible, CommandName.IS_ELEMENT_DISPLAYED, [true, true, false])
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
2016-01-25 14:30:35 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
it('elementIsEnabled', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
return runElementStateTest(until.elementIsEnabled, CommandName.IS_ELEMENT_ENABLED, [false, false, true])
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
2016-01-25 14:30:35 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
it('elementIsDisabled', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
return runElementStateTest(until.elementIsDisabled, CommandName.IS_ELEMENT_ENABLED, [true, true, false])
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
2016-01-25 14:30:35 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
it('elementIsSelected', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
return runElementStateTest(until.elementIsSelected, CommandName.IS_ELEMENT_SELECTED, [false, false, true])
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
2016-01-25 14:30:35 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
it('elementIsNotSelected', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
return runElementStateTest(until.elementIsNotSelected, CommandName.IS_ELEMENT_SELECTED, [true, true, false])
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
2016-01-25 14:30:35 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
it('elementTextIs', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
return runElementStateTest(until.elementTextIs, 'foobar', CommandName.GET_ELEMENT_TEXT, [
|
|
|
|
|
'foo',
|
|
|
|
|
'fooba',
|
2020-08-03 17:56:31 +03:00
|
|
|
'foobar',
|
2024-02-07 16:07:24 +00:00
|
|
|
])
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('elementTextContains', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
return runElementStateTest(until.elementTextContains, 'bar', CommandName.GET_ELEMENT_TEXT, [
|
|
|
|
|
'foo',
|
|
|
|
|
'foobaz',
|
|
|
|
|
'foobarbaz',
|
|
|
|
|
])
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('elementTextMatches', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
return runElementStateTest(until.elementTextMatches, /fo+bar{3}/, CommandName.GET_ELEMENT_TEXT, [
|
|
|
|
|
'foo',
|
|
|
|
|
'foobar',
|
|
|
|
|
'fooobarrr',
|
|
|
|
|
])
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('WebElementCondition', function () {
|
|
|
|
|
it('fails if wait completes with a non-WebElement value', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
let result = driver.wait(new webdriver.WebElementCondition('testing', () => 123), 1000)
|
2016-01-31 11:58:32 -08:00
|
|
|
|
|
|
|
|
return result.then(
|
2020-08-03 17:56:31 +03:00
|
|
|
() => assert.fail('expected to fail'),
|
|
|
|
|
function (e) {
|
|
|
|
|
assert.ok(e instanceof TypeError)
|
2024-02-07 16:07:24 +00:00
|
|
|
assert.strictEqual('WebElementCondition did not resolve to a WebElement: ' + '[object Number]', e.message)
|
|
|
|
|
},
|
2020-08-03 17:56:31 +03:00
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|