1
- using System ;
2
- using Boa . Constrictor . Screenplay ;
1
+ using Boa . Constrictor . Screenplay ;
3
2
using OpenQA . Selenium ;
3
+ using System ;
4
+ using System . Collections . Generic ;
5
+ using System . Linq ;
4
6
5
7
namespace Boa . Constrictor . Selenium
6
8
{
7
9
/// <summary>
8
10
/// Switches the frame.
11
+ /// Start from DefaultContent by default.
9
12
/// </summary>
10
13
public class SwitchFrame : AbstractWebTask
11
14
{
@@ -15,11 +18,12 @@ public class SwitchFrame : AbstractWebTask
15
18
/// Private constructor.
16
19
/// (Use static builder methods to construct.)
17
20
/// </summary>
18
- /// <param name="locator ">The locator .</param>
19
- /// <param name="useDefaultContent">If true use DefaultContent instead of the locator .</param>
20
- private SwitchFrame ( IWebLocator locator , bool useDefaultContent )
21
+ /// <param name="locators ">The list of locators .</param>
22
+ /// <param name="useDefaultContent">If true use DefaultContent instead of the locators .</param>
23
+ private SwitchFrame ( List < IWebLocator > locators , bool useDefaultContent )
21
24
{
22
- Locator = locator ;
25
+ Locators = locators ;
26
+ StartFromCurrentLocation = false ;
23
27
UseDefaultContent = useDefaultContent ;
24
28
}
25
29
@@ -28,12 +32,17 @@ private SwitchFrame(IWebLocator locator, bool useDefaultContent)
28
32
#region Properties
29
33
30
34
/// <summary>
31
- /// The target Web element's locator.
35
+ /// The list of target Web element locators.
36
+ /// </summary>
37
+ public List < IWebLocator > Locators { get ; }
38
+
39
+ /// <summary>
40
+ /// Start from current location instead of switching to DefaultContent first.
32
41
/// </summary>
33
- public IWebLocator Locator { get ; }
42
+ public bool StartFromCurrentLocation { get ; set ; }
34
43
35
44
/// <summary>
36
- /// The DefaultContent is used .
45
+ /// Switch to DefaultContent instead of a target frame .
37
46
/// </summary>
38
47
public bool UseDefaultContent { get ; }
39
48
@@ -46,13 +55,31 @@ private SwitchFrame(IWebLocator locator, bool useDefaultContent)
46
55
/// </summary>
47
56
/// <param name="locator">The locator.</param>
48
57
/// <returns></returns>
49
- public static SwitchFrame To ( IWebLocator locator ) => new SwitchFrame ( locator , false ) ;
58
+ public static SwitchFrame To ( IWebLocator locator ) =>
59
+ new SwitchFrame ( new List < IWebLocator > { locator } , false ) ;
50
60
51
61
/// <summary>
52
62
/// Constructs the Task object for DefaultContent.
53
63
/// </summary>
54
64
/// <returns></returns>
55
- public static SwitchFrame ToDefaultContent ( ) => new SwitchFrame ( null , true ) ;
65
+ public static SwitchFrame ToDefaultContent ( ) => new SwitchFrame ( new List < IWebLocator > ( ) , true ) ;
66
+
67
+ /// <summary>
68
+ /// Constructs the Task object for the given locator list.
69
+ /// </summary>
70
+ /// <param name="locators">The list of locators.</param>
71
+ /// <returns></returns>
72
+ public static SwitchFrame ToNested ( List < IWebLocator > locators ) => new SwitchFrame ( locators , false ) ;
73
+
74
+ /// <summary>
75
+ /// Sets the Task to start from current location instead of DefaultContent.
76
+ /// </summary>
77
+ /// <returns></returns>
78
+ public SwitchFrame AndStartFromCurrentLocation ( )
79
+ {
80
+ StartFromCurrentLocation = true ;
81
+ return this ;
82
+ }
56
83
57
84
#endregion
58
85
@@ -65,12 +92,16 @@ private SwitchFrame(IWebLocator locator, bool useDefaultContent)
65
92
/// <param name="driver">The WebDriver.</param>
66
93
public override void PerformAs ( IActor actor , IWebDriver driver )
67
94
{
68
- driver . SwitchTo ( ) . DefaultContent ( ) ;
95
+ if ( UseDefaultContent || ! StartFromCurrentLocation )
96
+ driver . SwitchTo ( ) . DefaultContent ( ) ;
69
97
70
98
if ( ! UseDefaultContent )
71
99
{
72
- actor . WaitsUntil ( Existence . Of ( Locator ) , IsEqualTo . True ( ) ) ;
73
- driver . SwitchTo ( ) . Frame ( Locator . FindElement ( driver ) ) ;
100
+ foreach ( IWebLocator locator in Locators )
101
+ {
102
+ actor . WaitsUntil ( Existence . Of ( locator ) , IsEqualTo . True ( ) ) ;
103
+ driver . SwitchTo ( ) . Frame ( locator . FindElement ( driver ) ) ;
104
+ }
74
105
}
75
106
}
76
107
@@ -80,24 +111,25 @@ public override void PerformAs(IActor actor, IWebDriver driver)
80
111
/// <param name="obj">The other object.</param>
81
112
public override bool Equals ( object obj ) =>
82
113
obj is SwitchFrame frame &&
83
- Locator . Equals ( frame . Locator ) &&
114
+ Locators . SequenceEqual ( frame . Locators ) &&
115
+ StartFromCurrentLocation == frame . StartFromCurrentLocation &&
84
116
UseDefaultContent == frame . UseDefaultContent ;
85
117
86
118
/// <summary>
87
119
/// Gets a unique hash code for this interaction.
88
120
/// </summary>
89
121
/// <returns></returns>
90
122
public override int GetHashCode ( ) =>
91
- HashCode . Combine ( GetType ( ) , Locator , UseDefaultContent ) ;
123
+ HashCode . Combine ( GetType ( ) , Locators , StartFromCurrentLocation , UseDefaultContent ) ;
92
124
93
125
/// <summary>
94
126
/// Returns a description of the Task.
95
127
/// </summary>
96
128
/// <returns></returns>
97
- public override string ToString ( ) =>
98
- UseDefaultContent
129
+ public override string ToString ( ) =>
130
+ UseDefaultContent
99
131
? "switch frame to DefaultContent"
100
- : $ "switch frame to '{ Locator . Description } '";
132
+ : $ "switch frame to '{ Locators . Last ( ) . Description } '";
101
133
102
134
#endregion
103
135
}
0 commit comments