package com.lambdatest; import java.net.URL; import java.util.ArrayList; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.testng.Assert; import org.testng.annotations.AfterTest; import org.testng.annotations.Test; public class HardAssertTest { private static RemoteWebDriver driver; private static String status; @Test public void hardAssertion() throws Exception { String username = "user_name"; //Enter your access key String accesskey = "access_key"; //Enter Grid URL String gridURL = "@hub.lambdatest.com/wd/hub"; //Set Desired capabilities to run on Lambdatest platform DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("browserName", "chrome"); //Set Browser Name capabilities.setCapability("version", "latest"); //Set browser version capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one capabilities.setCapability("build", "Hard Assert Test"); capabilities.setCapability("name", "Hard Assert Test"); capabilities.setCapability("visual", true); driver = new RemoteWebDriver(new URL("https://"+username+":"+accesskey+gridURL), capabilities); driver.get("https://www.lambdatest.com/"); // Define expected title String expectedtitle = "Most Powerful Cross Browser Testing Tool Online | LambdaTe"; // Extract Actual title String actualtitle = driver.getTitle(); ArrayList<String> exceptionCapture = new ArrayList<>(); try { //Assertion applied to validate the result Assert.assertEquals(actualtitle, expectedtitle); System.out.println("This statement will not be executed because the previous statement is failed"); status="passed"; } catch(AssertionError e) { //Logic to capture Assertion error message with user customized message in exception tab on lambdatest status = "failed"; exceptionCapture.add("Title not matched"+" "+e.getMessage()); ((JavascriptExecutor) driver).executeScript("lambda-exceptions", exceptionCapture); } } @AfterTest public void tearDown() throws Exception { if (driver != null) { ((JavascriptExecutor) driver).executeScript("lambda-status=" + status); driver.quit(); } } }