Skip to content

Commit 38dc3ce

Browse files
committed
Cookies handling selenium selenium4 java
0 parents  commit 38dc3ce

File tree

5 files changed

+243
-0
lines changed

5 files changed

+243
-0
lines changed

.gitignore

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Compiled class file
2+
*.class
3+
/target/
4+
.vscode/
5+
6+
# Log file
7+
*.log
8+
9+
# BlueJ files
10+
*.ctxt
11+
12+
# Mobile Tools for Java (J2ME)
13+
.mtj.tmp/
14+
15+
# Package Files #
16+
*.jar
17+
*.war
18+
*.nar
19+
*.ear
20+
*.zip
21+
*.tar.gz
22+
*.rar
23+
24+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
25+
hs_err_pid*

.gitpod.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# List the start up tasks. Learn more https://www.gitpod.io/docs/config-start-tasks/
2+
tasks:
3+
- init: echo 'init script' # runs during prebuild
4+
command: mvn clean install exec:java -Dexec.mainClass="com.lambdatest.Cookies" -Dexec.classpathScope=test -e

README.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Java-Selenium-Sample
2+
![Java](https://www.lambdatest.com/support/assets/images/og-images/Java-with-Selenium-1-1.jpg)
3+
4+
### Prerequisites
5+
1. Install and set environment variable for java.
6+
* Windows - https://www.oracle.com/java/technologies/downloads/
7+
* Linux - ``` sudo apt-get install openjdk-8-jre ```
8+
* MacOS - Java should already be present on Mac OS X by default.
9+
2 Install and set environment varibale for Maven.
10+
* Windows - https://maven.apache.org/install.html
11+
* Linux/ MacOS - [Homebrew](http://brew.sh/) (Easier)
12+
```
13+
install maven
14+
```
15+
16+
### Run your First Test
17+
1. Clone the Java-Selenium-Sample repository.
18+
```
19+
git clone https://github.com/LambdaTest/java-selenium-sample.git
20+
```
21+
2. Next get into Java-Selenium-Sample folder, and import Lamabdatest Credentials. You can get these from lambdatest automation dashboard.
22+
<p align="center">
23+
<b>For Linux/macOS:</b>:
24+
25+
```
26+
export LT_USERNAME="YOUR_USERNAME"
27+
export LT_ACCESS_KEY="YOUR ACCESS KEY"
28+
```
29+
<p align="center">
30+
<b>For Windows:</b>
31+
32+
```
33+
set LT_USERNAME="YOUR_USERNAME"
34+
set LT_ACCESS_KEY="YOUR ACCESS KEY"
35+
```
36+
Step 3. You may also want to run the command below to check for outdated dependencies. Please be sure to verify and review updates before editing your pom.xml file as they may not be compatible with your code.
37+
```
38+
mvn versions:display-dependency-updates
39+
```
40+
Step 4. Run single test.
41+
```
42+
mvn clean install exec:java -Dexec.mainClass="com.lambdatest.Cookies" -Dexec.classpathScope=test -e
43+
```
44+
45+
## About LambdaTest
46+
47+
[LambdaTest](https://www.lambdatest.com/) is a cloud based selenium grid infrastructure that can help you run automated cross browser compatibility tests on 2000+ different browser and operating system environments. LambdaTest supports all programming languages and frameworks that are supported with Selenium, and have easy integrations with all popular CI/CD platforms. It's a perfect solution to bring your [selenium automation testing](https://www.lambdatest.com/selenium-automation) to cloud based infrastructure that not only helps you increase your test coverage over multiple desktop and mobile browsers, but also allows you to cut down your test execution time by running tests on parallel.
48+

pom.xml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>java-selenium-sample</groupId>
4+
<artifactId>java-selenium-sample</artifactId>
5+
<version>0.0.1</version>
6+
<build>
7+
<sourceDirectory>src/test</sourceDirectory>
8+
<resources>
9+
<resource>
10+
<directory>src/test</directory>
11+
<excludes>
12+
<exclude>**/*.java</exclude>
13+
</excludes>
14+
</resource>
15+
</resources>
16+
<plugins>
17+
<plugin>
18+
<artifactId>maven-compiler-plugin</artifactId>
19+
<version>3.7.0</version>
20+
<configuration>
21+
<release>10</release>
22+
</configuration>
23+
</plugin>
24+
<plugin>
25+
<groupId>org.apache.maven.plugins</groupId>
26+
<artifactId>maven-surefire-plugin</artifactId>
27+
<version>3.0.0-M5</version>
28+
</plugin>
29+
</plugins>
30+
</build>
31+
<dependencies>
32+
<dependency>
33+
<groupId>org.seleniumhq.selenium</groupId>
34+
<artifactId>selenium-java</artifactId>
35+
<version>4.1.0</version>
36+
</dependency>
37+
38+
</dependencies>
39+
40+
</project>

src/test/com/lambdatest/Cookies.java

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package com.lambdatest;
2+
3+
import java.net.MalformedURLException;
4+
import java.net.URL;
5+
import java.util.Set;
6+
7+
import org.openqa.selenium.By;
8+
import org.openqa.selenium.Cookie;
9+
import org.openqa.selenium.remote.DesiredCapabilities;
10+
import org.openqa.selenium.remote.RemoteWebDriver;
11+
12+
public class Cookies {
13+
14+
private RemoteWebDriver driver;
15+
private String Status = "failed";
16+
17+
18+
public void setup() throws MalformedURLException {
19+
String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME");
20+
String authkey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY");
21+
;
22+
String hub = "@hub.lambdatest.com/wd/hub";
23+
24+
DesiredCapabilities caps = new DesiredCapabilities();
25+
caps.setCapability("platform", "Windows 10");
26+
caps.setCapability("browserName", "Chrome");
27+
caps.setCapability("version", "latest");
28+
caps.setCapability("build", "Cookies Test");
29+
caps.setCapability("name", this.getClass().getName());
30+
caps.setCapability("plugin", "git-testng");
31+
32+
String[] Tags = new String[] { "Feature", "Falcon", "Severe" };
33+
caps.setCapability("tags", Tags);
34+
35+
driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + hub), caps);
36+
37+
}
38+
39+
public void cookiesTest() throws InterruptedException {
40+
41+
String spanText;
42+
System.out.println("Loading Url");
43+
44+
driver.get("https://lambdatest.github.io/sample-todo-app/");
45+
46+
driver.manage().addCookie(new Cookie("cookieName", "lambdatest")); // Creates and adds the cookie
47+
48+
Set<Cookie> cookiesSet = driver.manage().getCookies(); // Returns the List of all Cookies
49+
50+
for (Cookie itemCookie : cookiesSet) {
51+
System.out.println((itemCookie.getName() + ";" + itemCookie.getValue() + ";" + itemCookie.getDomain() + ";"
52+
+ itemCookie.getPath() + ";" + itemCookie.getExpiry() + ";" + itemCookie.isSecure()));
53+
}
54+
55+
driver.manage().getCookieNamed("cookieName"); // Returns the specific cookie according to name
56+
57+
driver.manage().deleteCookie(driver.manage().getCookieNamed("cookieName")); // Deletes the specific cookie
58+
driver.manage().deleteCookieNamed("cookieName"); // Deletes the specific cookie according to the Name
59+
driver.manage().deleteAllCookies(); // Deletes all the cookies
60+
61+
System.out.println("Checking Box");
62+
driver.findElement(By.name("li1")).click();
63+
64+
System.out.println("Checking Another Box");
65+
driver.findElement(By.name("li2")).click();
66+
67+
System.out.println("Checking Box");
68+
driver.findElement(By.name("li3")).click();
69+
70+
System.out.println("Checking Another Box");
71+
driver.findElement(By.name("li4")).click();
72+
73+
driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 6");
74+
driver.findElement(By.id("addbutton")).click();
75+
76+
driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 7");
77+
driver.findElement(By.id("addbutton")).click();
78+
79+
driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 8");
80+
driver.findElement(By.id("addbutton")).click();
81+
82+
System.out.println("Checking Another Box");
83+
driver.findElement(By.name("li1")).click();
84+
85+
System.out.println("Checking Another Box");
86+
driver.findElement(By.name("li3")).click();
87+
88+
System.out.println("Checking Another Box");
89+
driver.findElement(By.name("li7")).click();
90+
91+
System.out.println("Checking Another Box");
92+
driver.findElement(By.name("li8")).click();
93+
Thread.sleep(300);
94+
95+
System.out.println("Entering Text");
96+
driver.findElement(By.id("sampletodotext")).sendKeys("Get Taste of Lambda and Stick to It");
97+
98+
driver.findElement(By.id("addbutton")).click();
99+
100+
System.out.println("Checking Another Box");
101+
driver.findElement(By.name("li9")).click();
102+
103+
// Let's also assert that the todo we added is present in the list.
104+
105+
spanText = driver.findElement(By.xpath("/html/body/div/div/div/ul/li[9]/span")).getText();
106+
"Get Taste of Lambda and Stick to It".equalsIgnoreCase(spanText);
107+
Status = "passed";
108+
Thread.sleep(150);
109+
110+
System.out.println("TestFinished");
111+
112+
}
113+
114+
public void tearDown() {
115+
driver.executeScript("lambda-status=" + Status);
116+
driver.quit();
117+
}
118+
119+
public static void main(String[] args) throws MalformedURLException, InterruptedException {
120+
Cookies test = new Cookies();
121+
test.setup();
122+
test.cookiesTest();
123+
test.tearDown();
124+
}
125+
126+
}

0 commit comments

Comments
 (0)