├── .gitignore ├── .gitpod.yml ├── README.md ├── cloudbuild.yaml ├── pom.xml ├── single.xml └── src └── test └── java └── com └── lambdatest └── TestNGRelativeLocator.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=single.xml -Dtestng.dtd.http=true # runs during prebuild 9 | command: echo 'Please check your test on https://automation.lambdatest.com/' 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # How to use relative locators for automation test in Java-TestNG on LambdaTest 2 | 3 | ### Environment Setup 4 | 5 | 1. Global Dependencies 6 | * Install [Maven](https://maven.apache.org/install.html) 7 | * Or Install Maven with [Homebrew](http://brew.sh/) (Easier) 8 | ``` 9 | $ install maven 10 | ``` 11 | 2. Project Dependencies 12 | * checkout the repository 13 | * Check that packages are available 14 | ``` 15 | $ cd Java-TestNG-Selenium 16 | ``` 17 | * 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. 18 | ``` 19 | $ mvn versions:display-dependency-updates 20 | ``` 21 | 22 | **LambdaTest Authentication Credentials:** Make sure you have your LambdaTest credentials with you to run test automation scripts with Jest on LambdaTest Selenium Grid. You can obtain these credentials from the [LambdaTest Automation Dashboard](https://automation.lambdatest.com/) or through [LambdaTest Profile](https://accounts.lambdatest.com/detail/profile). 23 | 24 | Set LambdaTest Username and Access Key in environment variables. 25 | 26 | * For Linux/macOS: 27 | ``` 28 | $ export LT_USERNAME="YOUR_USERNAME" 29 | $ export LT_ACCESS_KEY="YOUR ACCESS KEY" 30 | ``` 31 | 32 | * For Windows: 33 | ``` 34 | $ set LT_USERNAME="YOUR_USERNAME" 35 | $ set LT_ACCESS_KEY="YOUR ACCESS KEY" 36 | ``` 37 | ### Using relative locators 38 | The following are examples of using relative locators: 39 | ```java 40 | // find the height input using toRightOf relative locator 41 | // input is right of height label 42 | WebElement heightInput = driver.findElement(with(By.tagName("input")) 43 | .toRightOf(heightLabel)); 44 | 45 | // find the calculate button which is below the weight label 46 | WebElement calculateButton = driver.findElement(with(By.tagName("input")) 47 | .below(weightLabel)); 48 | ``` 49 | ### Running Tests 50 | 51 | ``` 52 | To run single test 53 | $ mvn test -D suite=single.xml -Dtestng.dtd.http=true 54 | ``` 55 | ## About LambdaTest 56 | 57 | [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. 58 | 59 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | Java-TestNG-Selenium 4 | Java-TestNG-Selenium 5 | 0.0.1-SNAPSHOT 6 | 7 | Java-TestNG-Selenium/src 8 | 9 | 10 | Java-TestNG-Selenium/src 11 | 12 | **/*.java 13 | 14 | 15 | 16 | 17 | 18 | maven-compiler-plugin 19 | 3.7.0 20 | 21 | 10 22 | 23 | 24 | 25 | 26 | org.apache.maven.plugins 27 | maven-surefire-plugin 28 | 3.0.0-M5 29 | 30 | 31 | 32 | test 33 | 34 | 35 | 36 | 37 | 38 | 39 | ${suite} 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.seleniumhq.selenium 49 | selenium-java 50 | 4.1.1 51 | 52 | 53 | 54 | org.testng 55 | testng 56 | 7.3.0 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /single.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/test/java/com/lambdatest/TestNGRelativeLocator.java: -------------------------------------------------------------------------------- 1 | package com.lambdatest; 2 | 3 | import static org.openqa.selenium.support.locators.RelativeLocator.with; 4 | 5 | import java.net.MalformedURLException; 6 | import java.net.URL; 7 | import java.util.HashMap; 8 | 9 | import org.openqa.selenium.By; 10 | import org.openqa.selenium.JavascriptExecutor; 11 | import org.openqa.selenium.WebDriver; 12 | import org.openqa.selenium.WebElement; 13 | import org.openqa.selenium.remote.DesiredCapabilities; 14 | import org.openqa.selenium.remote.RemoteWebDriver; 15 | import org.testng.ITestContext; 16 | import org.testng.annotations.AfterMethod; 17 | import org.testng.annotations.BeforeMethod; 18 | import org.testng.annotations.Test; 19 | import java.lang.reflect.Method; 20 | 21 | public class TestNGRelativeLocator { 22 | 23 | private WebDriver driver; 24 | 25 | @BeforeMethod 26 | public void setup(Method m, ITestContext ctx) throws MalformedURLException { 27 | 28 | String hubURL = "https://hub.lambdatest.com/wd/hub"; 29 | 30 | String[] Tags = new String[] { "Feature", "Falcon", "Severe" }; 31 | 32 | DesiredCapabilities capabilities = new DesiredCapabilities(); 33 | capabilities.setCapability("browserName", "Chrome"); 34 | capabilities.setCapability("browserVersion", "latest"); 35 | 36 | HashMap ltOptions = new HashMap(); 37 | ltOptions.put("user", System.getenv("LT_USERNAME")); 38 | ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); 39 | ltOptions.put("build", "Selenium 4"); 40 | ltOptions.put("name", this.getClass().getName()); 41 | ltOptions.put("platformName", "Windows 10"); 42 | ltOptions.put("seCdp", true); 43 | ltOptions.put("selenium_version", "4.0.0"); 44 | ltOptions.put("tags", Tags); 45 | capabilities.setCapability("LT:Options", ltOptions); 46 | 47 | driver = new RemoteWebDriver(new URL(hubURL), capabilities); 48 | System.out.println(driver); 49 | } 50 | 51 | @Test 52 | public void testNGRelativeLocator() throws InterruptedException { 53 | 54 | driver.get("http://cookbook.seleniumacademy.com/mobilebmicalculator.html"); 55 | 56 | // find the height and weight labels using css selector 57 | WebElement heightLabel = driver.findElement(By.cssSelector("label[for='heightCMS']")); 58 | WebElement weightLabel = driver.findElement(By.cssSelector("label[for='weightKg']")); 59 | 60 | // find the height input using toRightOf relative locator 61 | // input is right of height label 62 | WebElement heightInput = driver.findElement(with(By.tagName("input")) 63 | .toRightOf(heightLabel)); 64 | 65 | heightInput.sendKeys("181"); 66 | Thread.sleep(3000); 67 | 68 | // find the weight input using combination of below and toRightOf relative 69 | // locator 70 | // weight input is below height input and right of weight label 71 | WebElement weightInput = driver.findElement(with(By.tagName("input")) 72 | .below(heightInput).toRightOf(weightLabel)); 73 | 74 | weightInput.sendKeys("75"); 75 | Thread.sleep(3000); 76 | 77 | // find the calculate button which is below the weight label 78 | WebElement calculateButton = driver.findElement(with(By.tagName("input")) 79 | .below(weightLabel)); 80 | 81 | calculateButton.click(); 82 | Thread.sleep(3000); 83 | 84 | // find the read only input below calculate button to verify value 85 | if ("22.9".equalsIgnoreCase(driver.findElement(with(By.tagName("input")) 86 | .below(calculateButton)).getAttribute("value"))) { 87 | markStatus("passed", "Value Verified", driver); 88 | Thread.sleep(150); 89 | 90 | System.out.println("TestFinished"); 91 | } else { 92 | markStatus("failed", "Wrong calculation", driver); 93 | } 94 | 95 | } 96 | 97 | @AfterMethod 98 | public void tearDown() { 99 | driver.quit(); 100 | } 101 | 102 | public static void markStatus(String status, String reason, WebDriver driver) { 103 | JavascriptExecutor jsExecute = (JavascriptExecutor) driver; 104 | jsExecute.executeScript("lambda-status=" + status); 105 | System.out.println(reason); 106 | } 107 | 108 | } --------------------------------------------------------------------------------