Skip to content

Instantly share code, notes, and snippets.

@muditlambda
Created November 1, 2021 06:59
Show Gist options
  • Select an option

  • Save muditlambda/736ba60d5e0fc078c06e1f40f28d8904 to your computer and use it in GitHub Desktop.

Select an option

Save muditlambda/736ba60d5e0fc078c06e1f40f28d8904 to your computer and use it in GitHub Desktop.
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment