88import org .openqa .selenium .remote .RemoteWebDriver ;
99
1010import org .openqa .selenium .By ;
11+ import org .openqa .selenium .Cookie ;
1112import org .openqa .selenium .WebElement ;
1213import org .openqa .selenium .chrome .ChromeOptions ;
1314
1415import java .io .File ;
1516import java .net .MalformedURLException ;
1617import java .net .URL ;
18+ import java .util .Set ;
1719
18- public class ElementBySearch {
20+ public class Cookies {
1921 String username = System .getenv ("LT_USERNAME" ) == null ? "Your LT Username" : System .getenv ("LT_USERNAME" );
2022 String accessKey = System .getenv ("LT_ACCESS_KEY" ) == null ? "Your LT AccessKey" : System .getenv ("LT_ACCESS_KEY" );
2123 public static RemoteWebDriver driver = null ;
@@ -30,7 +32,7 @@ public void setUp() throws Exception {
3032 capabilities .setCapability ("platform" , "Windows 10" ); // If this cap isn't specified, it will just get the any
3133 // available one
3234 capabilities .setCapability ("build" , "Junit Testing Example" );
33- capabilities .setCapability ("name" , "ElementBySearch Test" );
35+ capabilities .setCapability ("name" , "Cookies Test" );
3436 capabilities .setCapability ("plugin" , "git-junit" );
3537
3638 try {
@@ -49,24 +51,21 @@ public void testSimple() throws Exception {
4951 System .out .println ("Loading Url" );
5052
5153 driver .get ("https://lambdatest.github.io/sample-todo-app/" );
52- /*
53- * text(): A built-in method in Selenium WebDriver that is used with XPath
54- * locator to locate an element based on its exact text value.
55- * Example: //*[ text() = ‘5 of 5 remaining’ ]
56- * contains(): Similar to the text() method, contains() is another built-in
57- * method used to locate an element based on partial text match.
58- * For example, if we need to locate a label that has “5 of 5 remaining” as its
59- * text, it can be located using the following line of code with Xpath.
60- * Example: //*[ contains (text(), ‘5 of 5’ ) ]
61- */
62-
63- // Locating element with text()
64- WebElement e = driver .findElement (By .xpath ("//*[text()='5 of 5 remaining']" ));
65- System .out .println (e .getText ());
66-
67- // located element with contains()
68- WebElement m = driver .findElement (By .xpath ("//*[contains(text(),'5 of 5')]" ));
69- System .out .println (m .getText ());
54+
55+ driver .manage ().addCookie (new Cookie ("cookieName" , "lambdatest" )); // Creates and adds the cookie
56+
57+ Set <Cookie > cookiesSet = driver .manage ().getCookies (); // Returns the List of all Cookies
58+
59+ for (Cookie itemCookie : cookiesSet ) {
60+ System .out .println ((itemCookie .getName () + ";" + itemCookie .getValue () + ";" + itemCookie .getDomain () + ";"
61+ + itemCookie .getPath () + ";" + itemCookie .getExpiry () + ";" + itemCookie .isSecure ()));
62+ }
63+
64+ driver .manage ().getCookieNamed ("cookieName" ); // Returns the specific cookie according to name
65+
66+ driver .manage ().deleteCookie (driver .manage ().getCookieNamed ("cookieName" )); // Deletes the specific cookie
67+ driver .manage ().deleteCookieNamed ("cookieName" ); // Deletes the specific cookie according to the Name
68+ driver .manage ().deleteAllCookies (); // Deletes all the cookies
7069
7170 System .out .println ("Checking Box" );
7271 driver .findElement (By .name ("li1" )).click ();
0 commit comments