|
| 1 | +// |
| 2 | +// BrowserIdCustomization.cs |
| 3 | +// |
| 4 | +// Author: |
| 5 | +// Craig Fowler <craig@csf-dev.com> |
| 6 | +// |
| 7 | +// Copyright (c) 2018 Craig Fowler |
| 8 | +// |
| 9 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +// of this software and associated documentation files (the "Software"), to deal |
| 11 | +// in the Software without restriction, including without limitation the rights |
| 12 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +// copies of the Software, and to permit persons to whom the Software is |
| 14 | +// furnished to do so, subject to the following conditions: |
| 15 | +// |
| 16 | +// The above copyright notice and this permission notice shall be included in |
| 17 | +// all copies or substantial portions of the Software. |
| 18 | +// |
| 19 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +// THE SOFTWARE. |
| 26 | +using System; |
| 27 | +using Moq; |
| 28 | +using OpenQA.Selenium; |
| 29 | +using Ploeh.AutoFixture; |
| 30 | + |
| 31 | +namespace CSF.WebDriverExtras.Tests.Autofixture |
| 32 | +{ |
| 33 | + public class WebDriverWithIdentificationCustomiszation<T> : ICustomization |
| 34 | + where T : class,IHasCapabilities |
| 35 | + { |
| 36 | + readonly string browser, version, requestedVersion; |
| 37 | + readonly PlatformType? platform; |
| 38 | + readonly bool hasRequestedVersion; |
| 39 | + |
| 40 | + public void Customize(IFixture fixture) |
| 41 | + { |
| 42 | + fixture.Customize<T>(c => { |
| 43 | + return c.FromFactory((PlatformType plat, string name, string ver) => CreateWebDriver(plat, name, ver)); |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + T CreateWebDriver(PlatformType plat, string name, string ver) |
| 48 | + { |
| 49 | + var driver = new Mock<T>(); |
| 50 | + |
| 51 | + var caps = Mock.Of<ICapabilities>(); |
| 52 | + |
| 53 | + driver.SetupGet(x => x.Capabilities).Returns(caps); |
| 54 | + |
| 55 | + var effectiveBrowserName = browser ?? name; |
| 56 | + var effectiveBrowserVersion = version ?? ver; |
| 57 | + var effectivePlatform = platform ?? plat; |
| 58 | + |
| 59 | + Mock.Get(caps).SetupGet(x => x.BrowserName).Returns(effectiveBrowserName); |
| 60 | + Mock.Get(caps).SetupGet(x => x.Version).Returns(effectiveBrowserVersion); |
| 61 | + Mock.Get(caps).SetupGet(x => x.Platform).Returns(new Platform(effectivePlatform)); |
| 62 | + |
| 63 | + if(hasRequestedVersion) |
| 64 | + { |
| 65 | + driver |
| 66 | + .As<IHasRequestedVersion>() |
| 67 | + .SetupGet(x => x.RequestedBrowserVersion) |
| 68 | + .Returns(requestedVersion); |
| 69 | + } |
| 70 | + |
| 71 | + return driver.Object; |
| 72 | + } |
| 73 | + |
| 74 | + public WebDriverWithIdentificationCustomiszation(string browser, |
| 75 | + string version, |
| 76 | + PlatformType? platform, |
| 77 | + bool hasRequestedVersion, |
| 78 | + string requestedVersion) |
| 79 | + { |
| 80 | + this.browser = browser; |
| 81 | + this.version = version; |
| 82 | + this.platform = platform; |
| 83 | + this.hasRequestedVersion = hasRequestedVersion; |
| 84 | + this.requestedVersion = requestedVersion; |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments