Skip to content

Commit 81ce7b7

Browse files
committed
cookies handling selenium junit java
1 parent 327bc4c commit 81ce7b7

File tree

3 files changed

+36
-29
lines changed

3 files changed

+36
-29
lines changed

README.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Run Selenium Tests With JUnit On LambdaTest (Locating element with text and partial text Example)
1+
# Run Selenium Tests With JUnit On LambdaTest (Cookies Handling Example)
22

33
![image](https://user-images.githubusercontent.com/70570645/171432631-dcc31b10-6590-4877-98c0-4ac702fbd441.png)
44

@@ -102,18 +102,26 @@ DesiredCapabilities capabilities = new DesiredCapabilities();
102102
capabilities.setCapability("name", "GeoLocation Test");
103103
capabilities.setCapability("plugin", "git-junit");
104104
```
105-
### Locating element with text and partial text testng java
105+
### Handling cookies in java Junit
106106
```java
107-
// Locating element with text()
108-
WebElement e = driver.findElement(By.xpath("//*[text()='5 of 5 remaining']"));
109-
System.out.println(e.getText());
107+
driver.manage().addCookie(new Cookie("cookieName", "lambdatest")); // Creates and adds the cookie
110108

111-
// located element with contains()
112-
WebElement m = driver.findElement(By.xpath("//*[contains(text(),'5 of 5')]"));
113-
System.out.println(m.getText());
109+
Set<Cookie> cookiesSet = driver.manage().getCookies(); // Returns the List of all Cookies
110+
111+
for (Cookie itemCookie : cookiesSet) {
112+
System.out.println((itemCookie.getName() + ";" + itemCookie.getValue() + ";" + itemCookie.getDomain() + ";"
113+
+ itemCookie.getPath() + ";" + itemCookie.getExpiry() + ";" + itemCookie.isSecure()));
114+
}
115+
116+
driver.manage().getCookieNamed("cookieName"); // Returns the specific cookie according to name
117+
118+
driver.manage().deleteCookie(driver.manage().getCookieNamed("cookieName")); // Deletes the specific cookie
119+
driver.manage().deleteCookieNamed("cookieName"); // Deletes the specific cookie according to the Name
120+
driver.manage().deleteAllCookies(); // Deletes all the cookies
114121
```
115122

116123

124+
117125
### Upload extension on to Lambda Storage and use in automation tests for Large extensions
118126
- To upload the extension.zip file onto the lambda storage, you can use the extensions API we have :
119127
- Apid Doc URL: https://www.lambdatest.com/support/docs/api-doc/#/extensions/UploadExtensions

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
<artifactId>maven-surefire-plugin</artifactId>
9393
<configuration>
9494
<includes>
95-
<include>com/lambdatest/ElementBySearch.java</include>
95+
<include>com/lambdatest/Cookies.java</include>
9696
</includes>
9797
</configuration>
9898
</plugin>

src/test/java/com/lambdatest/ElementBySearch.java renamed to src/test/java/com/lambdatest/Cookies.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
import org.openqa.selenium.remote.RemoteWebDriver;
99

1010
import org.openqa.selenium.By;
11+
import org.openqa.selenium.Cookie;
1112
import org.openqa.selenium.WebElement;
1213
import org.openqa.selenium.chrome.ChromeOptions;
1314

1415
import java.io.File;
1516
import java.net.MalformedURLException;
1617
import 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

Comments
 (0)