How to use CreateIncognitoBrowserContextAsync method of PuppeteerSharp.Browser class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Browser.CreateIncognitoBrowserContextAsync

PageServer.cs

Source:PageServer.cs Github

copy

Full Screen

...165 return result;166 }167 //string Url = "https://plogin.m.jd.com/login/login?appid=300&returnurl=https%3A%2F%2Fwq.jd.com%2Fpassport%2FLoginRedirect%3Fstate%3D2087738584%26returnurl%3Dhttps%253A%252F%252Fhome.m.jd.com%252FmyJd%252Fnewhome.action%253Fsceneval%253D2%2526ufc%253D%2526&source=wq_passport";168 string Url = "https://bean.m.jd.com/bean/signIndex.action";169 var context = await browser.CreateIncognitoBrowserContextAsync();170 page = await context.NewPageAsync();171 ///屏蔽 WebDriver 检测172 await page.EvaluateFunctionOnNewDocumentAsync("function(){Object.defineProperty(navigator, 'webdriver', {get: () => undefined})}");173 DeviceDescriptor deviceOptions = Puppeteer.Devices.GetValueOrDefault(DeviceDescriptorName.IPhone7);174 await page.EmulateAsync(deviceOptions);175 await page.GoToAsync(Url);176 await page.WaitForTimeoutAsync(500);177 await GetPhoneCode(Phone, page);178 var seven = await page.EvaluateExpressionAsync("document.querySelectorAll('button[report-eventid=MLoginRegister_SMSReceiveCode]')[0].innerText");179 await page.ClickAsync("button[report-eventid='MLoginRegister_SMSReceiveCode']");180 await page.ClickAsync("input[type=checkbox]");181 await page.WaitForTimeoutAsync(500);182 string js = "document.body.outerText";183 var pageouterText = await page.EvaluateExpressionAsync(js);...

Full Screen

Full Screen

BrowserContextTests.cs

Source:BrowserContextTests.cs Github

copy

Full Screen

