|
| 1 | +package com.lambdatest; |
| 2 | + |
| 3 | +import org.junit.After; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | +import org.openqa.selenium.remote.DesiredCapabilities; |
| 7 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 8 | +import org.openqa.selenium.By; |
| 9 | +import org.openqa.selenium.chrome.ChromeOptions; |
| 10 | +import java.net.MalformedURLException; |
| 11 | +import org.openqa.selenium.JavascriptExecutor; |
| 12 | +import java.net.URL; |
| 13 | +import java.util.*; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +public class SmartUI { |
| 18 | + String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME"); |
| 19 | + String accessKey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY"); |
| 20 | + public static RemoteWebDriver driver = null; |
| 21 | + |
| 22 | + public String gridURL = "@hub.lambdatest.com/wd/hub"; |
| 23 | + public String status = "failed"; |
| 24 | + |
| 25 | + @Before |
| 26 | + public void setUp() throws Exception { |
| 27 | + ChromeOptions browserOptions = new ChromeOptions(); |
| 28 | + browserOptions.setCapability("platformName", "Windows 10"); |
| 29 | + browserOptions.setCapability("browserVersion", "latest"); |
| 30 | + |
| 31 | + Map<String, Object> ltOptions = new HashMap<>(); |
| 32 | + ltOptions.put("build", "JUnitSampleTestApp"); |
| 33 | + ltOptions.put("name", "JUnitSampleTest"); |
| 34 | + ltOptions.put("selenium_version", "4.0.0"); |
| 35 | + ltOptions.put("project", "SmartUI-Junit-Selenium"); //Enter Project name here |
| 36 | + ltOptions.put("smartUI.project", "Junit-Selenium"); //Enter smartUI Project name here |
| 37 | + ltOptions.put("w3c", true); |
| 38 | + ltOptions.put("plugin", "junit-junit"); |
| 39 | + browserOptions.setCapability("LT:Options", ltOptions); |
| 40 | + |
| 41 | + try { |
| 42 | + driver = new RemoteWebDriver(new URL("https://" + username + ":" + accessKey + gridURL), browserOptions); |
| 43 | + } catch (MalformedURLException e) { |
| 44 | + System.out.println("Invalid grid URL"); |
| 45 | + } catch (Exception e) { |
| 46 | + System.out.println(e.getMessage()); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testSimple() throws Exception { |
| 52 | + try { |
| 53 | + driver.get("https://lambdatest.github.io/sample-todo-app/"); |
| 54 | + |
| 55 | + //Let's mark done first two items in the list. |
| 56 | + driver.findElement(By.name("li1")).click(); |
| 57 | + driver.findElement(By.name("li2")).click(); |
| 58 | + |
| 59 | + // Let's add an item in the list. |
| 60 | + driver.findElement(By.id("sampletodotext")).sendKeys("Yey, Let's add it to list"); |
| 61 | + driver.findElement(By.id("addbutton")).click(); |
| 62 | + |
| 63 | + // Let's check that the item we added is added in the list. |
| 64 | + String enteredText = driver.findElementByXPath("/html/body/div/div/div/ul/li[6]/span").getText(); |
| 65 | + if (enteredText.equals("Yey, Let's add it to list")) { |
| 66 | + status = "passed"; |
| 67 | + } |
| 68 | + |
| 69 | + // Webhook for Screenshot |
| 70 | + Map<String,Object> config = new HashMap<>(); |
| 71 | + config.put("screenshotName","todo-list"); //Add your snapshot name here for SmartUI |
| 72 | + ((JavascriptExecutor)driver).executeScript("smartui.takeScreenshot", config); //Hook for capturing snapshot on SmartUI |
| 73 | + |
| 74 | + } catch (Exception e) { |
| 75 | + System.out.println(e.getMessage()); |
| 76 | + } |
| 77 | + } |
| 78 | + @After |
| 79 | + public void tearDown() throws Exception { |
| 80 | + if (driver != null) { |
| 81 | + driver.executeScript("lambda-status=" + status); |
| 82 | + driver.quit(); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
0 commit comments