How to use SetContextAsCurrent method of Atata.AtataNavigator class

Best Atata code snippet using Atata.AtataNavigator.SetContextAsCurrent

AtataNavigator.cs

Source:AtataNavigator.cs Github

copy

Full Screen

...44        /// <returns>The page object.</returns>45        public T ToWindow<T>(T pageObject, string windowName, bool temporarily = false)46            where T : PageObject<T>47        {48            SetContextAsCurrent();49            _context.Log.Info($"Switch to \"{windowName}\" window");50            return To(pageObject, new GoOptions { Navigate = false, WindowName = windowName, Temporarily = temporarily });51        }52        /// <summary>53        /// Navigates to the window by name.54        /// </summary>55        /// <typeparam name="T">The type of the page object.</typeparam>56        /// <param name="windowName">Name of the browser window.</param>57        /// <param name="temporarily">If set to <see langword="true"/> navigates temporarily preserving current page object state.</param>58        /// <returns>The page object.</returns>59        public T ToWindow<T>(string windowName, bool temporarily = false)60            where T : PageObject<T>61        {62            SetContextAsCurrent();63            _context.Log.Info($"Switch to \"{windowName}\" window");64            return To<T>(null, new GoOptions { Navigate = false, WindowName = windowName, Temporarily = temporarily });65        }66        /// <summary>67        /// Navigates to the next window with the specified page object.68        /// </summary>69        /// <typeparam name="T">The type of the page object.</typeparam>70        /// <param name="pageObject">71        /// The page object.72        /// If set to <see langword="null"/> creates an instance using the default constructor.</param>73        /// <param name="temporarily">If set to <see langword="true"/> navigates temporarily preserving current page object state.</param>74        /// <returns>The page object.</returns>75        public T ToNextWindow<T>(T pageObject = null, bool temporarily = false)76            where T : PageObject<T>77        {78            SetContextAsCurrent();79            _context.Log.Info("Switch to next window");80            string currentWindowHandle = _context.Driver.CurrentWindowHandle;81            string nextWindowHandle = _context.Driver.WindowHandles.82                SkipWhile(x => x != currentWindowHandle).83                ElementAt(1);84            return To(pageObject, new GoOptions { Navigate = false, WindowName = nextWindowHandle, Temporarily = temporarily });85        }86        /// <summary>87        /// Navigates to the previous window with the specified page object.88        /// </summary>89        /// <typeparam name="T">The type of the page object.</typeparam>90        /// <param name="pageObject">91        /// The page object.92        /// If set to <see langword="null"/> creates an instance using the default constructor.</param>93        /// <param name="temporarily">If set to <see langword="true"/> navigates temporarily preserving current page object state.</param>94        /// <returns>The page object.</returns>95        public T ToPreviousWindow<T>(T pageObject = null, bool temporarily = false)96            where T : PageObject<T>97        {98            SetContextAsCurrent();99            _context.Log.Info("Switch to previous window");100            string currentWindowHandle = _context.Driver.CurrentWindowHandle;101            string previousWindowHandle = _context.Driver.WindowHandles102                .Reverse()103                .SkipWhile(x => x != currentWindowHandle)104                .ElementAt(1);105            return To(pageObject, new GoOptions { Navigate = false, WindowName = previousWindowHandle, Temporarily = temporarily });106        }107        private T To<T>(T pageObject, GoOptions options)108            where T : PageObject<T>109        {110            SetContextAsCurrent();111            var currentPageObject = _context.PageObject;112            return currentPageObject is null113                ? GoToInitialPageObject(pageObject, options)114                : GoToFollowingPageObject(currentPageObject, pageObject, options);115        }116        private T GoToInitialPageObject<T>(T pageObject, GoOptions options)117            where T : PageObject<T>118        {119            pageObject = pageObject ?? ActivatorEx.CreateInstance<T>();120            _context.PageObject = pageObject;121            if (!string.IsNullOrWhiteSpace(options.Url))122                ToUrl(options.Url);123            pageObject.NavigateOnInit = options.Navigate;124            pageObject.Init();125            return pageObject;126        }127        private T GoToFollowingPageObject<T>(128            UIComponent currentPageObject,129            T nextPageObject,130            GoOptions options)131            where T : PageObject<T>132        {133            bool isReturnedFromTemporary = TryResolvePreviousPageObjectNavigatedTemporarily(ref nextPageObject);134            nextPageObject = nextPageObject ?? ActivatorEx.CreateInstance<T>();135            if (!isReturnedFromTemporary)136            {137                if (!options.Temporarily)138                {139                    _context.CleanUpTemporarilyPreservedPageObjectList();140                }141                nextPageObject.NavigateOnInit = options.Navigate;142                if (options.Temporarily)143                {144                    nextPageObject.IsTemporarilyNavigated = options.Temporarily;145                    _context.TemporarilyPreservedPageObjectList.Add(currentPageObject);146                }147            }148            ((IPageObject)currentPageObject).DeInit();149            _context.PageObject = nextPageObject;150            // TODO: Review this condition.151            if (!options.Temporarily)152                UIComponentResolver.CleanUpPageObject(currentPageObject);153            if (!string.IsNullOrWhiteSpace(options.Url))154                Go.ToUrl(options.Url);155            if (!string.IsNullOrWhiteSpace(options.WindowName))156                ((IPageObject)currentPageObject).SwitchToWindow(options.WindowName);157            if (isReturnedFromTemporary)158            {159                _context.Log.Info("Go to {0}", nextPageObject.ComponentFullName);160            }161            else162            {163                nextPageObject.PreviousPageObject = currentPageObject;164                nextPageObject.Init();165            }166            return nextPageObject;167        }168        private bool TryResolvePreviousPageObjectNavigatedTemporarily<TPageObject>(ref TPageObject pageObject)169            where TPageObject : PageObject<TPageObject>170        {171            var tempPageObjectsEnumerable = _context.TemporarilyPreservedPageObjects.172                AsEnumerable().173                Reverse().174                OfType<TPageObject>();175            TPageObject pageObjectReferenceCopy = pageObject;176            TPageObject foundPageObject = pageObject == null177                ? tempPageObjectsEnumerable.FirstOrDefault(x => x.GetType() == typeof(TPageObject))178                : tempPageObjectsEnumerable.FirstOrDefault(x => x == pageObjectReferenceCopy);179            if (foundPageObject == null)180                return false;181            pageObject = foundPageObject;182            var tempPageObjectsToRemove = _context.TemporarilyPreservedPageObjects.183                SkipWhile(x => x != foundPageObject).184                ToArray();185            UIComponentResolver.CleanUpPageObjects(tempPageObjectsToRemove.Skip(1));186            foreach (var item in tempPageObjectsToRemove)187                _context.TemporarilyPreservedPageObjectList.Remove(item);188            return true;189        }190        /// <summary>191        /// Navigates to the specified URL.192        /// </summary>193        /// <param name="url">The URL.</param>194        public void ToUrl(string url)195        {196            SetContextAsCurrent();197            if (!UriUtils.TryCreateAbsoluteUrl(url, out Uri absoluteUrl))198            {199                if (!_context.IsNavigated && _context.BaseUrl is null)200                {201                    if (string.IsNullOrWhiteSpace(url))202                        throw new InvalidOperationException("Cannot navigate to empty or null URL. AtataContext.Current.BaseUrl can be set with base URL.");203                    else204                        throw new InvalidOperationException($"Cannot navigate to relative URL \"{url}\". AtataContext.Current.BaseUrl can be set with base URL.");205                }206                if (_context.BaseUrl is null)207                {208                    Uri currentUri = new Uri(_context.Driver.Url, UriKind.Absolute);209                    string domainPart = currentUri.GetComponents(UriComponents.SchemeAndServer | UriComponents.UserInfo, UriFormat.Unescaped);210                    Uri domainUri = new Uri(domainPart);211                    absoluteUrl = new Uri(domainUri, url);212                }213                else214                {215                    absoluteUrl = UriUtils.Concat(_context.BaseUrl, url);216                }217            }218            Navigate(absoluteUrl);219        }220        private void Navigate(Uri uri)221        {222            _context.Log.Info($"Go to URL \"{uri}\"");223            _context.Driver.Navigate().GoToUrl(uri);224            _context.IsNavigated = true;225        }226        private void SetContextAsCurrent()227        {228            AtataContext.Current = _context;229        }230    }231}...

