Best Playwright-dotnet code snippet using Microsoft.Playwright.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 Microsoft.Playwright;2using System.Threading.Tasks;3{4 static async Task Main(string[] args)5 {6 using var playwright = await Playwright.CreateAsync();7 await using var browser = await playwright.Chromium.LaunchAsync();8 var context = await browser.NewContextAsync();9 var page = await context.NewPageAsync();10 await page.EmulateMediaAsync(new PageEmulateMediaOptions11 {12 });13 }14}15using Microsoft.Playwright;16using System.Threading.Tasks;17{18 static async Task Main(string[] args)19 {20 using var playwright = await Playwright.CreateAsync();21 await using var browser = await playwright.Chromium.LaunchAsync();22 var context = await browser.NewContextAsync();23 var page = await context.NewPageAsync();24 await page.EmulateMediaAsync(new PageEmulateMediaOptions25 {26 });27 }28}29using Microsoft.Playwright;30using System.Threading.Tasks;31{32 static async Task Main(string[] args)33 {34 using var playwright = await Playwright.CreateAsync();35 await using var browser = await playwright.Chromium.LaunchAsync();36 var context = await browser.NewContextAsync();37 var page = await context.NewPageAsync();38 await page.EmulateMediaAsync(new PageEmulateMediaOptions39 {40 });41 }42}43using Microsoft.Playwright;44using System.Threading.Tasks;45{46 static async Task Main(string[] args)47 {48 using var playwright = await Playwright.CreateAsync();49 await using var browser = await playwright.Chromium.LaunchAsync();50 var context = await browser.NewContextAsync();51 var page = await context.NewPageAsync();52 await page.EmulateMediaAsync(new PageEmulateMediaOptions53 {54 });55 }56}57using Microsoft.Playwright;
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(new LaunchOptions10 {11 });12 var context = await browser.NewContextAsync(new BrowserNewContextOptions13 {14 {15 },16 UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.0 Safari/537.36",17 {18 },19 Permissions = new[] { "geolocation" },20 {21 },22 });23 var page = await context.NewPageAsync();24 Console.WriteLine("Hello World!");25 }26 }27}28await context.SetViewportSizeAsync(375, 812);29await context.SetUserAgentAsync("Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");30await context.SetGeolocationAsync(new Geolocation31{32});
PageEmulateMediaOptions
Using AI Code Generation
1using Microsoft.Playwright;2using System.Threading.Tasks;3{4 {5 static async Task Main(string[] args)6 {7 using var playwright = await Playwright.CreateAsync();8 await using var browser = await playwright.Chromium.LaunchAsync(headless: false);9 var context = await browser.NewContextAsync(new BrowserNewContextOptions10 {11 {12 },13 UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Mobile/15E148 Safari/604.1",14 });15 var page = await context.NewPageAsync();16 await page.EmulateMediaAsync(new PageEmulateMediaOptions17 {18 });19 }20 }21}
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(new()11 {12 ViewportSize = new()13 {14 },15 UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36",16 });17 var page = await context.NewPageAsync();18 await page.EmulateMediaAsync(new()19 {20 });21 await page.SetContentAsync("<html><body><p>Playwright</p></body></html>");22 await page.ScreenshotAsync(new()23 {24 });25 }26 }27}
PageEmulateMediaOptions
Using AI Code Generation
1using Microsoft.Playwright;2using System.Threading.Tasks;3{4 {5 static async Task Main(string[] args)6 {7 using var playwright = await Playwright.CreateAsync();8 await using var browser = await playwright.Firefox.LaunchAsync();9 var context = await browser.NewContextAsync(new BrowserNewContextOptions10 {11 ViewportSize = new ViewportSize { Width = 1920, Height = 1080 },12 });13 var page = await context.NewPageAsync();14 await page.ScreenshotAsync(new PageScreenshotOptions { Path = "screenshot.png" });15 }16 }17}
PageEmulateMediaOptions
Using AI Code Generation
1var playwright = await Playwright.CreateAsync();2var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions3{4});5var context = await browser.NewContextAsync(new BrowserNewContextOptions6{7 ViewportSize = new ViewportSize { Width = 1920, Height = 1080 },8 UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Mobile/15E148 Safari/604.1",9 {10 },11 Permissions = new[] { "geolocation" },12});13var page = await context.NewPageAsync();14await page.ScreenshotAsync("google.png");15await browser.CloseAsync();16var playwright = await Playwright.CreateAsync();17var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions18{19});20var context = await browser.NewContextAsync(new BrowserNewContextOptions21{22 ViewportSize = new ViewportSize { Width = 1920, Height = 1080 },23 UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Mobile/15E148 Safari/604.1",24 {25 },26 Permissions = new[] { "geolocation" },27});28var page = await context.NewPageAsync();29await page.EmulateMediaAsync(new PageEmulateMediaOptions30{31});32await page.ScreenshotAsync("google.png");33await browser.CloseAsync();
PageEmulateMediaOptions
Using AI Code Generation
1using Microsoft.Playwright;2using System.Threading.Tasks;3{4 {5 static async Task Main(string[] args)6 {7 await using var playwright = await Playwright.CreateAsync();8 await using var browser = await playwright.Chromium.LaunchAsync();9 var context = await browser.NewContextAsync(new BrowserNewContextOptions10 {11 ViewportSize = new ViewportSize { Width = 1920, Height = 1080 }12 });13 var page = await context.NewPageAsync();14 await page.EmulateMediaAsync(new PageEmulateMediaOptions { Media = Media.Print });15 await page.ScreenshotAsync("screenshot.png");16 }17 }18}19using Microsoft.Playwright;20using System.Threading.Tasks;21{22 {23 static async Task Main(string[] args)24 {25 await using var playwright = await Playwright.CreateAsync();26 await using var browser = await playwright.Chromium.LaunchAsync();27 var context = await browser.NewContextAsync(new BrowserNewContextOptions28 {29 ViewportSize = new ViewportSize { Width = 1920, Height = 1080 }30 });31 var page = await context.NewPageAsync();32 await page.EmulateMediaAsync(new PageEmulateMediaOptions { Media = Media.Print });33 await page.ScreenshotAsync("screenshot.png");34 }35 }36}37using Microsoft.Playwright;38using System.Threading.Tasks;39{40 {41 static async Task Main(string[] args)42 {43 await using var playwright = await Playwright.CreateAsync();44 await using var browser = await playwright.Chromium.LaunchAsync();45 var context = await browser.NewContextAsync(new BrowserNewContextOptions46 {
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!!