Skip to content

Commit 902e7cf

Browse files
committed
headless testing junit selenium java lambdatest
1 parent 512b767 commit 902e7cf

File tree

4 files changed

+148
-68
lines changed

4 files changed

+148
-68
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Run Selenium Tests With JUnit On LambdaTest (Timezone Testing Example)
1+
# Run Selenium Tests With JUnit On LambdaTest (Headless Testing Example)
22

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

@@ -102,8 +102,22 @@ DesiredCapabilities capabilities = new DesiredCapabilities();
102102
capabilities.setCapability("name", "GeoLocation Test");
103103
capabilities.setCapability("plugin", "git-junit");
104104

105-
capabilities.setCapability("timezone", "UTC+03:00"); // Timezone capability to set the timezone
105+
// Chrome flag for headless using chrome options
106+
ChromeOptions options = new ChromeOptions();
106107

108+
options.addArguments("--no-sandbox");
109+
options.addArguments("--headless"); // headless flag for chrome
110+
options.addArguments("disable-gpu");
111+
112+
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
113+
114+
/*
115+
* At lambdatest you can execute headless tests via `headless` capability on
116+
* chrome, firefox and microsoft edge browser
117+
*/
118+
119+
// lambdatest Headless capability
120+
// caps.setCapability("headless",true);
107121
```
108122

109123
You can generate capabilities for your test requirements with the help of our inbuilt [Desired Capability Generator](https://www.lambdatest.com/capabilities-generator/).

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/Timezone.java</include>
95+
<include>com/lambdatest/Headless.java</include>
9696
</includes>
9797
</configuration>
9898
</plugin>
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.lambdatest;
2+
3+
import org.junit.After;
4+
import org.junit.Assert;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.openqa.selenium.remote.DesiredCapabilities;
8+
import org.openqa.selenium.remote.RemoteWebDriver;
9+
10+
import org.openqa.selenium.By;
11+
import org.openqa.selenium.chrome.ChromeOptions;
12+
13+
import java.net.MalformedURLException;
14+
import java.net.URL;
15+
16+
public class Headless {
17+
String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME");
18+
String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY");
19+
public static RemoteWebDriver driver = null;
20+
public String gridURL = "@hub.lambdatest.com/wd/hub";
21+
public String status = "failed";
22+
23+
@Before
24+
public void setUp() throws Exception {
25+
DesiredCapabilities capabilities = new DesiredCapabilities();
26+
capabilities.setCapability("browserName", "chrome");
27+
capabilities.setCapability("version", "latest");
28+
capabilities.setCapability("platform", "Windows 10"); // If this cap isn't specified, it will just get the any
29+
// available one
30+
capabilities.setCapability("build", "Junit Testing Example");
31+
capabilities.setCapability("name", "Headless Test");
32+
capabilities.setCapability("plugin", "git-junit");
33+
34+
// Chrome flag for headless using chrome options
35+
ChromeOptions options = new ChromeOptions();
36+
37+
options.addArguments("--no-sandbox");
38+
options.addArguments("--headless"); // headless flag for chrome
39+
options.addArguments("disable-gpu");
40+
41+
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
42+
43+
/*
44+
* At lambdatest you can execute headless tests via `headless` capability on
45+
* chrome, firefox and microsoft edge browser
46+
*/
47+
48+
// lambdatest Headless capability
49+
// caps.setCapability("headless",true);
50+
51+
try {
52+
driver = new RemoteWebDriver(new URL("https://" + username + ":" + accessKey + gridURL), capabilities);
53+
} catch (MalformedURLException e) {
54+
System.out.println("Invalid grid URL");
55+
} catch (Exception e) {
56+
System.out.println(e.getMessage());
57+
}
58+
}
59+
60+
@Test
61+
public void testSimple() throws Exception {
62+
try {
63+
String spanText;
64+
System.out.println("Loading Url");
65+
66+
driver.get("https://lambdatest.github.io/sample-todo-app/");
67+
68+
System.out.println("Checking Box");
69+
driver.findElement(By.name("li1")).click();
70+
71+
System.out.println("Checking Another Box");
72+
driver.findElement(By.name("li2")).click();
73+
74+
System.out.println("Checking Box");
75+
driver.findElement(By.name("li3")).click();
76+
77+
System.out.println("Checking Another Box");
78+
driver.findElement(By.name("li4")).click();
79+
80+
driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 6");
81+
driver.findElement(By.id("addbutton")).click();
82+
83+
driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 7");
84+
driver.findElement(By.id("addbutton")).click();
85+
86+
driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 8");
87+
driver.findElement(By.id("addbutton")).click();
88+
89+
System.out.println("Checking Another Box");
90+
driver.findElement(By.name("li1")).click();
91+
92+
System.out.println("Checking Another Box");
93+
driver.findElement(By.name("li3")).click();
94+
95+
System.out.println("Checking Another Box");
96+
driver.findElement(By.name("li7")).click();
97+
98+
System.out.println("Checking Another Box");
99+
driver.findElement(By.name("li8")).click();
100+
Thread.sleep(300);
101+
102+
System.out.println("Entering Text");
103+
driver.findElement(By.id("sampletodotext")).sendKeys("Get Taste of Lambda and Stick to It");
104+
105+
driver.findElement(By.id("addbutton")).click();
106+
107+
System.out.println("Checking Another Box");
108+
driver.findElement(By.name("li9")).click();
109+
110+
// Let's also assert that the todo we added is present in the list.
111+
112+
spanText = driver.findElementByXPath("/html/body/div/div/div/ul/li[9]/span").getText();
113+
Assert.assertEquals("Get Taste of Lambda and Stick to It", spanText);
114+
status = "passed";
115+
Thread.sleep(150);
116+
117+
System.out.println("TestFinished");
118+
119+
} catch (Exception e) {
120+
System.out.println(e.getMessage());
121+
}
122+
}
123+
124+
@After
125+
public void tearDown() throws Exception {
126+
if (driver != null) {
127+
driver.executeScript("lambda-status=" + status);
128+
driver.quit();
129+
}
130+
}
131+
}

src/test/java/com/lambdatest/Timezone.java

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)