Skip to content

Commit 664555a

Browse files
added csharp cookie code (#2049)[deploy site]
1 parent a3018fd commit 664555a

File tree

5 files changed

+137
-457
lines changed

5 files changed

+137
-457
lines changed
+97-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,101 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
using System;
119
using Microsoft.VisualStudio.TestTools.UnitTesting;
20+
using OpenQA.Selenium;
21+
using OpenQA.Selenium.Chrome;
22+
23+
namespace SeleniumDocs.Interactions{
24+
25+
[TestClass]
26+
public class CookiesTest{
27+
28+
WebDriver driver = new ChromeDriver();
29+
30+
[TestMethod]
31+
public void addCookie(){
32+
driver.Url="https://www.selenium.dev/selenium/web/blank.html";
33+
// Add cookie into current browser context
34+
driver.Manage().Cookies.AddCookie(new Cookie("key", "value"));
35+
driver.Quit();
36+
}
37+
38+
[TestMethod]
39+
public void getNamedCookie(){
40+
driver.Url = "https://www.selenium.dev/selenium/web/blank.html";
41+
// Add cookie into current browser context
42+
driver.Manage().Cookies.AddCookie(new Cookie("foo", "bar"));
43+
// Get cookie details with named cookie 'foo'
44+
Cookie cookie = driver.Manage().Cookies.GetCookieNamed("foo");
45+
Assert.AreEqual(cookie.Value, "bar");
46+
driver.Quit();
47+
}
48+
49+
[TestMethod]
50+
public void getAllCookies(){
51+
driver.Url = "https://www.selenium.dev/selenium/web/blank.html";
52+
// Add cookies into current browser context
53+
driver.Manage().Cookies.AddCookie(new Cookie("test1", "cookie1"));
54+
driver.Manage().Cookies.AddCookie(new Cookie("test2", "cookie2"));
55+
// Get cookies
56+
var cookies = driver.Manage().Cookies.AllCookies;
57+
foreach (var cookie in cookies){
58+
if (cookie.Name.Equals("test1")){
59+
Assert.AreEqual("cookie1", cookie.Value);
60+
}
61+
if (cookie.Name.Equals("test2")){
62+
Assert.AreEqual("cookie2", cookie.Value);
63+
}
64+
}
65+
driver.Quit();
66+
}
67+
68+
[TestMethod]
69+
public void deleteCookieNamed(){
70+
driver.Url = "https://www.selenium.dev/selenium/web/blank.html";
71+
driver.Manage().Cookies.AddCookie(new Cookie("test1", "cookie1"));
72+
// delete cookie named
73+
driver.Manage().Cookies.DeleteCookieNamed("test1");
74+
driver.Quit();
75+
}
276

3-
namespace SeleniumDocs.Interactions
4-
{
5-
[TestClass]
6-
public class CookiesTest : BaseTest
7-
{
77+
[TestMethod]
78+
public void deleteCookieObject(){
79+
driver.Url = "https://www.selenium.dev/selenium/web/blank.html";
80+
Cookie cookie = new Cookie("test2", "cookie2");
81+
driver.Manage().Cookies.AddCookie(cookie);
82+
/*
83+
Selenium CSharp bindings also provides a way to delete
84+
cookie by passing cookie object of current browsing context
85+
*/
86+
driver.Manage().Cookies.DeleteCookie(cookie);
87+
driver.Quit();
88+
}
89+
90+
[TestMethod]
91+
public void deleteAllCookies(){
92+
driver.Url = "https://www.selenium.dev/selenium/web/blank.html";
93+
// Add cookies into current browser context
94+
driver.Manage().Cookies.AddCookie(new Cookie("test1", "cookie1"));
95+
driver.Manage().Cookies.AddCookie(new Cookie("test2", "cookie2"));
96+
// Delete All cookies
97+
driver.Manage().Cookies.DeleteAllCookies();
98+
driver.Quit();
99+
}
8100
}
9101
}

Diff for: website_and_docs/content/documentation/webdriver/interactions/cookies.en.md

+10-113
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,8 @@ driver.get("http://www.example.com")
4040
# Adds the cookie into current browser context
4141
driver.add_cookie({"name": "key", "value": "value"})
4242
{{< /tab >}}
43-
{{< tab header="CSharp" >}}
44-
using OpenQA.Selenium;
45-
using OpenQA.Selenium.Chrome;
46-
47-
namespace AddCookie {
48-
class AddCookie {
49-
public static void Main(string[] args) {
50-
IWebDriver driver = new ChromeDriver();
51-
try {
52-
// Navigate to Url
53-
driver.Navigate().GoToUrl("https://example.com");
54-
55-
// Adds the cookie into current browser context
56-
driver.Manage().Cookies.AddCookie(new Cookie("key", "value"));
57-
} finally {
58-
driver.Quit();
59-
}
60-
}
61-
}
62-
}
43+
{{< tab header="CSharp" text=true >}}
44+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/CookiesTest.cs#L32-L34" >}}
6345
{{< /tab >}}
6446
{{< tab header="Ruby" >}}
6547
require 'selenium-webdriver'
@@ -118,28 +100,8 @@ driver.add_cookie({"name": "foo", "value": "bar"})
118100
# Get cookie details with named cookie 'foo'
119101
print(driver.get_cookie("foo"))
120102
{{< /tab >}}
121-
{{< tab header="CSharp" >}}
122-
using OpenQA.Selenium;
123-
using OpenQA.Selenium.Chrome;
124-
125-
namespace GetCookieNamed {
126-
class GetCookieNamed {
127-
public static void Main(string[] args) {
128-
IWebDriver driver = new ChromeDriver();
129-
try {
130-
// Navigate to Url
131-
driver.Navigate().GoToUrl("https://example.com");
132-
driver.Manage().Cookies.AddCookie(new Cookie("foo", "bar"));
133-
134-
// Get cookie details with named cookie 'foo'
135-
var cookie = driver.Manage().Cookies.GetCookieNamed("foo");
136-
System.Console.WriteLine(cookie);
137-
} finally {
138-
driver.Quit();
139-
}
140-
}
141-
}
142-
}
103+
{{< tab header="CSharp" text=true >}}
104+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/CookiesTest.cs#L40-L44" >}}
143105
{{< /tab >}}
144106
{{< tab header="Ruby" >}}
145107
require 'selenium-webdriver'
@@ -202,28 +164,8 @@ driver.add_cookie({"name": "test2", "value": "cookie2"})
202164
# Get all available cookies
203165
print(driver.get_cookies())
204166
{{< /tab >}}
205-
{{< tab header="CSharp" >}}
206-
using OpenQA.Selenium;
207-
using OpenQA.Selenium.Chrome;
208-
209-
namespace GetAllCookies {
210-
class GetAllCookies {
211-
public static void Main(string[] args) {
212-
IWebDriver driver = new ChromeDriver();
213-
try {
214-
// Navigate to Url
215-
driver.Navigate().GoToUrl("https://example.com");
216-
driver.Manage().Cookies.AddCookie(new Cookie("test1", "cookie1"));
217-
driver.Manage().Cookies.AddCookie(new Cookie("test2", "cookie2"));
218-
219-
// Get All available cookies
220-
var cookies = driver.Manage().Cookies.AllCookies;
221-
} finally {
222-
driver.Quit();
223-
}
224-
}
225-
}
226-
}
167+
{{< tab header="CSharp" text=true >}}
168+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/CookiesTest.cs#L51-L64" >}}
227169
{{< /tab >}}
228170
{{< tab header="Ruby" >}}
229171
require 'selenium-webdriver'
@@ -286,33 +228,8 @@ driver.add_cookie({"name": "test2", "value": "cookie2"})
286228
# Delete a cookie with name 'test1'
287229
driver.delete_cookie("test1")
288230
{{< /tab >}}
289-
{{< tab header="CSharp" >}}
290-
using OpenQA.Selenium;
291-
using OpenQA.Selenium.Chrome;
292-
293-
namespace DeleteCookie {
294-
class DeleteCookie {
295-
public static void Main(string[] args) {
296-
IWebDriver driver = new ChromeDriver();
297-
try {
298-
// Navigate to Url
299-
driver.Navigate().GoToUrl("https://example.com");
300-
driver.Manage().Cookies.AddCookie(new Cookie("test1", "cookie1"));
301-
var cookie = new Cookie("test2", "cookie2");
302-
driver.Manage().Cookies.AddCookie(cookie);
303-
304-
// delete a cookie with name 'test1'
305-
driver.Manage().Cookies.DeleteCookieNamed("test1");
306-
307-
// Selenium .net bindings also provides a way to delete
308-
// cookie by passing cookie object of current browsing context
309-
driver.Manage().Cookies.DeleteCookie(cookie);
310-
} finally {
311-
driver.Quit();
312-
}
313-
}
314-
}
315-
}
231+
{{< tab header="CSharp" text=true >}}
232+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/CookiesTest.cs#L70-L73" >}}
316233
{{< /tab >}}
317234
{{< tab header="Ruby" >}}
318235
require 'selenium-webdriver'
@@ -378,28 +295,8 @@ driver.add_cookie({"name": "test2", "value": "cookie2"})
378295
# Deletes all cookies
379296
driver.delete_all_cookies()
380297
{{< /tab >}}
381-
{{< tab header="CSharp" >}}
382-
using OpenQA.Selenium;
383-
using OpenQA.Selenium.Chrome;
384-
385-
namespace DeleteAllCookies {
386-
class DeleteAllCookies {
387-
public static void Main(string[] args) {
388-
IWebDriver driver = new ChromeDriver();
389-
try {
390-
// Navigate to Url
391-
driver.Navigate().GoToUrl("https://example.com");
392-
driver.Manage().Cookies.AddCookie(new Cookie("test1", "cookie1"));
393-
driver.Manage().Cookies.AddCookie(new Cookie("test2", "cookie2"));
394-
395-
// deletes all cookies
396-
driver.Manage().Cookies.DeleteAllCookies();
397-
} finally {
398-
driver.Quit();
399-
}
400-
}
401-
}
402-
}
298+
{{< tab header="CSharp" text=true >}}
299+
{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/CookiesTest.cs#L92-L97" >}}
403300
{{< /tab >}}
404301
{{< tab header="Ruby" >}}
405302
require 'selenium-webdriver'

0 commit comments

Comments
 (0)