diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml new file mode 100644 index 0000000..251a424 --- /dev/null +++ b/.github/workflows/maven.yml @@ -0,0 +1,26 @@ +# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven + +name: Java CI with Maven + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + java-version: '11' + distribution: 'temurin' + cache: maven + - name: Build with Maven + run: mvn test -D suite=single.xml diff --git a/.github/workflows/maventest.yml b/.github/workflows/maventest.yml new file mode 100644 index 0000000..65b2bf7 --- /dev/null +++ b/.github/workflows/maventest.yml @@ -0,0 +1,27 @@ +# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven + +name: Java CI with Maven + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up JDK 11 + uses: actions/setup-java@v3 + with: + java-version: '11' + distribution: 'temurin' + cache: maven + - name: Build with Maven + run: mvn test -D suite=single.xml + diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..79a1aee --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,40 @@ +pipeline { + agent any + + stages { + stage('Checkout') { + steps { + // Checkout the source code from your version control system + // For example, using Git + git 'https://github.com/deepanshusri/java-testng-selenium.git' + } + } + + stage('Build') { + steps { + // Build your Java project + // For example, using Apache Maven + sh 'mvn clean package' + sh 'mvn install' + } + } + + stage('Test') { + steps { + // Run tests for your Java project + // For example, using Apache Maven + // bat 'mvn test' + sh 'mvn test -D suite=single.xml' + } + } + + stage('Deploy') { + steps { + // Deploy your Java project to a server or a container + // For example, using Docker + sh 'docker build -t your-image .' + sh 'docker run -d -p 8080:8080 your-image' + } + } + } +} diff --git a/pom.xml b/pom.xml index 2f4b6f9..a902d9a 100644 --- a/pom.xml +++ b/pom.xml @@ -1,62 +1,80 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0%20http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 Java-TestNG-Selenium Java-TestNG-Selenium 0.0.1-SNAPSHOT + - Java-TestNG-Selenium/src + src/test/java + src/test/java - Java-TestNG-Selenium/src - - **/*.java - + src/main/resources maven-compiler-plugin - 3.7.0 + 3.10.1 - 10 + 11 org.apache.maven.plugins maven-surefire-plugin - 2.19.1 - - - - test - - - + 3.0.0-M5 - ${suite} - + org.seleniumhq.selenium selenium-java - 3.13.0 + 4.24.0 - org.testng testng - 6.14.3 + 7.7.0 + + org.apache.httpcomponents + httpclient + 4.5.14 + + + io.appium + java-client + 9.3.0 + + + org.json + json + 20210307 + + - + + + org.slf4j + slf4j-api + 1.7.30 + - \ No newline at end of file + + + ch.qos.logback + logback-classic + 1.2.3 + + + diff --git a/single.xml b/single.xml index 38a023f..b90b198 100644 --- a/single.xml +++ b/single.xml @@ -3,7 +3,7 @@ - + diff --git a/src/test/java/com/lambdatest/TestNGTodo1.java b/src/test/java/com/lambdatest/TestNGTodo1.java index 62577d1..f8fc989 100644 --- a/src/test/java/com/lambdatest/TestNGTodo1.java +++ b/src/test/java/com/lambdatest/TestNGTodo1.java @@ -1,13 +1,24 @@ package com.lambdatest; -import java.lang.reflect.Method; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.math.BigInteger; +import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; +import java.time.LocalDateTime; +import java.util.HashMap; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; -import org.openqa.selenium.By; +import org.json.JSONArray; +import org.json.JSONObject; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; -import org.testng.Assert; +import org.openqa.selenium.remote.SessionId; import org.testng.ITestContext; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; @@ -16,94 +27,261 @@ public class TestNGTodo1 { private RemoteWebDriver driver; - private String Status = "failed"; + private String testStatus; @BeforeMethod public void setup(Method m, ITestContext ctx) throws MalformedURLException { - String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME"); - String authkey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY"); - ; + String username = "deepanshulambdatest"; + String accessKey = "SMKwpdntRhBpUbdLrUqk0tU0TjAZquV9kz0YxnOWUo56EaqtQw"; String hub = "@hub.lambdatest.com/wd/hub"; - DesiredCapabilities caps = new DesiredCapabilities(); - caps.setCapability("platform", "MacOS Catalina"); - caps.setCapability("browserName", "Safari"); - caps.setCapability("version", "latest"); - caps.setCapability("build", "TestNG With Java"); - caps.setCapability("name", m.getName() + " - " + this.getClass().getName()); - caps.setCapability("plugin", "git-testng"); + DesiredCapabilities capabilities = new DesiredCapabilities(); + HashMap ltOptions = new HashMap<>(); + ltOptions.put("platform", "windows 11"); + ltOptions.put("browserName", "chrome"); + ltOptions.put("version", "125"); + ltOptions.put("build", "Qmetry test case build"); + ltOptions.put("console", true); + ltOptions.put("visual", true); + ltOptions.put("network", true); + ltOptions.put("name", "SCRUM-TC-"); + ltOptions.put("w3c", true); + capabilities.setCapability("lt:options", ltOptions); + driver = new RemoteWebDriver(new URL("https://" + username + ":" + accessKey + hub), capabilities); + } - String[] Tags = new String[] { "Feature", "Falcon", "Severe" }; - caps.setCapability("tags", Tags); + @Test + public void basicTest() { + driver.get("https://lambdatest.com/"); + testStatus = "passed"; + } - driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + hub), caps); + private String getGETApiResponse(String apiUrl, String requestPropertyKey, String requestPropertyValue) throws IOException { + URL url = new URL(apiUrl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setRequestProperty("accept", "application/json"); + connection.setRequestProperty(requestPropertyKey, requestPropertyValue); + try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { + StringBuilder response = new StringBuilder(); + String inputLine; + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + return response.toString(); + } } - @Test - public void basicTest() throws InterruptedException { - String spanText; - System.out.println("Loading Url"); + private String getPOSTApiResponse(String apiUrl, String requestBody) throws IOException { + URL url = new URL(apiUrl); + String apiKey = "cd9a7315357f0e8f6866ba50515928617182c4d8f58fec0b1397c8534c93537f73bf8fad9d0be32f4f6d07d26502f6949780e00365547ccfc7dfc56b33d3382d616a1443aa0cc38531566209d5c33d75"; + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setRequestProperty("apiKey", apiKey); + connection.setDoOutput(true); + + try (OutputStream os = connection.getOutputStream()) { + byte[] input = requestBody.getBytes("utf-8"); + os.write(input, 0, input.length); + } - driver.get("https://lambdatest.github.io/sample-todo-app/"); + int responseCode = connection.getResponseCode(); + if (responseCode >= HttpURLConnection.HTTP_BAD_REQUEST) { + try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getErrorStream()))) { + StringBuilder errorResponse = new StringBuilder(); + String inputLine; + while ((inputLine = in.readLine()) != null) { + errorResponse.append(inputLine); + } + throw new IOException("Error: " + errorResponse.toString()); + } + } - System.out.println("Checking Box"); - driver.findElement(By.name("li1")).click(); + try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { + StringBuilder response = new StringBuilder(); + String inputLine; + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + return response.toString(); + } + } - System.out.println("Checking Another Box"); - driver.findElement(By.name("li2")).click(); + private void getPUTApiResponse(String apiUrl, BigInteger dynamicId) throws IOException { + URL url = new URL(apiUrl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - System.out.println("Checking Box"); - driver.findElement(By.name("li3")).click(); + JSONObject jsonBody = new JSONObject(); + jsonBody.put("executionResultId", dynamicId); + String requestBody = jsonBody.toString(); - System.out.println("Checking Another Box"); - driver.findElement(By.name("li4")).click(); + String apiKey = "cd9a7315357f0e8f6866ba50515928617182c4d8f58fec0b1397c8534c93537f73bf8fad9d0be32f4f6d07d26502f6949780e00365547ccfc7dfc56b33d3382d616a1443aa0cc38531566209d5c33d75"; - driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 6"); - driver.findElement(By.id("addbutton")).click(); + connection.setRequestMethod("PUT"); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setRequestProperty("apiKey", apiKey); + connection.setDoOutput(true); - driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 7"); - driver.findElement(By.id("addbutton")).click(); + try (OutputStream os = connection.getOutputStream()) { + byte[] input = requestBody.getBytes("utf-8"); + os.write(input, 0, input.length); + } - driver.findElement(By.id("sampletodotext")).sendKeys(" List Item 8"); - driver.findElement(By.id("addbutton")).click(); + int responseCode = connection.getResponseCode(); + } - System.out.println("Checking Another Box"); - driver.findElement(By.name("li1")).click(); + private String getAddExecutionResultResponse(String apiUrl, String userTestResultStatus) throws IOException { + URL url = new URL(apiUrl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - System.out.println("Checking Another Box"); - driver.findElement(By.name("li3")).click(); + JSONObject jsonBody = new JSONObject(); + jsonBody.put("color", userTestResultStatus.equals("passed") ? "#BAB86C" : "#800000"); + jsonBody.put("name", userTestResultStatus); + jsonBody.put("description", "Test result updated according to the USER's test case result"); + String requestBody = jsonBody.toString(); - System.out.println("Checking Another Box"); - driver.findElement(By.name("li7")).click(); + String apiKey = "cd9a7315357f0e8f6866ba50515928617182c4d8f58fec0b1397c8534c93537f73bf8fad9d0be32f4f6d07d26502f6949780e00365547ccfc7dfc56b33d3382d616a1443aa0cc38531566209d5c33d75"; + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setRequestProperty("apiKey", apiKey); + connection.setDoOutput(true); - System.out.println("Checking Another Box"); - driver.findElement(By.name("li8")).click(); - Thread.sleep(300); + try (OutputStream os = connection.getOutputStream()) { + byte[] input = requestBody.getBytes("utf-8"); + os.write(input, 0, input.length); + } - System.out.println("Entering Text"); - driver.findElement(By.id("sampletodotext")).sendKeys("Get Taste of Lambda and Stick to It"); + int responseCode = connection.getResponseCode(); - driver.findElement(By.id("addbutton")).click(); + BufferedReader in; + if (responseCode >= HttpURLConnection.HTTP_BAD_REQUEST) { + in = new BufferedReader(new InputStreamReader(connection.getErrorStream())); + } else { + in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + } - System.out.println("Checking Another Box"); - driver.findElement(By.name("li9")).click(); + StringBuilder response = new StringBuilder(); + String inputLine; + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); - // Let's also assert that the todo we added is present in the list. + return response.toString(); + } - spanText = driver.findElementByXPath("/html/body/div/div/div/ul/li[9]/span").getText(); - Assert.assertEquals("Get Taste of Lambda and Stick to It", spanText); - Status = "passed"; - Thread.sleep(150); + private Map getStatusIndFromJsonResponse(String jsonResponse) { + JSONObject jsonObject = new JSONObject(jsonResponse); + JSONObject dataObject = jsonObject.getJSONObject("data"); + String testName = dataObject.optString("name", "unknown"); + String status = dataObject.optString("status_ind", "unknown"); + // Create a map to hold the results + Map resultMap = new HashMap<>(); + resultMap.put("testName", testName); + resultMap.put("status", status); + + return resultMap; // Return the map + } - System.out.println("TestFinished"); + private BigInteger getExecutionResultUsersStatus(String jsonResponse) { + JSONObject jsonObject = new JSONObject(jsonResponse); + return jsonObject.optBigInteger("id", BigInteger.valueOf(0)); + } + private JSONArray getSearchTestCycleResponse(String jsonResponse) { + JSONObject jsonObject = new JSONObject(jsonResponse); + return jsonObject.getJSONArray("data"); } @AfterMethod public void tearDown() { - driver.executeScript("lambda-status=" + Status); - driver.quit(); - } + try { + if (driver != null) { + driver.executeScript("lambda-status=" + testStatus); + SessionId session = driver.getSessionId(); + String sessionApiUrl = "https://api.lambdatest.com/automation/api/v1/sessions/" + session; + String authKey = "Basic ZGVlcGFuc2h1bGFtYmRhdGVzdDpTTUt3cGRudFJoQnBVYmRMclVxazB0VTBUakFacXVWOWt6MFl4bk9XVW81NkVhcXRRdw=="; + String response = getGETApiResponse(sessionApiUrl, "Authorization", authKey); + Map result = getStatusIndFromJsonResponse(response); + String userTestResultStatus = result.get("status"); + String userTestResultName = result.get("testName"); + + // POST API for getting test cycle + String searchTestCycleApiUrl = "https://qtmcloud.qmetry.com/rest/api/latest/testcycles/search/"; + String searchTestCycleResponse = getPOSTApiResponse(searchTestCycleApiUrl, "{\"filter\":{\"projectId\":10000}}"); + JSONArray searchTestCycleResult = getSearchTestCycleResponse(searchTestCycleResponse); + String testCycleId = ""; + for (int i = 0; i < searchTestCycleResult.length(); i++) { + JSONObject obj = searchTestCycleResult.getJSONObject(i); + String key = obj.getString("key"); + if ("SCRUM-TR-9".equals(key)) { + String id = obj.getString("id"); + testCycleId = id; + break; + } + } -} \ No newline at end of file + // POST API for get linked test cases of test cycle + String getLinkedTestCasesApiUrl = "https://qtmcloud.qmetry.com/rest/api/latest/testcycles/" + testCycleId + "/testcases/search/"; + String linkedTestCasesResponse = getPOSTApiResponse(getLinkedTestCasesApiUrl, "{\n" + + " \"filter\": {\n" + + " \"status\": [\n" + + " \"In Progress\",\n" + + " \"To Do\",\n" + + " \"Passed\",\n" + + " \"Failed\",\n" + + " \"Blocked\",\n" + + " \"Not Executed\"\n" + + " ]\n" + + " }\n" + + "}"); + + JSONArray linkedTestCasesResult = getSearchTestCycleResponse(linkedTestCasesResponse); + BigInteger testCaseExecutionId = BigInteger.valueOf(0); + for (int i = 0; i < linkedTestCasesResult.length(); i++) { + JSONObject obj = linkedTestCasesResult.getJSONObject(i); + String key = obj.getString("key"); + if (userTestResultName.equals(key)) { + BigInteger id = BigInteger.valueOf(obj.getLong("testCaseExecutionId")); + System.out.println("----------LT id-----------" + id); + testCaseExecutionId = id; + break; + } + } + // JSONObject linkedTestCaseResponseObj = linkedTestCasesResult.getJSONObject(0); + // BigInteger testCaseExecutionId = linkedTestCaseResponseObj.getBigInteger("testCaseExecutionId"); + + // GET execution result + String getExecutionResultApiUrl = "https://qtmcloud.qmetry.com/rest/api/latest/projects/10000/execution-results"; + String apiKey = "cd9a7315357f0e8f6866ba50515928617182c4d8f58fec0b1397c8534c93537f73bf8fad9d0be32f4f6d07d26502f6949780e00365547ccfc7dfc56b33d3382d616a1443aa0cc38531566209d5c33d75"; + String getExecutionResultResponse = getGETApiResponse(getExecutionResultApiUrl, "apiKey", apiKey); + JSONArray jsonArray = new JSONArray(getExecutionResultResponse); + BigInteger executionResultId = BigInteger.valueOf(0); + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject executionResultResponseObj = jsonArray.getJSONObject(i); // get the individual obj + String name = executionResultResponseObj.getString("name"); + if (userTestResultStatus.equals(name)) { + executionResultId = executionResultResponseObj.getBigInteger("id"); + break; + } + } + if (executionResultId.equals(BigInteger.valueOf(0))) { + String addExecutionResultApiUrl = "https://qtmcloud.qmetry.com/rest/api/latest/projects/10000/execution-results"; + String addExecutionResultResponse = getAddExecutionResultResponse(addExecutionResultApiUrl, userTestResultStatus); + executionResultId = getExecutionResultUsersStatus(addExecutionResultResponse); + } + + String updateTestCaseExecutionApiUrl = "https://qtmcloud.qmetry.com/rest/api/latest/testcycles/" + testCycleId + "/testcase-executions/" + testCaseExecutionId; + getPUTApiResponse(updateTestCaseExecutionApiUrl, executionResultId); + } + } catch (Exception e) { + System.out.println("----------EXCEPTIONS-----------" + e); + } finally { + if (driver != null) { + driver.quit(); // Ensure driver quits + } + } + } +} diff --git a/src/test/java/com/lambdatest/TestNGTodo2.java b/src/test/java/com/lambdatest/TestNGTodo2.java index 3530993..304f313 100644 --- a/src/test/java/com/lambdatest/TestNGTodo2.java +++ b/src/test/java/com/lambdatest/TestNGTodo2.java @@ -20,8 +20,8 @@ public class TestNGTodo2 { @BeforeMethod public void setup(Method m, ITestContext ctx) throws MalformedURLException { - String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME"); - String authkey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY"); + String username = "deepanshulambdatest"; + String authkey = "rsoHTTxvRGQOV7jvQJJwPzKYQSyXYHKqC3SOgIWgKJ9G9sWZbk"; ; /* @@ -118,4 +118,4 @@ public void tearDown() { driver.quit(); } -} \ No newline at end of file +} diff --git a/src/test/java/com/lambdatest/visualui.java b/src/test/java/com/lambdatest/visualui.java new file mode 100644 index 0000000..5eaf738 --- /dev/null +++ b/src/test/java/com/lambdatest/visualui.java @@ -0,0 +1,74 @@ +package com.lambdatest; + +import java.lang.reflect.Method; +import java.net.MalformedURLException; +import java.net.URL; + +import org.openqa.selenium.By; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.remote.RemoteWebDriver; +import org.testng.Assert; +import org.testng.ITestContext; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +public class visualui { + + private RemoteWebDriver driver; + private String Status = "failed"; + + @BeforeMethod + public void setup(Method m, ITestContext ctx) throws MalformedURLException { + String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME"); + String authkey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY"); + String hub = "@hub.lambdatest.com/wd/hub"; + + DesiredCapabilities caps = new DesiredCapabilities(); + caps.setCapability("platform", "MacOS Catalina"); + caps.setCapability("browserName", "Chrome"); + caps.setCapability("version", "121"); + caps.setCapability("build", "TestNG With Java + Visual UI"); + caps.setCapability("name", m.getName() + " - " + this.getClass().getName()); + caps.setCapability("plugin", "git-testng"); + caps.setCapability("visual",true); + caps.setCapability("smartUI.project","Feb_12"); + caps.setCapability("smartUI.build","build 1"); + + driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + hub), caps); + + } + + @Test + public void basicTest() throws InterruptedException { + String spanText; + System.out.println("Loading Url"); + + + driver.get("https://www.lambdatest.com/"); + Thread.sleep(5000); + driver.executeScript("smartui.takeScreenshot=pic1"); + Thread.sleep(1000); + + driver.get("https://www.lambdatest.com/support/docs/"); + Thread.sleep(5000); + driver.executeScript("smartui.takeScreenshot=pic2"); + Thread.sleep(1000); + + driver.get("https://www.lambdatest.com/pricing"); + Thread.sleep(5000); + driver.executeScript("smartui.takeScreenshot=pic3"); + Thread.sleep(1000); + + Status = "passed"; + Thread.sleep(800); + System.out.println("TestFinished"); + } + + @AfterMethod + public void tearDown() { + driver.executeScript("lambda-status=" + Status); + driver.quit(); + } + +}