|
| 1 | +# TestNG with Selenium |
| 2 | +*** |
1 | 3 |
|
2 |
| -# Java-TestNG-Selenium-Sample |
3 |
| - |
4 |
| - |
5 |
| -### Prerequisites |
6 |
| -1. Install and set environment variable for java. |
7 |
| - * Windows - https://www.oracle.com/java/technologies/downloads/ |
8 |
| - * Linux - ``` sudo apt-get install openjdk-8-jre ``` |
9 |
| - * MacOS - Java should already be present on Mac OS X by default. |
10 |
| -2. Install and set environment varibale for Maven. |
11 |
| - * Windows - https://maven.apache.org/install.html |
12 |
| - * Linux/ MacOS - [Homebrew](http://brew.sh/) (Easier) |
13 |
| - ``` |
14 |
| - install maven |
15 |
| - ``` |
16 |
| - |
17 |
| -### Run your First Test |
18 |
| -1. Clone the Java-TestNG-Selenium repository. |
19 |
| -``` |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +*Learn how to configure and run your Java automation testing scripts on [LambdaTest platform](https://www.lambdatest.com/selenium-automation) using **TestNG**.* |
| 8 | + |
| 9 | +## Table of Contents: |
| 10 | +*** |
| 11 | + |
| 12 | +* [Pre-requisites](#pre-requisites) |
| 13 | +* [Run Your First Test](#run-your-first-test) |
| 14 | +* [Parallel Testing With TestNG](#running-parallel-tests-using-testng-framework) |
| 15 | +* [Local Testing With TestNG](#testing-locally-hosted-or-privately-hosted-projects) |
| 16 | +* [Additional Links](#additional-links) |
| 17 | +## Prerequisites |
| 18 | +*** |
| 19 | + |
| 20 | +Before you can start performing Java automation testing with Selenium, you would need to: |
| 21 | + |
| 22 | +* Install the latest **Java development environment** i.e. **JDK 1.6** or higher. We recommend using the latest version. |
| 23 | + |
| 24 | +* 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. |
| 25 | + |
| 26 | +* Install **Maven** which supports **TestNG** 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. |
| 27 | + |
| 28 | +### Installing Selenium Dependencies and tutorial repo ⬇️ |
| 29 | +*** |
| 30 | + |
| 31 | +**Step 1:** Clone the LambdaTest’s [Java-TestNG-Selenium](https://github.com/LambdaTest/Java-TestNG-Selenium) repository and navigate to the code directory as shown below: |
| 32 | +```bash |
20 | 33 | git clone https://github.com/LambdaTest/Java-TestNG-Selenium
|
| 34 | +cd Java-TestNG-Selenium |
21 | 35 | ```
|
22 |
| -2. Next get into Java-TestNG-Selenium folder, and import Lamabdatest Credentials. You can get these from lambdatest automation dashboard. |
23 |
| - <p align="center"> |
24 |
| - <b>For Linux/macOS:</b>: |
25 |
| - |
26 |
| -``` |
27 |
| -export LT_USERNAME="YOUR_USERNAME" |
28 |
| -export LT_ACCESS_KEY="YOUR ACCESS KEY" |
| 36 | +You may also want to run the command below to check for outdated dependencies. |
| 37 | +```bash |
| 38 | +mvn versions:display-dependency-updates |
29 | 39 | ```
|
30 |
| -<p align="center"> |
31 |
| - <b>For Windows:</b> |
32 | 40 |
|
| 41 | +### Setting up Your Authentication ⚙️ |
| 42 | +*** |
| 43 | + |
| 44 | +Make sure you have your LambdaTest credentials with you to run test automation scripts on LambdaTest Selenium Grid. You can obtain these credentials from the [LambdaTest Automation Dashboard](https://automation.lambdatest.com/build) or through [LambdaTest Profile](https://accounts.lambdatest.com/login). |
| 45 | + |
| 46 | +**Step 2:** Set LambdaTest `Username` and `Access Key` in environment variables. |
| 47 | + * For **Linux/macOS**: |
| 48 | + ```bash |
| 49 | + export LT_USERNAME="YOUR_USERNAME" export LT_ACCESS_KEY="YOUR ACCESS KEY" |
| 50 | + ``` |
| 51 | + * For **Windows**: |
| 52 | + ```bash |
| 53 | + set LT_USERNAME="YOUR_USERNAME" set LT_ACCESS_KEY="YOUR ACCESS KEY" |
| 54 | + ``` |
| 55 | + |
| 56 | + |
| 57 | +## Run Your First Test |
| 58 | +*** |
| 59 | + |
| 60 | +### Sample Test with TestNG |
| 61 | +*** |
| 62 | + |
| 63 | +```java |
| 64 | +//TestNG Todo : Sample App |
| 65 | + |
| 66 | +import org.openqa.selenium.By; |
| 67 | +import org.openqa.selenium.Platform; |
| 68 | +import org.openqa.selenium.WebDriver; |
| 69 | +import org.openqa.selenium.remote.DesiredCapabilities; |
| 70 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 71 | +import org.testng.Assert; |
| 72 | +import org.testng.annotations.AfterClass; |
| 73 | +import org.testng.annotations.BeforeClass; |
| 74 | +import org.testng.annotations.Test; |
| 75 | +import java.net.MalformedURLException; |
| 76 | +import java.net.URL; |
| 77 | +public class TestNGTodo{ |
| 78 | + public String username = "YOUR_LAMBDATEST_USERNAME"; |
| 79 | + public String accesskey = "YOUR_LAMBDATEST_ACCESS_KEY"; |
| 80 | + public static RemoteWebDriver driver = null; |
| 81 | + public String gridURL = "@hub.lambdatest.com/wd/hub"; |
| 82 | + boolean status = false; |
| 83 | + @BeforeClass |
| 84 | + public void setUp() throws Exception { |
| 85 | + DesiredCapabilities capabilities = new DesiredCapabilities(); |
| 86 | + capabilities.setCapability("browserName", "chrome"); |
| 87 | + capabilities.setCapability("version", "70.0"); |
| 88 | + capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one |
| 89 | + capabilities.setCapability("build", "LambdaTestSampleApp"); |
| 90 | + capabilities.setCapability("name", "LambdaTestJavaSample"); |
| 91 | + try { |
| 92 | + driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities); |
| 93 | + } catch (MalformedURLException e) { |
| 94 | + System.out.println("Invalid grid URL"); |
| 95 | + } catch (Exception e) { |
| 96 | + System.out.println(e.getMessage()); |
| 97 | + } |
| 98 | + } |
| 99 | + @Test |
| 100 | + public void testSimple() throws Exception { |
| 101 | + try {//Change it to production page |
| 102 | + driver.get("https://lambdatest.github.io/sample-todo-app/"); |
| 103 | + //Let's mark done first two items in the list. |
| 104 | + driver.findElement(By.name("li1")).click(); |
| 105 | + driver.findElement(By.name("li2")).click(); |
| 106 | + // Let's add an item in the list. |
| 107 | + driver.findElement(By.id("sampletodotext")).sendKeys("Yey, Let's add it to list"); |
| 108 | + driver.findElement(By.id("addbutton")).click(); |
| 109 | + // Let's check that the item we added is added in the list. |
| 110 | + String enteredText = driver.findElementByXPath("/html/body/div/div/div/ul/li[6]/span").getText(); |
| 111 | + if (enteredText.equals("Yey, Let's add it to list")) { |
| 112 | + status = true; |
| 113 | + } |
| 114 | + } catch (Exception e) { |
| 115 | + System.out.println(e.getMessage()); |
| 116 | + } |
| 117 | + } |
| 118 | + @AfterClass |
| 119 | + public void tearDown() throws Exception { |
| 120 | + if (driver != null) { |
| 121 | + ((JavascriptExecutor) driver).executeScript("lambda-status=" + status); |
| 122 | + driver.quit(); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
33 | 126 | ```
|
34 |
| -set LT_USERNAME="YOUR_USERNAME" |
35 |
| -set LT_ACCESS_KEY="YOUR ACCESS KEY" |
36 |
| -``` |
37 |
| -Step 3. Make sure to install the mandatory Selenium dependencies for Maven by running the below command. |
| 127 | +### Configuration of Your Test Capabilities |
| 128 | +*** |
| 129 | + |
| 130 | +**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: |
| 131 | +```java |
| 132 | +DesiredCapabilities capabilities = new DesiredCapabilities(); |
| 133 | + capabilities.setCapability("browserName", "chrome"); |
| 134 | + capabilities.setCapability("version", "70.0"); |
| 135 | + capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one |
| 136 | + capabilities.setCapability("build", "LambdaTestSampleApp"); |
| 137 | + capabilities.setCapability("name", "LambdaTestJavaSample"); |
38 | 138 | ```
|
39 |
| -mvn compile |
40 |
| -mvn versions:display-dependency-updates |
| 139 | +> **Note:** You can generate capabilities for your test requirements with the help of our inbuilt **[Capabilities Generator tool](https://www.lambdatest.com/capabilities-generator/)**. |
| 140 | +
|
| 141 | +### Executing the Test |
| 142 | +*** |
| 143 | + |
| 144 | +**Step 4:** The tests can be executed in the terminal using the following command. |
| 145 | +```bash |
| 146 | +mvn test -D suite=single.xml |
41 | 147 | ```
|
42 |
| -### Run Single Test |
43 |
| -Use the command below from the root of the project to run single tests. |
| 148 | +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://accounts.lambdatest.com/login). LambdaTest Automation Dashboard will help you view all your text logs, screenshots and video recording for your entire automation tests. |
| 149 | + |
| 150 | +## Running Parallel Tests Using TestNG Framework |
| 151 | +*** |
| 152 | + |
| 153 | +### Setting up the Parallel Environment |
| 154 | +*** |
| 155 | + |
| 156 | +Here is an example `testng.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`). |
| 157 | + |
| 158 | +```xml |
| 159 | +<?xml version="1.0" encoding="UTF-8"?> |
| 160 | +<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> |
| 161 | +<suite thread-count="3" name="LambaTestSuite" parallel="tests"> |
| 162 | + |
| 163 | + <test name="WIN8TEST"> |
| 164 | + <parameter name="browser" value="firefox"/> |
| 165 | + <parameter name="version" value="62.0"/> |
| 166 | + <parameter name="platform" value="WIN8"/> |
| 167 | + <classes> |
| 168 | + <class name="LambdaTest.TestNGToDo"/> |
| 169 | + </classes> |
| 170 | + </test> <!-- Test --> |
| 171 | + |
| 172 | + <test name="WIN10TEST"> |
| 173 | + <parameter name="browser" value="chrome"/> |
| 174 | + <parameter name="version" value="79.0"/> |
| 175 | + <parameter name="platform" value="WIN10"/> |
| 176 | + <classes> |
| 177 | + <class name="LambdaTest.TestNGToDo"/> |
| 178 | + </classes> |
| 179 | + </test> <!-- Test --> |
| 180 | + <test name="MACTEST"> |
| 181 | + <parameter name="browser" value="safari"/> |
| 182 | + <parameter name="version" value="11.0"/> |
| 183 | + <parameter name="platform" value="macos 10.13"/> |
| 184 | + <classes> |
| 185 | + <class name="LambdaTest.TestNGToDo"/> |
| 186 | + </classes> |
| 187 | + </test> <!-- Test --> |
| 188 | + |
| 189 | +</suite> |
44 | 190 | ```
|
45 |
| -mvn test -D suite=single.xml |
| 191 | +### Executing Parallel Tests Using TestNG |
| 192 | +*** |
| 193 | + |
| 194 | +To run parallel tests using **TestNG**, we would have to execute the below commands in the terminal: |
| 195 | + |
| 196 | +* For the above example code |
| 197 | + ```bash |
| 198 | + mvn test |
| 199 | + ``` |
| 200 | +* For the cloned Java-TestNG-Selenium repo used to run our first sample test |
| 201 | + ```bash |
| 202 | + mvn test -D suite=parallel.xml |
| 203 | + ``` |
| 204 | + |
| 205 | +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://accounts.lambdatest.com/login). |
| 206 | + |
| 207 | +## Testing Locally Hosted or Privately Hosted Projects |
| 208 | +*** |
| 209 | + |
| 210 | +You can test your locally hosted or privately hosted projects with [LambdaTest Selenium grid cloud](https://www.lambdatest.com/selenium-automation) using LambdaTest Tunnel app. All you would have to do is set up an SSH tunnel using LambdaTest Tunnel app 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 made live. |
| 211 | + |
| 212 | +>Refer our [LambdaTest Tunnel documentation](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/) for more information. |
| 213 | +
|
| 214 | +Here’s how you can establish LambdaTest Tunnel. |
| 215 | + |
| 216 | +>Download the binary file of: |
| 217 | +* [LambdaTest Tunnel for Windows](https://downloads.lambdatest.com/tunnel/windows/64bit/LT_Windows.zip) |
| 218 | +* [LambdaTest Tunnel for Mac](https://downloads.lambdatest.com/tunnel/mac/64bit/LT_Mac.zip) |
| 219 | +* [LambdaTest Tunnel for Linux](https://downloads.lambdatest.com/tunnel/linux/64bit/LT_Linux.zip) |
| 220 | + |
| 221 | +Open command prompt and navigate to the binary folder. |
| 222 | + |
| 223 | +Run the following command: |
| 224 | +```bash |
| 225 | +LT -user {user’s login email} -key {user’s access key} |
46 | 226 | ```
|
47 |
| -### Run Parallel Test |
48 |
| -Use the command below from the root of the project to run parallel tests. |
| 227 | +So if your user name is lambdatest@example.com and key is 123456, the command would be: |
| 228 | +```bash |
| 229 | +LT -user lambdatest@example.com -key 123456 |
49 | 230 | ```
|
50 |
| -mvn test -D suite=parallel.xml |
| 231 | +Once you are able to connect **LambdaTest Tunnel** successfully, you would just have to pass on tunnel capabilities in the code shown below : |
| 232 | + |
| 233 | +**Tunnel Capability** |
| 234 | +```java |
| 235 | +DesiredCapabilities capabilities = new DesiredCapabilities(); |
| 236 | + capabilities.setCapability("tunnel", true); |
51 | 237 | ```
|
52 |
| -### Results |
53 |
| -You can check the test results on LambdaTest [Automation Dashboard](https://automation.lambdatest.com/build) |
54 | 238 |
|
55 |
| -## About LambdaTest |
56 |
| -[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. |
| 239 | + |
| 240 | +## Additional Links 🔗 |
| 241 | +*** |
| 242 | +* [Advanced Configuration for Capabilities](https://www.lambdatest.com/support/docs/selenium-automation-capabilities/) |
| 243 | +* [How to test locally hosted apps](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/) |
| 244 | +* [How to integrate LambdaTest with CI/CD](https://www.lambdatest.com/support/docs/integrations-with-ci-cd-tools/) |
| 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 | +## Documentation & Resources :books: |
| 251 | +*** |
| 252 | + |
| 253 | +If you want to learn more about the LambdaTest's features, setup, and usage, visit the [LambdaTest documentation](https://www.lambdatest.com/support/docs/). You can also find in-depth tutorials around test automation, mobile app testing, responsive testing, manual testing on [LambdaTest Blog](https://www.lambdatest.com/blog/) and [LambdaTest Learning Hub](https://www.lambdatest.com/learning-hub/). |
| 254 | + |
| 255 | + ## About LambdaTest |
| 256 | +*** |
| 257 | +[LambdaTest](https://www.lambdatest.com) 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. |
| 258 | + |
| 259 | +[<img height="70" src="https://user-images.githubusercontent.com/70570645/169649126-ed61f6de-49b5-4593-80cf-3391ca40d665.PNG">](https://accounts.lambdatest.com/register) |
| 260 | + |
| 261 | +## We are here to help you :headphones: |
| 262 | +*** |
| 263 | +* Got a query? we are available 24x7 to help. [Contact Us](mailto:support@lambdatest.com) |
| 264 | +* For more info, visit - https://www.lambdatest.com |
0 commit comments