File tree Expand file tree Collapse file tree 3 files changed +67
-0
lines changed
PuppeteerSharp.Tests/ElementHandleTests Expand file tree Collapse file tree 3 files changed +67
-0
lines changed Original file line number Diff line number Diff line change 1+ using System . Threading . Tasks ;
2+ using PuppeteerSharp . Tests . Attributes ;
3+ using PuppeteerSharp . Nunit ;
4+ using NUnit . Framework ;
5+
6+ namespace PuppeteerSharp . Tests . ElementHandleTests
7+ {
8+ public class IsVisibleIsHiddenTests : PuppeteerPageBaseTest
9+ {
10+ public IsVisibleIsHiddenTests ( ) : base ( )
11+ {
12+ }
13+
14+ [ PuppeteerTest ( "elementhandle.spec.ts" , "ElementHandle.isVisible and ElementHandle.isHidden" , "should work" ) ]
15+ [ PuppeteerTimeout ]
16+ public async Task ShouldWork ( )
17+ {
18+ await Page . SetContentAsync ( "<div style='display: none'>text</div>" ) ;
19+ var element = await Page . WaitForSelectorAsync ( "div" ) . ConfigureAwait ( false ) ;
20+ Assert . False ( await element . IsVisibleAsync ( ) ) ;
21+ Assert . True ( await element . IsHiddenAsync ( ) ) ;
22+
23+ await element . EvaluateFunctionAsync ( "e => e.style.removeProperty('display')" ) ;
24+ Assert . True ( await element . IsVisibleAsync ( ) ) ;
25+ Assert . False ( await element . IsHiddenAsync ( ) ) ;
26+ }
27+ }
28+ }
Original file line number Diff line number Diff line change @@ -671,6 +671,33 @@ public async Task ScrollIntoViewIfNeededAsync()
671671 }
672672 }
673673
674+ /// <inheritdoc/>
675+ public Task < bool > IsVisibleAsync ( ) => CheckVisibilityAsync ( true ) ;
676+
677+ /// <inheritdoc/>
678+ public Task < bool > IsHiddenAsync ( ) => CheckVisibilityAsync ( false ) ;
679+
680+ private async Task < bool > CheckVisibilityAsync ( bool visibility )
681+ {
682+ var element = await Frame . PuppeteerWorld . AdoptHandleAsync ( this ) . ConfigureAwait ( false ) ;
683+
684+ try
685+ {
686+ return await Frame . PuppeteerWorld . EvaluateFunctionAsync < bool > (
687+ @"async (PuppeteerUtil, element, visibility) =>
688+ {
689+ return Boolean(PuppeteerUtil.checkVisibility(element, visibility));
690+ }" ,
691+ new LazyArg ( async context => await context . GetPuppeteerUtilAsync ( ) . ConfigureAwait ( false ) ) ,
692+ element ,
693+ visibility ) . ConfigureAwait ( false ) ;
694+ }
695+ finally
696+ {
697+ await element . DisposeAsync ( ) . ConfigureAwait ( false ) ;
698+ }
699+ }
700+
674701 private void CheckForFileAccess ( string [ ] files )
675702 {
676703 foreach ( var file in files )
Original file line number Diff line number Diff line change @@ -299,5 +299,17 @@ public interface IElementHandle : IJSHandle
299299 /// <returns>Task which resolves to an array of <see cref="IElementHandle"/>.</returns>
300300 [ Obsolete ( "Use " + nameof ( QuerySelectorAsync ) + " instead" ) ]
301301 Task < IElementHandle [ ] > XPathAsync ( string expression ) ;
302+
303+ /// <summary>
304+ /// Checks if an element is visible using the same mechanism as <see cref="WaitForSelectorAsync"/>.
305+ /// </summary>
306+ /// <returns>Task which resolves to true if the element is visible.</returns>
307+ Task < bool > IsVisibleAsync ( ) ;
308+
309+ /// <summary>
310+ /// Checks if an element is hidden using the same mechanism as <see cref="WaitForSelectorAsync"/>.
311+ /// </summary>
312+ /// <returns>Task which resolves to true if the element is hidden.</returns>
313+ Task < bool > IsHiddenAsync ( ) ;
302314 }
303315}
You can’t perform that action at this time.
0 commit comments