Full Screen

Full Screen

SetContextAsCurrent

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Atata;7{8    {9        static void Main(string[] args)10        {11            AtataContext.Current.LogIn<HomePage>(out HomePage homePage)

Full Screen

Full Screen

SetContextAsCurrent

Using AI Code Generation

copy

Full Screen

1using System;2using Atata;3using NUnit.Framework;4{5    {6        public void SetContextAsCurrent()7        {8            using (AtataContext.Configure().UseChrome().Build())9            {10                Go.To<HomePage>();11                Go.To<LoginPage>();12                Go.To<HomePage>();13            }14        }15    }16}17using System;18using Atata;19using NUnit.Framework;20{21    {22        public void GoTo()23        {24            using (AtataContext.Configure().UseChrome().Build())25            {26                Go.To<HomePage>();27                Go.To<LoginPage>();28                Go.To<HomePage>();29            }30        }31    }32}33using System;34using Atata;35using NUnit.Framework;36{37    {38        public void GoToUrl()39        {40            using (AtataContext.Configure().UseChrome().Build())41            {42            }43        }44    }45}46using System;47using Atata;48using NUnit.Framework;49{50    {51        public void GoToUrl()52        {53            using (AtataContext.Configure().UseChrome().Build())54            {55            }56        }57    }58}59using System;60using Atata;61using NUnit.Framework;62{63    {64        public void GoToUrl()65        {66            using (AtataContext.Configure().UseChrome().Build())67            {

Full Screen

Full Screen

SetContextAsCurrent

Using AI Code Generation

copy

Full Screen

1{2    using NUnit.Framework;3    {4        public void _5()5        {6                Build();

Full Screen

Full Screen

SetContextAsCurrent

Using AI Code Generation

copy

Full Screen

1{2    {3        public void SetContextAsCurrent()4        {5            AtataContext.Current = new AtataContext();6        }7    }8}9{10    {11        public static AtataContext Current { get; set; }12        public void NavigateTo(string url)13        {14            Console.WriteLine("Navigating to " + url);15        }16    }17}18{19    {20        public static AtataContext Current { get; set; }21        public void NavigateTo(string url)22        {23            Console.WriteLine("Navigating to " + url);24        }25    }26}27{28    {29        public static AtataContext Current { get; set; }30        public void NavigateTo(string url)31        {32            Console.WriteLine("Navigating to " + url);33        }34    }35}36{37    {38        public static AtataContext Current { get; set; }39        public void NavigateTo(string url)40        {41            Console.WriteLine("Navigating to " + url);42        }43    }44}45{46    {47        public static AtataContext Current { get; set; }48        public void NavigateTo(string url)49        {50            Console.WriteLine("Navigating to " + url);51        }52    }53}54{55    {56        public static AtataContext Current { get; set; }57        public void NavigateTo(string url)58        {59            Console.WriteLine("Navigating to " + url);60        }61    }62}63{

Full Screen

Full Screen

SetContextAsCurrent

Using AI Code Generation

copy

Full Screen

1using System;2using Atata;3using OpenQA.Selenium.Chrome;4{5    {6        static void Main(string[] args)7        {8            var chromeDriver = new ChromeDriver();9            AtataContext.Configure().UseChrome().Build();10            AtataContext.Current.UseDriver(chromeDriver);11            AtataContext.Current.SetContextAsCurrent();12            AtataContext.Current.Log.Info("Hello World!");13            AtataContext.Current.Driver.Quit();14        }15    }16}

Full Screen

Full Screen

SetContextAsCurrent

Using AI Code Generation

copy

Full Screen

1using System;2using NUnit.Framework;3using Atata;4{5    {6        public void _5()7        {8            Go.To<HomePage>()9                .SignIn.ClickAndGo()10                .SetContextAsCurrent<HomePage>()11                .SignIn.ClickAndGo();12        }13    }14}15using System;16using NUnit.Framework;17using Atata;18{19    {20        public void _6()21        {22            Go.To<HomePage>()23                .SignIn.ClickAndGo()24                .Go.To<HomePage>()25                .SignIn.ClickAndGo();26        }27    }28}29using System;30using NUnit.Framework;31using Atata;32{33    {34        public void _7()35        {36            Go.To<HomePage>()37                .SignIn.ClickAndGo()38                .Go.To<HomePage>()39                .SignIn.ClickAndGo();40        }41    }42}

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.

Run Atata automation tests on LambdaTest cloud grid

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

Most used method in AtataNavigator

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful