1
+
2
+ namespace Framework
3
+ {
4
+ internal abstract class BasePage
5
+ {
6
+ protected readonly IWebDriver Driver ;
7
+ //Locators to find elements on the page
8
+ private readonly By CaseStatusText = By . Id ( "Body_ctlRegistrationHeader_lblStatus" ) ;
9
+
10
+ protected BasePage ( IWebDriver driver )
11
+ {
12
+ Driver = driver ;
13
+ }
14
+ protected void Visit ( string url )
15
+ {
16
+ if ( url . StartsWith ( "http" ) )
17
+ Driver . Navigate ( ) . GoToUrl ( url ) ;
18
+ else
19
+ Driver . Navigate ( ) . GoToUrl ( BaseTest . ApplicationBaseUrl + url ) ;
20
+ }
21
+ private IWebElement Find ( By locator ) => Driver . FindElement ( locator ) ;
22
+ protected void Click ( By locator )
23
+ {
24
+ Find ( locator ) . Click ( ) ;
25
+ }
26
+ protected void EnabledClick ( By locator )
27
+ {
28
+ if ( IsEnabled ( locator ) )
29
+ Find ( locator ) . Click ( ) ;
30
+ }
31
+ protected void Type ( By locator , string inputText )
32
+ {
33
+ Find ( locator ) . SendKeys ( inputText ) ;
34
+ }
35
+ protected void EnabledType ( By locator , string inputText )
36
+ {
37
+ if ( IsEnabled ( locator ) )
38
+ Find ( locator ) . SendKeys ( inputText ) ;
39
+ }
40
+ protected void DisplayedType ( By locator , string inputText )
41
+ {
42
+ if ( IsDisplayed ( locator ) )
43
+ Find ( locator ) . SendKeys ( inputText ) ;
44
+ }
45
+ protected void Replace ( By locator , string inputText )
46
+ {
47
+ Find ( locator ) . Clear ( ) ;
48
+ Find ( locator ) . SendKeys ( inputText ) ;
49
+ }
50
+ protected void EnabledReplace ( By locator , string inputText )
51
+ {
52
+ if ( IsEnabled ( locator ) )
53
+ {
54
+ Find ( locator ) . Clear ( ) ;
55
+ Find ( locator ) . SendKeys ( inputText ) ;
56
+ }
57
+ }
58
+ protected void DisplayedReplace ( By locator , string inputText )
59
+ {
60
+ if ( IsDisplayed ( locator ) )
61
+ {
62
+ Find ( locator ) . Clear ( ) ;
63
+ Find ( locator ) . SendKeys ( inputText ) ;
64
+ }
65
+ }
66
+ protected void Select ( By locator , string inputText )
67
+ {
68
+ new SelectElement ( Find ( locator ) ) . SelectByText ( inputText ) ;
69
+ }
70
+ protected void EnabledSelect ( By locator , string inputText )
71
+ {
72
+ if ( IsEnabled ( locator ) )
73
+ new SelectElement ( Find ( locator ) ) . SelectByText ( inputText ) ;
74
+ }
75
+ protected void DisplayedSelect ( By locator , string inputText )
76
+ {
77
+ if ( IsDisplayed ( locator ) )
78
+ new SelectElement ( Find ( locator ) ) . SelectByText ( inputText ) ;
79
+ }
80
+ protected void SelectComboBox ( By locator , string inputText )
81
+ {
82
+ Find ( locator ) . SendKeys ( inputText ) ;
83
+ Thread . Sleep ( 1000 ) ;
84
+ Click ( ComboBoxSelection ) ;
85
+ }
86
+ protected void Check ( By locator , string check )
87
+ {
88
+ if ( ! string . IsNullOrEmpty ( check ) )
89
+ Find ( locator ) . Click ( ) ;
90
+ }
91
+ protected void EnabledCheck ( By locator , string check )
92
+ {
93
+ if ( IsEnabled ( locator ) )
94
+ {
95
+ if ( ! string . IsNullOrEmpty ( check ) )
96
+ Find ( locator ) . Click ( ) ;
97
+ }
98
+ }
99
+ public void SelectAllCheckboxes ( )
100
+ {
101
+ IList < IWebElement > ckOverrides = Driver . FindElements ( By . XPath ( "//input[@type='checkbox']" ) ) ;
102
+
103
+ foreach ( var ck in ckOverrides )
104
+ if ( ck . Displayed && ! ck . Selected )
105
+ ck . Click ( ) ;
106
+ }
107
+ protected void DeselectAllCheckBoxes ( )
108
+ {
109
+ IList < IWebElement > ckOverrides = Driver . FindElements ( By . XPath ( "//input[@type='checkbox']" ) ) ;
110
+
111
+ foreach ( var ck in ckOverrides )
112
+ if ( ck . Displayed && ck . Selected )
113
+ ck . Click ( ) ;
114
+ }
115
+ protected void TabOut ( By locator )
116
+ {
117
+ Type ( locator , Keys . Tab ) ;
118
+ }
119
+ protected bool IsDisplayed ( By locator )
120
+ {
121
+ try
122
+ {
123
+ return Find ( locator ) . Displayed ;
124
+ } catch ( NoSuchElementException )
125
+ {
126
+ return false ;
127
+ }
128
+ }
129
+ protected bool IsDisplayed ( By locator , int maxWaitTime )
130
+ {
131
+ try
132
+ {
133
+ var wait = new WebDriverWait ( Driver , TimeSpan . FromSeconds ( maxWaitTime ) ) ;
134
+ wait . Until ( ExpectedConditions . ElementIsVisible ( locator ) ) ;
135
+ return true ;
136
+ } catch ( WebDriverTimeoutException )
137
+ {
138
+ return false ;
139
+ }
140
+ }
141
+ protected bool IsDisplayed ( By locator , string text , int maxWaitTime )
142
+ {
143
+ try
144
+ {
145
+ var wait = new WebDriverWait ( Driver , TimeSpan . FromSeconds ( maxWaitTime ) ) ;
146
+ wait . Until ( ExpectedConditions . TextToBePresentInElementValue ( locator , text ) ) ;
147
+ return true ;
148
+ } catch ( WebDriverTimeoutException )
149
+ {
150
+ return false ;
151
+ }
152
+ }
153
+ protected bool IsEnabled ( By locator )
154
+ {
155
+ try
156
+ {
157
+ return Find ( locator ) . Enabled ;
158
+ } catch ( InvalidElementStateException )
159
+ {
160
+ return false ;
161
+ }
162
+ }
163
+ protected void SwitchToFrame ( string locator )
164
+ {
165
+ Driver . SwitchTo ( ) . Frame ( locator ) ;
166
+ }
167
+ protected void SwitchToDefaultFrame ( ) => Driver . SwitchTo ( ) . DefaultContent ( ) ;
168
+ protected string GetText ( By locator )
169
+ {
170
+ var text = Find ( locator ) ;
171
+ return text . Text ;
172
+ }
173
+ protected void AcceptPopup ( )
174
+ {
175
+ try
176
+ {
177
+ Driver . SwitchTo ( ) . Alert ( ) . Accept ( ) ;
178
+ } catch ( NoAlertPresentException e ) { }
179
+ }
180
+ }
181
+ }
0 commit comments