...30 [PuppeteerFact]31 public async Task ShouldCreateNewIncognitoContext()32 {33 Assert.Single(Browser.BrowserContexts());34 var context = await Browser.CreateIncognitoBrowserContextAsync();35 Assert.True(context.IsIncognito);36 Assert.Equal(2, Browser.BrowserContexts().Length);37 Assert.Contains(context, Browser.BrowserContexts());38 await context.CloseAsync();39 Assert.Single(Browser.BrowserContexts());40 }41 [PuppeteerTest("browsercontext.spec.ts", "BrowserContext", "should close all belonging targets once closing context")]42 [PuppeteerFact]43 public async Task ShouldCloseAllBelongingTargetsOnceClosingContext()44 {45 Assert.Single((await Browser.PagesAsync()));46 var context = await Browser.CreateIncognitoBrowserContextAsync();47 await context.NewPageAsync();48 Assert.Equal(2, (await Browser.PagesAsync()).Length);49 Assert.Single((await context.PagesAsync()));50 await context.CloseAsync();51 Assert.Single((await Browser.PagesAsync()));52 }53 [PuppeteerTest("browsercontext.spec.ts", "BrowserContext", "window.open should use parent tab context")]54 [SkipBrowserFact(skipFirefox: true)]55 public async Task WindowOpenShouldUseParentTabContext()56 {57 var context = await Browser.CreateIncognitoBrowserContextAsync();58 var page = await context.NewPageAsync();59 await page.GoToAsync(TestConstants.EmptyPage);60 var popupTargetCompletion = new TaskCompletionSource<Target>();61 Browser.TargetCreated += (_, e) => popupTargetCompletion.SetResult(e.Target);62 await Task.WhenAll(63 popupTargetCompletion.Task,64 page.EvaluateFunctionAsync("url => window.open(url)", TestConstants.EmptyPage)65 );66 var popupTarget = await popupTargetCompletion.Task;67 Assert.Same(context, popupTarget.BrowserContext);68 await context.CloseAsync();69 }70 [PuppeteerTest("browsercontext.spec.ts", "BrowserContext", "should fire target events")]71 [SkipBrowserFact(skipFirefox: true)]72 public async Task ShouldFireTargetEvents()73 {74 var context = await Browser.CreateIncognitoBrowserContextAsync();75 var events = new List<string>();76 context.TargetCreated += (_, e) => events.Add("CREATED: " + e.Target.Url);77 context.TargetChanged += (_, e) => events.Add("CHANGED: " + e.Target.Url);78 context.TargetDestroyed += (_, e) => events.Add("DESTROYED: " + e.Target.Url);79 var page = await context.NewPageAsync();80 await page.GoToAsync(TestConstants.EmptyPage);81 await page.CloseAsync();82 Assert.Equal(new[] {83 $"CREATED: {TestConstants.AboutBlank}",84 $"CHANGED: {TestConstants.EmptyPage}",85 $"DESTROYED: {TestConstants.EmptyPage}"86 }, events);87 await context.CloseAsync();88 }89 [PuppeteerTest("browsercontext.spec.ts", "BrowserContext", "should isolate local storage and cookies")]90 [PuppeteerFact]91 public async Task ShouldIsolateLocalStorageAndCookies()92 {93 // Create two incognito contexts.94 var context1 = await Browser.CreateIncognitoBrowserContextAsync();95 var context2 = await Browser.CreateIncognitoBrowserContextAsync();96 Assert.Empty(context1.Targets());97 Assert.Empty(context2.Targets());98 // Create a page in first incognito context.99 var page1 = await context1.NewPageAsync();100 await page1.GoToAsync(TestConstants.EmptyPage);101 await page1.EvaluateExpressionAsync(@"{102 localStorage.setItem('name', 'page1');103 document.cookie = 'name=page1';104 }");105 Assert.Single(context1.Targets());106 Assert.Empty(context2.Targets());107 // Create a page in second incognito context.108 var page2 = await context2.NewPageAsync();109 await page2.GoToAsync(TestConstants.EmptyPage);110 await page2.EvaluateExpressionAsync(@"{111 localStorage.setItem('name', 'page2');112 document.cookie = 'name=page2';113 }");114 Assert.Single(context1.Targets());115 Assert.Equal(page1.Target, context1.Targets()[0]);116 Assert.Single(context2.Targets());117 Assert.Equal(page2.Target, context2.Targets()[0]);118 // Make sure pages don't share localstorage or cookies.119 Assert.Equal("page1", await page1.EvaluateExpressionAsync<string>("localStorage.getItem('name')"));120 Assert.Equal("name=page1", await page1.EvaluateExpressionAsync<string>("document.cookie"));121 Assert.Equal("page2", await page2.EvaluateExpressionAsync<string>("localStorage.getItem('name')"));122 Assert.Equal("name=page2", await page2.EvaluateExpressionAsync<string>("document.cookie"));123 // Cleanup contexts.124 await Task.WhenAll(context1.CloseAsync(), context2.CloseAsync());125 Assert.Single(Browser.BrowserContexts());126 }127 [PuppeteerTest("browsercontext.spec.ts", "BrowserContext", "should work across sessions")]128 [SkipBrowserFact(skipFirefox: true)]129 public async Task ShouldWorkAcrossSessions()130 {131 Assert.Single(Browser.BrowserContexts());132 var context = await Browser.CreateIncognitoBrowserContextAsync();133 Assert.Equal(2, Browser.BrowserContexts().Length);134 var remoteBrowser = await Puppeteer.ConnectAsync(new ConnectOptions135 {136 BrowserWSEndpoint = Browser.WebSocketEndpoint137 });138 var contexts = remoteBrowser.BrowserContexts();139 Assert.Equal(2, contexts.Length);140 remoteBrowser.Disconnect();141 await context.CloseAsync();142 }143 [PuppeteerTest("browsercontext.spec.ts", "BrowserContext", "should wait for target")]144 [SkipBrowserFact(skipFirefox: true)]145 public async Task ShouldWaitForTarget()146 {147 var context = await Browser.CreateIncognitoBrowserContextAsync();148 var targetPromise = context.WaitForTargetAsync((target) => target.Url == TestConstants.EmptyPage);149 var page = await context.NewPageAsync();150 await page.GoToAsync(TestConstants.EmptyPage);151 var promiseTarget = await targetPromise;152 var targetPage = await promiseTarget.PageAsync();153 Assert.Equal(targetPage, page);154 await context.CloseAsync();155 }156 [PuppeteerTest("browsercontext.spec.ts", "BrowserContext", "should timeout waiting for non existant target")]157 [PuppeteerFact]158 public async Task ShouldTimeoutWaitingForNonExistantTarget()159 {160 var context = await Browser.CreateIncognitoBrowserContextAsync();161 await Assert.ThrowsAsync<TimeoutException>(()162 => context.WaitForTargetAsync((target) => target.Url == TestConstants.EmptyPage, new WaitForOptions { Timeout = 1 }));163 await context.CloseAsync();164 }165 }166}...

Full Screen

Full Screen

Bot.cs

Source:Bot.cs Github

copy

Full Screen

...108 {109 Page page;110 if (isIncognito)111 {112 var context = await browser.CreateIncognitoBrowserContextAsync();113 page = await context.NewPageAsync();114 }115 //else page = await browser.NewPageAsync();116 else page = (await browser.PagesAsync())[0];117 #region commented118 //await SetRequestHook(page);119 //var deviceDescriptorName = GetDeviceDescriptorName();120 //if (deviceDescriptorName != DeviceDescriptorName.BlackberryPlayBook)121 //{122 // await page.EmulateAsync(Puppeteer.Devices[deviceDescriptorName]);123 //}124 #endregion125 if (_chromiumSettings != null) await PuppeteerBrowser.Authenticate(page, _chromiumSettings.Proxy);126 await page.GoToAsync(GetRegistrationUrl(), _navigationOptions);...

Full Screen

Full Screen

BrowserContextManager.cs

Source:BrowserContextManager.cs Github

copy

Full Screen

...29 {30 state.RestoreState(whatsapp).GetAwaiter().GetResult();31 }32 private async Task<BrowserContext> NewContext(Browser browser)33 => await browser.CreateIncognitoBrowserContextAsync();34 private async Task<Page> NewWhatsappPage(BrowserContext context)35 {36 var page = await context.NewPageAsync();37 await page.SetUserAgentAsync(USER_AGENT);38 await page.SetViewportAsync(VIEW_PORT);39 await page.GoToAsync("https://web.whatsapp.com/");40 41 await page.ExposeFunctionAsync("messageReceived", (string text) =>42 {43 Console.WriteLine(text);44 return Task.CompletedTask;45 });46 await page.EvaluateFunctionAsync(@"47() => { ...

Full Screen

Full Screen

VkBrwCommunity.cs

Source:VkBrwCommunity.cs Github

copy

Full Screen

...33 Browser browser = await Puppeteer.LaunchAsync(new LaunchOptions34 {35 Headless = false36 });37 BrowserContext userContext = await browser.CreateIncognitoBrowserContextAsync();38 Page communityPage = await userContext.NewPageAsync();39 await communityPage.SetViewportAsync(new ViewPortOptions40 {41 Height = 768,42 Width = 102443 });44 string ver = await browser.GetVersionAsync();45 string userAgent = await browser.GetUserAgentAsync();46 await communityPage.SetUserAgentAsync("Mozilla / 5.0(Windows NT 10.0; Win64; x64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 74.0.3723.0 Safari / 537.36");47 48 await communityPage.GoToAsync(CommunityUrl);49 ElementHandle followersLoaded = await communityPage.WaitForSelectorAsync("#public_followers");50 }51 }...

Full Screen

Full Screen

BrowserContextAccessor.cs

Source:BrowserContextAccessor.cs Github

copy

Full Screen

...10 var browserFetcher = new BrowserFetcher();11 await browserFetcher.DownloadAsync().AnyContext();12 var browser = await Puppeteer.LaunchAsync(13 new LaunchOptions { Headless = true });14 return await browser.CreateIncognitoBrowserContextAsync();15 }16 public Task<BrowserContext> GetBrowserContextAsync() => browserContext.Value;17 async ValueTask IAsyncDisposable.DisposeAsync()18 {19 GC.SuppressFinalize(this);20 if (browserContext.IsValueCreated)21 {22 var value = await browserContext.Value.AnyContext();23 await value.CloseAsync();24 await value.Browser.DisposeAsync().ConfigureAwait(false);25 }26 }27 }28}...

