How to use LocatorDragToOptions class of Microsoft.Playwright package

Best Playwright-dotnet code snippet using Microsoft.Playwright.LocatorDragToOptions

Run Playwright-dotnet automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

LocatorDragToOptions.cs

Source: LocatorDragToOptions.cs Github

copy
1/*
2 * MIT License
3 *
4 * Copyright (c) Microsoft Corporation.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25using System;
26using System.Collections.Generic;
27using System.ComponentModel.DataAnnotations;
28using System.Drawing;
29using System.Globalization;
30using System.IO;
31using System.Runtime.Serialization;
32using System.Text.Json;
33using System.Text.Json.Serialization;
34using System.Text.RegularExpressions;
35using System.Threading;
36using System.Threading.Tasks;
37
38#nullable enable
39
40namespace Microsoft.Playwright
41{
42    public class LocatorDragToOptions
43    {
44        public LocatorDragToOptions() { }
45
46        public LocatorDragToOptions(LocatorDragToOptions clone)
47        {
48            if (clone == null)
49            {
50                return;
51            }
52
53            Force = clone.Force;
54            NoWaitAfter = clone.NoWaitAfter;
55            SourcePosition = clone.SourcePosition;
56            TargetPosition = clone.TargetPosition;
57            Timeout = clone.Timeout;
58            Trial = clone.Trial;
59        }
60
61        /// <summary>
62        /// <para>
63        /// Whether to bypass the <a href="https://playwright.dev/dotnet/docs/actionability">actionability</a>
64        /// checks. Defaults to <c>false</c>.
65        /// </para>
66        /// </summary>
67        [JsonPropertyName("force")]
68        public bool? Force { get; set; }
69
70        /// <summary>
71        /// <para>
72        /// Actions that initiate navigations are waiting for these navigations to happen and
73        /// for pages to start loading. You can opt out of waiting via setting this flag. You
74        /// would only need this option in the exceptional cases such as navigating to inaccessible
75        /// pages. Defaults to <c>false</c>.
76        /// </para>
77        /// </summary>
78        [JsonPropertyName("noWaitAfter")]
79        public bool? NoWaitAfter { get; set; }
80
81        /// <summary>
82        /// <para>
83        /// Clicks on the source element at this point relative to the top-left corner of the
84        /// element's padding box. If not specified, some visible point of the element is used.
85        /// </para>
86        /// </summary>
87        [JsonPropertyName("sourcePosition")]
88        public SourcePosition? SourcePosition { get; set; }
89
90        /// <summary>
91        /// <para>
92        /// Drops on the target element at this point relative to the top-left corner of the
93        /// element's padding box. If not specified, some visible point of the element is used.
94        /// </para>
95        /// </summary>
96        [JsonPropertyName("targetPosition")]
97        public TargetPosition? TargetPosition { get; set; }
98
99        /// <summary>
100        /// <para>
101        /// Maximum time in milliseconds, defaults to 30 seconds, pass <c>0</c> to disable timeout.
102        /// The default value can be changed by using the <see cref="IBrowserContext.SetDefaultTimeout"/>
103        /// or <see cref="IPage.SetDefaultTimeout"/> methods.
104        /// </para>
105        /// </summary>
106        [JsonPropertyName("timeout")]
107        public float? Timeout { get; set; }
108
109        /// <summary>
110        /// <para>
111        /// When set, this method only performs the <a href="https://playwright.dev/dotnet/docs/actionability">actionability</a>
112        /// checks and skips the action. Defaults to <c>false</c>. Useful to wait until the
113        /// element is ready for the action without performing it.
114        /// </para>
115        /// </summary>
116        [JsonPropertyName("trial")]
117        public bool? Trial { get; set; }
118    }
119}
120
121#nullable disable
122
Full Screen

PlaywrightSyncElement.cs

Source: PlaywrightSyncElement.cs Github

copy
1//--------------------------------------------------
2// <copyright file="PlaywrightSyncElement.cs" company="Cognizant">
3//  Copyright 2022 Cognizant, All rights Reserved
4// </copyright>
5// <summary>Playwright synchronous element wrapper</summary>
6//--------------------------------------------------
7using Microsoft.Playwright;
8using System.Collections.Generic;
9using System.Text.Json;
10
11namespace CognizantSoftvision.Maqs.BasePlaywrightTest
12{
13    /// <summary>
14    /// Playwright synchronous element
15    /// </summary>
16    public class PlaywrightSyncElement
17    {
18        /// <summary>
19        /// Initializes a new instance of the <see cref="PlaywrightSyncElement" /> class
20        /// </summary>
21        /// <param name="page">The assoicated playwright page</param>
22        /// <param name="selector">Element selector</param>
23        /// <param name="options">Advanced locator options</param>
24        public PlaywrightSyncElement(IPage page, string selector, PageLocatorOptions? options = null)
25        {
26            this.ParentPage = page;
27            this.Selector = selector;
28            this.PageOptions = options;
29        }
30
31        /// <summary>
32        /// Initializes a new instance of the <see cref="PlaywrightSyncElement" /> class
33        /// </summary>
34        /// <param name="parent">The parent playwright element</param>
35        /// <param name="selector">Sub element selector</param>
36        /// <param name="options">Advanced locator options</param>
37        public PlaywrightSyncElement(PlaywrightSyncElement parent, string selector, LocatorLocatorOptions? options = null)
38        {
39            this.ParentLocator = parent.ElementLocator();
40            this.Selector = selector;
41            this.LocatorOptions = options;
42        }
43
44        /// <summary>
45        /// Initializes a new instance of the <see cref="PlaywrightSyncElement" /> class
46        /// </summary>
47        /// <param name="frame">The assoicated playwright frame locator</param>
48        /// <param name="selector">Element selector</param>
49        public PlaywrightSyncElement(IFrameLocator frame, string selector)
50        {
51            this.ParentFrameLocator = frame;
52            this.Selector = selector;
53        }
54
55        /// <summary>
56        /// Initializes a new instance of the <see cref="PlaywrightSyncElement" /> class
57        /// </summary>
58        /// <param name="testObject">The assoicated playwright test object</param>
59        /// <param name="selector">Element selector</param>
60        /// <param name="options">Advanced locator options</param>
61        public PlaywrightSyncElement(IPlaywrightTestObject testObject, string selector, PageLocatorOptions? options = null) : this(testObject.PageDriver.AsyncPage, selector, options)
62        {
63        }
64
65        /// <summary>
66        /// Initializes a new instance of the <see cref="PlaywrightSyncElement" /> class
67        /// </summary>
68        /// <param name="driver">The assoicated playwright page driver</param>
69        /// <param name="selector">Element selector</param>
70        /// <param name="options">Advanced locator options</param>
71        public PlaywrightSyncElement(PageDriver driver, string selector, PageLocatorOptions? options = null) : this(driver.AsyncPage, selector, options)
72        {
73        }
74
75        /// <summary>
76        /// Gets the parent async page
77        /// </summary>
78        public IPage? ParentPage { get; private set; }
79
80        /// <summary>
81        /// Gets the parent locator
82        /// </summary>
83        public ILocator? ParentLocator { get; private set; }
84
85        /// <summary>
86        /// Gets the parent frame locator
87        /// </summary>
88        public IFrameLocator? ParentFrameLocator { get; private set; }
89
90        /// <summary>
91        /// Gets the page locator options
92        /// </summary>
93        public PageLocatorOptions? PageOptions { get; private set; }
94
95        /// <summary>
96        /// Gets the page locator options
97        /// </summary>
98        public LocatorLocatorOptions? LocatorOptions { get; private set; }
99
100        /// <summary>
101        /// Gets the selector string
102        /// </summary>
103        public string Selector { get; private set; }
104
105        /// <summary>
106        /// ILocator for this element
107        /// </summary>
108        /// <returns></returns>
109        public ILocator ElementLocator()
110        {
111            if (this.ParentPage != null)
112            {
113                return this.ParentPage.Locator(Selector, PageOptions);
114            }
115            else if (this.ParentLocator != null)
116            {
117                return this.ParentLocator.Locator(Selector, LocatorOptions);
118            }
119            else if (this.ParentFrameLocator != null)
120            {
121                return this.ParentFrameLocator.Locator(Selector);
122            }
123
124            throw new PlaywrightException("Both parent IPage and PlaywrightElement are null");
125        }
126
127        /// <inheritdoc cref = "ILocator.CheckAsync" /> 
128        public void Check(LocatorCheckOptions? options = null)
129        {
130            ElementLocator().CheckAsync(options).Wait();
131        }
132
133        /// <inheritdoc cref = "ILocator.ClickAsync" /> 
134        public void Click(LocatorClickOptions? options = null)
135        {
136            ElementLocator().ClickAsync(options).Wait();
137        }
138
139        /// <inheritdoc cref = "ILocator.DblClickAsync" /> 
140        public void DblClick(LocatorDblClickOptions? options = null)
141        {
142            ElementLocator().DblClickAsync(options).Wait();
143        }
144
145        /// <inheritdoc cref = "ILocator.DispatchEventAsync" /> 
146        public void DispatchEvent(string type, object? eventInit = null, LocatorDispatchEventOptions? options = null)
147        {
148            ElementLocator().DispatchEventAsync(type, eventInit, options).Wait();
149        }
150
151        /// <inheritdoc cref = "ILocator.DragToAsync(ILocator, LocatorDragToOptions?)" /> 
152        public void DragTo(ILocator target, LocatorDragToOptions? options = null)
153        {
154            ElementLocator().DragToAsync(target, options).Wait();
155        }
156
157        /// <inheritdoc cref = "ILocator.FillAsync" /> 
158        public void Fill(string value, LocatorFillOptions? options = null)
159        {
160            ElementLocator().FillAsync(value, options).Wait();
161        }
162
163        /// <inheritdoc cref = "ILocator.FocusAsync" /> 
164        public void Focus(LocatorFocusOptions? options = null)
165        {
166            ElementLocator().FocusAsync(options).Wait();
167        }
168
169        /// <inheritdoc cref = "ILocator.HoverAsync" /> 
170        public void Hover(LocatorHoverOptions? options = null)
171        {
172            ElementLocator().HoverAsync(options).Wait();
173        }
174
175        /// <inheritdoc cref = "ILocator.PressAsync" /> 
176        public void Press(string key, LocatorPressOptions? options = null)
177        {
178            ElementLocator().PressAsync(key, options).Wait();
179        }
180
181        /// <inheritdoc cref = "ILocator.SetCheckedAsync" /> 
182        public void SetChecked(bool checkedState, LocatorSetCheckedOptions? options = null)
183        {
184            ElementLocator().SetCheckedAsync(checkedState, options).Wait();
185        }
186
187        /// <inheritdoc cref = "ILocator.SetInputFilesAsync(FilePayload, LocatorSetInputFilesOptions)" /> 
188        public void SetInputFiles(FilePayload files, LocatorSetInputFilesOptions? options = null)
189        {
190            ElementLocator().SetInputFilesAsync(files, options).Wait();
191        }
192
193        /// <inheritdoc cref = "ILocator.SetInputFilesAsync(IEnumerable{FilePayload}, LocatorSetInputFilesOptions)" /> 
194        public void SetInputFiles(IEnumerable<FilePayload> files, LocatorSetInputFilesOptions? options = null)
195        {
196            ElementLocator().SetInputFilesAsync(files, options).Wait();
197        }
198
199        /// <inheritdoc cref = "ILocator.SetInputFilesAsync(IEnumerable{string}, LocatorSetInputFilesOptions)" /> 
200        public void SetInputFiles(IEnumerable<string> files, LocatorSetInputFilesOptions? options = null)
201        {
202            ElementLocator().SetInputFilesAsync(files, options).Wait();
203        }
204
205        /// <inheritdoc cref = "ILocator.SetInputFilesAsync(string, LocatorSetInputFilesOptions)" /> 
206        public void SetInputFiles(string files, LocatorSetInputFilesOptions? options = null)
207        {
208            ElementLocator().SetInputFilesAsync(files, options).Wait();
209        }
210
211        /// <inheritdoc cref = "ILocator.TapAsync" /> 
212        public void Tap(LocatorTapOptions? options = null)
213        {
214            ElementLocator().TapAsync(options).Wait();
215        }
216
217        /// <inheritdoc cref = "ILocator.TypeAsync" /> 
218        public void Type(string text, LocatorTypeOptions? options = null)
219        {
220            ElementLocator().TypeAsync(text, options).Wait();
221        }
222
223        /// <inheritdoc cref = "ILocator.UncheckAsync" /> 
224        public void Uncheck(LocatorUncheckOptions? options = null)
225        {
226            ElementLocator().UncheckAsync(options).Wait();
227        }
228
229        /// <inheritdoc cref = "ILocator.IsCheckedAsync"  />
230        public bool IsChecked(LocatorIsCheckedOptions? options = null)
231        {
232            return ElementLocator().IsCheckedAsync(options).Result;
233        }
234
235        /// <inheritdoc cref = "ILocator.IsDisabledAsync"  />
236        public bool IsDisabled(LocatorIsDisabledOptions? options = null)
237        {
238            return ElementLocator().IsDisabledAsync(options).Result;
239        }
240
241        /// <inheritdoc cref = "ILocator.IsEditableAsync"  />
242        public bool IsEditable(LocatorIsEditableOptions? options = null)
243        {
244            return ElementLocator().IsEditableAsync(options).Result;
245        }
246
247        /// <inheritdoc cref = "ILocator.IsEnabledAsync"  />
248        public bool IsEnabled(LocatorIsEnabledOptions? options = null)
249        {
250            return ElementLocator().IsEnabledAsync(options).Result;
251        }
252
253        /// <inheritdoc cref = "ILocator.IsHiddenAsync"  />
254        public bool IsHidden(LocatorIsHiddenOptions? options = null)
255        {
256            return ElementLocator().IsHiddenAsync(options).Result;
257        }
258
259        /// <inheritdoc cref = "ILocator.IsVisibleAsync"  />
260        public bool IsVisible(LocatorIsVisibleOptions? options = null)
261        {
262            return ElementLocator().IsVisibleAsync(options).Result;
263        }
264
265        /// <summary>
266        /// Check that the element is eventually visible
267        /// </summary>
268        /// <returns>True if the element becomes visible within the page timeout</returns>
269        public bool IsEventualyVisible()
270        {
271            try
272            {
273                this.ElementLocator().WaitForAsync(new LocatorWaitForOptions
274                {
275                    State = WaitForSelectorState.Visible,
276                }).Wait();
277                return true;
278            }
279            catch
280            {
281                return false;
282            }
283        }
284
285        /// <summary>
286        /// Check that the element stops being visible
287        /// </summary>
288        /// <returns>True if the element becomes is hidden or gone within the page timeout</returns>
289        public bool IsEventualyGone()
290        {
291            try
292            {
293                this.ElementLocator().WaitForAsync(new LocatorWaitForOptions
294                {
295                    State = WaitForSelectorState.Hidden,
296                }).Wait();
297                return true;
298            }
299            catch
300            {
301                return false;
302            }
303        }
304
305        /// <inheritdoc cref = "ILocator.SelectOptionAsync(IElementHandle, LocatorSelectOptionOptions)"  />
306        public IReadOnlyList<string> SelectOption(IElementHandle values, LocatorSelectOptionOptions? options = null)
307        {
308            return ElementLocator().SelectOptionAsync(values, options).Result;
309        }
310
311        /// <inheritdoc cref = "ILocator.SelectOptionAsync(IEnumerable{IElementHandle}, LocatorSelectOptionOptions)"  />
312        public IReadOnlyList<string> SelectOption(IEnumerable<IElementHandle> values, LocatorSelectOptionOptions? options = null)
313        {
314            return ElementLocator().SelectOptionAsync(values, options).Result;
315        }
316
317        /// <inheritdoc cref = "ILocator.SelectOptionAsync(IEnumerable{SelectOptionValue}, LocatorSelectOptionOptions)"  />
318        public IReadOnlyList<string> SelectOption(IEnumerable<SelectOptionValue> values, LocatorSelectOptionOptions? options = null)
319        {
320            return ElementLocator().SelectOptionAsync(values, options).Result;
321        }
322
323        /// <inheritdoc cref = "ILocator.SelectOptionAsync(IEnumerable{string}, LocatorSelectOptionOptions)"  />
324        public IReadOnlyList<string> SelectOption(IEnumerable<string> values, LocatorSelectOptionOptions? options = null)
325        {
326            return ElementLocator().SelectOptionAsync(values, options).Result;
327        }
328
329        /// <inheritdoc cref = "ILocator.SelectOptionAsync(SelectOptionValue, LocatorSelectOptionOptions)"  />
330        public IReadOnlyList<string> SelectOption(SelectOptionValue values, LocatorSelectOptionOptions? options = null)
331        {
332            return ElementLocator().SelectOptionAsync(values, options).Result;
333        }
334
335        /// <inheritdoc cref = "ILocator.SelectOptionAsync(string, LocatorSelectOptionOptions)"  />
336        public IReadOnlyList<string> SelectOption(string values, LocatorSelectOptionOptions? options = null)
337        {
338            return ElementLocator().SelectOptionAsync(values, options).Result;
339        }
340
341        /// <inheritdoc cref = "ILocator.EvaluateAllAsync"  />
342        public T EvaluateAll<T>(string expression, object? arg = null)
343        {
344            return ElementLocator().EvaluateAllAsync<T>(expression, arg).Result;
345        }
346
347        /// <inheritdoc cref = "ILocator.EvaluateAsync"  />
348        public JsonElement? Evaluate(string expression, object? arg = null)
349        {
350            return ElementLocator().EvaluateAsync(expression, arg).Result;
351        }
352
353        /// <inheritdoc cref = "ILocator.GetAttributeAsync"  />
354        public string? GetAttribute(string name, LocatorGetAttributeOptions? options = null)
355        {
356            return ElementLocator().GetAttributeAsync(name, options).Result;
357        }
358
359        /// <inheritdoc cref = "ILocator.TextContentAsync"  />
360        public string? TextContent(LocatorTextContentOptions? options = null)
361        {
362            return ElementLocator().TextContentAsync(options).Result;
363        }
364
365        /// <inheritdoc cref = "ILocator.AllTextContentsAsync"  />
366        public IReadOnlyList<string> AllTextContents()
367        {
368            return ElementLocator().AllTextContentsAsync().Result;
369        }
370
371        /// <inheritdoc cref = "ILocator.AllInnerTextsAsync"  />
372        public IReadOnlyList<string> AllInnerTexts()
373        {
374            return ElementLocator().AllInnerTextsAsync().Result;
375        }
376
377        /// <inheritdoc cref = "ILocator.InnerHTMLAsync"  />
378        public string InnerHTML(LocatorInnerHTMLOptions? options = null)
379        {
380            return ElementLocator().InnerHTMLAsync(options).Result;
381        }
382
383        /// <inheritdoc cref = "ILocator.InnerTextAsync"  />
384        public string InnerText(LocatorInnerTextOptions? options = null)
385        {
386            return ElementLocator().InnerTextAsync(options).Result;
387        }
388
389        /// <inheritdoc cref = "ILocator.InputValueAsync"  />
390        public string InputValue(LocatorInputValueOptions? options = null)
391        {
392            return ElementLocator().InputValueAsync(options).Result;
393        }
394    }
395}
396
Full Screen

Locator.cs

Source: Locator.cs Github

copy
1/*
2 * MIT License
3 *
4 * Copyright (c) 2020 Darío Kondratiuk
5 * Copyright (c) 2020 Meir Blachman
6 * Modifications copyright (c) Microsoft Corporation.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in all
16 * copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26using System;
27using System.Collections.Generic;
28using System.Text.Json;
29using System.Threading.Tasks;
30using Microsoft.Playwright.Helpers;
31using Microsoft.Playwright.Transport.Protocol;
32
33namespace Microsoft.Playwright.Core
34{
35    internal class Locator : ILocator
36    {
37        internal readonly Frame _frame;
38        internal readonly string _selector;
39
40        private readonly LocatorLocatorOptions _options;
41
42        public Locator(Frame parent, string selector, LocatorLocatorOptions options = null)
43        {
44            _frame = parent;
45            _selector = selector;
46            _options = options;
47
48            if (options?.HasTextRegex != null)
49            {
50                _selector += $" >> :scope:text-matches({options.HasTextRegex.ToString().EscapeWithQuotes("\"")}, {options.HasTextRegex.Options.GetInlineFlags().EscapeWithQuotes("\"")})";
51            }
52            if (options?.HasTextString != null)
53            {
54                _selector += $" >> :scope:has-text({options.HasTextString.EscapeWithQuotes("\"")})";
55            }
56
57            if (options?.Has != null)
58            {
59                var has = (Locator)options.Has;
60                if (has._frame != _frame)
61                {
62                    throw new ArgumentException("Inner \"has\" locator must belong to the same frame.");
63                }
64                _selector += " >> has=" + JsonSerializer.Serialize(has._selector);
65            }
66        }
67
68        public ILocator First => new Locator(_frame, $"{_selector} >> nth=0");
69
70        public ILocator Last => new Locator(_frame, $"{_selector} >> nth=-1");
71
72        IPage ILocator.Page => _frame.Page;
73
74        public async Task<IReadOnlyList<string>> AllInnerTextsAsync()
75            => await EvaluateAllAsync<string[]>("ee => ee.map(e => e.innerText)").ConfigureAwait(false);
76
77        public async Task<IReadOnlyList<string>> AllTextContentsAsync()
78            => await EvaluateAllAsync<string[]>("ee => ee.map(e => e.textContent || '')").ConfigureAwait(false);
79
80        public async Task<LocatorBoundingBoxResult> BoundingBoxAsync(LocatorBoundingBoxOptions options = null)
81            => await WithElementAsync(
82                async (h, _) =>
83                {
84                    var bb = await h.BoundingBoxAsync().ConfigureAwait(false);
85                    if (bb == null)
86                    {
87                        return null;
88                    }
89
90                    return new LocatorBoundingBoxResult()
91                    {
92                        Height = bb.Height,
93                        Width = bb.Width,
94                        X = bb.X,
95                        Y = bb.Y,
96                    };
97                },
98                options).ConfigureAwait(false);
99
100        public Task CheckAsync(LocatorCheckOptions options = null)
101            => _frame.CheckAsync(
102                _selector,
103                ConvertOptions<FrameCheckOptions>(options));
104
105        public Task ClickAsync(LocatorClickOptions options = null)
106            => _frame.ClickAsync(
107                _selector,
108                ConvertOptions<FrameClickOptions>(options));
109
110        public Task SetCheckedAsync(bool checkedState, LocatorSetCheckedOptions options = null)
111            => checkedState ?
112                CheckAsync(ConvertOptions<LocatorCheckOptions>(options))
113                : UncheckAsync(ConvertOptions<LocatorUncheckOptions>(options));
114
115        public Task<int> CountAsync()
116            => _frame.QueryCountAsync(_selector);
117
118        public Task DblClickAsync(LocatorDblClickOptions options = null)
119            => _frame.DblClickAsync(_selector, ConvertOptions<FrameDblClickOptions>(options));
120
121        public Task DispatchEventAsync(string type, object eventInit = null, LocatorDispatchEventOptions options = null)
122            => _frame.DispatchEventAsync(_selector, type, eventInit, ConvertOptions<FrameDispatchEventOptions>(options));
123
124        public Task DragToAsync(ILocator target, LocatorDragToOptions options = null)
125            => _frame.DragAndDropAsync(_selector, ((Locator)target)._selector, ConvertOptions<FrameDragAndDropOptions>(options));
126
127        public async Task<IElementHandle> ElementHandleAsync(LocatorElementHandleOptions options = null)
128            => await _frame.WaitForSelectorAsync(
129                _selector,
130                ConvertOptions<FrameWaitForSelectorOptions>(options)).ConfigureAwait(false);
131
132        public Task<IReadOnlyList<IElementHandle>> ElementHandlesAsync()
133            => _frame.QuerySelectorAllAsync(_selector);
134
135        public Task<T> EvaluateAllAsync<T>(string expression, object arg = null)
136            => _frame.EvalOnSelectorAllAsync<T>(_selector, expression, arg);
137
138        public Task<JsonElement?> EvaluateAsync(string expression, object arg = null, LocatorEvaluateOptions options = null)
139            => EvaluateAsync<JsonElement?>(expression, arg, options);
140
141        public Task<T> EvaluateAsync<T>(string expression, object arg = null, LocatorEvaluateOptions options = null)
142            => _frame.EvalOnSelectorAsync<T>(_selector, expression, arg, ConvertOptions<FrameEvalOnSelectorOptions>(options));
143
144        public async Task<IJSHandle> EvaluateHandleAsync(string expression, object arg = null, LocatorEvaluateHandleOptions options = null)
145            => await WithElementAsync(async (e, _) => await e.EvaluateHandleAsync(expression, arg).ConfigureAwait(false), options).ConfigureAwait(false);
146
147        public async Task FillAsync(string value, LocatorFillOptions options = null)
148            => await _frame.FillAsync(_selector, value, ConvertOptions<FrameFillOptions>(options)).ConfigureAwait(false);
149
150        public Task FocusAsync(LocatorFocusOptions options = null)
151            => _frame.FocusAsync(_selector, ConvertOptions<FrameFocusOptions>(options));
152
153        IFrameLocator ILocator.FrameLocator(string selector) =>
154            new FrameLocator(_frame, $"{_selector} >> {selector}");
155
156        public Task<string> GetAttributeAsync(string name, LocatorGetAttributeOptions options = null)
157            => _frame.GetAttributeAsync(_selector, name, ConvertOptions<FrameGetAttributeOptions>(options));
158
159        public Task HoverAsync(LocatorHoverOptions options = null)
160            => _frame.HoverAsync(_selector, ConvertOptions<FrameHoverOptions>(options));
161
162        public Task<string> InnerHTMLAsync(LocatorInnerHTMLOptions options = null)
163            => _frame.InnerHTMLAsync(_selector, ConvertOptions<FrameInnerHTMLOptions>(options));
164
165        public Task<string> InnerTextAsync(LocatorInnerTextOptions options = null)
166            => _frame.InnerTextAsync(_selector, ConvertOptions<FrameInnerTextOptions>(options));
167
168        public Task<string> InputValueAsync(LocatorInputValueOptions options = null)
169            => _frame.InputValueAsync(_selector, ConvertOptions<FrameInputValueOptions>(options));
170
171        public Task<bool> IsCheckedAsync(LocatorIsCheckedOptions options = null)
172            => _frame.IsCheckedAsync(_selector, ConvertOptions<FrameIsCheckedOptions>(options));
173
174        public Task<bool> IsDisabledAsync(LocatorIsDisabledOptions options = null)
175            => _frame.IsDisabledAsync(_selector, ConvertOptions<FrameIsDisabledOptions>(options));
176
177        public Task<bool> IsEditableAsync(LocatorIsEditableOptions options = null)
178            => _frame.IsEditableAsync(_selector, ConvertOptions<FrameIsEditableOptions>(options));
179
180        public Task<bool> IsEnabledAsync(LocatorIsEnabledOptions options = null)
181            => _frame.IsEnabledAsync(_selector, ConvertOptions<FrameIsEnabledOptions>(options));
182
183        public Task<bool> IsHiddenAsync(LocatorIsHiddenOptions options = null)
184            => _frame.IsHiddenAsync(_selector, ConvertOptions<FrameIsHiddenOptions>(options));
185
186        public Task<bool> IsVisibleAsync(LocatorIsVisibleOptions options = null)
187            => _frame.IsVisibleAsync(_selector, ConvertOptions<FrameIsVisibleOptions>(options));
188
189        public ILocator Nth(int index)
190            => new Locator(_frame, $"{_selector} >> nth={index}");
191
192        public Task PressAsync(string key, LocatorPressOptions options = null)
193            => _frame.PressAsync(_selector, key, ConvertOptions<FramePressOptions>(options));
194
195        public Task<byte[]> ScreenshotAsync(LocatorScreenshotOptions options = null)
196            => WithElementAsync(async (h, o) => await h.ScreenshotAsync(ConvertOptions<ElementHandleScreenshotOptions>(o)).ConfigureAwait(false), options);
197
198        public Task ScrollIntoViewIfNeededAsync(LocatorScrollIntoViewIfNeededOptions options = null)
199            => WithElementAsync(async (h, o) => await h.ScrollIntoViewIfNeededAsync(ConvertOptions<ElementHandleScrollIntoViewIfNeededOptions>(o)).ConfigureAwait(false), options);
200
201        public Task<IReadOnlyList<string>> SelectOptionAsync(string values, LocatorSelectOptionOptions options = null)
202            => _frame.SelectOptionAsync(_selector, values, ConvertOptions<FrameSelectOptionOptions>(options));
203
204        public Task<IReadOnlyList<string>> SelectOptionAsync(IElementHandle values, LocatorSelectOptionOptions options = null)
205            => _frame.SelectOptionAsync(_selector, values, ConvertOptions<FrameSelectOptionOptions>(options));
206
207        public Task<IReadOnlyList<string>> SelectOptionAsync(IEnumerable<string> values, LocatorSelectOptionOptions options = null)
208            => _frame.SelectOptionAsync(_selector, values, ConvertOptions<FrameSelectOptionOptions>(options));
209
210        public Task<IReadOnlyList<string>> SelectOptionAsync(SelectOptionValue values, LocatorSelectOptionOptions options = null)
211            => _frame.SelectOptionAsync(_selector, values, ConvertOptions<FrameSelectOptionOptions>(options));
212
213        public Task<IReadOnlyList<string>> SelectOptionAsync(IEnumerable<IElementHandle> values, LocatorSelectOptionOptions options = null)
214            => _frame.SelectOptionAsync(_selector, values, ConvertOptions<FrameSelectOptionOptions>(options));
215
216        public Task<IReadOnlyList<string>> SelectOptionAsync(IEnumerable<SelectOptionValue> values, LocatorSelectOptionOptions options = null)
217            => _frame.SelectOptionAsync(_selector, values, ConvertOptions<FrameSelectOptionOptions>(options));
218
219        public Task SelectTextAsync(LocatorSelectTextOptions options = null)
220            => WithElementAsync((h, o) => h.SelectTextAsync(ConvertOptions<ElementHandleSelectTextOptions>(o)), options);
221
222        public Task SetInputFilesAsync(string files, LocatorSetInputFilesOptions options = null)
223            => _frame.SetInputFilesAsync(_selector, files, ConvertOptions<FrameSetInputFilesOptions>(options));
224
225        public Task SetInputFilesAsync(IEnumerable<string> files, LocatorSetInputFilesOptions options = null)
226            => _frame.SetInputFilesAsync(_selector, files, ConvertOptions<FrameSetInputFilesOptions>(options));
227
228        public Task SetInputFilesAsync(FilePayload files, LocatorSetInputFilesOptions options = null)
229            => _frame.SetInputFilesAsync(_selector, files, ConvertOptions<FrameSetInputFilesOptions>(options));
230
231        public Task SetInputFilesAsync(IEnumerable<FilePayload> files, LocatorSetInputFilesOptions options = null)
232            => _frame.SetInputFilesAsync(_selector, files, ConvertOptions<FrameSetInputFilesOptions>(options));
233
234        public Task TapAsync(LocatorTapOptions options = null)
235            => _frame.TapAsync(_selector, ConvertOptions<FrameTapOptions>(options));
236
237        public Task<string> TextContentAsync(LocatorTextContentOptions options = null)
238            => _frame.TextContentAsync(_selector, ConvertOptions<FrameTextContentOptions>(options));
239
240        public Task TypeAsync(string text, LocatorTypeOptions options = null)
241            => _frame.TypeAsync(_selector, text, ConvertOptions<FrameTypeOptions>(options));
242
243        public Task UncheckAsync(LocatorUncheckOptions options = null)
244            => _frame.UncheckAsync(_selector, ConvertOptions<FrameUncheckOptions>(options));
245
246        ILocator ILocator.Locator(string selector, LocatorLocatorOptions options)
247            => new Locator(_frame, $"{_selector} >> {selector}", options);
248
249        public Task WaitForAsync(LocatorWaitForOptions options = null)
250            => _frame.LocatorWaitForAsync(_selector, ConvertOptions<LocatorWaitForOptions>(options));
251
252        internal Task<FrameExpectResult> ExpectAsync(string expression, FrameExpectOptions options = null)
253            => _frame.ExpectAsync(
254                _selector,
255                expression,
256                options);
257
258        public override string ToString() => "[email protected]" + _selector;
259
260        private T ConvertOptions<T>(object source)
261            where T : class, new()
262        {
263            T target = new();
264            var targetType = target.GetType();
265            if (source != null)
266            {
267                var sourceType = source.GetType();
268                foreach (var sourceProperty in sourceType.GetProperties())
269                {
270                    var targetProperty = targetType.GetProperty(sourceProperty.Name);
271                    if (targetProperty != null)
272                    {
273                        targetProperty.SetValue(target, sourceProperty.GetValue(source));
274                    }
275                }
276            }
277            var strictProperty = targetType.GetProperty("Strict");
278            if (strictProperty != null && strictProperty.GetValue(target) == null)
279            {
280                strictProperty.SetValue(target, true);
281            }
282            return target;
283        }
284
285        private async Task<TResult> WithElementAsync<TOptions, TResult>(Func<IElementHandle, TOptions, Task<TResult>> callback, TOptions options)
286            where TOptions : class
287            where TResult : class
288        {
289            IElementHandle handle = await ElementHandleAsync(ConvertOptions<LocatorElementHandleOptions>(options)).ConfigureAwait(false);
290            try
291            {
292                return await callback(handle, options).ConfigureAwait(false);
293            }
294            finally
295            {
296                await handle.DisposeAsync().ConfigureAwait(false);
297            }
298        }
299
300        private async Task WithElementAsync<TOptions>(Func<IElementHandle, TOptions, Task> callback, TOptions options)
301            where TOptions : class
302        {
303            IElementHandle handle = await ElementHandleAsync(ConvertOptions<LocatorElementHandleOptions>(options)).ConfigureAwait(false);
304            try
305            {
306                await callback(handle, options).ConfigureAwait(false);
307            }
308            finally
309            {
310                await handle.DisposeAsync().ConfigureAwait(false);
311            }
312        }
313
314        public Task HighlightAsync() => _frame.HighlightAsync(_selector);
315    }
316}
317
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in LocatorDragToOptions

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)