Thursday, May 9, 2019

Alternative to Assert in TestNG Java

package selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class GoogleTest {
@Test
public void launchPage() throws Exception {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Mitturaj.h\\Desktop\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.google.com/");
// by using try and catch
try {
Assert.assertEquals(driver.getTitle(), "Goo");
} catch (Throwable t) {
System.out.println("Failed " + t);
}
System.out.println("##THIS LINE WILL BE EXECUTED##");
}
}
package selenium;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class GoogleTest {
@Test
public void launchPage() throws Exception {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Mitturaj.h\\Desktop\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.google.com/");
// by using Soft Assert
SoftAssert softAssert = new SoftAssert();
softAssert.assertEquals(driver.getTitle(), "Goo");
System.out.println("##THIS LINE WILL BE EXECUTED##");
// use assertAll at the end of all the statement
softAssert.assertAll();
}
}

No comments:

Post a Comment