public async Task ShouldNotGetCookiesRelatedToCurrentDomainWithoutLeadingPeriod()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            string cookieName = "name";

            await AssertCookieIsNotPresentWithName(cookieName);

            Regex  replaceRegex = new Regex(".*?\\.");
            string subdomain    = replaceRegex.Replace(_hostname, "subdomain.", 1);
            Cookie cookie       = new Cookie(cookieName, "value", subdomain, "/", GetTimeInTheFuture());

            string originalUrl = await driver.GetUrl();

            string subdomainUrl = originalUrl.Replace(_hostname, subdomain);
            await driver.GoToUrl(subdomainUrl);

            await driver.Options().Cookies.AddCookie(cookie);

            await driver.GoToUrl(originalUrl);

            await AssertCookieIsNotPresentWithName(cookieName);
        }
        public async Task ShouldBeAbleToIncludeLeadingPeriodInDomainName()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            // Cookies cannot be set on domain names with less than 2 dots, so
            // localhost is out. If we are in that boat, bail the test.
            string hostName = EnvironmentManager.Instance.UrlBuilder.HostName;

            string[] hostNameParts = hostName.Split(new char[] { '.' });
            if (hostNameParts.Length < 3)
            {
                Assert.Ignore("Skipping test: Cookies can only be set on fully-qualified domain names.");
            }

            await AssertCookieIsNotPresentWithName("name");

            // Replace the first part of the name with a period
            Regex  replaceRegex = new Regex(".*?\\.");
            string shorter      = replaceRegex.Replace(_hostname, ".", 1);
            Cookie cookie       = new Cookie("name", "value", shorter, "/", DateTime.Now.AddSeconds(100000));

            await driver.Options().Cookies.AddCookie(cookie);

            await AssertCookieIsPresentWithName("name");
        }
        public async Task ShouldBeAbleToAddToADomainWhichIsRelatedToTheCurrentDomain()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            // Cookies cannot be set on domain names with less than 2 dots, so
            // localhost is out. If we are in that boat, bail the test.
            string hostName = EnvironmentManager.Instance.UrlBuilder.HostName;

            string[] hostNameParts = hostName.Split(new char[] { '.' });
            if (hostNameParts.Length < 3)
            {
                Assert.Ignore("Skipping test: Cookies can only be set on fully-qualified domain names.");
            }

            await AssertCookieIsNotPresentWithName("name");

            Regex  replaceRegex = new Regex(".*?\\.");
            string shorter      = replaceRegex.Replace(_hostname, ".", 1);
            Cookie cookie       = new Cookie("name", "value", shorter, "/", GetTimeInTheFuture());

            await driver.Options().Cookies.AddCookie(cookie);

            await AssertCookieIsPresentWithName("name");
        }
        public async Task ShouldDeleteCookie()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            await driver.GoToUrl(macbethPage);

            IOptions options        = driver.Options();
            Cookie   cookieToDelete = new Cookie("answer", "42");
            Cookie   cookieToKeep   = new Cookie("canIHaz", "Cheeseburguer");
            await options.Cookies.AddCookie(cookieToDelete);

            await options.Cookies.AddCookie(cookieToKeep);

            ReadOnlyCollection <Cookie> cookies = await options.Cookies.AllCookies();

            await options.Cookies.DeleteCookie(cookieToDelete);

            ReadOnlyCollection <Cookie> cookies2 = await options.Cookies.AllCookies();

            Assert.That(cookies2, Does.Not.Contain(cookieToDelete), "Cookie was not deleted successfully");
            Assert.That(cookies2.Contains(cookieToKeep), "Valid cookie was not returned");
        }
        public async Task ShouldNotDeleteCookiesWithASimilarName()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            string   cookieOneName = "fish";
            Cookie   cookie1       = new Cookie(cookieOneName, "cod");
            Cookie   cookie2       = new Cookie(cookieOneName + "x", "earth");
            IOptions options       = driver.Options();

            await AssertCookieIsNotPresentWithName(cookie1.Name);

            await options.Cookies.AddCookie(cookie1);

            await options.Cookies.AddCookie(cookie2);

            await AssertCookieIsPresentWithName(cookie1.Name);

            await options.Cookies.DeleteCookieNamed(cookieOneName);

            Assert.That(await driver.Options().Cookies.AllCookies(), Does.Not.Contain(cookie1));
            Assert.That(await driver.Options().Cookies.AllCookies(), Does.Contain(cookie2));
        }
        public async Task CanSetCookiesOnADifferentPathOfTheSameHost()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            string basePath = EnvironmentManager.Instance.UrlBuilder.Path;
            Cookie cookie1  = new Cookie("fish", "cod", "/" + basePath + "/animals");
            Cookie cookie2  = new Cookie("planet", "earth", "/" + basePath + "/galaxy");

            IOptions options = driver.Options();
            ReadOnlyCollection <Cookie> count = await options.Cookies.AllCookies();

            await options.Cookies.AddCookie(cookie1);

            await options.Cookies.AddCookie(cookie2);

            string url = EnvironmentManager.Instance.UrlBuilder.WhereIs("animals");
            await driver.GoToUrl(url);

            ReadOnlyCollection <Cookie> cookies = await options.Cookies.AllCookies();

            Assert.That(cookies, Does.Contain(cookie1));
            Assert.That(cookies, Does.Not.Contain(cookie2));

            await driver.GoToUrl(EnvironmentManager.Instance.UrlBuilder.WhereIs("galaxy"));

            cookies = await options.Cookies.AllCookies();

            Assert.That(cookies, Does.Not.Contain(cookie1));
            Assert.That(cookies, Does.Contain(cookie2));
        }
        public async Task ShouldBeAbleToSetDomainToTheCurrentDomain()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            // Cookies cannot be set on domain names with less than 2 dots, so
            // localhost is out. If we are in that boat, bail the test.
            string hostName = EnvironmentManager.Instance.UrlBuilder.HostName;

            string[] hostNameParts = hostName.Split(new char[] { '.' });
            if (hostNameParts.Length < 3)
            {
                Assert.Ignore("Skipping test: Cookies can only be set on fully-qualified domain names.");
            }

            Uri    url  = new Uri(await driver.GetUrl());
            String host = url.Host + ":" + url.Port.ToString();

            Cookie   cookie1 = new Cookie("fish", "cod", host, "/", null);
            IOptions options = driver.Options();
            await options.Cookies.AddCookie(cookie1);

            await driver.GoToUrl(javascriptPage);

            ReadOnlyCollection <Cookie> cookies = await options.Cookies.AllCookies();

            Assert.That(cookies, Does.Contain(cookie1));
        }
        public async Task AddCookiesWithDifferentPathsThatAreRelatedToOurs()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            string basePath = EnvironmentManager.Instance.UrlBuilder.Path;

            Cookie   cookie1 = new Cookie("fish", "cod", "/" + basePath + "/animals");
            Cookie   cookie2 = new Cookie("planet", "earth", "/" + basePath + "/");
            IOptions options = driver.Options();
            await options.Cookies.AddCookie(cookie1);

            await options.Cookies.AddCookie(cookie2);

            UrlBuilder builder = EnvironmentManager.Instance.UrlBuilder;
            await driver.GoToUrl(builder.WhereIs("animals"));

            ReadOnlyCollection <Cookie> cookies = await options.Cookies.AllCookies();

            await AssertCookieIsPresentWithName(cookie1.Name);
            await AssertCookieIsPresentWithName(cookie2.Name);

            await driver.GoToUrl(builder.WhereIs("simpleTest.html"));

            await AssertCookieIsNotPresentWithName(cookie1.Name);
        }
        public async Task ShouldRetainCookieExpiry()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            string url = EnvironmentManager.Instance.UrlBuilder.WhereElseIs("animals");

            await driver.GoToUrl(url);

            await driver.Options().Cookies.DeleteAllCookies();

            // DateTime.Now contains milliseconds; the returned cookie expire date
            // will not. So we need to truncate the milliseconds.
            DateTime current    = DateTime.Now;
            DateTime expireDate = new DateTime(current.Year, current.Month, current.Day, current.Hour, current.Minute, current.Second, DateTimeKind.Local).AddDays(1);

            Cookie   addCookie = new Cookie("fish", "cod", "/common/animals", expireDate);
            IOptions options   = driver.Options();
            await options.Cookies.AddCookie(addCookie);

            Cookie retrieved = await options.Cookies.GetCookieNamed("fish");

            Assert.That(retrieved, Is.Not.Null);
            Assert.AreEqual(addCookie.Expiry, retrieved.Expiry, "Cookies are not equal");
        }
        public async Task ShouldNotShowCookieAddedToDifferentPath()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            // Cookies cannot be set on domain names with less than 2 dots, so
            // localhost is out. If we are in that boat, bail the test.
            string hostName = EnvironmentManager.Instance.UrlBuilder.HostName;

            string[] hostNameParts = hostName.Split(new char[] { '.' });
            if (hostNameParts.Length < 3)
            {
                Assert.Ignore("Skipping test: Cookies can only be set on fully-qualified domain names.");
            }

            await driver.GoToUrl(macbethPage);

            IOptions options = driver.Options();
            Cookie   cookie  = new Cookie("Lisa", "Simpson", EnvironmentManager.Instance.UrlBuilder.HostName, "/" + EnvironmentManager.Instance.UrlBuilder.Path + "IDoNotExist", null);
            await options.Cookies.AddCookie(cookie);

            ReadOnlyCollection <Cookie> cookies = await options.Cookies.AllCookies();

            Assert.That(cookies, Does.Not.Contain(cookie), "Invalid cookie was returned");
        }
        public async Task CookieEqualityAfterSetAndGet()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            string url = EnvironmentManager.Instance.UrlBuilder.WhereElseIs("animals");

            await driver.GoToUrl(url);

            await driver.Options().Cookies.DeleteAllCookies();

            DateTime time    = DateTime.Now.AddDays(1);
            Cookie   cookie1 = new Cookie("fish", "cod", null, "/common/animals", time);
            IOptions options = driver.Options();
            await options.Cookies.AddCookie(cookie1);

            ReadOnlyCollection <Cookie> cookies = await options.Cookies.AllCookies();

            Cookie retrievedCookie = null;

            foreach (Cookie tempCookie in cookies)
            {
                if (cookie1.Equals(tempCookie))
                {
                    retrievedCookie = tempCookie;
                    break;
                }
            }

            Assert.That(retrievedCookie, Is.Not.Null);
            //Cookie.equals only compares name, domain and path
            Assert.AreEqual(cookie1, retrievedCookie);
        }
        public async Task ShouldIgnoreThePortNumberOfTheHostWhenSettingTheCookie()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            // Cookies cannot be set on domain names with less than 2 dots, so
            // localhost is out. If we are in that boat, bail the test.
            string hostName = EnvironmentManager.Instance.UrlBuilder.HostName;

            string[] hostNameParts = hostName.Split(new char[] { '.' });
            if (hostNameParts.Length < 3)
            {
                Assert.Ignore("Skipping test: Cookies can only be set on fully-qualified domain names.");
            }

            Uri    uri        = new Uri(await driver.GetUrl());
            string host       = $"{uri.Host}:{uri.Port}";
            string cookieName = "name";

            await AssertCookieIsNotPresentWithName(cookieName);

            Cookie cookie = new Cookie(cookieName, "value", host, "/", null);
            await driver.Options().Cookies.AddCookie(cookie);

            await AssertCookieIsPresentWithName(cookieName);
        }
        public async Task GetAllCookies()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            string key1 = GenerateUniqueKey();
            string key2 = GenerateUniqueKey();

            await AssertCookieIsNotPresentWithName(key1);
            await AssertCookieIsNotPresentWithName(key2);

            ReadOnlyCollection <Cookie> cookies = await driver.Options().Cookies.AllCookies();

            int count = cookies.Count;

            Cookie one = new Cookie(key1, "value");
            Cookie two = new Cookie(key2, "value");

            await driver.Options().Cookies.AddCookie(one);

            await driver.Options().Cookies.AddCookie(two);

            await driver.GoToUrl(simpleTestPage);

            cookies = await driver.Options().Cookies.AllCookies();

            Assert.AreEqual(count + 2, cookies.Count);

            Assert.That(cookies, Does.Contain(one));
            Assert.That(cookies, Does.Contain(two));
        }
        public async Task CanHandleSecureCookie()
        {
            await driver.GoToUrl(EnvironmentManager.Instance.UrlBuilder.WhereIsSecure("animals"));

            Cookie addedCookie = new ReturnedCookie("fish", "cod", null, "/common/animals", null, true, false);
            await driver.Options().Cookies.AddCookie(addedCookie);

            await driver.Navigate().Refresh();

            Cookie retrieved = await driver.Options().Cookies.GetCookieNamed("fish");

            Assert.That(retrieved, Is.Not.Null);
        }
        public async Task ShouldRetainCookieSecure()
        {
            await driver.GoToUrl(EnvironmentManager.Instance.UrlBuilder.WhereIsSecure("animals"));

            ReturnedCookie addedCookie = new ReturnedCookie("fish", "cod", string.Empty, "/common/animals", null, true, false);

            await driver.Options().Cookies.AddCookie(addedCookie);

            await driver.Navigate().Refresh();

            Cookie retrieved = await driver.Options().Cookies.GetCookieNamed("fish");

            Assert.That(retrieved, Is.Not.Null);
            Assert.That(retrieved.Secure, "Secure attribute not set to true");
        }
        public async Task ShouldGetCookieByName()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            string key = $"key_{new Random().Next()}";

            await((IJavaScriptExecutor)driver).ExecuteScript("document.cookie = arguments[0] + '=set';", new CancellationToken(), key);

            Cookie cookie = await driver.Options().Cookies.GetCookieNamed(key);

            Assert.AreEqual("set", cookie.Value);
        }
        public async Task ShouldWalkThePathToDeleteACookie()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            string basePath = EnvironmentManager.Instance.UrlBuilder.Path;

            Cookie cookie1 = new Cookie("fish", "cod");
            await driver.Options().Cookies.AddCookie(cookie1);

            int count = await driver.Options().Cookies.AllCookies().Count();

            await driver.GoToUrl(childPage);

            Cookie cookie2 = new Cookie("rodent", "hamster", "/" + basePath + "/child");
            await driver.Options().Cookies.AddCookie(cookie2);

            count = await driver.Options().Cookies.AllCookies().Count();

            await driver.GoToUrl(grandchildPage);

            Cookie cookie3 = new Cookie("dog", "dalmation", "/" + basePath + "/child/grandchild/");
            await driver.Options().Cookies.AddCookie(cookie3);

            count = await driver.Options().Cookies.AllCookies().Count();

            await driver.GoToUrl(EnvironmentManager.Instance.UrlBuilder.WhereIs("child/grandchild"));

            await driver.Options().Cookies.DeleteCookieNamed("rodent");

            count = await driver.Options().Cookies.AllCookies().Count();

            Assert.That(driver.Options().Cookies.GetCookieNamed("rodent"), Is.Null);

            ReadOnlyCollection <Cookie> cookies = await driver.Options().Cookies.AllCookies();

            Assert.That(cookies, Has.Count.EqualTo(2));
            Assert.That(cookies, Does.Contain(cookie1));
            Assert.That(cookies, Does.Contain(cookie3));

            await driver.Options().Cookies.DeleteAllCookies();

            await driver.GoToUrl(grandchildPage);

            await AssertNoCookiesArePresent();
        }
        public async Task ShouldNotBeAbleToSetDomainToSomethingThatIsUnrelatedToTheCurrentDomain()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            Cookie   cookie1 = new Cookie("fish", "cod");
            IOptions options = driver.Options();
            await options.Cookies.AddCookie(cookie1);

            string url = EnvironmentManager.Instance.UrlBuilder.WhereElseIs("simpleTest.html");
            await driver.GoToUrl(url);

            Assert.That(options.Cookies.GetCookieNamed("fish"), Is.Null);
        }
        public async Task SettingACookieThatExpiredInThePast()
        {
            string url = EnvironmentManager.Instance.UrlBuilder.WhereElseIs("animals");

            await driver.GoToUrl(url);

            await driver.Options().Cookies.DeleteAllCookies();

            DateTime expires = DateTime.Now.AddSeconds(-1000);
            Cookie   cookie  = new Cookie("expired", "yes", "/common/animals", expires);
            IOptions options = driver.Options();
            await options.Cookies.AddCookie(cookie);

            cookie = await options.Cookies.GetCookieNamed("expired");

            Assert.That(cookie, Is.Null, "Cookie expired before it was set, so nothing should be returned: " + cookie);
        }
        public async Task CanSetCookieWithoutOptionalFieldsSet()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            string key    = GenerateUniqueKey();
            string value  = "foo";
            Cookie cookie = new Cookie(key, value);

            await AssertCookieIsNotPresentWithName(key);

            await driver.Options().Cookies.AddCookie(cookie);

            await AssertCookieHasValue(key, value);
        }
        public async Task ShouldThrowExceptionWhenAddingCookieToNonExistingDomain()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            await driver.GoToUrl(macbethPage);

            await driver.GoToUrl("http://nonexistent-origin.seleniumhq-test.test");

            IOptions options = driver.Options();
            Cookie   cookie  = new Cookie("question", "dunno");
            //Assert.That(() => options.Cookies.AddCookie(cookie), Throws.InstanceOf<InvalidCookieDomainException>().Or.InstanceOf<InvalidOperationException>());
            await AssertEx.ThrowsAsync <WebBrowserException>(async() => await options.Cookies.AddCookie(cookie),
                                                             exception => Assert.AreEqual("InvalidCookieDomainException", exception.Error));
        }
        public async Task ShouldAddCookieToCurrentDomain()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            await driver.GoToUrl(macbethPage);

            IOptions options = driver.Options();
            Cookie   cookie  = new Cookie("Marge", "Simpson", "/");
            await options.Cookies.AddCookie(cookie);

            ReadOnlyCollection <Cookie> cookies = await options.Cookies.AllCookies();

            Assert.That(cookies.Contains(cookie), "Valid cookie was not returned");
        }
        public async Task ShouldNotShowCookieAddedToDifferentDomain()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                Assert.Ignore("Not on a standard domain for cookies (localhost doesn't count).");
            }

            await driver.GoToUrl(macbethPage);

            IOptions options = driver.Options();
            Cookie   cookie  = new Cookie("Bart", "Simpson", EnvironmentManager.Instance.UrlBuilder.HostName + ".com", EnvironmentManager.Instance.UrlBuilder.Path, null);

            Assert.That(() => options.Cookies.AddCookie(cookie), Throws.InstanceOf <WebDriverException>().Or.InstanceOf <InvalidOperationException>());
            ReadOnlyCollection <Cookie> cookies = await options.Cookies.AllCookies();

            Assert.That(cookies, Does.Not.Contain(cookie), "Invalid cookie was returned");
        }
        public async Task CanHandleHttpOnlyCookie()
        {
            StringBuilder url = new StringBuilder(EnvironmentManager.Instance.UrlBuilder.WhereIs("cookie"));

            url.Append("?action=add");
            url.Append("&name=").Append("fish");
            url.Append("&value=").Append("cod");
            url.Append("&path=").Append("/common/animals");
            url.Append("&httpOnly=").Append("true");

            await driver.GoToUrl(url.ToString());

            await driver.GoToUrl(EnvironmentManager.Instance.UrlBuilder.WhereIs("animals"));

            Cookie retrieved = await driver.Options().Cookies.GetCookieNamed("fish");

            Assert.That(retrieved, Is.Not.Null);
        }
        public async Task GetCookieDoesNotRetriveBeyondCurrentDomain()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            Cookie   cookie1 = new Cookie("fish", "cod");
            IOptions options = driver.Options();
            await options.Cookies.AddCookie(cookie1);

            String url = EnvironmentManager.Instance.UrlBuilder.WhereElseIs("");
            await driver.GoToUrl(url);

            ReadOnlyCollection <Cookie> cookies = await options.Cookies.AllCookies();

            Assert.That(cookies, Does.Not.Contain(cookie1));
        }
        public async Task ShouldBeAbleToAddCookie()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            string key    = GenerateUniqueKey();
            string value  = "foo";
            Cookie cookie = new Cookie(key, value);

            await AssertCookieIsNotPresentWithName(key);

            await driver.Options().Cookies.AddCookie(cookie);

            await AssertCookieHasValue(key, value);

            Assert.That(await driver.Options().Cookies.AllCookies().Contains(cookie), "Cookie was not added successfully");
        }
        public async Task ShouldReturnNullBecauseCookieRetainsExpiry()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            string url = EnvironmentManager.Instance.UrlBuilder.WhereElseIs("animals");
            await driver.GoToUrl(url);

            await driver.Options().Cookies.DeleteAllCookies();

            Cookie   addCookie = new Cookie("fish", "cod", "/common/animals", DateTime.Now.AddHours(-1));
            IOptions options   = driver.Options();
            await options.Cookies.AddCookie(addCookie);

            Cookie retrieved = await options.Cookies.GetCookieNamed("fish");

            Assert.That(retrieved, Is.Null);
        }
        public async Task DeleteAllCookiesDifferentUrls()
        {
            if (!CheckIsOnValidHostNameForCookieTests())
            {
                return;
            }

            Cookie cookie1 = new Cookie("fish1", "cod", EnvironmentManager.Instance.UrlBuilder.HostName, null, null);
            Cookie cookie2 = new Cookie("fish2", "tune", EnvironmentManager.Instance.UrlBuilder.AlternateHostName, null, null);

            string url1 = EnvironmentManager.Instance.UrlBuilder.WhereIs("");
            string url2 = EnvironmentManager.Instance.UrlBuilder.WhereElseIs("");

            IOptions options = driver.Options();

            await options.Cookies.AddCookie(cookie1);

            await AssertCookieIsPresentWithName(cookie1.Name);

            await driver.GoToUrl(url2);

            await options.Cookies.AddCookie(cookie2);

            await AssertCookieIsNotPresentWithName(cookie1.Name);
            await AssertCookieIsPresentWithName(cookie2.Name);

            await driver.GoToUrl(url1);

            await AssertCookieIsPresentWithName(cookie1.Name);
            await AssertCookieIsNotPresentWithName(cookie2.Name);

            await options.Cookies.DeleteAllCookies();

            await AssertCookieIsNotPresentWithName(cookie1.Name);

            await driver.GoToUrl(url2);

            await AssertCookieIsPresentWithName(cookie2.Name);
        }