SeleniumClasses.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

if ('ValidateURIAttribute' -as [type])
{
    class ValidateURIAttribute :  System.Management.Automation.ValidateArgumentsAttribute {
        [void] Validate([object] $arguments , [System.Management.Automation.EngineIntrinsics]$EngineIntrinsics) {
            $Out = $null
            if   ([uri]::TryCreate($arguments,[System.UriKind]::Absolute, [ref]$Out)) {return}
            else {throw  [System.Management.Automation.ValidationMetadataException]::new('Incorrect StartURL please make sure the URL starts with http:// or https://')}
            return
        }
    }    
    class ValidateURI : ValidateURIAttribute {}
}


if ('ValidateIsWebDriverAttribute' -as [type])
{
    class ValidateIsWebDriverAttribute :  System.Management.Automation.ValidateArgumentsAttribute {
        [void] Validate([object] $arguments , [System.Management.Automation.EngineIntrinsics]$EngineIntrinsics) {
            if ($arguments -isnot [OpenQA.Selenium.Remote.RemoteWebDriver]) {
                    throw  [System.Management.Automation.ValidationMetadataException]::new('Target was not a valid web driver')
            }
            return
        }
    }
    class ValidateIsWebDriver : ValidateIsWebDriverAttribute {}
}

if ('ByTransformAttribute' -as [type])
{
    #Allow BY to shorten cssSelector, ClassName, LinkText, and TagName
    class ByTransformAttribute : System.Management.Automation.ArgumentTransformationAttribute  {
        [object] Transform([System.Management.Automation.EngineIntrinsics]$EngineIntrinsics, [object] $InputData) {
            if ($inputData -match 'CssSelector|Name|Id|ClassName|LinkText|PartialLinkText|TagName|XPath') {
                return $InputData
            }
            switch -regex ($InputData) {
                "^css"    {return 'CssSelector'; break}
                "^class"  {return 'ClassName'  ; break}
                "^link"   {return 'LinkText'   ; break}
                "^tag"    {return 'TagName'    ; break}
            }
            return $InputData
        }
    }

    class ByTransform : ByTransformAttribute {}
}

if ('OperatorTransformAttribute' -as [type])
{
    #Allow operator to use containing, matching, matches, equals etc.
    class OperatorTransformAttribute : System.Management.Automation.ArgumentTransformationAttribute  {
        [object] Transform([System.Management.Automation.EngineIntrinsics]$EngineIntrinsics, [object] $InputData) {
            if ($inputData -match '^(contains|like|notlike|match|notmatch|eq|ne|gt|lt)$#') {
                return $InputData
            }
            switch -regex ($InputData) {
                "^contain"    {return 'contains' ; break}
                "^match"      {return 'match'    ; break}
                "^n\w*match"  {return 'notmatch' ; break}
                "^eq"         {return 'eq'       ; break}
                "^n\w*eq"     {return 'ne'       ; break}
            }
            return $InputData
        }
    }

    class OperatorTransform : OperatorTransformAttribute {}
}

$dll1Path = Join-path -path (Join-path -path $PSScriptRoot -ChildPath 'assemblies') -ChildPath 'WebDriver.dll'
$dll2Path = Join-path -path (Join-path -path $PSScriptRoot -ChildPath 'assemblies') -ChildPath 'WebDriver.Support.dll'

Add-type @"
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System.Collections.Generic;
namespace SeleniumSelection {
    public class Option {
        public static bool IsMultiSelect(IWebElement element) {
            var selection = new SelectElement(element);
            return selection.IsMultiple;
        }
        public static IList<IWebElement> GetOptions(IWebElement element) {
            var selection = new SelectElement(element);
            return selection.Options;
        }
        public static void SelectByValue(IWebElement element, string value) {
            var selection = new SelectElement(element);
            selection.SelectByValue(value);
        }
        public static void DeselectByValue(IWebElement element, string value) {
            var selection = new SelectElement(element);
            selection.DeselectByValue(value);
        }
        public static void SelectByText(IWebElement element, string text, bool partialMatch = false) {
            var selection = new SelectElement(element);
            selection.SelectByText(text,partialMatch);
        }
        public static void DeselectByText(IWebElement element, string text) {
            var selection = new SelectElement(element);
            selection.DeselectByText(text);
        }
        public static void SelectByIndex(IWebElement element, int index) {
            var selection = new SelectElement(element);
            selection.SelectByIndex(index);
        }
        public static void DeselectByIndex(IWebElement element, int index) {
            var selection = new SelectElement(element);
            selection.DeselectByIndex(index);
        }
        public static void DeselectAll(IWebElement element) {
            var selection = new SelectElement(element);
            selection.DeselectAll();
        }
        public static IWebElement GetSelectedOption(IWebElement element) {
            var selection = new SelectElement(element);
            return selection.SelectedOption;
        }
        public static IList<IWebElement> GetAllSelectedOptions(IWebElement element) {
            var selection = new SelectElement(element);
            return selection.AllSelectedOptions;
        }
    }
}
"@
 -ReferencedAssemblies $dll1Path,$dll2Path, mscorlib