Full Screen

Full Screen

PuppeteerPageBaseTest.cs

Source:PuppeteerPageBaseTest.cs Github

copy

Full Screen

...12 Browser = await Puppeteer.LaunchAsync(new LaunchOptions13 {14 Headless = true15 });16 Context = await Browser.CreateIncognitoBrowserContextAsync();17 Page = await Context.NewPageAsync();18 await SetUp();19 }20 public async Task DisposeAsync()21 {22 await TearDown();23 await Page.CloseAsync();24 await Browser.CloseAsync();25 }26 protected virtual async Task SetUp()27 {28 await Task.CompletedTask;29 }30 protected virtual async Task TearDown()...

Full Screen

Full Screen

PuppeteerBrowserContextBaseTest.cs

Source:PuppeteerBrowserContextBaseTest.cs Github

copy

Full Screen

...12 protected BrowserContext Context { get; set; }13 public override async Task InitializeAsync()14 {15 await base.InitializeAsync();16 Context = await Browser.CreateIncognitoBrowserContextAsync();17 }18 }19}...

Full Screen

Full Screen

CreateIncognitoBrowserContextAsync

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });9 var context = await browser.CreateIncognitoBrowserContextAsync();10 var page = await context.NewPageAsync();11 await page.ScreenshotAsync("google.png");12 await browser.CloseAsync();13 }14 }15}

