├── .gitignore
├── .gitpod.yml
├── README.md
├── cloudbuild.yaml
├── dashboard.png
├── pom.xml
├── scrollingExample.xml
└── src
└── test
└── java
└── com
└── lambdatest
└── ScrollingExample.java
/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 | /bin/
3 | .project
4 | .settings
5 | .theia
6 |
--------------------------------------------------------------------------------
/.gitpod.yml:
--------------------------------------------------------------------------------
1 | # List the ports you want to expose and what to do when they are served. See https://www.gitpod.io/docs/config-ports/
2 | ports:
3 | - port: 3000
4 | onOpen: open-preview
5 |
6 | # List the start up tasks. You can start them in parallel in multiple terminals. See https://www.gitpod.io/docs/config-start-tasks/
7 | tasks:
8 | - init: mvn install -Dsuite=scrollingExample.xml # runs during prebuild
9 | command: echo 'Please check your test on https://automation.lambdatest.com/'
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Run Selenium Tests With TestNG On LambdaTest (Scrolling element into view and scroll by pixel testng selenium Example)
2 |
3 | 
4 |
5 |
6 |
7 | Blog
8 | ⋅
9 | Docs
10 | ⋅
11 | Learning Hub
12 | ⋅
13 | Newsletter
14 | ⋅
15 | Certifications
16 | ⋅
17 | YouTube
18 |
19 |
20 |
21 |
22 |
23 | *Learn how to use TestNG framework to configure and run your Java automation testing scripts on the LambdaTest platform*
24 |
25 | [
](https://accounts.lambdatest.com/register)
26 |
27 | ## Table Of Contents
28 |
29 | * [Pre-requisites](#pre-requisites)
30 | * [Run Your First Test](#run-your-first-test)
31 | * [Parallel Testing With TestNG](#executing-parallel-tests-using-testng)
32 | * [Local Testing With TestNG](#testing-locally-hosted-or-privately-hosted-projects)
33 |
34 | ## Pre-requisites
35 |
36 | Before you can start performing Java automation testing with Selenium, you would need to:
37 |
38 | - Install the latest **Java development environment** i.e. **JDK 1.6** or higher. We recommend using the latest version.
39 |
40 | - Download the latest **Selenium Client** and its **WebDriver bindings** from the [official website](https://www.selenium.dev/downloads/). Latest versions of Selenium Client and WebDriver are ideal for running your automation script on LambdaTest Selenium cloud grid.
41 |
42 | - Install **Maven** which supports **JUnit** framework out of the box. **Maven** can be downloaded and installed following the steps from [the official website](https://maven.apache.org/). Maven can also be installed easily on **Linux/MacOS** using [Homebrew](https://brew.sh/) package manager.
43 |
44 | ### Cloning Repo And Installing Dependencies
45 |
46 | **Step 1:** Clone the LambdaTest’s Java-TestNG-Selenium repository and navigate to the code directory as shown below:
47 |
48 | ```bash
49 | git clone https://github.com/LambdaTest/Java-TestNG-Selenium
50 | cd Java-TestNG-Selenium
51 | ```
52 |
53 | You can also run the command below to check for outdated dependencies.
54 |
55 | ```bash
56 | mvn versions:display-dependency-updates
57 | ```
58 |
59 | ### Setting Up Your Authentication
60 |
61 | Make sure you have your LambdaTest credentials with you to run test automation scripts. You can get these credentials from the [LambdaTest Automation Dashboard](https://automation.lambdatest.com/build) or by your [LambdaTest Profile](https://accounts.lambdatest.com/login).
62 |
63 | **Step 2:** Set LambdaTest **Username** and **Access Key** in environment variables.
64 |
65 | * For **Linux/macOS**:
66 |
67 | ```bash
68 | export LT_USERNAME="YOUR_USERNAME"
69 | export LT_ACCESS_KEY="YOUR ACCESS KEY"
70 | ```
71 | * For **Windows**:
72 | ```bash
73 | set LT_USERNAME="YOUR_USERNAME"
74 | set LT_ACCESS_KEY="YOUR ACCESS KEY"
75 | ```
76 |
77 | ## Run Your First Test
78 |
79 | >**Test Scenario**: The sample [TestNGTodo1.java](https://github.com/LambdaTest/Java-TestNG-Selenium/blob/master/src/test/java/com/lambdatest/TestNGTodo1.java) tests a sample to-do list app by marking couple items as done, adding a new item to the list and finally displaying the count of pending items as output.
80 |
81 |
82 | ### Configuring Your Test Capabilities
83 |
84 | **Step 3:** In the test script, you need to update your test capabilities. In this code, we are passing browser, browser version, and operating system information, along with LambdaTest Selenium grid capabilities via capabilities object. The capabilities object in the above code are defined as:
85 |
86 | ```java
87 | DesiredCapabilities capabilities = new DesiredCapabilities();
88 | capabilities.setCapability("browserName", "chrome");
89 | capabilities.setCapability("version", "latest");
90 | capabilities.setCapability("platform", "Windows 10"); // If this cap isn't specified, it will just get the any available one
91 | capabilities.setCapability("build", "LambdaTestSampleApp");
92 | capabilities.setCapability("name", "LambdaTestJavaSample");
93 |
94 | ```
95 | ### Scrolling element into view and scroll by pixel testng selenium code
96 | ```java
97 | // Locating element by link text
98 | WebElement Element = driver.findElement(By.linkText("Book a Demo"));
99 |
100 | // Scrolling down the page till the element is found
101 | driver.executeScript("arguments[0].scrollIntoView();", Element);
102 | Thread.sleep(1500);
103 |
104 | // Scrolling down by pixels
105 | driver.executeScript("window.scrollBy(0,-500)", "");
106 |
107 | Thread.sleep(1500);
108 |
109 | // Scrolling up by pixels
110 | driver.executeScript("window.scrollBy(0,500)", "");
111 | ```
112 |
113 |
114 | You can generate capabilities for your test requirements with the help of our inbuilt [Desired Capability Generator](https://www.lambdatest.com/capabilities-generator/).
115 |
116 | ### Executing The Test
117 |
118 | **Step 4:** The tests can be executed in the terminal using the following command.
119 |
120 | ```bash
121 | mvn test -D suite=single.xml
122 | ```
123 |
124 | Your test results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on [LambdaTest automation dashboard](https://automation.lambdatest.com/build).
125 |
126 | ## Run Parallel Tests Using TestNG
127 |
128 |
129 | Here is an example `xml` file which would help you to run a single test on various browsers at the same time, you would also need to generate a testcase which makes use of **TestNG** framework parameters (`org.testng.annotations.Parameters`).
130 |
131 | ```xml title="testng.xml"
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 | ```
164 |
165 | ### Executing Parallel Tests Using TestNG
166 |
167 | To run parallel tests using **TestNG**, we would have to execute the below commands in the terminal:
168 |
169 | - For the above example code
170 | ```bash
171 | mvn test
172 | ```
173 | - For the cloned Java-TestNG-Selenium repo used to run our first sample test
174 | ```bash
175 | mvn test -D suite=scrollingExample.xml
176 | ```
177 |
178 | ## Testing Locally Hosted Or Privately Hosted Projects
179 |
180 | You can test your locally hosted or privately hosted projects with LambdaTest Selenium grid using LambdaTest Tunnel. All you would have to do is set up an SSH tunnel using tunnel and pass toggle `tunnel = True` via desired capabilities. LambdaTest Tunnel establishes a secure SSH protocol based tunnel that allows you in testing your locally hosted or privately hosted pages, even before they are live.
181 |
182 | Refer our [LambdaTest Tunnel documentation](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/) for more information.
183 |
184 | Here’s how you can establish LambdaTest Tunnel.
185 |
186 | Download the binary file of:
187 | * [LambdaTest Tunnel for Windows](https://downloads.lambdatest.com/tunnel/v3/windows/64bit/LT_Windows.zip)
188 | * [LambdaTest Tunnel for macOS](https://downloads.lambdatest.com/tunnel/v3/mac/64bit/LT_Mac.zip)
189 | * [LambdaTest Tunnel for Linux](https://downloads.lambdatest.com/tunnel/v3/linux/64bit/LT_Linux.zip)
190 |
191 | Open command prompt and navigate to the binary folder.
192 |
193 | Run the following command:
194 |
195 | ```bash
196 | LT -user {user’s login email} -key {user’s access key}
197 | ```
198 | So if your user name is lambdatest@example.com and key is 123456, the command would be:
199 |
200 | ```bash
201 | LT -user lambdatest@example.com -key 123456
202 | ```
203 | Once you are able to connect **LambdaTest Tunnel** successfully, you would just have to pass on tunnel capabilities in the code shown below :
204 |
205 | **Tunnel Capability**
206 |
207 | ```java
208 | DesiredCapabilities capabilities = new DesiredCapabilities();
209 | capabilities.setCapability("tunnel", true);
210 | ```
211 |
212 | ## Tutorials 📙
213 |
214 | Check out our latest tutorials on TestNG automation testing 👇
215 |
216 | * [JUnit 5 vs TestNG: Choosing the Right Framework for Automation Testing](https://www.lambdatest.com/blog/junit-5-vs-testng/)
217 | * [How To Install TestNG?](https://www.lambdatest.com/blog/how-to-install-testng-in-eclipse-step-by-step-guide/)
218 | * [Create TestNG Project in Eclipse & Run Selenium Test Script](https://www.lambdatest.com/blog/create-testng-project-in-eclipse-run-selenium-test-script/)
219 | * [A Complete Guide for Your First TestNG Automation Script](https://www.lambdatest.com/blog/a-complete-guide-for-your-first-testng-automation-script/)
220 | * [How to Automate using TestNG in Selenium?](https://www.lambdatest.com/blog/testng-in-selenium/)
221 | * [How to Perform Parallel Test Execution in TestNG with Selenium](https://www.lambdatest.com/blog/parallel-test-execution-in-testng/)
222 | * [Creating TestNG XML File & Execute Parallel Testing](https://www.lambdatest.com/blog/create-testng-xml-file-execute-parallel-testing/)
223 | * [Speed Up Automated Parallel Testing in Selenium with TestNG](https://www.lambdatest.com/blog/speed-up-automated-parallel-testing-in-selenium-with-testng/)
224 | * [Automation Testing With Selenium, Cucumber & TestNG](https://www.lambdatest.com/blog/automation-testing-with-selenium-cucumber-testng/)
225 | * [How to Run JUnit Selenium Tests using TestNG](https://www.lambdatest.com/blog/test-example-junit-and-testng-in-selenium/)
226 | * [How to Group Test Cases in TestNG [With Examples]](https://www.lambdatest.com/blog/grouping-test-cases-in-testng/)
227 | * [How to Set Test Case Priority in TestNG with Selenium](https://www.lambdatest.com/blog/prioritizing-tests-in-testng-with-selenium/)
228 | * [How to Use Assertions in TestNG with Selenium](https://www.lambdatest.com/blog/asserts-in-testng/)
229 | * [How to Use DataProviders in TestNG [With Examples]](https://www.lambdatest.com/blog/how-to-use-dataproviders-in-testng-with-examples/)
230 | * [Parameterization in TestNG - DataProvider and TestNG XML [With Examples]](https://www.lambdatest.com/blog/parameterization-in-testng-dataprovider-and-testng-xml-examples/)
231 | * [TestNG Listeners in Selenium WebDriver [With Examples]](https://www.lambdatest.com/blog/testng-listeners-in-selenium-webdriver-with-examples/)
232 | * [TestNG Annotations Tutorial with Examples for Selenium Automation](https://www.lambdatest.com/blog/complete-guide-on-testng-annotations-for-selenium-webdriver/)
233 | * [How to Use TestNG Reporter Log in Selenium](https://www.lambdatest.com/blog/how-to-use-testng-reporter-log-in-selenium/)
234 | * [How to Generate TestNG Reports in Jenkins](https://www.lambdatest.com/blog/how-to-generate-testng-reports-in-jenkins/)
235 |
236 |
237 | ## Documentation & Resources :books:
238 |
239 |
240 | Visit the following links to learn more about LambdaTest's features, setup and tutorials around test automation, mobile app testing, responsive testing, and manual testing.
241 |
242 | * [LambdaTest Documentation](https://www.lambdatest.com/support/docs/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium)
243 | * [LambdaTest Blog](https://www.lambdatest.com/blog/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium)
244 | * [LambdaTest Learning Hub](https://www.lambdatest.com/learning-hub/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium)
245 |
246 | ## LambdaTest Community :busts_in_silhouette:
247 |
248 | The [LambdaTest Community](https://community.lambdatest.com/) allows people to interact with tech enthusiasts. Connect, ask questions, and learn from tech-savvy people. Discuss best practises in web development, testing, and DevOps with professionals from across the globe 🌎
249 |
250 | ## What's New At LambdaTest ❓
251 |
252 | To stay updated with the latest features and product add-ons, visit [Changelog](https://changelog.lambdatest.com/)
253 |
254 | ## About LambdaTest
255 |
256 | [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium) is a leading test execution and orchestration platform that is fast, reliable, scalable, and secure. It allows users to run both manual and automated testing of web and mobile apps across 3000+ different browsers, operating systems, and real device combinations. Using LambdaTest, businesses can ensure quicker developer feedback and hence achieve faster go to market. Over 500 enterprises and 1 Million + users across 130+ countries rely on LambdaTest for their testing needs.
257 |
258 | ### Features
259 |
260 | * Run Selenium, Cypress, Puppeteer, Playwright, and Appium automation tests across 3000+ real desktop and mobile environments.
261 | * Real-time cross browser testing on 3000+ environments.
262 | * Test on Real device cloud
263 | * Blazing fast test automation with HyperExecute
264 | * Accelerate testing, shorten job times and get faster feedback on code changes with Test At Scale.
265 | * Smart Visual Regression Testing on cloud
266 | * 120+ third-party integrations with your favorite tool for CI/CD, Project Management, Codeless Automation, and more.
267 | * Automated Screenshot testing across multiple browsers in a single click.
268 | * Local testing of web and mobile apps.
269 | * Online Accessibility Testing across 3000+ desktop and mobile browsers, browser versions, and operating systems.
270 | * Geolocation testing of web and mobile apps across 53+ countries.
271 | * LT Browser - for responsive testing across 50+ pre-installed mobile, tablets, desktop, and laptop viewports
272 |
273 |
274 | [
](https://accounts.lambdatest.com/register)
275 |
276 |
277 |
278 | ## We are here to help you :headphones:
279 |
280 | * Got a query? we are available 24x7 to help. [Contact Us](support@lambdatest.com)
281 | * For more info, visit - [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=Java-TestNG-Selenium)
282 |
--------------------------------------------------------------------------------
/cloudbuild.yaml:
--------------------------------------------------------------------------------
1 | steps:
2 | - name: gcr.io/cloud-builders/docker
3 | args: ['run' , '-d', '--name=lt', '--network=cloudbuild', 'lambdatest/tunnel', '--user', '${_LT_USERNAME}', '--key', '${_LT_ACCESS_KEY}', '--tunnelName', 'GCloud', '--infoAPIPort', '15000','--load-balanced']
4 | - name: curlimages/curl
5 | args: ['-s', '--retry-connrefused', '--connect-timeout', '5', '--max-time', '5', '--retry', '30', '--retry-delay', '2', '--retry-max-time', '60', 'http://lt:15000/api/v1.0/info']
6 | - name: 'ubuntu'
7 | args: ['sleep', '20']
8 | - name: 'bash'
9 | args: ['ls']
10 | - name: 'maven'
11 | entrypoint: 'mvn'
12 | args: ['test', '-P', 'local']
13 | env:
14 | - 'LT_USERNAME=${_LT_USERNAME}'
15 | - 'LT_ACCESS_KEY=${_LT_ACCESS_KEY}'
16 |
--------------------------------------------------------------------------------
/dashboard.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/praveenLT/java-testng-scroll-example/ca882e90ecd55f1f8835a908735eed2b3900accd/dashboard.png
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
4 | 4.0.0
5 | Java-TestNG-Selenium
6 | Java-TestNG-Selenium
7 | 0.0.1-SNAPSHOT
8 |
9 | Java-TestNG-Selenium/src
10 |
11 |
12 | Java-TestNG-Selenium/src
13 |
14 | **/*.java
15 |
16 |
17 |
18 |
19 |
20 | maven-compiler-plugin
21 | 3.7.0
22 |
23 | 10
24 |
25 |
26 |
27 | org.apache.maven.plugins
28 | maven-surefire-plugin
29 | 2.19.1
30 |
31 |
32 |
33 | test
34 |
35 |
36 |
37 |
38 |
39 |
40 | ${suite}
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 | org.seleniumhq.selenium
50 | selenium-java
51 | 3.13.0
52 |
53 |
54 |
55 | org.testng
56 | testng
57 | 6.14.3
58 |
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/scrollingExample.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/src/test/java/com/lambdatest/ScrollingExample.java:
--------------------------------------------------------------------------------
1 | package com.lambdatest;
2 |
3 | import java.lang.reflect.Method;
4 | import java.net.MalformedURLException;
5 | import java.net.URL;
6 | import java.io.File;
7 |
8 | import org.openqa.selenium.By;
9 | import org.openqa.selenium.WebElement;
10 | import org.openqa.selenium.remote.DesiredCapabilities;
11 | import org.openqa.selenium.remote.RemoteWebDriver;
12 | import org.testng.Assert;
13 | import org.testng.ITestContext;
14 | import org.testng.annotations.AfterMethod;
15 | import org.testng.annotations.BeforeMethod;
16 | import org.testng.annotations.Test;
17 |
18 | public class ScrollingExample {
19 |
20 | private RemoteWebDriver driver;
21 | private String Status = "failed";
22 |
23 | @BeforeMethod
24 | public void setup(Method m, ITestContext ctx) throws MalformedURLException {
25 | String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME");
26 | String authkey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY");
27 | ;
28 | String hub = "@hub.lambdatest.com/wd/hub";
29 |
30 | DesiredCapabilities caps = new DesiredCapabilities();
31 | caps.setCapability("platform", "Windows 10");
32 | caps.setCapability("browserName", "Chrome");
33 | caps.setCapability("version", "latest");
34 | caps.setCapability("build", "Extension Test");
35 | caps.setCapability("name", m.getName() + " - " + this.getClass().getName());
36 | caps.setCapability("plugin", "git-testng");
37 |
38 | String[] Tags = new String[] { "Feature", "Falcon", "Severe" };
39 | caps.setCapability("tags", Tags);
40 |
41 | driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + hub), caps);
42 |
43 | }
44 |
45 | @Test
46 | public void ScrollingExampleTest() throws InterruptedException {
47 | System.out.println("Loading Url");
48 |
49 | driver.get("https://lambdatest.com");
50 |
51 | // Locating element by link text
52 | WebElement Element = driver.findElement(By.linkText("Book a Demo"));
53 |
54 | // Scrolling down the page till the element is found
55 | driver.executeScript("arguments[0].scrollIntoView();", Element);
56 | Thread.sleep(1500);
57 |
58 | // Scrolling down by pixels
59 | driver.executeScript("window.scrollBy(0,-500)", "");
60 |
61 | Thread.sleep(1500);
62 |
63 | // Scrolling up by pixels
64 | driver.executeScript("window.scrollBy(0,500)", "");
65 |
66 | Thread.sleep(1500);
67 |
68 | Status = "passed";
69 | Thread.sleep(150);
70 |
71 | System.out.println("TestFinished");
72 |
73 | }
74 |
75 | @AfterMethod
76 | public void tearDown() {
77 | driver.executeScript("lambda-status=" + Status);
78 | driver.quit();
79 | }
80 |
81 | }
--------------------------------------------------------------------------------