Skip to content

Commit 2e0c2ab

Browse files
authored
Merge pull request #6 from 4msha/new
Readme+screenshot
2 parents b330c2e + 4916a7e commit 2e0c2ab

File tree

2 files changed

+39
-284
lines changed

2 files changed

+39
-284
lines changed

README.md

Lines changed: 39 additions & 284 deletions
Original file line numberDiff line numberDiff line change
@@ -1,317 +1,72 @@
1-
# JUnit-Tutorial
2-
![LambdaTest Logo](https://www.lambdatest.com/static/images/logo.svg)
1+
# JUnit Selenium tutorial
32

4-
![Junit Logo](https://www.lambdatest.com/blog/wp-content/uploads/2020/08/junit.png)
3+
![MSTest](https://www.lambdatest.com/resources/images/meta/With-Junit.jpg)
54

6-
This post will help you in getting started with configuring and running your JUnit automation testing scripts on LambdaTest cloud Selenium grid platform.
5+
## Prerequisites
76

8-
## Prerequisite To Perform JUnit Automation Testing
97
To run your test script using JUnit with Selenium, first you need to setup the environment.
108

11-
1)Install JDK 1.6 or higher version
12-
2)Latest Selenium client and it’s WebDriver Bindings.
13-
3) It would be very beneficial if you use code project management options like Maven or Ant. Maven supports JUnit out of the box. You would just have to define Selenium dependencies in it’s project object model file or pom.xml file.
14-
4)To test your locally or privately hosted files, you need LambdaTest Tunnel binary file.
9+
1. Install JDK 1.6 or higher version
10+
2. Latest Selenium client and it’s WebDriver Bindings.
11+
3. Download Maven([Windows](https://maven.apache.org/download.cgi), [Linux](https://maven.apache.org/install.html), and [Mac](https://maven.apache.org/install.html)) or Ant. Maven supports JUnit out of the box. You would just have to define Selenium dependencies in it’s project object model file or pom.xml file.
12+
4. Optional - To test your locally or privately hosted files, you need LambdaTest Tunnel binary file.
1513

16-
## Maven Dependency
17-
```
18-
<dependency>
19-
<groupId>junit</groupId>
20-
<artifactId>junit</artifactId>
21-
<version>4.12</version>
22-
<scope>test</scope>
23-
</dependency>
24-
25-
```
26-
## Your First Test With JUnit Framework
27-
28-
Now let’s start with a simple Selenium WebDriver test. Below is a Selenium script for JUnit automation testing that will open a sample to-do application which will do following task:
14+
## Steps to Run your First Test
2915

30-
* Mark first two items as mark done.
31-
* Add a new item in the list.
32-
* Return the added item.
33-
34-
To run your first test using JUnit with Selenium, follow the simple example below. Same code can be downloaded from our repository: Java-JUnit-Selenium
35-
36-
**JUnit Todo : Sample App**
16+
Step 1. Clone the Junit-Selenium-Sample Repository.
3717

3818
```
39-
import org.junit.After;
40-
import org.junit.Before;
41-
import org.junit.Test;
42-
import org.junit.runner.RunWith;
43-
import org.openqa.selenium.JavascriptExecutor;
44-
import org.openqa.selenium.WebDriver;
45-
import org.openqa.selenium.remote.CapabilityType;
46-
import org.openqa.selenium.remote.DesiredCapabilities;
47-
import org.openqa.selenium.remote.RemoteWebDriver;
48-
import java.net.URL;
49-
public class JUnitTodo {
50-
public String username = "YOUR_USERNAME";
51-
public String accesskey = "YOUR_ACCESS_KEY";
52-
public static RemoteWebDriver driver = null;
53-
public String gridURL = "@hub.lambdatest.com/wd/hub";
54-
boolean status = false;
55-
@Before
56-
public void setUp() throws Exception {
57-
DesiredCapabilities capabilities = new DesiredCapabilities();
58-
capabilities.setCapability("browserName", "chrome");
59-
capabilities.setCapability("version", "70.0");
60-
capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one
61-
capabilities.setCapability("build", "LambdaTestSampleApp");
62-
capabilities.setCapability("name", "LambdaTestJavaSample");
63-
capabilities.setCapability("network", true); // To enable network logs
64-
capabilities.setCapability("visual", true); // To enable step by step screenshot
65-
capabilities.setCapability("video", true); // To enable video recording
66-
capabilities.setCapability("console", true); // To capture console logs
67-
try {
68-
driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
69-
} catch (MalformedURLException e) {
70-
System.out.println("Invalid grid URL");
71-
} catch (Exception e) {
72-
System.out.println(e.getMessage());
73-
}
74-
}
75-
76-
@Test
77-
public void testSimple() throws Exception {
78-
try {
79-
//Change it to production page
80-
driver.get("https://lambdatest.github.io/sample-todo-app/");
81-
82-
//Let's mark done first two items in the list.
83-
driver.findElement(By.name("li1")).click();
84-
driver.findElement(By.name("li2")).click();
85-
86-
// Let's add an item in the list.
87-
driver.findElement(By.id("sampletodotext")).sendKeys("Yey, Let's add it to list");
88-
driver.findElement(By.id("addbutton")).click();
89-
90-
// Let's check that the item we added is added in the list.
91-
String enteredText = driver.findElementByXPath("/html/body/div/div/div/ul/li[6]/span").getText();
92-
if (enteredText.equals("Yey, Let's add it to list")) {
93-
status = true;
94-
}
95-
} catch (Exception e) {
96-
System.out.println(e.getMessage());
97-
}
98-
}
99-
@After
100-
public void tearDown() throws Exception {
101-
if (driver != null) {
102-
((JavascriptExecutor) driver).executeScript("lambda-status=" + status);
103-
driver.quit();
104-
}
105-
}
106-
}
107-
19+
git clone https://github.com/LambdaTest/junit-selenium-sample.git
10820
```
10921

110-
## Executing The Test
22+
Step 2. Inside Junit-selenium-sample folder, export the Lambda-test Credentials. You can get these from your automation dashboard.
11123

112-
You would need to execute the below command in your terminal/cmd.
24+
<p align="center">
25+
<b>For Linux/macOS:</b>
11326

114-
<code>mvn test -P single</code>
115-
116-
## Configuring Desired Capabilities
117-
118-
First step is to configure your test scripts to connect with LambdaTest Selenium automation gird. You would have to invoke of remote WebDriver instead of the native browser WebDrivers.
119-
120-
**Local WebDriver:**
121-
122-
<code>FirefoxDriver driver = new FirefoxDriver():</code>
123-
124-
You would have to change it remote WebDriver and at the same time pass capabilities related to the browser, browser versions etc. In simple terms, it would look something like this:
125-
126-
**Remote WebDriver:**
12727
```
128-
WebDriver driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + "@hub.lambdatest.com/wd/hub"),
129-
DesiredCapabilities.firefox());
28+
export LT_USERNAME="YOUR_USERNAME"
29+
export LT_ACCESS_KEY="YOUR ACCESS KEY"
13030
```
13131

132-
## Capabilities Generator At LambdaTest Will Provide You With The Below Program:
133-
134-
LambdaTest Provides you with the capabilities generator for your Selenium script. You an select your cofigrations on which you wnat to test and it will automatically generate the code for you desired capabilities class.
135-
136-
![Desired Capabilities](https://www.lambdatest.com/blog/wp-content/uploads/2020/04/Selenium-test-automation-1.png)
137-
138-
Here is capability class:
32+
<p align="center">
33+
<b>For Windows:</b>
13934

14035
```
141-
DesiredCapabilities capabilities = new DesiredCapabilities();
142-
capabilities.setCapability("build", "your build name");
143-
capabilities.setCapability("name", "your test name");
144-
capabilities.setCapability("platform", "macOS High Sierra");
145-
capabilities.setCapability("browserName", "Firefox");
146-
capabilities.setCapability("version","64.0");
147-
capabilities.setCapability("selenium_version","3.13.0");
148-
capabilities.setCapability("visual",true);
149-
capabilities.setCapability("firefox.driver",v0.23.0);
36+
set LT_USERNAME="YOUR_USERNAME"
37+
set LT_ACCESS_KEY="YOUR ACCESS KEY"
15038
```
15139

152-
## Testing Locally Hosted Projects Using JUnit With Selenium
153-
154-
To help you perform cross browser testing of locally stored web pages, LambdaTest provides an SSH(Secure Shell) tunnel connection with the name Lambda Tunnel. With Lambda Tunnel, you can test your locally hosted files before you make them live over the internet. You could even perform cross browser testing from different IP addresses belonging to various geographic locations. You can also use LambdaTest Tunnel to test web-apps and websites that are permissible inside your corporate firewall. The SSH tunnel provided by LambdaTest acts as a proxy server for hosting your web pages from your local machine to Virtual machines running on LambdaTest cloud servers.
155-
156-
Download the binary file of:
157-
158-
* [Lambda Tunnel for Windows](http://downloads.lambdatest.com/tunnel/windows/64bit/LT_Windows.zip)
159-
* [Lambda Tunnel for Mac](http://downloads.lambdatest.com/tunnel/mac/64bit/LT_Mac.zip)
160-
* [Lambda Tunnel for Linux](http://downloads.lambdatest.com/tunnel/linux/64bit/LT_Linux.zip)
161-
162-
After you start the Lambda Tunnel you need to set the tunnel capability to true.
163-
164-
**Tunnel Capability**
40+
Step 3. To run your First Test.
16541

16642
```
167-
DesiredCapabilities capabilities = new DesiredCapabilities();
168-
capabilities.setCapability("tunnel", true);
43+
mvn test -P single
16944
```
17045

171-
## Parallel Testing using JUnit with Selenium
172-
173-
Parallel Testing using JUnit with Selenium is one of the most demanding features of LambdaTest Selenium Grid. By parallel testing, you can run more than one test case, simultaneously. This means that Parallel testing would allow you to execute numerous automation test cases altogether. So you execute a single test scenario across different browsers or could run different test scenarios across the same browser but with different browser versions. Wondering how many parallel test cases can you run? That depends entirely on the number of concurrent session under your opted plan.
174-
175-
For instance, if you have a bucket of 100 automated test cases, and the average time for execution of a single test is around 6 minutes. The time taken normally to completely execute all your test cases sequentially would be 600 minutes i.e. 10 hours. However, if you are opting for a plan which offers 2 concurrent sessions then you can perform 2 parallel test cases. This would divide your time in half as the total time taken through parallel testing with 2 concurrent sessions for the above scenario would be 300 minutes i.e. 5 hours. Similarly, if you have got 4 concurrent sessions with you then the time taken would be quarter with respect to sequential testing. This way you could fasten your release cycles as much as you want.
176-
177-
**JUnit: Parallelized Class**
46+
Step 4. To run Parallel Test.
17847

17948
```
180-
import java.util.concurrent.ExecutorService;
181-
import java.util.concurrent.Executors;
182-
import java.util.concurrent.TimeUnit;
183-
184-
import org.junit.runners.Parameterized;
185-
import org.junit.runners.model.RunnerScheduler;
186-
187-
public class Parallelized extends Parameterized {
188-
private static class ThreadPoolScheduler implements RunnerScheduler {
189-
private ExecutorService executor;
190-
public ThreadPoolScheduler() {
191-
String threads = System.getProperty("junit.parallel.threads", "15");
192-
int numThreads = Integer.parseInt(threads);
193-
executor = Executors.newFixedThreadPool(numThreads);
194-
}
195-
@Override
196-
public void finished() {
197-
executor.shutdown();
198-
try {
199-
executor.awaitTermination(10, TimeUnit.MINUTES);
200-
} catch (InterruptedException exc) {
201-
throw new RuntimeException(exc);
202-
}
203-
}
204-
@Override
205-
public void schedule(Runnable childStatement) {
206-
executor.submit(childStatement);
207-
}
208-
}
209-
210-
public Parallelized(Class<?> klass) throws Throwable {
211-
super(klass);
212-
setScheduler(new ThreadPoolScheduler());
213-
}
214-
}
49+
mvn test -P parallel
21550
```
216-
Let us take a look at a reference of the above helper class for executing parallel test using JUnit automation framework.
21751

218-
**JUnit Concurrent Todo : Sample App**
52+
## See the Results
21953

220-
```
221-
import org.openqa.selenium.By;
222-
import org.openqa.selenium.Platform;
223-
import org.openqa.selenium.WebDriver;
224-
import org.openqa.selenium.remote.DesiredCapabilities;
225-
import org.openqa.selenium.remote.RemoteWebDriver;
226-
import org.junit.After;
227-
import org.junit.Before;
228-
import org.junit.Test;
229-
import org.junit.runner.RunWith;
230-
import org.junit.runners.Parameterized;
231-
import java.net.MalformedURLException;
232-
import java.net.URL;
233-
import java.util.LinkedList;
234-
235-
@RunWith(Parallelized.class)
236-
public class JUnitConcurrentTodo {
237-
public String username = "YOUR_USERNAME";
238-
public String accesskey = "YOUR_ACCESS_KEY";
239-
public String gridURL = "@hub.lambdatest.com/wd/hub";
240-
241-
public String platform;
242-
public String browserName;
243-
public String browserVersion;
244-
public RemoteWebDriver driver = null;
245-
boolean status = false;
246-
@Parameterized.Parameters
247-
public static LinkedList<String[]> getEnvironments() throws Exception {
248-
LinkedList<String[]> env = new LinkedList<String[]>();
249-
env.add(new String[]{"WIN10", "chrome", "70.0"});
250-
env.add(new String[]{"macos 10.12","firefox","62.0"});
251-
env.add(new String[]{"WIN8","internet explorer","10"});
252-
return env;
253-
}
254-
public JUnitConcurrentTodo(String platform, String browserName, String browserVersion) {
255-
this.platform = platform;
256-
this.browserName = browserName;
257-
this.browserVersion = browserVersion;
258-
}
259-
@Before
260-
public void setUp() throws Exception {
261-
DesiredCapabilities capabilities = new DesiredCapabilities();
262-
capabilities.setCapability("browserName", browserName);
263-
capabilities.setCapability("version", browserVersion);
264-
capabilities.setCapability("platform", platform); // If this cap isn't specified, it will just get the any available one
265-
capabilities.setCapability("build", "JUnitParallelSample");
266-
capabilities.setCapability("name", "JUnitParallelSampleTest");
267-
capabilities.setCapability("network", true); // To enable network logs
268-
capabilities.setCapability("visual", true); // To enable step by step screenshot
269-
capabilities.setCapability("video", true); // To enable video recording
270-
capabilities.setCapability("console", true); // To capture console logs
271-
try {
272-
driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
273-
} catch (MalformedURLException e) {
274-
System.out.println("Invalid grid URL");
275-
} catch (Exception e) {
276-
System.out.println(e.getMessage());
277-
}
278-
}
279-
@Test
280-
public void testParallel() throws Exception {
281-
try {
282-
//Change it to production page
283-
driver.get("https://lambdatest.github.io/sample-todo-app/");
284-
//Let's mark done first two items in the list.
285-
driver.findElement(By.name("li1")).click();
286-
driver.findElement(By.name("li2")).click();
287-
288-
// Let's add an item in the list.
289-
driver.findElement(By.id("sampletodotext")).sendKeys("Yey, Let's add it to list");
290-
driver.findElement(By.id("addbutton")).click();
291-
292-
// Let's check that the item we added is added in the list.
293-
String enteredText = driver.findElementByXPath("/html/body/div/div/div/ul/li[6]/span").getText();
294-
if (enteredText.equals("Yey, Let's add it to list")) {
295-
status = true;
296-
}
297-
} catch (Exception e) {
298-
System.out.println(e.getMessage());
299-
}
300-
}
301-
@After
302-
public void tearDown() throws Exception {
303-
if (driver != null) {
304-
((JavascriptExecutor) driver).executeScript("lambda-status=" + status);
305-
driver.quit();
306-
}
307-
}
308-
}
309-
```
54+
You can see the results of the test on Lambdatest [Automation Dashboard](https://automation.lambdatest.com/build)
55+
![Dashboard](https://github.com/LambdaTest/junit-selenium-sample/dashboard)
31056

311-
## About LambdaTest
57+
## Testing Locally Hosted or Privately Hosted Projects
58+
59+
To help you perform cross browser testing of your locally stored web pages, LambdaTest provides an SSH(Secure Shell) tunnel connection with the name Lambda Tunnel. With Lambda Tunnel, you can test your locally hosted files before you make them live over the internet. You could even perform cross browser testing from different IP addresses belonging to various geographic locations. You can also use LambdaTest Tunnel to test web-apps and websites that are permissible inside your corporate firewall.
31260

313-
[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.
61+
- Set tunnel value to True in test capabilities
62+
> OS specific instructions to download and setup tunnel binary can be found at the following links.
63+
>
64+
> - [Windows](https://www.lambdatest.com/support/docs/display/TD/Local+Testing+For+Windows)
65+
> - [Mac](https://www.lambdatest.com/support/docs/display/TD/Local+Testing+For+MacOS)
66+
> - [Linux](https://www.lambdatest.com/support/docs/display/TD/Local+Testing+For+Linux)
67+
> After setting tunnel you can also see the active tunnel in our LambdaTest dashboard:
68+
> ![tunnel active](https://github.com/LambdaTest/Robot-Selenium-Sample/blob/master/tutorial-images/tn.PNG)
31469
315-
## Resources:
70+
## About LambdaTest
31671

317-
**[JUnit with Selenium](https://www.lambdatest.com/support/docs/junit-with-selenium-running-junit-automation-scripts-on-lambdatest-selenium-grid/)**
72+
[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.
376 KB
Loading

0 commit comments

Comments
 (0)