Full Screen

Full Screen

CreateIncognitoBrowserContextAsync

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static void Main(string[] args)7 {8 MainAsync().GetAwaiter().GetResult();9 }10 static async Task MainAsync()11 {12 var browser = await Puppeteer.LaunchAsync(new LaunchOptions13 {14 Args = new string[] { "--disable-extensions" }15 });16 var context = await browser.CreateIncognitoBrowserContextAsync();17 var page = await context.NewPageAsync();18 await page.ScreenshotAsync("google.png");19 await browser.CloseAsync();20 }21 }22}

Full Screen

Full Screen

CreateIncognitoBrowserContextAsync

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp;2 using System;3 using System.Threading.Tasks;4{5 {6 static async Task Main( string [] args)7 {8 var browser = await Puppeteer.LaunchAsync( new LaunchOptions9 {10 });11 var context = await browser.CreateIncognitoBrowserContextAsync();12 var page = await context.NewPageAsync();13 await page.ScreenshotAsync( "screenshot.png" );14 await browser.CloseAsync();15 }16 }17}18using PuppeteerSharp;19 using System;20 using System.Threading.Tasks;21{22 {23 static async Task Main( string [] args)24 {25 var browser = await Puppeteer.LaunchAsync( new LaunchOptions26 {27 });28 var context = await browser.CreateIncognitoBrowserContextAsync();29 var page = await context.NewPageAsync();30 await page.ScreenshotAsync( "screenshot.png" );31 await browser.CloseAsync();32 }33 }34}35using PuppeteerSharp;36 using System;37 using System.Threading.Tasks;38{39 {40 static async Task Main( string [] args)41 {42 var browser = await Puppeteer.LaunchAsync( new LaunchOptions43 {44 });45 var context = await browser.CreateIncognitoBrowserContextAsync();46 var page = await context.NewPageAsync();47 await page.ScreenshotAsync( "screenshot.png" );48 await browser.CloseAsync();49 }50 }51}

Full Screen

Full Screen

CreateIncognitoBrowserContextAsync

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static void Main(string[] args)7 {8 Console.WriteLine("Hello World!");9 Console.WriteLine("Press any key to exit.");10 MainAsync().GetAwaiter().GetResult();11 Console.ReadKey();12 }13 static async Task MainAsync()14 {15 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true, Args = new string[] { "--disable-web-security", "--user-data-dir" } });16 var context = await browser.CreateIncognitoBrowserContextAsync();17 var page = await context.NewPageAsync();18 await browser.CloseAsync();19 }20 }21}22using System;23using System.Threading.Tasks;24using PuppeteerSharp;25{26 {27 static void Main(string[] args)28 {29 Console.WriteLine("Hello World!");30 Console.WriteLine("Press any key to exit.");31 MainAsync().GetAwaiter().GetResult();32 Console.ReadKey();33 }34 static async Task MainAsync()35 {36 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true, Args = new string[] { "--disable-web-security", "--user-data-dir" } });37 var page = await browser.NewPageAsync();38 await browser.CloseAsync();39 }40 }41}42using System;43using System.Threading.Tasks;44using PuppeteerSharp;45{46 {47 static void Main(string[] args)48 {49 Console.WriteLine("Hello World!");50 Console.WriteLine("Press any key to exit.");51 MainAsync().GetAwaiter().GetResult();52 Console.ReadKey();53 }54 static async Task MainAsync()55 {56 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true, Args = new string[] { "--disable-web-security", "--user-data-dir" } });57 var context = await browser.CreateIncognitoBrowserContextAsync();

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful