// // 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. // using System.Collections; using System.Collections.ObjectModel; using Moq; using OpenQA.Selenium.Support.Extensions; namespace OpenQA.Selenium.Support.Tests.Extensions; public interface IJavaScriptExecutingWebDriver : IWebDriver, IJavaScriptExecutor { } public class SubClassOfReadOnlyCollectionOfObject : ReadOnlyCollection { public SubClassOfReadOnlyCollectionOfObject(IList list) : base(list) { } } [TestFixture] public class ExecuteJavaScriptTests { private const string JavaScript = "Hello, World"; private static readonly object[] JavaScriptParameters = new object[0]; private Mock driver; [SetUp] public void TestSetUp() { driver = new Mock(); } [Test] public void ShouldConvertToIEnumerable() { var expected = new ReadOnlyCollection(new List()); driver.Setup(_ => _.ExecuteScript(It.IsAny(), It.IsAny())).Returns(expected); Assert.That( () => driver.Object.ExecuteJavaScript(JavaScript, JavaScriptParameters), Throws.Nothing); } [Test] public void ShouldConvertToIEnumerableOfObject() { var expected = new ReadOnlyCollection(new List()); driver.Setup(_ => _.ExecuteScript(It.IsAny(), It.IsAny())).Returns(expected); Assert.That( () => driver.Object.ExecuteJavaScript>(JavaScript, JavaScriptParameters), Throws.Nothing); } [Test] public void ShouldNotConvertToIEnumerableOfInteger() { var expected = new ReadOnlyCollection(new List()); driver.Setup(_ => _.ExecuteScript(It.IsAny(), It.IsAny())).Returns(expected); Assert.That( () => driver.Object.ExecuteJavaScript>(JavaScript, JavaScriptParameters), Throws.TypeOf()); } [Test] public void ShouldConvertToReadOnlyCollectionOfObject() { var expected = new ReadOnlyCollection(new List()); driver.Setup(_ => _.ExecuteScript(It.IsAny(), It.IsAny())).Returns(expected); Assert.That( () => driver.Object.ExecuteJavaScript>(JavaScript, JavaScriptParameters), Throws.Nothing); } [Test] public void ShouldNotConvertToSubClassOfReadOnlyCollectionOfObject() { var expected = new ReadOnlyCollection(new List()); driver.Setup(_ => _.ExecuteScript(It.IsAny(), It.IsAny())).Returns(expected); Assert.That( () => driver.Object.ExecuteJavaScript(JavaScript, JavaScriptParameters), Throws.TypeOf()); } [Test] public void ShouldNotThrowWhenNullIsReturned() { driver.Setup(_ => _.ExecuteScript(It.IsAny(), It.IsAny())).Returns(null); Assert.That( () => driver.Object.ExecuteJavaScript(JavaScript, JavaScriptParameters), Throws.Nothing); } [Test] public void ShouldNotThrowWhenNullIsReturnedForNullableValueType() { driver.Setup(_ => _.ExecuteScript(It.IsAny(), It.IsAny())).Returns(null); Assert.That( () => driver.Object.ExecuteJavaScript(JavaScript, JavaScriptParameters), Throws.Nothing); } [Test] public void ShouldThrowWhenNullIsReturnedForValueType() { driver.Setup(_ => _.ExecuteScript(It.IsAny(), It.IsAny())).Returns(null); Assert.That( () => driver.Object.ExecuteJavaScript(JavaScript, JavaScriptParameters), Throws.TypeOf()); } [Test] public void ShouldAllowExecutingJavaScriptWithoutReturningResult() { driver.Setup(_ => _.ExecuteScript(It.IsAny(), It.IsAny())).Returns(null); Assert.That( () => driver.Object.ExecuteJavaScript(JavaScript, JavaScriptParameters), Throws.Nothing); } }