|
| 1 | +using NUnit.Framework; |
| 2 | +using NUnit.Framework.Interfaces; |
| 3 | +using System; |
| 4 | +using NUnit.Framework.Internal; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using OpenQA.Selenium.Environment; |
| 8 | + |
| 9 | +#if !NET45 && !NET46 && !NET47 |
| 10 | +using System.Runtime.InteropServices; |
| 11 | +using OSPlatform = System.Runtime.InteropServices.OSPlatform; |
| 12 | +#endif |
| 13 | + |
| 14 | + |
| 15 | +namespace OpenQA.Selenium |
| 16 | +{ |
| 17 | + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)] |
| 18 | + public class IgnorePlatformAttribute : NUnitAttribute, IApplyToTest |
| 19 | + { |
| 20 | + private readonly String platform; |
| 21 | + private readonly string ignoreReason = string.Empty; |
| 22 | + |
| 23 | + public IgnorePlatformAttribute(string platform) |
| 24 | + { |
| 25 | + this.platform = platform.ToLower(); |
| 26 | + } |
| 27 | + |
| 28 | + public IgnorePlatformAttribute(string platform, string reason) |
| 29 | + : this(platform) |
| 30 | + { |
| 31 | + this.ignoreReason = reason; |
| 32 | + } |
| 33 | + |
| 34 | + public string Value |
| 35 | + { |
| 36 | + get { return platform; } |
| 37 | + } |
| 38 | + |
| 39 | + public string Reason |
| 40 | + { |
| 41 | + get { return ignoreReason; } |
| 42 | + } |
| 43 | + |
| 44 | + public void ApplyToTest(Test test) |
| 45 | + { |
| 46 | + if (test.RunState != RunState.NotRunnable) |
| 47 | + { |
| 48 | + List<Attribute> ignoreAttributes = new List<Attribute>(); |
| 49 | + if (test.IsSuite) |
| 50 | + { |
| 51 | + Attribute[] ignoreClassAttributes = |
| 52 | + test.TypeInfo.GetCustomAttributes<IgnorePlatformAttribute>(true); |
| 53 | + if (ignoreClassAttributes.Length > 0) |
| 54 | + { |
| 55 | + ignoreAttributes.AddRange(ignoreClassAttributes); |
| 56 | + } |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + IgnorePlatformAttribute[] ignoreMethodAttributes = |
| 61 | + test.Method.GetCustomAttributes<IgnorePlatformAttribute>(true); |
| 62 | + if (ignoreMethodAttributes.Length > 0) |
| 63 | + { |
| 64 | + ignoreAttributes.AddRange(ignoreMethodAttributes); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + foreach (Attribute attr in ignoreAttributes) |
| 69 | + { |
| 70 | + IgnorePlatformAttribute platformToIgnoreAttr = attr as IgnorePlatformAttribute; |
| 71 | + if (platformToIgnoreAttr != null && IgnoreTestForPlatform(platformToIgnoreAttr.Value)) |
| 72 | + { |
| 73 | + string ignoreReason = |
| 74 | + "Ignoring platform " + EnvironmentManager.Instance.Browser.ToString() + "."; |
| 75 | + if (!string.IsNullOrEmpty(platformToIgnoreAttr.Reason)) |
| 76 | + { |
| 77 | + ignoreReason = ignoreReason + " " + platformToIgnoreAttr.Reason; |
| 78 | + } |
| 79 | + |
| 80 | + test.RunState = RunState.Ignored; |
| 81 | + test.Properties.Set(PropertyNames.SkipReason, platformToIgnoreAttr.Reason); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private bool IgnoreTestForPlatform(string platformToIgnore) |
| 88 | + { |
| 89 | + return CurrentPlatform() != null && platformToIgnore.Equals(CurrentPlatform()); |
| 90 | + } |
| 91 | + |
| 92 | + private string CurrentPlatform() |
| 93 | + { |
| 94 | +#if NET45 || NET46 || NET47 |
| 95 | + return null; |
| 96 | +#else |
| 97 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 98 | + { |
| 99 | + return "windows"; |
| 100 | + } |
| 101 | + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) |
| 102 | + { |
| 103 | + return "linux"; |
| 104 | + } |
| 105 | + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) |
| 106 | + { |
| 107 | + return "mac"; |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + throw new WebDriverException("Selenium Manager did not find supported operating system"); |
| 112 | + } |
| 113 | +#endif |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments