本文整理汇总了C#中OpenQA.Selenium.Cookie类的典型用法代码示例。如果您正苦于以下问题:C# Cookie类的具体用法?C# Cookie怎么用?C# Cookie使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Cookie类属于OpenQA.Selenium命名空间,在下文中一共展示了Cookie类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GivenIAmLoggedIn
public void GivenIAmLoggedIn()
{
var ticket = new FormsAuthenticationTicket(1, "[email protected]", DateTime.Now, DateTime.Now.AddDays(4), false, String.Empty);
string encrypted = FormsAuthentication.Encrypt(ticket);
var cookie = new Cookie(FormsAuthentication.FormsCookieName, encrypted);
Driver.Manage().Cookies.AddCookie(cookie);
}
示例2: GetAllCookies
public void GetAllCookies()
{
if (!CheckIsOnValidHostNameForCookieTests())
{
return;
}
string key1 = GenerateUniqueKey();
string key2 = GenerateUniqueKey();
AssertCookieIsNotPresentWithName(key1);
AssertCookieIsNotPresentWithName(key2);
ReadOnlyCollection<Cookie> cookies = driver.Manage().Cookies.AllCookies;
int count = cookies.Count;
Cookie one = new Cookie(key1, "value");
Cookie two = new Cookie(key2, "value");
driver.Manage().Cookies.AddCookie(one);
driver.Manage().Cookies.AddCookie(two);
driver.Url = simpleTestPage;
cookies = driver.Manage().Cookies.AllCookies;
Assert.AreEqual(count + 2, cookies.Count);
Assert.IsTrue(cookies.Contains(one));
Assert.IsTrue(cookies.Contains(two));
}
示例3: AddCookiesWithDifferentPathsThatAreRelatedToOurs
public void 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.Manage();
options.Cookies.AddCookie(cookie1);
options.Cookies.AddCookie(cookie2);
UrlBuilder builder = EnvironmentManager.Instance.UrlBuilder;
driver.Url = builder.WhereIs("animals");
ReadOnlyCollection<Cookie> cookies = options.Cookies.AllCookies;
AssertCookieIsPresentWithName(cookie1.Name);
AssertCookieIsPresentWithName(cookie2.Name);
driver.Url = builder.WhereIs("simpleTest.html");
AssertCookieIsNotPresentWithName(cookie1.Name);
}
示例4: CookieIntegrity
public void CookieIntegrity()
{
string url = EnvironmentManager.Instance.UrlBuilder.WhereElseIs("animals");
driver.Url = url;
driver.Manage().DeleteAllCookies();
DateTime time = DateTime.Now.AddDays(1);
Cookie cookie1 = new Cookie("fish", "cod", null, "/common/animals", time);
IOptions options = driver.Manage();
options.AddCookie(cookie1);
ReadOnlyCollection<Cookie> cookies = options.GetCookies();
Cookie retrievedCookie = null;
foreach (Cookie tempCookie in cookies)
{
if (cookie1.Equals(tempCookie))
{
retrievedCookie = tempCookie;
break;
}
}
Assert.IsNotNull(retrievedCookie);
//Cookie.equals only compares name, domain and path
Assert.AreEqual(cookie1, retrievedCookie);
}
示例5: CanSetCookiesOnADifferentPathOfTheSameHost
public void 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.Manage();
ReadOnlyCollection<Cookie> count = options.Cookies.AllCookies;
options.Cookies.AddCookie(cookie1);
options.Cookies.AddCookie(cookie2);
string url = EnvironmentManager.Instance.UrlBuilder.WhereIs("animals");
driver.Url = url;
ReadOnlyCollection<Cookie> cookies = options.Cookies.AllCookies;
Assert.IsTrue(cookies.Contains(cookie1));
Assert.IsFalse(cookies.Contains(cookie2));
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("galaxy");
cookies = options.Cookies.AllCookies;
Assert.IsFalse(cookies.Contains(cookie1));
Assert.IsTrue(cookies.Contains(cookie2));
}
示例6: ShouldThrowExceptionWhenAddingCookieToAPageThatIsNotHtml
public void ShouldThrowExceptionWhenAddingCookieToAPageThatIsNotHtml()
{
driver.Url = textPage;
Cookie cookie = new Cookie("hello", "goodbye");
driver.Manage().Cookies.AddCookie(cookie);
}
示例7: AddCookieTest
public void AddCookieTest()
{
HomePage.WebDriver.Manage().Cookies.DeleteAllCookies();
new Check().IsTrue(HomePage.WebDriver.Manage().Cookies.AllCookies.Count == 0);
Cookie cookie = new Cookie("key", "value");
ContactFormPage.AddCookie(cookie);
new Check().AreEquals(HomePage.WebDriver.Manage().Cookies.GetCookieNamed(cookie.Name).Value, cookie.Value);
}
示例8: ClearCacheTest
public void ClearCacheTest()
{
Cookie cookie = new Cookie("key", "value");
HomePage.WebDriver.Manage().Cookies.AddCookie(cookie);
new Check().IsFalse(HomePage.WebDriver.Manage().Cookies.AllCookies.Count == 0);
ContactFormPage.ClearCache();
new Check().IsTrue(HomePage.WebDriver.Manage().Cookies.AllCookies.Count == 0);
}
示例9: ShouldBeAbleToAddCookie
public void ShouldBeAbleToAddCookie()
{
if (!CheckIsOnValidHostNameForCookieTests())
{
return;
}
string key = GenerateUniqueKey();
string value = "foo";
Cookie cookie = new Cookie(key, value);
AssertCookieIsNotPresentWithName(key);
driver.Manage().Cookies.AddCookie(cookie);
AssertCookieHasValue(key, value);
Assert.That(driver.Manage().Cookies.AllCookies.Contains(cookie), "Cookie was not added successfully");
}
示例10: HandleSeleneseCommand
/// <summary>
/// Handles the command.
/// </summary>
/// <param name="driver">The driver used to execute the command.</param>
/// <param name="locator">The first parameter to the command.</param>
/// <param name="value">The second parameter to the command.</param>
/// <returns>The result of the command.</returns>
protected override object HandleSeleneseCommand(IWebDriver driver, string locator, string value)
{
if (!this.NameValuePairRegex.IsMatch(locator))
{
throw new SeleniumException("Invalid parameter: " + locator);
}
Match nameValueMatch = this.NameValuePairRegex.Match(locator);
string cookieName = nameValueMatch.Groups[1].Value;
string cookieValue = nameValueMatch.Groups[2].Value;
DateTime? maxAge = null;
if (this.MaxAgeRegex.IsMatch(value))
{
Match maxAgeMatch = this.MaxAgeRegex.Match(value);
maxAge = DateTime.Now.AddSeconds(int.Parse(maxAgeMatch.Groups[1].Value, CultureInfo.InvariantCulture));
}
string path = string.Empty;
if (this.PathRegex.IsMatch(value))
{
Match pathMatch = this.PathRegex.Match(value);
path = pathMatch.Groups[1].Value;
try
{
if (path.StartsWith("http", StringComparison.Ordinal))
{
path = new Uri(path).AbsolutePath;
}
}
catch (UriFormatException)
{
// Fine.
}
}
else
{
path = new Uri(driver.Url).AbsolutePath;
}
Cookie cookie = new Cookie(cookieName, cookieValue, path, maxAge);
driver.Manage().Cookies.AddCookie(cookie);
return null;
}
示例11: AddCookiesWithDifferentPathsThatAreRelatedToOurs
public void AddCookiesWithDifferentPathsThatAreRelatedToOurs()
{
string basePath = EnvironmentManager.Instance.UrlBuilder.Path;
Cookie cookie1 = new Cookie("fish", "cod", "/" + basePath + "/animals");
Cookie cookie2 = new Cookie("planet", "earth", "/" + basePath + "/");
IOptions options = driver.Manage();
options.AddCookie(cookie1);
options.AddCookie(cookie2);
UrlBuilder builder = EnvironmentManager.Instance.UrlBuilder;
driver.Url = builder.WhereIs("animals");
ReadOnlyCollection<Cookie> cookies = options.GetCookies();
Assert.IsTrue(cookies.Contains(cookie1));
Assert.IsTrue(cookies.Contains(cookie2));
driver.Url = builder.WhereIs("");
cookies = options.GetCookies();
Assert.IsFalse(cookies.Contains(cookie1));
Assert.IsTrue(cookies.Contains(cookie2));
}
示例12: ShouldNotShowCookieAddedToDifferentPath
public void 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.");
}
driver.Url = macbethPage;
IOptions options = driver.Manage();
Cookie cookie = new Cookie("Lisa", "Simpson", EnvironmentManager.Instance.UrlBuilder.HostName, "/" + EnvironmentManager.Instance.UrlBuilder.Path + "IDoNotExist", null);
options.Cookies.AddCookie(cookie);
ReadOnlyCollection<Cookie> cookies = options.Cookies.AllCookies;
Assert.IsFalse(cookies.Contains(cookie), "Invalid cookie was returned");
}
示例13: ShouldRetainCookieExpiry
public void ShouldRetainCookieExpiry()
{
if (!CheckIsOnValidHostNameForCookieTests())
{
return;
}
string url = EnvironmentManager.Instance.UrlBuilder.WhereElseIs("animals");
driver.Url = url;
driver.Manage().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.Manage();
options.Cookies.AddCookie(addCookie);
Cookie retrieved = options.Cookies.GetCookieNamed("fish");
Assert.IsNotNull(retrieved);
Assert.AreEqual(addCookie.Expiry, retrieved.Expiry, "Cookies are not equal");
}
示例14: ShouldNotGetCookiesRelatedToCurrentDomainWithoutLeadingPeriod
public void ShouldNotGetCookiesRelatedToCurrentDomainWithoutLeadingPeriod()
{
if (!CheckIsOnValidHostNameForCookieTests())
{
return;
}
string cookieName = "name";
AssertCookieIsNotPresentWithName(cookieName);
Regex replaceRegex = new Regex(".*?\\.");
string shorter = replaceRegex.Replace(this.hostname, ".", 1);
Cookie cookie = new Cookie(cookieName, "value", shorter, "/", GetTimeInTheFuture());
driver.Manage().Cookies.AddCookie(cookie);
AssertCookieIsNotPresentWithName(cookieName);
}
示例15: ShouldNotShowCookieAddedToDifferentDomain
public void ShouldNotShowCookieAddedToDifferentDomain()
{
if (!CheckIsOnValidHostNameForCookieTests())
{
Assert.Ignore("Not on a standard domain for cookies (localhost doesn't count).");
}
driver.Url = macbethPage;
IOptions options = driver.Manage();
Cookie cookie = new Cookie("Bart", "Simpson", EnvironmentManager.Instance.UrlBuilder.HostName + ".com", EnvironmentManager.Instance.UrlBuilder.Path, null);
Assert.Throws<WebDriverException>(() => options.Cookies.AddCookie(cookie));
ReadOnlyCollection<Cookie> cookies = options.Cookies.AllCookies;
Assert.IsFalse(cookies.Contains(cookie), "Invalid cookie was returned");
}