Best Playwright-dotnet code snippet using Microsoft.Playwright.PageEmulateMediaOptions.PageEmulateMediaOptions
IPage.cs
Source:IPage.cs
...518 /// // â true<br/>519 /// await page.EvaluateAsync("() => matchMedia('print').matches");<br/>520 /// // â false<br/>521 /// <br/>522 /// await page.EmulateMediaAsync(new PageEmulateMediaOptions { Media = Media.Print });<br/>523 /// await page.EvaluateAsync("() => matchMedia('screen').matches");<br/>524 /// // â false<br/>525 /// await page.EvaluateAsync("() => matchMedia('print').matches");<br/>526 /// // â true<br/>527 /// <br/>528 /// await page.EmulateMediaAsync(new PageEmulateMediaOptions { Media = Media.Screen });<br/>529 /// await page.EvaluateAsync("() => matchMedia('screen').matches");<br/>530 /// // â true<br/>531 /// await page.EvaluateAsync("() => matchMedia('print').matches");<br/>532 /// // â false533 /// </code>534 /// <code>535 /// await page.EmulateMediaAsync(new PageEmulateMediaOptions { ColorScheme = ColorScheme.Dark });<br/>536 /// await page.EvaluateAsync("matchMedia('(prefers-color-scheme: dark)').matches");<br/>537 /// // â true<br/>538 /// await page.EvaluateAsync("matchMedia('(prefers-color-scheme: light)').matches");<br/>539 /// // â false<br/>540 /// await page.EvaluateAsync("matchMedia('(prefers-color-scheme: no-preference)').matches");<br/>541 /// // â false542 /// </code>543 /// </summary>544 /// <param name="options">Call options</param>545 Task EmulateMediaAsync(PageEmulateMediaOptions? options = default);546 /// <summary>547 /// <para>548 /// The method finds an element matching the specified selector within the page and549 /// passes it as a first argument to <paramref name="expression"/>. If no elements match550 /// the selector, the method throws an error. Returns the value of <paramref name="expression"/>.551 /// </para>552 /// <para>553 /// If <paramref name="expression"/> returns a <see cref="Task"/>, then <see cref="IPage.EvalOnSelectorAsync"/>554 /// would wait for the promise to resolve and return its value.555 /// </para>556 /// <para>Examples:</para>557 /// <code>558 /// var searchValue = await page.EvalOnSelectorAsync<string>("#search", "el => el.value");<br/>559 /// var preloadHref = await page.EvalOnSelectorAsync<string>("link[rel=preload]", "el => el.href");<br/>560 /// var html = await page.EvalOnSelectorAsync(".main-container", "(e, suffix) => e.outerHTML + suffix", "hello");561 /// </code>562 /// <para>Shortcut for main frame's <see cref="IFrame.EvalOnSelectorAsync"/>.</para>563 /// </summary>564 /// <remarks>565 /// <para>566 /// This method does not wait for the element to pass actionability checks and therefore567 /// can lead to the flaky tests. Use <see cref="ILocator.EvaluateAsync"/>, other <see568 /// cref="ILocator"/> helper methods or web-first assertions instead.569 /// </para>570 /// </remarks>571 /// <param name="selector">572 /// A selector to query for. See <a href="https://playwright.dev/dotnet/docs/selectors">working573 /// with selectors</a> for more details.574 /// </param>575 /// <param name="expression">576 /// JavaScript expression to be evaluated in the browser context. If it looks like a577 /// function declaration, it is interpreted as a function. Otherwise, evaluated as an578 /// expression.579 /// </param>580 /// <param name="arg">Optional argument to pass to <paramref name="expression"/>.</param>581 /// <param name="options">Call options</param>582 Task<T> EvalOnSelectorAsync<T>(string selector, string expression, object? arg = default, PageEvalOnSelectorOptions? options = default);583 /// <summary>584 /// <para>585 /// The method finds all elements matching the specified selector within the page and586 /// passes an array of matched elements as a first argument to <paramref name="expression"/>.587 /// Returns the result of <paramref name="expression"/> invocation.588 /// </para>589 /// <para>590 /// If <paramref name="expression"/> returns a <see cref="Task"/>, then <see cref="IPage.EvalOnSelectorAllAsync"/>591 /// would wait for the promise to resolve and return its value.592 /// </para>593 /// <para>Examples:</para>594 /// <code>var divsCount = await page.EvalOnSelectorAllAsync<bool>("div", "(divs, min) => divs.length >= min", 10);</code>595 /// </summary>596 /// <remarks>597 /// <para>598 /// In most cases, <see cref="ILocator.EvaluateAllAsync"/>, other <see cref="ILocator"/>599 /// helper methods and web-first assertions do a better job.600 /// </para>601 /// </remarks>602 /// <param name="selector">603 /// A selector to query for. See <a href="https://playwright.dev/dotnet/docs/selectors">working604 /// with selectors</a> for more details.605 /// </param>606 /// <param name="expression">607 /// JavaScript expression to be evaluated in the browser context. If it looks like a608 /// function declaration, it is interpreted as a function. Otherwise, evaluated as an609 /// expression.610 /// </param>611 /// <param name="arg">Optional argument to pass to <paramref name="expression"/>.</param>612 Task<T> EvalOnSelectorAllAsync<T>(string selector, string expression, object? arg = default);613 /// <summary>614 /// <para>Returns the value of the <paramref name="expression"/> invocation.</para>615 /// <para>616 /// If the function passed to the <see cref="IPage.EvaluateAsync"/> returns a <see cref="Task"/>,617 /// then <see cref="IPage.EvaluateAsync"/> would wait for the promise to resolve and618 /// return its value.619 /// </para>620 /// <para>621 /// If the function passed to the <see cref="IPage.EvaluateAsync"/> returns a non-<see622 /// cref="Serializable"/> value, then <see cref="IPage.EvaluateAsync"/> resolves to623 /// <c>undefined</c>. Playwright also supports transferring some additional values that624 /// are not serializable by <c>JSON</c>: <c>-0</c>, <c>NaN</c>, <c>Infinity</c>, <c>-Infinity</c>.625 /// </para>626 /// <para>Passing argument to <paramref name="expression"/>:</para>627 /// <code>628 /// var result = await page.EvaluateAsync<int>("([x, y]) => Promise.resolve(x * y)", new[] { 7, 8 });<br/>629 /// Console.WriteLine(result);630 /// </code>631 /// <para>A string can also be passed in instead of a function:</para>632 /// <code>Console.WriteLine(await page.EvaluateAsync<int>("1 + 2")); // prints "3"</code>633 /// <para>634 /// <see cref="IElementHandle"/> instances can be passed as an argument to the <see635 /// cref="IPage.EvaluateAsync"/>:636 /// </para>637 /// <code>638 /// var bodyHandle = await page.EvaluateAsync("document.body");<br/>639 /// var html = await page.EvaluateAsync<string>("([body, suffix]) => body.innerHTML + suffix", new object [] { bodyHandle, "hello" });<br/>640 /// await bodyHandle.DisposeAsync();641 /// </code>642 /// <para>Shortcut for main frame's <see cref="IFrame.EvaluateAsync"/>.</para>643 /// </summary>644 /// <param name="expression">645 /// JavaScript expression to be evaluated in the browser context. If it looks like a646 /// function declaration, it is interpreted as a function. Otherwise, evaluated as an647 /// expression.648 /// </param>649 /// <param name="arg">Optional argument to pass to <paramref name="expression"/>.</param>650 Task<T> EvaluateAsync<T>(string expression, object? arg = default);651 /// <summary>652 /// <para>Returns the value of the <paramref name="expression"/> invocation as a <see cref="IJSHandle"/>.</para>653 /// <para>654 /// The only difference between <see cref="IPage.EvaluateAsync"/> and <see cref="IPage.EvaluateHandleAsync"/>655 /// is that <see cref="IPage.EvaluateHandleAsync"/> returns <see cref="IJSHandle"/>.656 /// </para>657 /// <para>658 /// If the function passed to the <see cref="IPage.EvaluateHandleAsync"/> returns a659 /// <see cref="Task"/>, then <see cref="IPage.EvaluateHandleAsync"/> would wait for660 /// the promise to resolve and return its value.661 /// </para>662 /// <code>663 /// // Handle for the window object.<br/>664 /// var aWindowHandle = await page.EvaluateHandleAsync("() => Promise.resolve(window)");665 /// </code>666 /// <para>A string can also be passed in instead of a function:</para>667 /// <code>var docHandle = await page.EvaluateHandleAsync("document"); // Handle for the `document`</code>668 /// <para><see cref="IJSHandle"/> instances can be passed as an argument to the <see cref="IPage.EvaluateHandleAsync"/>:</para>669 /// <code>670 /// var handle = await page.EvaluateHandleAsync("() => document.body");<br/>671 /// var resultHandle = await page.EvaluateHandleAsync("([body, suffix]) => body.innerHTML + suffix", new object[] { handle, "hello" });<br/>672 /// Console.WriteLine(await resultHandle.JsonValueAsync<string>());<br/>673 /// await resultHandle.DisposeAsync();674 /// </code>675 /// </summary>676 /// <param name="expression">677 /// JavaScript expression to be evaluated in the browser context. If it looks like a678 /// function declaration, it is interpreted as a function. Otherwise, evaluated as an679 /// expression.680 /// </param>681 /// <param name="arg">Optional argument to pass to <paramref name="expression"/>.</param>682 Task<IJSHandle> EvaluateHandleAsync(string expression, object? arg = default);683 /// <summary>684 /// <para>685 /// The method adds a function called <paramref name="name"/> on the <c>window</c> object686 /// of every frame in this page. When called, the function executes <paramref name="callback"/>687 /// and returns a <see cref="Task"/> which resolves to the return value of <paramref688 /// name="callback"/>. If the <paramref name="callback"/> returns a <see cref="Promise"/>,689 /// it will be awaited.690 /// </para>691 /// <para>692 /// The first argument of the <paramref name="callback"/> function contains information693 /// about the caller: <c>{ browserContext: BrowserContext, page: Page, frame: Frame694 /// }</c>.695 /// </para>696 /// <para>See <see cref="IBrowserContext.ExposeBindingAsync"/> for the context-wide version.</para>697 /// <para>An example of exposing page URL to all frames in a page:</para>698 /// <code>699 /// using Microsoft.Playwright;<br/>700 /// using System.Threading.Tasks;<br/>701 /// <br/>702 /// class PageExamples<br/>703 /// {<br/>704 /// public static async Task Main()<br/>705 /// {<br/>706 /// using var playwright = await Playwright.CreateAsync();<br/>707 /// await using var browser = await playwright.Webkit.LaunchAsync(new BrowserTypeLaunchOptions<br/>708 /// {<br/>709 /// Headless: false<br/>710 /// });<br/>711 /// var page = await browser.NewPageAsync();<br/>712 /// <br/>713 /// await page.ExposeBindingAsync("pageUrl", (source) => source.Page.Url);<br/>714 /// await page.SetContentAsync("<script>\n" +<br/>715 /// " async function onClick() {\n" +<br/>716 /// " document.querySelector('div').textContent = await window.pageURL();\n" +<br/>717 /// " }\n" +<br/>718 /// "</script>\n" +<br/>719 /// "<button onclick=\"onClick()\">Click me</button>\n" +<br/>720 /// "<div></div>");<br/>721 /// <br/>722 /// await page.ClickAsync("button");<br/>723 /// }<br/>724 /// }725 /// </code>726 /// <para>An example of passing an element handle:</para>727 /// <code>728 /// var result = new TaskCompletionSource<string>();<br/>729 /// await page.ExposeBindingAsync("clicked", async (BindingSource _, IJSHandle t) =><br/>730 /// {<br/>731 /// return result.TrySetResult(await t.AsElement().TextContentAsync());<br/>732 /// });<br/>733 /// <br/>734 /// await page.SetContentAsync("<script>\n" +<br/>735 /// " document.addEventListener('click', event => window.clicked(event.target));\n" +<br/>736 /// "</script>\n" +<br/>737 /// "<div>Click me</div>\n" +<br/>738 /// "<div>Or click me</div>\n");<br/>739 /// <br/>740 /// await page.ClickAsync("div");<br/>741 /// Console.WriteLine(await result.Task);742 /// </code>743 /// </summary>744 /// <remarks><para>Functions installed via <see cref="IPage.ExposeBindingAsync"/> survive navigations.</para></remarks>745 /// <param name="name">Name of the function on the window object.</param>746 /// <param name="callback">Callback function that will be called in the Playwright's context.</param>747 /// <param name="options">Call options</param>748 Task ExposeBindingAsync(string name, Action callback, PageExposeBindingOptions? options = default);749 /// <summary>750 /// <para>751 /// The method adds a function called <paramref name="name"/> on the <c>window</c> object752 /// of every frame in the page. When called, the function executes <paramref name="callback"/>753 /// and returns a <see cref="Task"/> which resolves to the return value of <paramref754 /// name="callback"/>.755 /// </para>756 /// <para>If the <paramref name="callback"/> returns a <see cref="Task"/>, it will be awaited.</para>757 /// <para>See <see cref="IBrowserContext.ExposeFunctionAsync"/> for context-wide exposed function.</para>758 /// <para>An example of adding a <c>sha256</c> function to the page:</para>759 /// <code>760 /// using Microsoft.Playwright;<br/>761 /// using System;<br/>762 /// using System.Security.Cryptography;<br/>763 /// using System.Threading.Tasks;<br/>764 /// <br/>765 /// class PageExamples<br/>766 /// {<br/>767 /// public static async Task Main()<br/>768 /// {<br/>769 /// using var playwright = await Playwright.CreateAsync();<br/>770 /// await using var browser = await playwright.Webkit.LaunchAsync(new BrowserTypeLaunchOptions<br/>771 /// {<br/>772 /// Headless: false<br/>773 /// });<br/>774 /// var page = await browser.NewPageAsync();<br/>775 /// <br/>776 /// await page.ExposeFunctionAsync("sha256", (string input) =><br/>777 /// {<br/>778 /// return Convert.ToBase64String(<br/>779 /// SHA256.Create().ComputeHash(System.Text.Encoding.UTF8.GetBytes(input)));<br/>780 /// });<br/>781 /// <br/>782 /// await page.SetContentAsync("<script>\n" +<br/>783 /// " async function onClick() {\n" +<br/>784 /// " document.querySelector('div').textContent = await window.sha256('PLAYWRIGHT');\n" +<br/>785 /// " }\n" +<br/>786 /// "</script>\n" +<br/>787 /// "<button onclick=\"onClick()\">Click me</button>\n" +<br/>788 /// "<div></div>");<br/>789 /// <br/>790 /// await page.ClickAsync("button");<br/>791 /// Console.WriteLine(await page.TextContentAsync("div"));<br/>792 /// }<br/>793 /// }794 /// </code>795 /// </summary>796 /// <remarks><para>Functions installed via <see cref="IPage.ExposeFunctionAsync"/> survive navigations.</para></remarks>797 /// <param name="name">Name of the function on the window object</param>798 /// <param name="callback">Callback function which will be called in Playwright's context.</param>799 Task ExposeFunctionAsync(string name, Action callback);800 /// <summary>801 /// <para>802 /// This method waits for an element matching <paramref name="selector"/>, waits for803 /// <a href="https://playwright.dev/dotnet/docs/actionability">actionability</a> checks,804 /// focuses the element, fills it and triggers an <c>input</c> event after filling.805 /// Note that you can pass an empty string to clear the input field.806 /// </para>807 /// <para>808 /// If the target element is not an <c><input></c>, <c><textarea></c> or809 /// <c>[contenteditable]</c> element, this method throws an error. However, if the element810 /// is inside the <c><label></c> element that has an associated <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLLabelElement/control">control</a>,811 /// the control will be filled instead.812 /// </para>813 /// <para>To send fine-grained keyboard events, use <see cref="IPage.TypeAsync"/>.</para>814 /// <para>Shortcut for main frame's <see cref="IFrame.FillAsync"/>.</para>815 /// </summary>816 /// <param name="selector">817 /// A selector to search for an element. If there are multiple elements satisfying the818 /// selector, the first will be used. See <a href="https://playwright.dev/dotnet/docs/selectors">working819 /// with selectors</a> for more details.820 /// </param>821 /// <param name="value">822 /// Value to fill for the <c><input></c>, <c><textarea></c> or <c>[contenteditable]</c>823 /// element.824 /// </param>825 /// <param name="options">Call options</param>826 Task FillAsync(string selector, string value, PageFillOptions? options = default);827 /// <summary>828 /// <para>829 /// This method fetches an element with <paramref name="selector"/> and focuses it.830 /// If there's no element matching <paramref name="selector"/>, the method waits until831 /// a matching element appears in the DOM.832 /// </para>833 /// <para>Shortcut for main frame's <see cref="IFrame.FocusAsync"/>.</para>834 /// </summary>835 /// <param name="selector">836 /// A selector to search for an element. If there are multiple elements satisfying the837 /// selector, the first will be used. See <a href="https://playwright.dev/dotnet/docs/selectors">working838 /// with selectors</a> for more details.839 /// </param>840 /// <param name="options">Call options</param>841 Task FocusAsync(string selector, PageFocusOptions? options = default);842 /// <summary>843 /// <para>844 /// Returns frame matching the specified criteria. Either <c>name</c> or <c>url</c>845 /// must be specified.846 /// </para>847 /// <code>var frame = page.Frame("frame-name");</code>848 /// <code>var frame = page.FrameByUrl(".*domain.*");</code>849 /// </summary>850 /// <param name="name">Frame name specified in the <c>iframe</c>'s <c>name</c> attribute.</param>851 IFrame? Frame(string name);852 /// <summary><para>Returns frame with matching URL.</para></summary>853 /// <param name="url">854 /// A glob pattern, regex pattern or predicate receiving frame's <c>url</c> as a <see855 /// cref="URL"/> object.856 /// </param>857 IFrame? FrameByUrl(string url);858 /// <summary><para>Returns frame with matching URL.</para></summary>859 /// <param name="url">860 /// A glob pattern, regex pattern or predicate receiving frame's <c>url</c> as a <see861 /// cref="URL"/> object.862 /// </param>863 IFrame? FrameByUrl(Regex url);864 /// <summary><para>Returns frame with matching URL.</para></summary>865 /// <param name="url">866 /// A glob pattern, regex pattern or predicate receiving frame's <c>url</c> as a <see867 /// cref="URL"/> object.868 /// </param>869 IFrame? FrameByUrl(Func<string, bool> url);870 /// <summary>871 /// <para>872 /// When working with iframes, you can create a frame locator that will enter the iframe873 /// and allow selecting elements in that iframe. Following snippet locates element with874 /// text "Submit" in the iframe with id <c>my-frame</c>, like <c><iframe id="my-frame"></c>:875 /// </para>876 /// <code>877 /// var locator = page.FrameLocator("#my-iframe").Locator("text=Submit");<br/>878 /// await locator.ClickAsync();879 /// </code>880 /// </summary>881 /// <param name="selector">882 /// A selector to use when resolving DOM element. See <a href="https://playwright.dev/dotnet/docs/selectors">working883 /// with selectors</a> for more details.884 /// </param>885 IFrameLocator FrameLocator(string selector);886 /// <summary><para>An array of all frames attached to the page.</para></summary>887 IReadOnlyList<IFrame> Frames { get; }888 /// <summary><para>Returns element attribute value.</para></summary>889 /// <param name="selector">890 /// A selector to search for an element. If there are multiple elements satisfying the891 /// selector, the first will be used. See <a href="https://playwright.dev/dotnet/docs/selectors">working892 /// with selectors</a> for more details.893 /// </param>894 /// <param name="name">Attribute name to get the value for.</param>895 /// <param name="options">Call options</param>896 Task<string?> GetAttributeAsync(string selector, string name, PageGetAttributeOptions? options = default);897 /// <summary>898 /// <para>899 /// Returns the main resource response. In case of multiple redirects, the navigation900 /// will resolve with the response of the last redirect. If can not go back, returns901 /// <c>null</c>.902 /// </para>903 /// <para>Navigate to the previous page in history.</para>904 /// </summary>905 /// <param name="options">Call options</param>906 Task<IResponse?> GoBackAsync(PageGoBackOptions? options = default);907 /// <summary>908 /// <para>909 /// Returns the main resource response. In case of multiple redirects, the navigation910 /// will resolve with the response of the last redirect. If can not go forward, returns911 /// <c>null</c>.912 /// </para>913 /// <para>Navigate to the next page in history.</para>914 /// </summary>915 /// <param name="options">Call options</param>916 Task<IResponse?> GoForwardAsync(PageGoForwardOptions? options = default);917 /// <summary>918 /// <para>919 /// Returns the main resource response. In case of multiple redirects, the navigation920 /// will resolve with the response of the last redirect.921 /// </para>922 /// <para>The method will throw an error if:</para>923 /// <list type="bullet">924 /// <item><description>there's an SSL error (e.g. in case of self-signed certificates).</description></item>925 /// <item><description>target URL is invalid.</description></item>926 /// <item><description>the <paramref name="timeout"/> is exceeded during navigation.</description></item>927 /// <item><description>the remote server does not respond or is unreachable.</description></item>928 /// <item><description>the main resource failed to load.</description></item>929 /// </list>930 /// <para>931 /// The method will not throw an error when any valid HTTP status code is returned by932 /// the remote server, including 404 "Not Found" and 500 "Internal Server Error". The933 /// status code for such responses can be retrieved by calling <see cref="IResponse.Status"/>.934 /// </para>935 /// <para>Shortcut for main frame's <see cref="IFrame.GotoAsync"/></para>936 /// </summary>937 /// <remarks>938 /// <para>939 /// The method either throws an error or returns a main resource response. The only940 /// exceptions are navigation to <c>about:blank</c> or navigation to the same URL with941 /// a different hash, which would succeed and return <c>null</c>.942 /// </para>943 /// <para>944 /// Headless mode doesn't support navigation to a PDF document. See the <a href="https://bugs.chromium.org/p/chromium/issues/detail?id=761295">upstream945 /// issue</a>.946 /// </para>947 /// </remarks>948 /// <param name="url">949 /// URL to navigate page to. The url should include scheme, e.g. <c>https://</c>. When950 /// a <paramref name="baseURL"/> via the context options was provided and the passed951 /// URL is a path, it gets merged via the <a href="https://developer.mozilla.org/en-US/docs/Web/API/URL/URL"><c>new952 /// URL()</c></a> constructor.953 /// </param>954 /// <param name="options">Call options</param>955 Task<IResponse?> GotoAsync(string url, PageGotoOptions? options = default);956 /// <summary>957 /// <para>958 /// This method hovers over an element matching <paramref name="selector"/> by performing959 /// the following steps:960 /// </para>961 /// <list type="ordinal">962 /// <item><description>963 /// Find an element matching <paramref name="selector"/>. If there is none, wait until964 /// a matching element is attached to the DOM.965 /// </description></item>966 /// <item><description>967 /// Wait for <a href="https://playwright.dev/dotnet/docs/actionability">actionability</a>968 /// checks on the matched element, unless <paramref name="force"/> option is set. If969 /// the element is detached during the checks, the whole action is retried.970 /// </description></item>971 /// <item><description>Scroll the element into view if needed.</description></item>972 /// <item><description>973 /// Use <see cref="IPage.Mouse"/> to hover over the center of the element, or the specified974 /// <paramref name="position"/>.975 /// </description></item>976 /// <item><description>977 /// Wait for initiated navigations to either succeed or fail, unless <c>noWaitAfter</c>978 /// option is set.979 /// </description></item>980 /// </list>981 /// <para>982 /// When all steps combined have not finished during the specified <paramref name="timeout"/>,983 /// this method throws a <see cref="TimeoutException"/>. Passing zero timeout disables984 /// this.985 /// </para>986 /// <para>Shortcut for main frame's <see cref="IFrame.HoverAsync"/>.</para>987 /// </summary>988 /// <param name="selector">989 /// A selector to search for an element. If there are multiple elements satisfying the990 /// selector, the first will be used. See <a href="https://playwright.dev/dotnet/docs/selectors">working991 /// with selectors</a> for more details.992 /// </param>993 /// <param name="options">Call options</param>994 Task HoverAsync(string selector, PageHoverOptions? options = default);995 /// <summary><para>Returns <c>element.innerHTML</c>.</para></summary>996 /// <param name="selector">997 /// A selector to search for an element. If there are multiple elements satisfying the998 /// selector, the first will be used. See <a href="https://playwright.dev/dotnet/docs/selectors">working999 /// with selectors</a> for more details.1000 /// </param>1001 /// <param name="options">Call options</param>1002 Task<string> InnerHTMLAsync(string selector, PageInnerHTMLOptions? options = default);1003 /// <summary><para>Returns <c>element.innerText</c>.</para></summary>1004 /// <param name="selector">1005 /// A selector to search for an element. If there are multiple elements satisfying the1006 /// selector, the first will be used. See <a href="https://playwright.dev/dotnet/docs/selectors">working1007 /// with selectors</a> for more details.1008 /// </param>1009 /// <param name="options">Call options</param>1010 Task<string> InnerTextAsync(string selector, PageInnerTextOptions? options = default);1011 /// <summary>1012 /// <para>1013 /// Returns <c>input.value</c> for the selected <c><input></c> or <c><textarea></c>1014 /// or <c><select></c> element. Throws for non-input elements.1015 /// </para>1016 /// </summary>1017 /// <param name="selector">1018 /// A selector to search for an element. If there are multiple elements satisfying the1019 /// selector, the first will be used. See <a href="https://playwright.dev/dotnet/docs/selectors">working1020 /// with selectors</a> for more details.1021 /// </param>1022 /// <param name="options">Call options</param>1023 Task<string> InputValueAsync(string selector, PageInputValueOptions? options = default);1024 /// <summary>1025 /// <para>1026 /// Returns whether the element is checked. Throws if the element is not a checkbox1027 /// or radio input.1028 /// </para>1029 /// </summary>1030 /// <param name="selector">1031 /// A selector to search for an element. If there are multiple elements satisfying the1032 /// selector, the first will be used. See <a href="https://playwright.dev/dotnet/docs/selectors">working1033 /// with selectors</a> for more details.1034 /// </param>1035 /// <param name="options">Call options</param>1036 Task<bool> IsCheckedAsync(string selector, PageIsCheckedOptions? options = default);1037 /// <summary><para>Indicates that the page has been closed.</para></summary>1038 bool IsClosed { get; }1039 /// <summary><para>Returns whether the element is disabled, the opposite of <a href="https://playwright.dev/dotnet/docs/actionability#enabled">enabled</a>.</para></summary>1040 /// <param name="selector">1041 /// A selector to search for an element. If there are multiple elements satisfying the1042 /// selector, the first will be used. See <a href="https://playwright.dev/dotnet/docs/selectors">working1043 /// with selectors</a> for more details.1044 /// </param>1045 /// <param name="options">Call options</param>1046 Task<bool> IsDisabledAsync(string selector, PageIsDisabledOptions? options = default);1047 /// <summary><para>Returns whether the element is <a href="https://playwright.dev/dotnet/docs/actionability#editable">editable</a>.</para></summary>1048 /// <param name="selector">1049 /// A selector to search for an element. If there are multiple elements satisfying the1050 /// selector, the first will be used. See <a href="https://playwright.dev/dotnet/docs/selectors">working1051 /// with selectors</a> for more details.1052 /// </param>1053 /// <param name="options">Call options</param>1054 Task<bool> IsEditableAsync(string selector, PageIsEditableOptions? options = default);1055 /// <summary><para>Returns whether the element is <a href="https://playwright.dev/dotnet/docs/actionability#enabled">enabled</a>.</para></summary>1056 /// <param name="selector">1057 /// A selector to search for an element. If there are multiple elements satisfying the1058 /// selector, the first will be used. See <a href="https://playwright.dev/dotnet/docs/selectors">working1059 /// with selectors</a> for more details.1060 /// </param>1061 /// <param name="options">Call options</param>1062 Task<bool> IsEnabledAsync(string selector, PageIsEnabledOptions? options = default);1063 /// <summary>1064 /// <para>1065 /// Returns whether the element is hidden, the opposite of <a href="https://playwright.dev/dotnet/docs/actionability#visible">visible</a>.1066 /// <paramref name="selector"/> that does not match any elements is considered hidden.1067 /// </para>1068 /// </summary>1069 /// <param name="selector">1070 /// A selector to search for an element. If there are multiple elements satisfying the1071 /// selector, the first will be used. See <a href="https://playwright.dev/dotnet/docs/selectors">working1072 /// with selectors</a> for more details.1073 /// </param>1074 /// <param name="options">Call options</param>1075 Task<bool> IsHiddenAsync(string selector, PageIsHiddenOptions? options = default);1076 /// <summary>1077 /// <para>1078 /// Returns whether the element is <a href="https://playwright.dev/dotnet/docs/actionability#visible">visible</a>.1079 /// <paramref name="selector"/> that does not match any elements is considered not visible.1080 /// </para>1081 /// </summary>1082 /// <param name="selector">1083 /// A selector to search for an element. If there are multiple elements satisfying the1084 /// selector, the first will be used. See <a href="https://playwright.dev/dotnet/docs/selectors">working1085 /// with selectors</a> for more details.1086 /// </param>1087 /// <param name="options">Call options</param>1088 Task<bool> IsVisibleAsync(string selector, PageIsVisibleOptions? options = default);1089 public IKeyboard Keyboard { get; }1090 /// <summary>1091 /// <para>1092 /// The method returns an element locator that can be used to perform actions on the1093 /// page. Locator is resolved to the element immediately before performing an action,1094 /// so a series of actions on the same locator can in fact be performed on different1095 /// DOM elements. That would happen if the DOM structure between those actions has changed.1096 /// </para>1097 /// <para>Shortcut for main frame's <see cref="IFrame.Locator"/>.</para>1098 /// </summary>1099 /// <param name="selector">1100 /// A selector to use when resolving DOM element. See <a href="https://playwright.dev/dotnet/docs/selectors">working1101 /// with selectors</a> for more details.1102 /// </param>1103 /// <param name="options">Call options</param>1104 ILocator Locator(string selector, PageLocatorOptions? options = default);1105 /// <summary>1106 /// <para>1107 /// The page's main frame. Page is guaranteed to have a main frame which persists during1108 /// navigations.1109 /// </para>1110 /// </summary>1111 IFrame MainFrame { get; }1112 public IMouse Mouse { get; }1113 /// <summary>1114 /// <para>1115 /// Returns the opener for popup pages and <c>null</c> for others. If the opener has1116 /// been closed already the returns <c>null</c>.1117 /// </para>1118 /// </summary>1119 Task<IPage?> OpenerAsync();1120 /// <summary>1121 /// <para>1122 /// Pauses script execution. Playwright will stop executing the script and wait for1123 /// the user to either press 'Resume' button in the page overlay or to call <c>playwright.resume()</c>1124 /// in the DevTools console.1125 /// </para>1126 /// <para>1127 /// User can inspect selectors or perform manual steps while paused. Resume will continue1128 /// running the original script from the place it was paused.1129 /// </para>1130 /// </summary>1131 /// <remarks>1132 /// <para>1133 /// This method requires Playwright to be started in a headed mode, with a falsy <paramref1134 /// name="headless"/> value in the <see cref="IBrowserType.LaunchAsync"/>.1135 /// </para>1136 /// </remarks>1137 Task PauseAsync();1138 /// <summary>1139 /// <para>Returns the PDF buffer.</para>1140 /// <para>1141 /// <c>page.pdf()</c> generates a pdf of the page with <c>print</c> css media. To generate1142 /// a pdf with <c>screen</c> media, call <see cref="IPage.EmulateMediaAsync"/> before1143 /// calling <c>page.pdf()</c>:1144 /// </para>1145 /// <code>1146 /// // Generates a PDF with 'screen' media type<br/>1147 /// await page.EmulateMediaAsync(new PageEmulateMediaOptions { Media = Media.Screen });<br/>1148 /// await page.PdfAsync(new PagePdfOptions { Path = "page.pdf" });1149 /// </code>1150 /// <para>1151 /// The <paramref name="width"/>, <paramref name="height"/>, and <paramref name="margin"/>1152 /// options accept values labeled with units. Unlabeled values are treated as pixels.1153 /// </para>1154 /// <para>A few examples:</para>1155 /// <list type="bullet">1156 /// <item><description><c>page.pdf({width: 100})</c> - prints with width set to 100 pixels</description></item>1157 /// <item><description><c>page.pdf({width: '100px'})</c> - prints with width set to 100 pixels</description></item>1158 /// <item><description><c>page.pdf({width: '10cm'})</c> - prints with width set to 10 centimeters.</description></item>1159 /// </list>1160 /// <para>All possible units are:</para>1161 /// <list type="bullet">...
PageSynchronous.cs
Source:PageSynchronous.cs
...1161 /// calling <c>page.pdf()</c>:1162 /// </para>1163 /// <code>1164 /// // Generates a PDF with 'screen' media type<br/>1165 /// await page.EmulateMediaAsync(new PageEmulateMediaOptions { Media = Media.Screen });<br/>1166 /// await page.PdfAsync(new PagePdfOptions { Path = "page.pdf" });1167 /// </code>1168 /// <para>1169 /// The <paramref name="width"/>, <paramref name="height"/>, and <paramref name="margin"/>1170 /// options accept values labeled with units. Unlabeled values are treated as pixels.1171 /// </para>1172 /// <para>A few examples:</para>1173 /// <list type="bullet">1174 /// <item><description><c>page.pdf({width: 100})</c> - prints with width set to 100 pixels</description></item>1175 /// <item><description><c>page.pdf({width: '100px'})</c> - prints with width set to 100 pixels</description></item>1176 /// <item><description><c>page.pdf({width: '10cm'})</c> - prints with width set to 10 centimeters.</description></item>1177 /// </list>1178 /// <para>All possible units are:</para>1179 /// <list type="bullet">1180 /// <item><description><c>px</c> - pixel</description></item>1181 /// <item><description><c>in</c> - inch</description></item>1182 /// <item><description><c>cm</c> - centimeter</description></item>1183 /// <item><description><c>mm</c> - millimeter</description></item>1184 /// </list>1185 /// <para>The <paramref name="format"/> options are:</para>1186 /// <list type="bullet">1187 /// <item><description><c>Letter</c>: 8.5in x 11in</description></item>1188 /// <item><description><c>Legal</c>: 8.5in x 14in</description></item>1189 /// <item><description><c>Tabloid</c>: 11in x 17in</description></item>1190 /// <item><description><c>Ledger</c>: 17in x 11in</description></item>1191 /// <item><description><c>A0</c>: 33.1in x 46.8in</description></item>1192 /// <item><description><c>A1</c>: 23.4in x 33.1in</description></item>1193 /// <item><description><c>A2</c>: 16.54in x 23.4in</description></item>1194 /// <item><description><c>A3</c>: 11.7in x 16.54in</description></item>1195 /// <item><description><c>A4</c>: 8.27in x 11.7in</description></item>1196 /// <item><description><c>A5</c>: 5.83in x 8.27in</description></item>1197 /// <item><description><c>A6</c>: 4.13in x 5.83in</description></item>1198 /// </list>1199 /// </summary>1200 /// <remarks>1201 /// <para>Generating a pdf is currently only supported in Chromium headless.</para>1202 /// <para>1203 /// By default, <c>page.pdf()</c> generates a pdf with modified colors for printing.1204 /// Use the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-print-color-adjust"><c>-webkit-print-color-adjust</c></a>1205 /// property to force rendering of exact colors.1206 /// </para>1207 /// <para>1208 /// <paramref name="headerTemplate"/> and <paramref name="footerTemplate"/> markup have1209 /// the following limitations: > 1. Script tags inside templates are not evaluated.1210 /// > 2. Page styles are not visible inside templates.1211 /// </para>1212 /// </remarks>1213 /// <param name="options">Call options</param>1214 public static byte[] Pdf(this IPage page, PagePdfOptions? options = null)1215 {1216 return page.PdfAsync(options).GetAwaiter().GetResult();1217 }1218 /// <summary><para>Returns the buffer with the captured screenshot.</para></summary>1219 /// <param name="options">Call options</param>1220 public static byte[] Screenshot(this IPage page, PageScreenshotOptions? options = null)1221 {1222 return page.ScreenshotAsync(options).GetAwaiter().GetResult();1223 }1224 /// <summary>1225 /// <para>Adds a script which would be evaluated in one of the following scenarios:</para>1226 /// <list type="bullet">1227 /// <item><description>Whenever the page is navigated.</description></item>1228 /// <item><description>1229 /// Whenever the child frame is attached or navigated. In this case, the script is evaluated1230 /// in the context of the newly attached frame.1231 /// </description></item>1232 /// </list>1233 /// <para>1234 /// The script is evaluated after the document was created but before any of its scripts1235 /// were run. This is useful to amend the JavaScript environment, e.g. to seed <c>Math.random</c>.1236 /// </para>1237 /// <para>An example of overriding <c>Math.random</c> before the page loads:</para>1238 /// <code>await page.AddInitScriptAsync(new PageAddInitScriptOption { ScriptPath = "./preload.js" });</code>1239 /// </summary>1240 /// <remarks>1241 /// <para>1242 /// The order of evaluation of multiple scripts installed via <see cref="IBrowserContext.AddInitScriptAsync"/>1243 /// and <see cref="IPage.AddInitScriptAsync"/> is not defined.1244 /// </para>1245 /// </remarks>1246 /// <param name="script">Script to be evaluated in all pages in the browser context.</param>1247 /// <param name="scriptPath">Instead of specifying <paramref name="script"/>, gives the file name to load from.</param>1248 public static IPage AddInitScript(this IPage page, string? script = null, string? scriptPath = null)1249 {1250 page.AddInitScriptAsync(script, scriptPath).GetAwaiter().GetResult();1251 return page;1252 }1253 /// <summary>1254 /// <para>1255 /// Adds a <c><script></c> tag into the page with the desired url or content.1256 /// Returns the added tag when the script's onload fires or when the script content1257 /// was injected into frame.1258 /// </para>1259 /// <para>Shortcut for main frame's <see cref="IFrame.AddScriptTagAsync"/>.</para>1260 /// </summary>1261 /// <param name="options">Call options</param>1262 public static IPage AddScriptTag(this IPage page, PageAddScriptTagOptions? options = null)1263 {1264 page.AddScriptTagAsync(options).GetAwaiter().GetResult();1265 return page;1266 }1267 /// <summary>1268 /// <para>1269 /// Adds a <c><link rel="stylesheet"></c> tag into the page with the desired url1270 /// or a <c><style type="text/css"></c> tag with the content. Returns the added1271 /// tag when the stylesheet's onload fires or when the CSS content was injected into1272 /// frame.1273 /// </para>1274 /// <para>Shortcut for main frame's <see cref="IFrame.AddStyleTagAsync"/>.</para>1275 /// </summary>1276 /// <param name="options">Call options</param>1277 public static IPage AddStyleTag(this IPage page, PageAddStyleTagOptions? options = null)1278 {1279 page.AddStyleTagAsync(options).GetAwaiter().GetResult();1280 return page;1281 }1282 /// <summary><para>Brings page to front (activates tab).</para></summary>1283 public static IPage BringToFront(this IPage page)1284 {1285 page.BringToFrontAsync().GetAwaiter().GetResult();1286 return page;1287 }1288 /// <summary>1289 /// <para>1290 /// The snippet below dispatches the <c>click</c> event on the element. Regardless of1291 /// the visibility state of the element, <c>click</c> is dispatched. This is equivalent1292 /// to calling <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click">element.click()</a>.1293 /// </para>1294 /// <code>await page.DispatchEventAsync("button#submit", "click");</code>1295 /// <para>1296 /// Under the hood, it creates an instance of an event based on the given <paramref1297 /// name="type"/>, initializes it with <paramref name="eventInit"/> properties and dispatches1298 /// it on the element. Events are <c>composed</c>, <c>cancelable</c> and bubble by default.1299 /// </para>1300 /// <para>1301 /// Since <paramref name="eventInit"/> is event-specific, please refer to the events1302 /// documentation for the lists of initial properties:1303 /// </para>1304 /// <list type="bullet">1305 /// <item><description><a href="https://developer.mozilla.org/en-US/docs/Web/API/DragEvent/DragEvent">DragEvent</a></description></item>1306 /// <item><description><a href="https://developer.mozilla.org/en-US/docs/Web/API/FocusEvent/FocusEvent">FocusEvent</a></description></item>1307 /// <item><description><a href="https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent">KeyboardEvent</a></description></item>1308 /// <item><description><a href="https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/MouseEvent">MouseEvent</a></description></item>1309 /// <item><description><a href="https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/PointerEvent">PointerEvent</a></description></item>1310 /// <item><description><a href="https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/TouchEvent">TouchEvent</a></description></item>1311 /// <item><description><a href="https://developer.mozilla.org/en-US/docs/Web/API/Event/Event">Event</a></description></item>1312 /// </list>1313 /// <para>1314 /// You can also specify <c>JSHandle</c> as the property value if you want live objects1315 /// to be passed into the event:1316 /// </para>1317 /// <code>1318 /// var dataTransfer = await page.EvaluateHandleAsync("() => new DataTransfer()");<br/>1319 /// await page.DispatchEventAsync("#source", "dragstart", new { dataTransfer });1320 /// </code>1321 /// </summary>1322 /// <param name="selector">1323 /// A selector to search for an element. If there are multiple elements satisfying the1324 /// selector, the first will be used. See <a href="./selectors.md">working with selectors</a>1325 /// for more details.1326 /// </param>1327 /// <param name="type">DOM event type: <c>"click"</c>, <c>"dragstart"</c>, etc.</param>1328 /// <param name="eventInit">Optional event-specific initialization properties.</param>1329 /// <param name="options">Call options</param>1330 public static IPage DispatchEvent(this IPage page, string selector, string type, object? eventInit = null, PageDispatchEventOptions? options = null)1331 {1332 page.DispatchEventAsync(selector, type, eventInit, options).GetAwaiter().GetResult();1333 return page;1334 }1335 /// <summary>1336 /// <para>1337 /// This method changes the <c>CSS media type</c> through the <c>media</c> argument,1338 /// and/or the <c>'prefers-colors-scheme'</c> media feature, using the <c>colorScheme</c>1339 /// argument.1340 /// </para>1341 /// <code>1342 /// await page.EvaluateAsync("() => matchMedia('screen').matches");<br/>1343 /// // â true<br/>1344 /// await page.EvaluateAsync("() => matchMedia('print').matches");<br/>1345 /// // â false<br/>1346 /// <br/>1347 /// await page.EmulateMediaAsync(new PageEmulateMediaOptions { Media = Media.Print });<br/>1348 /// await page.EvaluateAsync("() => matchMedia('screen').matches");<br/>1349 /// // â false<br/>1350 /// await page.EvaluateAsync("() => matchMedia('print').matches");<br/>1351 /// // â true<br/>1352 /// <br/>1353 /// await page.EmulateMediaAsync(new PageEmulateMediaOptions { Media = Media.Screen });<br/>1354 /// await page.EvaluateAsync("() => matchMedia('screen').matches");<br/>1355 /// // â true<br/>1356 /// await page.EvaluateAsync("() => matchMedia('print').matches");<br/>1357 /// // â false1358 /// </code>1359 /// <code>1360 /// await page.EmulateMediaAsync(new PageEmulateMediaOptions { ColorScheme = ColorScheme.Dark });<br/>1361 /// await page.EvaluateAsync("matchMedia('(prefers-color-scheme: dark)').matches");<br/>1362 /// // â true<br/>1363 /// await page.EvaluateAsync("matchMedia('(prefers-color-scheme: light)').matches");<br/>1364 /// // â false<br/>1365 /// await page.EvaluateAsync("matchMedia('(prefers-color-scheme: no-preference)').matches");<br/>1366 /// // â false1367 /// </code>1368 /// </summary>1369 /// <param name="options">Call options</param>1370 public static IPage EmulateMedia(this IPage page, PageEmulateMediaOptions? options = null)1371 {1372 page.EmulateMediaAsync(options).GetAwaiter().GetResult();1373 return page;1374 }1375 /// <summary>1376 /// <para>1377 /// The method finds an element matching the specified selector within the page and1378 /// passes it as a first argument to <paramref name="expression"/>. If no elements match1379 /// the selector, the method throws an error. Returns the value of <paramref name="expression"/>.1380 /// </para>1381 /// <para>1382 /// If <paramref name="expression"/> returns a <see cref="Task"/>, then <see cref="IPage.EvalOnSelectorAsync"/>1383 /// would wait for the promise to resolve and return its value.1384 /// </para>...
Page.cs
Source:Page.cs
...230 IFrameLocator IPage.FrameLocator(string selector) => MainFrame.FrameLocator(selector);231 public Task<string> TitleAsync() => MainFrame.TitleAsync();232 public Task BringToFrontAsync() => _channel.BringToFrontAsync();233 public Task<IPage> OpenerAsync() => Task.FromResult<IPage>(Opener?.IsClosed == false ? Opener : null);234 public Task EmulateMediaAsync(PageEmulateMediaOptions options = default)235 {236 var args = new Dictionary<string, object>237 {238 ["media"] = options?.Media,239 ["colorScheme"] = options?.ColorScheme,240 ["reducedMotion"] = options?.ReducedMotion,241 ["forcedColors"] = options?.ForcedColors,242 };243 return _channel.EmulateMediaAsync(args);244 }245 public Task<IResponse> GotoAsync(string url, PageGotoOptions options = default)246 => MainFrame.GotoAsync(url, new() { WaitUntil = options?.WaitUntil, Timeout = options?.Timeout, Referer = options?.Referer });247 public Task WaitForURLAsync(string url, PageWaitForURLOptions options = default)248 => MainFrame.WaitForURLAsync(url, new() { WaitUntil = options?.WaitUntil, Timeout = options?.Timeout });...
PageModel.cs
Source:PageModel.cs
...278 protected virtual void DispatchEvent(string selector, string type, object? eventInit = null, PageDispatchEventOptions? options = null)279 {280 this.Page.DispatchEvent(selector, type, eventInit, options); ;281 }282 protected virtual void EmulateMedia(PageEmulateMediaOptions? options = null)283 {284 this.Page.EmulateMedia(options);285 }286 protected virtual void EvalOnSelectorAll(string selector, string expression, object? arg = null)287 {288 this.Page.EvalOnSelectorAll(selector, expression, arg);289 }290 protected virtual void EvalOnSelectorAll<T>(string selector, string expression, object? arg = null)291 {292 this.Page.EvalOnSelectorAll<T>(selector, expression, arg);293 }294 protected virtual void EvalOnSelector(string selector, string expression, object? arg = null)295 {296 this.Page.EvalOnSelector(selector, expression, arg);...
PageEmulateMediaOptions.cs
Source:PageEmulateMediaOptions.cs
...35using System.Threading.Tasks;36#nullable enable37namespace Microsoft.Playwright38{39 public class PageEmulateMediaOptions40 {41 public PageEmulateMediaOptions() { }42 public PageEmulateMediaOptions(PageEmulateMediaOptions clone)43 {44 if (clone == null)45 {46 return;47 }48 ColorScheme = clone.ColorScheme;49 ForcedColors = clone.ForcedColors;50 Media = clone.Media;51 ReducedMotion = clone.ReducedMotion;52 }53 /// <summary>54 /// <para>55 /// Emulates <c>'prefers-colors-scheme'</c> media feature, supported values are <c>'light'</c>,56 /// <c>'dark'</c>, <c>'no-preference'</c>. Passing <c>'Null'</c> disables color scheme...
PdfExporter.cs
Source:PdfExporter.cs
...17 Headless = true18 });19 await using var context = await browser.NewContextAsync();20 var page = await context.NewPageAsync();21 await page.EmulateMediaAsync(new PageEmulateMediaOptions {Media = Media.Print});22 var encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(content));23 await page.GotoAsync($"data:text/html;base64,{encoded}", new PageGotoOptions {WaitUntil = WaitUntilState.NetworkIdle});24 await page.PdfAsync(new PagePdfOptions25 {26 Path = filePath,27 Format = "Letter",28 PrintBackground = true,29 Scale = 0.8f30 });31 }32 }33}...
PageEmulateMediaOptions
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.Playwright;7{8 {9 static async Task Main(string[] args)10 {11 using var playwright = await Playwright.CreateAsync();12 await using var browser = await playwright.Firefox.LaunchAsync(new BrowserTypeLaunchOptions13 {14 });15 var context = await browser.NewContextAsync(new BrowserNewContextOptions16 {17 Viewport = new ViewportSize { Width = 1920, Height = 1080 },18 });19 var page = await context.NewPageAsync();20 await page.ScreenshotAsync("google.png");21 await page.EmulateMediaAsync(new PageEmulateMediaOptions22 {23 });24 await page.ScreenshotAsync("googleDark.png");25 await browser.CloseAsync();26 }27 }28}
PageEmulateMediaOptions
Using AI Code Generation
1using Microsoft.Playwright;2using Microsoft.Playwright.Core;3using Microsoft.Playwright.Transport;4using Microsoft.Playwright.Transport.Channels;5using Microsoft.Playwright.Transport.Protocol;6using System;7using System.Collections.Generic;8using System.Linq;9using System.Text;10using System.Threading.Tasks;11{12 {13 static async Task Main(string[] args)14 {15 using var playwright = await Playwright.CreateAsync();16 var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions { Headless = false });17 var page = await browser.NewPageAsync();18 await page.ScreenshotAsync(new PageScreenshotOptions { Path = "screenshot.png" });19 await browser.CloseAsync();20 }21 }22}23using Microsoft.Playwright;24using Microsoft.Playwright.Core;25using Microsoft.Playwright.Transport;26using Microsoft.Playwright.Transport.Channels;27using Microsoft.Playwright.Transport.Protocol;28using System;29using System.Collections.Generic;30using System.Linq;31using System.Text;32using System.Threading.Tasks;33{34 {35 static async Task Main(string[] args)36 {37 using var playwright = await Playwright.CreateAsync();38 var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions { Headless = false });39 var page = await browser.NewPageAsync();40 await page.ScreenshotAsync(new PageScreenshotOptions { Path = "screenshot.png" });41 await browser.CloseAsync();42 }43 }44}45using Microsoft.Playwright;46using Microsoft.Playwright.Core;47using Microsoft.Playwright.Transport;48using Microsoft.Playwright.Transport.Channels;49using Microsoft.Playwright.Transport.Protocol;50using System;51using System.Collections.Generic;52using System.Linq;53using System.Text;54using System.Threading.Tasks;55{56 {57 static async Task Main(string[] args)58 {59 using var playwright = await Playwright.CreateAsync();60 var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions { Headless = false });61 var page = await browser.NewPageAsync();62 await page.GotoAsync("https
PageEmulateMediaOptions
Using AI Code Generation
1using Microsoft.Playwright;2using System;3using System.Threading.Tasks;4{5 {6 static async Task Main(string[] args)7 {8 using var playwright = await Playwright.CreateAsync();9 await using var browser = await playwright.Chromium.LaunchAsync();10 var context = await browser.NewContextAsync();11 var page = await context.NewPageAsync();12 await page.EmulateMediaAsync(new PageEmulateMediaOptions()13 {14 });15 await page.ScreenshotAsync("screenshot.png");16 }17 }18}
PageEmulateMediaOptions
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using Microsoft.Playwright;4{5 {6 static async Task Main(string[] args)7 {8 using var playwright = await Playwright.CreateAsync();9 await using var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions { Headless = false });10 var page = await browser.NewPageAsync();11 await page.EmulateMediaAsync(new PageEmulateMediaOptions12 {13 });14 }15 }16}17using System;18using System.Threading.Tasks;19using Microsoft.Playwright;20{21 {22 static async Task Main(string[] args)23 {24 using var playwright = await Playwright.CreateAsync();25 await using var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions { Headless = false });26 var page = await browser.NewPageAsync();27 await page.EmulateMediaAsync(new PageEmulateMediaOptions28 {29 });30 }31 }32}33using System;34using System.Threading.Tasks;35using Microsoft.Playwright;36{37 {38 static async Task Main(string[] args)39 {40 using var playwright = await Playwright.CreateAsync();41 await using var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions { Headless = false });42 var page = await browser.NewPageAsync();43 await page.EmulateMediaAsync(new PageEmulateMediaOptions44 {45 {46 {47 },48 },49 });50 }
PageEmulateMediaOptions
Using AI Code Generation
1PageEmulateMediaOptions pageEmulateMediaOptions = new PageEmulateMediaOptions();2pageEmulateMediaOptions.ColorScheme = ColorSchemeEnum.Light;3pageEmulateMediaOptions.Media = MediaEnum.Print;4pageEmulateMediaOptions.Features = new Dictionary<string, object>()5{6 { "prefers-color-scheme", "dark" },7 { "prefers-reduced-motion", "reduce" }8};9await page.EmulateMediaAsync(pageEmulateMediaOptions);10PageEmulateMediaOptions pageEmulateMediaOptions = new PageEmulateMediaOptions();11pageEmulateMediaOptions.ColorScheme = ColorSchemeEnum.Dark;12pageEmulateMediaOptions.Media = MediaEnum.Screen;13pageEmulateMediaOptions.Features = new Dictionary<string, object>()14{15 { "prefers-color-scheme", "light" },16 { "prefers-reduced-motion", "no-preference" }17};18await page.EmulateMediaAsync(pageEmulateMediaOptions);19PageEmulateMediaOptions pageEmulateMediaOptions = new PageEmulateMediaOptions();20pageEmulateMediaOptions.ColorScheme = ColorSchemeEnum.Dark;21pageEmulateMediaOptions.Media = MediaEnum.Print;22pageEmulateMediaOptions.Features = new Dictionary<string, object>()23{24 { "prefers-color-scheme", "light" },25 { "prefers-reduced-motion", "reduce" }26};27await page.EmulateMediaAsync(pageEmulateMediaOptions);28PageEmulateMediaOptions pageEmulateMediaOptions = new PageEmulateMediaOptions();29pageEmulateMediaOptions.ColorScheme = ColorSchemeEnum.Light;30pageEmulateMediaOptions.Media = MediaEnum.Screen;31pageEmulateMediaOptions.Features = new Dictionary<string, object>()32{33 { "prefers-color-scheme", "dark" },34 { "prefers-reduced-motion", "no-preference" }35};36await page.EmulateMediaAsync(pageEmulateMediaOptions);
PageEmulateMediaOptions
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Threading.Tasks;4using Microsoft.Playwright;5{6 {7 static async Task Main(string[] args)8 {9 await using var playwright = await Playwright.CreateAsync();10 await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions11 {12 });13 var context = await browser.NewContextAsync(new BrowserNewContextOptions14 {15 {16 },17 });18 var page = await context.NewPageAsync();19 await page.TypeAsync("[name=\"q\"]", "playwright");20 await page.ClickAsync("[value=\"Google Search\"]");21 await page.ScreenshotAsync("example.png");22 await page.CloseAsync();23 await context.CloseAsync();24 await browser.CloseAsync();25 }26 }27}28using System;29using System.Collections.Generic;30using System.Threading.Tasks;31using Microsoft.Playwright;32{33 {34 static async Task Main(string[] args)35 {36 await using var playwright = await Playwright.CreateAsync();37 await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions38 {39 });40 var context = await browser.NewContextAsync(new BrowserNewContextOptions41 {42 {43 },44 });45 var page = await context.NewPageAsync();46 await page.TypeAsync("[name=\"q\"]", "playwright");47 await page.ClickAsync("[value=\"Google Search\"]");48 await page.ScreenshotAsync("example.png");49 await page.CloseAsync();50 await context.CloseAsync();51 await browser.CloseAsync();52 }53 }54}
PageEmulateMediaOptions
Using AI Code Generation
1using Microsoft.Playwright;2using System;3using System.Threading.Tasks;4{5{6static async Task Main(string[] args)7{8await using var playwright = await Playwright.CreateAsync();9await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });10var page = await browser.NewPageAsync();11await page.EmulateMediaAsync(new PageEmulateMediaOptions { ColorScheme = ColorScheme.Dark, Media = Media.PrefersReducedMotion });12}13}14}
PageEmulateMediaOptions
Using AI Code Generation
1Playwright playwright = await Playwright.CreateAsync();2var browser = await playwright.Chromium.LaunchAsync();3var page = await browser.NewPageAsync();4Playwright playwright = await Playwright.CreateAsync();5var browser = await playwright.Chromium.LaunchAsync();6var page = await browser.NewPageAsync();7Playwright playwright = await Playwright.CreateAsync();8var browser = await playwright.Chromium.LaunchAsync();9var page = await browser.NewPageAsync();10Playwright playwright = await Playwright.CreateAsync();11var browser = await playwright.Chromium.LaunchAsync();12var page = await browser.NewPageAsync();13Playwright playwright = await Playwright.CreateAsync();14var browser = await playwright.Chromium.LaunchAsync();15var page = await browser.NewPageAsync();16Playwright playwright = await Playwright.CreateAsync();17var browser = await playwright.Chromium.LaunchAsync();18var page = await browser.NewPageAsync();19Playwright playwright = await Playwright.CreateAsync();20var browser = await playwright.Chromium.LaunchAsync();
PageEmulateMediaOptions
Using AI Code Generation
1Playwright playwright = await Playwright.CreateAsync();2var browser = await playwright.Chromium.LaunchAsync();3var page = await browser.NewPageAsync();4Playwright playwright = await Playwright.CreateAsync();5var browser = await playwright.Chromium.LaunchAsync();6var page = await browser.NewPageAsync();7Playwright playwright = await Playwright.CreateAsync();8var browser = await playwright.Chromium.LaunchAsync();9var page = await browser.NewPageAsync();10Playwright playwright = await Playwright.CreateAsync();11var browser = await playwright.Chromium.LaunchAsync();12var page = await browser.NewPageAsync();13Playwright playwright = await Playwright.CreateAsync();14var browser = await playwright.Chromium.LaunchAsync();15var page = await browser.NewPageAsync();16Playwright playwright = await Playwright.CreateAsync();17var browser = await playwright.Chromium.LaunchAsync();18var page = await browser.NewPageAsync();19Playwright playwright = await Playwright.CreateAsync();20var browser = await playwright.Chromium.LaunchAsync();
PageEmulateMediaOptions
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Threading.Tasks;4using Microsoft.Playwright;5{6 {7 static async Task Main(string[] args)8 {9 await using var playwright = await Playwright.CreateAsync();10 await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions11 {12 });13 var context = await browser.NewContextAsync(new BrowserNewContextOptions14 {15 {16 },17 });18 var page = await context.NewPageAsync();19 await page.TypeAsync("[name=\"q\"]", "playwright");20 await page.ClickAsync("[value=\"Google Search\"]");21 await page.ScreenshotAsync("example.png");22 await page.CloseAsync();23 await context.CloseAsync();24 await browser.CloseAsync();25 }26 }27}28using System;29using System.Collections.Generic;30using System.Threading.Tasks;31using Microsoft.Playwright;32{33 {34 static async Task Main(string[] args)35 {36 await using var playwright = await Playwright.CreateAsync();37 await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions38 {39 });40 var context = await browser.NewContextAsync(new BrowserNewContextOptions41 {42 {43 },44 });45 var page = await context.NewPageAsync();46 await page.TypeAsync("[name=\"q\"]", "playwright");47 await page.ClickAsync("[value=\"Google Search\"]");48 await page.ScreenshotAsync("example.png");49 await page.CloseAsync();50 await context.CloseAsync();51 await browser.CloseAsync();52 }53 }54}
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!