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 ;
119using 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}
0 commit comments