8
8
import org .openqa .selenium .remote .RemoteWebDriver ;
9
9
10
10
import org .openqa .selenium .By ;
11
+ import org .openqa .selenium .Cookie ;
11
12
import org .openqa .selenium .WebElement ;
12
13
import org .openqa .selenium .chrome .ChromeOptions ;
13
14
14
15
import java .io .File ;
15
16
import java .net .MalformedURLException ;
16
17
import java .net .URL ;
18
+ import java .util .Set ;
17
19
18
- public class ElementBySearch {
20
+ public class Cookies {
19
21
String username = System .getenv ("LT_USERNAME" ) == null ? "Your LT Username" : System .getenv ("LT_USERNAME" );
20
22
String accessKey = System .getenv ("LT_ACCESS_KEY" ) == null ? "Your LT AccessKey" : System .getenv ("LT_ACCESS_KEY" );
21
23
public static RemoteWebDriver driver = null ;
@@ -30,7 +32,7 @@ public void setUp() throws Exception {
30
32
capabilities .setCapability ("platform" , "Windows 10" ); // If this cap isn't specified, it will just get the any
31
33
// available one
32
34
capabilities .setCapability ("build" , "Junit Testing Example" );
33
- capabilities .setCapability ("name" , "ElementBySearch Test" );
35
+ capabilities .setCapability ("name" , "Cookies Test" );
34
36
capabilities .setCapability ("plugin" , "git-junit" );
35
37
36
38
try {
@@ -49,24 +51,21 @@ public void testSimple() throws Exception {
49
51
System .out .println ("Loading Url" );
50
52
51
53
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
70
69
71
70
System .out .println ("Checking Box" );
72
71
driver .findElement (By .name ("li1" )).click ();
0 commit comments