Source:Avoiding NullPointerException in Java
a.f(b); <-> b.f(a);
Best Testng code snippet using org.testng.Assert
Source:Assertion.java
...5/**6 * An assert class with various hooks allowing its behavior to be modified7 * by subclasses.8 */9public class Assertion implements IAssertLifecycle {10 protected void doAssert(IAssert<?> assertCommand) {11 onBeforeAssert(assertCommand);12 try {13 executeAssert(assertCommand);14 onAssertSuccess(assertCommand);15 } catch(AssertionError ex) {16 onAssertFailure(assertCommand, ex);17 throw ex;18 } finally {19 onAfterAssert(assertCommand);20 }21 }22 /**23 * Run the assert command in parameter. Meant to be overridden by subclasses.24 */25 @Override26 public void executeAssert(IAssert<?> assertCommand) {27 assertCommand.doAssert();28 }29 /**30 * Invoked when an assert succeeds. Meant to be overridden by subclasses.31 */32 @Override33 public void onAssertSuccess(IAssert<?> assertCommand) {34 }35 /**36 * Invoked when an assert fails. Meant to be overridden by subclasses.37 * 38 * @deprecated use onAssertFailure(IAssert assertCommand, AssertionError ex) instead of.39 */40 @Deprecated41 @Override42 public void onAssertFailure(IAssert<?> assertCommand) {43 }44 45 @Override46 public void onAssertFailure(IAssert<?> assertCommand, AssertionError ex) {47 onAssertFailure(assertCommand);48 }49 /**50 * Invoked before an assert is run. Meant to be overridden by subclasses.51 */52 @Override53 public void onBeforeAssert(IAssert<?> assertCommand) {54 }55 /**56 * Invoked after an assert is run. Meant to be overridden by subclasses.57 */58 @Override59 public void onAfterAssert(IAssert<?> assertCommand) {60 }61 abstract private static class SimpleAssert<T> implements IAssert<T> {62 private final T actual;63 private final T expected;64 private final String m_message;65 public SimpleAssert(String message) {66 this(null, null, message);67 }68 public SimpleAssert(T actual, T expected) {69 this(actual, expected, null);70 }71 public SimpleAssert(T actual, T expected, String message) {72 this.actual = actual;73 this.expected = expected;74 m_message = message;75 }76 @Override77 public String getMessage() {78 return m_message;79 }80 @Override81 public T getActual() {82 return actual;83 }84 @Override85 public T getExpected() {86 return expected;87 }88 @Override89 abstract public void doAssert();90 }91 public void assertTrue(final boolean condition, final String message) {92 doAssert(new SimpleAssert<Boolean>(condition, Boolean.TRUE, message) {93 @Override94 public void doAssert() {95 org.testng.Assert.assertTrue(condition, message);96 }97 });98 }99 100 public void assertTrue(final boolean condition) {101 doAssert(new SimpleAssert<Boolean>(condition, Boolean.TRUE) {102 @Override103 public void doAssert() {104 org.testng.Assert.assertTrue(condition);105 }106 });107 }108 public void assertFalse(final boolean condition, final String message) {109 doAssert(new SimpleAssert<Boolean>(condition, Boolean.FALSE, message) {110 @Override111 public void doAssert() {112 org.testng.Assert.assertFalse(condition, message);113 }114 });115 }116 public void assertFalse(final boolean condition) {117 doAssert(new SimpleAssert<Boolean>(condition, Boolean.FALSE) {118 @Override119 public void doAssert() {120 org.testng.Assert.assertFalse(condition);121 }122 });123 }124 public void fail(final String message, final Throwable realCause) {125 doAssert(new SimpleAssert<Object>(message) {126 @Override127 public void doAssert() {128 org.testng.Assert.fail(message, realCause);129 }130 });131 }132 public void fail(final String message) {133 doAssert(new SimpleAssert<Object>(message) {134 @Override135 public void doAssert() {136 org.testng.Assert.fail(message);137 }138 });139 }140 public void fail() {141 doAssert(new SimpleAssert<Object>(null) {142 @Override143 public void doAssert() {144 org.testng.Assert.fail();145 }146 });147 }148 public <T> void assertEquals(final T actual, final T expected, final String message) {149 doAssert(new SimpleAssert<T>(actual, expected, message) {150 @Override151 public void doAssert() {152 org.testng.Assert.assertEquals(actual, expected, message);153 }154 });155 }156 public <T> void assertEquals(final T actual, final T expected) {157 doAssert(new SimpleAssert<T>(actual, expected) {158 @Override159 public void doAssert() {160 org.testng.Assert.assertEquals(actual, expected);161 }162 });163 }164 public void assertEquals(final String actual, final String expected, final String message) {165 doAssert(new SimpleAssert<String>(actual, expected, message) {166 @Override167 public void doAssert() {168 org.testng.Assert.assertEquals(actual, expected, message);169 }170 });171 }172 public void assertEquals(final String actual, final String expected) {173 doAssert(new SimpleAssert<String>(actual, expected) {174 @Override175 public void doAssert() {176 org.testng.Assert.assertEquals(actual, expected);177 }178 });179 }180 public void assertEquals(final double actual, final double expected, final double delta,181 final String message) {182 doAssert(new SimpleAssert<Double>(actual, expected, message) {183 @Override184 public void doAssert() {185 org.testng.Assert.assertEquals(actual, expected, delta, message);186 }187 });188 }189 public void assertEquals(final double actual, final double expected, final double delta) {190 doAssert(new SimpleAssert<Double>(actual, expected) {191 @Override192 public void doAssert() {193 org.testng.Assert.assertEquals(actual, expected, delta);194 }195 });196 }197 public void assertEquals(final float actual, final float expected, final float delta,198 final String message) {199 doAssert(new SimpleAssert<Float>(actual, expected, message) {200 @Override201 public void doAssert() {202 org.testng.Assert.assertEquals(actual, expected, delta, message);203 }204 });205 }206 public void assertEquals(final float actual, final float expected, final float delta) {207 doAssert(new SimpleAssert<Float>(actual, expected) {208 @Override209 public void doAssert() {210 org.testng.Assert.assertEquals(actual, expected, delta);211 }212 });213 }214 public void assertEquals(final long actual, final long expected, final String message) {215 doAssert(new SimpleAssert<Long>(actual, expected, message) {216 @Override217 public void doAssert() {218 org.testng.Assert.assertEquals(actual, expected, message);219 }220 });221 }222 public void assertEquals(final long actual, final long expected) {223 doAssert(new SimpleAssert<Long>(actual, expected) {224 @Override225 public void doAssert() {226 org.testng.Assert.assertEquals(actual, expected);227 }228 });229 }230 public void assertEquals(final boolean actual, final boolean expected, final String message) {231 doAssert(new SimpleAssert<Boolean>(actual, expected, message) {232 @Override233 public void doAssert() {234 org.testng.Assert.assertEquals(actual, expected, message);235 }236 });237 }238 public void assertEquals(final boolean actual, final boolean expected) {239 doAssert(new SimpleAssert<Boolean>(actual, expected) {240 @Override241 public void doAssert() {242 org.testng.Assert.assertEquals(actual, expected);243 }244 });245 }246 public void assertEquals(final byte actual, final byte expected, final String message) {247 doAssert(new SimpleAssert<Byte>(actual, expected, message) {248 @Override249 public void doAssert() {250 org.testng.Assert.assertEquals(actual, expected, message);251 }252 });253 }254 public void assertEquals(final byte actual, final byte expected) {255 doAssert(new SimpleAssert<Byte>(actual, expected) {256 @Override257 public void doAssert() {258 org.testng.Assert.assertEquals(actual, expected);259 }260 });261 }262 public void assertEquals(final char actual, final char expected, final String message) {263 doAssert(new SimpleAssert<Character>(actual, expected, message) {264 @Override265 public void doAssert() {266 org.testng.Assert.assertEquals(actual, expected, message);267 }268 });269 }270 public void assertEquals(final char actual, final char expected) {271 doAssert(new SimpleAssert<Character>(actual, expected) {272 @Override273 public void doAssert() {274 org.testng.Assert.assertEquals(actual, expected);275 }276 });277 }278 public void assertEquals(final short actual, final short expected, final String message) {279 doAssert(new SimpleAssert<Short>(actual, expected, message) {280 @Override281 public void doAssert() {282 org.testng.Assert.assertEquals(actual, expected, message);283 }284 });285 }286 public void assertEquals(final short actual, final short expected) {287 doAssert(new SimpleAssert<Short>(actual, expected) {288 @Override289 public void doAssert() {290 org.testng.Assert.assertEquals(actual, expected);291 }292 });293 }294 public void assertEquals(final int actual, final int expected, final String message) {295 doAssert(new SimpleAssert<Integer>(actual, expected, message) {296 @Override297 public void doAssert() {298 org.testng.Assert.assertEquals(actual, expected, message);299 }300 });301 }302 public void assertEquals(final int actual, final int expected) {303 doAssert(new SimpleAssert<Integer>(actual, expected) {304 @Override305 public void doAssert() {306 org.testng.Assert.assertEquals(actual, expected);307 }308 });309 }310 public void assertNotNull(final Object object) {311 doAssert(new SimpleAssert<Object>(object, null) {312 @Override313 public void doAssert() {314 org.testng.Assert.assertNotNull(object);315 }316 });317 }318 public void assertNotNull(final Object object, final String message) {319 doAssert(new SimpleAssert<Object>(object, null, message) {320 @Override321 public void doAssert() {322 org.testng.Assert.assertNotNull(object, message);323 }324 });325 }326 public void assertNull(final Object object) {327 doAssert(new SimpleAssert<Object>(object, null) {328 @Override329 public void doAssert() {330 org.testng.Assert.assertNull(object);331 }332 });333 }334 public void assertNull(final Object object, final String message) {335 doAssert(new SimpleAssert<Object>(object, null, message) {336 @Override337 public void doAssert() {338 org.testng.Assert.assertNull(object, message);339 }340 });341 }342 public void assertSame(final Object actual, final Object expected, final String message) {343 doAssert(new SimpleAssert<Object>(actual, expected, message) {344 @Override345 public void doAssert() {346 org.testng.Assert.assertSame(actual, expected, message);347 }348 });349 }350 public void assertSame(final Object actual, final Object expected) {351 doAssert(new SimpleAssert<Object>(actual, expected) {352 @Override353 public void doAssert() {354 org.testng.Assert.assertSame(actual, expected);355 }356 });357 }358 public void assertNotSame(final Object actual, final Object expected, final String message) {359 doAssert(new SimpleAssert<Object>(actual, expected, message) {360 @Override361 public void doAssert() {362 org.testng.Assert.assertNotSame(actual, expected, message);363 }364 });365 }366 public void assertNotSame(final Object actual, final Object expected) {367 doAssert(new SimpleAssert<Object>(actual, expected) {368 @Override369 public void doAssert() {370 org.testng.Assert.assertNotSame(actual, expected);371 }372 });373 }374 public void assertEquals(final Collection<?> actual, final Collection<?> expected) {375 doAssert(new SimpleAssert<Collection<?>>(actual, expected) {376 @Override377 public void doAssert() {378 org.testng.Assert.assertEquals(actual, expected);379 }380 });381 }382 public void assertEquals(final Collection<?> actual, final Collection<?> expected,383 final String message) {384 doAssert(new SimpleAssert<Collection<?>>(actual, expected, message) {385 @Override386 public void doAssert() {387 org.testng.Assert.assertEquals(actual, expected, message);388 }389 });390 }391 public void assertEquals(final Object[] actual, final Object[] expected, final String message) {392 doAssert(new SimpleAssert<Object[]>(actual, expected, message) {393 @Override394 public void doAssert() {395 org.testng.Assert.assertEquals(actual, expected, message);396 }397 });398 }399 public void assertEqualsNoOrder(final Object[] actual, final Object[] expected,400 final String message) {401 doAssert(new SimpleAssert<Object[]>(actual, expected, message) {402 @Override403 public void doAssert() {404 org.testng.Assert.assertEqualsNoOrder(actual, expected, message);405 }406 });407 }408 public void assertEquals(final Object[] actual, final Object[] expected) {409 doAssert(new SimpleAssert<Object[]>(actual, expected) {410 @Override411 public void doAssert() {412 org.testng.Assert.assertEquals(actual, expected);413 }414 });415 }416 public void assertEqualsNoOrder(final Object[] actual, final Object[] expected) {417 doAssert(new SimpleAssert<Object[]>(actual, expected) {418 @Override419 public void doAssert() {420 org.testng.Assert.assertEqualsNoOrder(actual, expected);421 }422 });423 }424 public void assertEquals(final byte[] actual, final byte[] expected) {425 doAssert(new SimpleAssert<byte[]>(actual, expected) {426 @Override427 public void doAssert() {428 org.testng.Assert.assertEquals(actual, expected);429 }430 });431 }432 public void assertEquals(final byte[] actual, final byte[] expected,433 final String message) {434 doAssert(new SimpleAssert<byte[]>(actual, expected, message) {435 @Override436 public void doAssert() {437 org.testng.Assert.assertEquals(actual, expected, message);438 }439 });440 }441 public void assertEquals(final Set<?> actual, final Set<?> expected) {442 doAssert(new SimpleAssert<Set<?>>(actual, expected) {443 @Override444 public void doAssert() {445 org.testng.Assert.assertEquals(actual, expected);446 }447 });448 }449 public void assertEquals(final Set<?> actual, final Set<?> expected, final String message) {450 doAssert(new SimpleAssert<Set<?>>(actual, expected, message) {451 @Override452 public void doAssert() {453 org.testng.Assert.assertEquals(actual, expected, message);454 }455 });456 }457 public void assertEquals(final Map<?, ?> actual, final Map<?, ?> expected) {458 doAssert(new SimpleAssert<Map<?, ?>>(actual, expected) {459 @Override460 public void doAssert() {461 org.testng.Assert.assertEquals(actual, expected);462 }463 });464 }465 public void assertNotEquals(final Object actual, final Object expected, final String message) {466 doAssert(new SimpleAssert<Object>(actual, expected, message) {467 @Override468 public void doAssert() {469 org.testng.Assert.assertNotEquals(actual, expected, message);470 }471 });472 }473 public void assertNotEquals(final Object actual, final Object expected) {474 doAssert(new SimpleAssert<Object>(actual, expected) {475 @Override476 public void doAssert() {477 org.testng.Assert.assertNotEquals(actual, expected);478 }479 });480 }481 void assertNotEquals(final String actual, final String expected, final String message) {482 doAssert(new SimpleAssert<String>(actual, expected, message) {483 @Override484 public void doAssert() {485 org.testng.Assert.assertNotEquals(actual, expected, message);486 }487 });488 }489 void assertNotEquals(final String actual, final String expected) {490 doAssert(new SimpleAssert<String>(actual, expected) {491 @Override492 public void doAssert() {493 org.testng.Assert.assertNotEquals(actual, expected);494 }495 });496 }497 void assertNotEquals(final long actual, final long expected, final String message) {498 doAssert(new SimpleAssert<Long>(actual, expected, message) {499 @Override500 public void doAssert() {501 org.testng.Assert.assertNotEquals(actual, expected, message);502 }503 });504 }505 void assertNotEquals(final long actual, final long expected) {506 doAssert(new SimpleAssert<Long>(actual, expected) {507 @Override508 public void doAssert() {509 org.testng.Assert.assertNotEquals(actual, expected);510 }511 });512 }513 void assertNotEquals(final boolean actual, final boolean expected, final String message) {514 doAssert(new SimpleAssert<Boolean>(actual, expected, message) {515 @Override516 public void doAssert() {517 org.testng.Assert.assertNotEquals(actual, expected, message);518 }519 });520 }521 void assertNotEquals(final boolean actual, final boolean expected) {522 doAssert(new SimpleAssert<Boolean>(actual, expected) {523 @Override524 public void doAssert() {525 org.testng.Assert.assertNotEquals(actual, expected);526 }527 });528 }529 void assertNotEquals(final byte actual, final byte expected, final String message) {530 doAssert(new SimpleAssert<Byte>(actual, expected, message) {531 @Override532 public void doAssert() {533 org.testng.Assert.assertNotEquals(actual, expected, message);534 }535 });536 }537 void assertNotEquals(final byte actual, final byte expected) {538 doAssert(new SimpleAssert<Byte>(actual, expected) {539 @Override540 public void doAssert() {541 org.testng.Assert.assertNotEquals(actual, expected);542 }543 });544 }545 void assertNotEquals(final char actual, final char expected, final String message) {546 doAssert(new SimpleAssert<Character>(actual, expected, message) {547 @Override548 public void doAssert() {549 org.testng.Assert.assertNotEquals(actual, expected, message);550 }551 });552 }553 void assertNotEquals(final char actual, final char expected) {554 doAssert(new SimpleAssert<Character>(actual, expected) {555 @Override556 public void doAssert() {557 org.testng.Assert.assertNotEquals(actual, expected);558 }559 });560 }561 void assertNotEquals(final short actual, final short expected, final String message) {562 doAssert(new SimpleAssert<Short>(actual, expected, message) {563 @Override564 public void doAssert() {565 org.testng.Assert.assertNotEquals(actual, expected, message);566 }567 });568 }569 void assertNotEquals(final short actual, final short expected) {570 doAssert(new SimpleAssert<Short>(actual, expected) {571 @Override572 public void doAssert() {573 org.testng.Assert.assertNotEquals(actual, expected);574 }575 });576 }577 void assertNotEquals(final int actual, final int expected, final String message) {578 doAssert(new SimpleAssert<Integer>(actual, expected, message) {579 @Override580 public void doAssert() {581 org.testng.Assert.assertNotEquals(actual, expected, message);582 }583 });584 }585 void assertNotEquals(final int actual, final int expected) {586 doAssert(new SimpleAssert<Integer>(actual, expected) {587 @Override588 public void doAssert() {589 org.testng.Assert.assertNotEquals(actual, expected);590 }591 });592 }593 public void assertNotEquals(final float actual, final float expected, final float delta,594 final String message) {595 doAssert(new SimpleAssert<Float>(actual, expected, message) {596 @Override597 public void doAssert() {598 org.testng.Assert.assertNotEquals(actual, expected, delta, message);599 }600 });601 }602 public void assertNotEquals(final float actual, final float expected, final float delta) {603 doAssert(new SimpleAssert<Float>(actual, expected) {604 @Override605 public void doAssert() {606 org.testng.Assert.assertNotEquals(actual, expected, delta);607 }608 });609 }610 public void assertNotEquals(final double actual, final double expected, final double delta,611 final String message) {612 doAssert(new SimpleAssert<Double>(actual, expected, message) {613 @Override614 public void doAssert() {615 org.testng.Assert.assertNotEquals(actual, expected, delta, message);616 }617 });618 }619 public void assertNotEquals(final double actual, final double expected, final double delta) {620 doAssert(new SimpleAssert<Double>(actual, expected) {621 @Override622 public void doAssert() {623 org.testng.Assert.assertNotEquals(actual, expected, delta);624 }625 });626 }627}...
Source:TestNGActiTimeHeader.java
...34import org.testng.annotations.AfterClass;5import org.testng.annotations.Test;6import org.testng.annotations.BeforeClass;7import org.testng.AssertJUnit;8import pom.ActiTimeHeader;9import pom.LoginPage;10import utils.Utility;1112import java.io.File;13import java.io.IOException;14import java.util.concurrent.TimeUnit;1516import org.testng.annotations.BeforeClass;17import org.testng.Assert;18import org.testng.ITestResult;19import org.testng.annotations.AfterClass;20import org.testng.annotations.Test;21import org.testng.asserts.SoftAssert;2223import com.aventstack.extentreports.ExtentReports;24import com.aventstack.extentreports.ExtentTest;25import com.aventstack.extentreports.reporter.ExtentHtmlReporter;2627import base.Base;2829import org.apache.poi.EncryptedDocumentException;30import org.openqa.selenium.WebDriver;31import org.testng.annotations.AfterMethod;32import org.testng.annotations.AfterTest;33import org.testng.annotations.BeforeMethod;34import org.testng.annotations.BeforeTest;35import org.testng.annotations.Parameters;3637383940public class TestNGActiTimeHeader extends Base {41 42 private int testId;43 WebDriver driver;44 ActiTimeHeader actiTimeHeader;45 LoginPage loginPage;46 SoftAssert softAssert;47 static ExtentTest test;48 static ExtentHtmlReporter reporter;49 50 @BeforeTest51 @Parameters("browser")52 public void launchBrowser(String browser) {53 reporter = new ExtentHtmlReporter("test-output"+File.separator+"ExtendReport"+File.separator+"extendReport.html");54 ExtentReports extend = new ExtentReports();55 extend.attachReporter(reporter);56 57 if(browser.equals("chrome")) 58 {59 driver = Base.openChromeDriver();60 }61 if(browser.equals("firefox"))62 {63 driver = Base.openFirefoxDriver();64 }65 if(browser.equals("opera"))66 {67 driver = Base.openOperaDriver();68 }69 if(browser.equals("edge"))70 {71 driver = Base.openEdgeDriver();72 }73 74 //driver.get("http://localhost/login.do");75 driver.manage().window().maximize();76 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);77 }78 79 @BeforeClass80 public void initiationPomClasses(){81 System.out.println("initiationPomClasses");82 actiTimeHeader = new ActiTimeHeader(driver);83 loginPage = new LoginPage(driver);84 85 86 }87 88 @BeforeMethod89 public void launchActiTime() throws EncryptedDocumentException, IOException{90 System.out.println("LaunchActiTime");91 driver.get("http://localhost/login.do");92 loginPage.sendUser();93 loginPage.sendPassword();94 loginPage.clickOnLoginButton();95 96 softAssert = new SoftAssert();97 }98 99 @Test100 public void toVerifyUserTab(){101 testId= 101;102 System.out.println("toVerifyUserTab");103 actiTimeHeader.clickOnUser();104 String url = driver.getCurrentUrl();105 System.out.println(url);106 AssertJUnit.assertEquals(url, "http://localhost/administration/userlist.do");107108 }109 110 @Test111 public void toVerifyReportTab() {112 testId= 102;113 System.out.println("toVerifyReportTab");114 actiTimeHeader.clickOnReport();115 String url = driver.getCurrentUrl();116 System.out.println(url);117// if (url.equals("http://localhost/reports/reports.do"))118// {119// System.out.println("PASS");120// }121// else {122// System.out.println("FAIL");123// }124 125 // here we used Soft Assert126 AssertJUnit.assertEquals(url, "http://localhost/reports/reports.do");127 softAssert.assertAll();128 }129 130 @Test131 public void toVerifyTaskTab() {132 testId = 103;133 System.out.println("toVerifyTaskTab");134 actiTimeHeader.clickOntask();135 String url = driver.getCurrentUrl();136 System.out.println(url);137// if (url.equals("http://localhost/tasks/otasklist.do"))138// {139// System.out.println("PASS");140// }141// else {142// System.out.println("FAIL");143// }144 145 146 AssertJUnit.assertEquals(url, "http://localhost/tasks/otasklist.do");147 148 }149 150 @AfterMethod151 public void logoutFromActiTime(ITestResult result) throws IOException{152 System.out.println("logoutFromActiTime");153 154 if(ITestResult.FAILURE == result.getStatus())155 {156 Utility.saveScreenshot(driver, testId);157 }158 159 actiTimeHeader.clickOnLogout();160 }
...
Source:TestNgtestTest.java
1package seniumWebdriver;2import java.util.Date;3import org.testng.Assert;4import org.testng.annotations.AfterClass;5import org.testng.annotations.AfterMethod;6import org.testng.annotations.AfterSuite;7import org.testng.annotations.AfterTest;8import org.testng.annotations.BeforeClass;9import org.testng.annotations.BeforeMethod;10import org.testng.annotations.BeforeSuite;11import org.testng.annotations.BeforeTest;12import org.testng.annotations.Test;13public class TestNgtestTest extends TestNgtest {14 @BeforeMethod15 public void beforeMethod() {16 Date date=new Date();17 System.out.println("Before Method");18 System.out.println(date);19 }20 @AfterMethod21 public void afterMethod() {22 Date date=new Date();23 System.out.println("After Method");24 System.out.println(date);25 }26 @BeforeClass27 public void beforeClass() {28 Date date=new Date();29 System.out.println("Bofore Class");30 System.out.println(date);31 32 }33 @AfterClass34 public void afterClass() {35 Date date=new Date();36 37 System.out.println("After Class");38 System.out.println(date);39}40 41 @BeforeTest42 public void beforeTest() {43 44 System.out.println("Before Test");45 Date date=new Date();46 System.out.println(date);47 48 }49 @AfterTest50 public void afterTest() {51 Date date=new Date();52 System.out.println("After Test");53 System.out.println(date);54 }55 56 @BeforeSuite57 public void beforeSuite() {58 Date date=new Date();59 System.out.println("Before Suite");60 System.out.println(date);61 62 }63 @AfterSuite64 public void afterSuite() {65 Date date=new Date();66 System.out.println("After Suite");67 System.out.println(date);68 }69 70 @Test(dependsOnMethods="TestCase5")71 public void TestCase1() {72 Assert.assertEquals(testAdd(5, 5), 10);73 System.out.println("Test1 Run Sucessfully");74 }75 @Test(dependsOnMethods="TestCase3")76 public void TestCase2() {77 Assert.assertNotEquals(testSub(10, 5), 7);78 System.out.println("Test2 Run Sucessfully");79 80}81 @Test()82 public void TestCase3() {83 Assert.assertNotNull(testAdd(5, 5), "Not Null");84 System.out.println("Test3 Run Sucessfully");85 86 }87 @Test(priority=1)88 public void TestCase4() {89 Assert.assertNull(testNull(), "Null String");90 System.out.println("Test4 Run Sucessfully");91 92 }@Test(priority=2)93 public void TestCase5() {94 Assert.assertSame(testAdd(5, 5), testSub(15, 5), "Assert Same");95 System.out.println("Test5 Run Sucessfully");96 97 }@Test98 public void TestCase6() {99 Assert.assertNotSame(testMul(5, 5), testDiv(125, 3));100 System.out.println("Test6 Run Sucessfully");101 102 }103 @Test104 public void TestCase7() {105 Assert.assertFalse(testFalse(), "Assert False");106 107 System.out.println("Test7 Run Sucessfully");108 109 }110 @Test111 public void TestCas8() {112 Assert.assertTrue(testTrue(),"Assert True");113 System.out.println("Test8 Run Sucessfully");114 115 }116}...
Source:BenchTileTest.java
1package com.pld.Test;2import java.io.IOException;3import org.testng.Assert;4import org.testng.annotations.AfterClass;5import org.testng.annotations.AfterMethod;6import org.testng.annotations.AfterSuite;7import org.testng.annotations.BeforeClass;8import org.testng.annotations.BeforeMethod;9import org.testng.annotations.BeforeSuite;10import org.testng.annotations.Test;11import com.pld.Base.PldTestBase;12import com.pld.Pages.BenchTile;13import com.pld.Pages.Loginpage;14import com.pld.Pages.UtilizationPage;15public class BenchTileTest extends PldTestBase{16 public Loginpage loginpage;17 public UtilizationPage utilizationpage;18 public BenchTile benchtile;19 public BenchTileTest() throws IOException {20 super();21 22 }23 24 /*@BeforeClass()25 public void setUp() throws IOException {26 System.out.println("Bench Tile Execution started");27 28 loginpage = new Loginpage();29 utilizationpage = loginpage.pldLogin(prop.getProperty("username"), prop.getProperty("password"));30 benchtile = new BenchTile();31 32 }*/33 34 @BeforeClass35 public void setUp() throws Exception36 {37 System.out.println("Bench Tile Execution started");38 benchtile = new BenchTile();39 }40 41 @Test(priority =1)42 public void verifyBenchTileTest()43 {44 Assert.assertTrue(benchtile.verifyBenchTile());45 46 47 }48 49 @Test(priority=2)50 public void verifyGetSkillTest()51 {52 53 Assert.assertTrue(benchtile.getSkillvalues());54 }55 56 @Test(priority=3)57 public void verifySkillsInShortageTest()58 {59 Assert.assertTrue(benchtile.verifySkillsinshortage());60 }61 @Test(priority=4)62 public void verifyMTMTest()63 {64 Assert.assertTrue(benchtile.verifyMTMText());65 }66 @Test(priority=5)67 public void verifyHCTest()68 {69 Assert.assertTrue(benchtile.verifyHCText());70 }71 @Test(priority=6)72 public void verifyCriticalSkillWithExcessTest()73 {74 Assert.assertTrue(benchtile.verifyCriticalskillwithexcessText());75 }76 @Test(priority=7)77 public void verifyBenchMixTest()78 {79 Assert.assertTrue(benchtile.verifyBenchMixText());80 }81 @Test(priority=8)82 public void verifyBenchByDateTest()83 {84 Assert.assertTrue(benchtile.verifyBenchbyDate());85 }86 @Test(priority=9)87 public void verifyAsOfTest()88 {89 Assert.assertTrue(benchtile.verifyAsOfText());90 }91 @Test(priority=10)92 public void verifyAgedBenchTest()93 {94 Assert.assertTrue(benchtile.verifyAgedBenchText());95 }96 @Test(priority=11)97 public void verifyWeekRoll_OffsTest()98 {99 Assert.assertTrue(benchtile.verifyWeekRoll_offsText());100 }101 102 @Test(priority=12)103 public void verifyPercentOfTotTest()104 {105 Assert.assertTrue(benchtile.verifyPercentOfTot());106 } 107 108 @Test(priority=13)109 public void verifyBenchDoughnutChartTest()110 {111 Assert.assertTrue(benchtile.verifyBenchDoughnutChart());112 }113 /*@AfterClass114 public void tearDown()115 {116 117 System.out.println("Bench tile execution ended");118 driver.quit();119 }*/120}...
Source:DemandTest.java
1package com.pld.Test;2import java.io.IOException;3import java.sql.Driver;4import org.testng.Assert;5import org.testng.annotations.AfterClass;6import org.testng.annotations.AfterMethod;7import org.testng.annotations.AfterSuite;8import org.testng.annotations.BeforeClass;9import org.testng.annotations.BeforeMethod;10import org.testng.annotations.BeforeSuite;11import org.testng.annotations.Test;12import com.pld.Base.PldTestBase;13import com.pld.Pages.DemandPage;14import com.pld.Pages.Loginpage;15import com.pld.Pages.UtilizationPage;16public class DemandTest extends PldTestBase{17 18 public Loginpage loginpage;19 public UtilizationPage utilizationpage;20 public DemandPage demandpage;21 public DemandTest() throws IOException {22 super();23 24 }25 26 /*@BeforeClass27 public void setUp() throws IOException28 {29 30 System.out.println("Demand Tile execution started");31 initialization();32 loginpage = new Loginpage();33 utilizationpage = loginpage.pldLogin(prop.getProperty("username"), prop.getProperty("password"));34 demandpage = new DemandPage();35 36 }*/37 38 @BeforeClass39 public void setUp() throws Exception40 {41 System.out.println("Demand Tile execution started");42 demandpage = new DemandPage();43 }44 45 46 @Test47 public void verifyDemandTileTest()48 {49 Assert.assertTrue(demandpage.verifyDemandTile());50 51 }52 @Test53 public void verifyBacklogTest()54 {55 Assert.assertTrue(demandpage.verifyBacklogText());56 }57 /*@Test58 public void verifyCEandD_IBM_InteractiveTest()59 {60 Assert.assertTrue(demandpage.verifyCEandD_IBM_InteractiveText());61 }*/62 @Test63 public void verifyCommitTest()64 {65 Assert.assertTrue(demandpage.verifyCommitText());66 }67 /*@Test68 public void verifyJavaAndWebDevTest()69 {70 71 Assert.assertTrue(demandpage.verifyJavaAndWebDevtext());72 }73 @Test74 public void verifyNotApplicableTest()75 {76 Assert.assertTrue(demandpage.verifyNotApplicableText());77 }*/78 @Test79 public void verifyOpenPositionsTest()80 {81 82 Assert.assertTrue(demandpage.verifyOpenPositionsText());83 }84 @Test85 public void verifyOpptyTest()86 {87 Assert.assertTrue(demandpage.verifyOpptyText());88 }89 @Test90 public void verifyUnassignedTest()91 {92 Assert.assertTrue(demandpage.verifyUnassignedText());93 94 95 96 }97 98 @Test99 public void verifyDemandbarChartTest()100 {101 Assert.assertTrue(demandpage.verifyDemand_barchart());102 103 104 105 }106 107 @Test108 public void verifyDemandChartTest()109 {110 Assert.assertTrue(demandpage.verifyDemandChart());111 112 113 114 }115 116 /*@AfterClass117 public void tearDown()118 {119 System.out.println("No of elements executed are : 9");120 121 System.out.println("Demand Tile execution ended");122 driver.quit();123 }*/124}...
Source:BandPyramidTest.java
1package com.pld.Test;2import java.io.IOException;3import org.testng.Assert;4import org.testng.ITestResult;5import org.testng.annotations.AfterClass;6import org.testng.annotations.AfterMethod;7import org.testng.annotations.AfterSuite;8import org.testng.annotations.BeforeClass;9import org.testng.annotations.BeforeMethod;10import org.testng.annotations.BeforeSuite;11import org.testng.annotations.Test;12import com.pld.Base.PldTestBase;13import com.pld.Pages.BandPyramidTile;14import com.pld.Pages.Loginpage;15import com.pld.Pages.UtilizationPage;16public class BandPyramidTest extends PldTestBase{17 18 public static Loginpage loginpage;19 public static UtilizationPage utilizationpage;20 public static BandPyramidTile bandpyramidpage;21 public BandPyramidTest() throws IOException {22 super();23 24 }25 26 /*@BeforeClass27 public void setUp() throws IOException28 {29 System.out.println("BandPyramid tile execution started");30 initialization();31 loginpage = new Loginpage();32 utilizationpage = loginpage.pldLogin(prop.getProperty("username"), prop.getProperty("password"));33 bandpyramidpage = new BandPyramidTile();34 35 }*/36 37 @BeforeClass38 public void setUp() throws Exception39 {40 System.out.println("BandPyramid tile execution started");41 bandpyramidpage = new BandPyramidTile();42 }43 @Test44 public void verifyBandPyramidTileTest()45 {46 Assert.assertTrue(bandpyramidpage.verifyBandPyramidTile());47 48 }49 50 @Test51 public void verifyActualHeadCountTest()52 {53 Assert.assertTrue(bandpyramidpage.verifyActualHeadCount());54 }55 56 @Test57 public void verifyAvgbandTest()58 {59 Assert.assertTrue(bandpyramidpage.verifyAvgband());60 }61 62 @Test63 public void verifyBandTest()64 {65 Assert.assertTrue(bandpyramidpage.verifyBandText());66 }67 68 @Test69 public void verifyDistribTest()70 {71 Assert.assertTrue(bandpyramidpage.verifyDistribText());72 }73 74 @Test75 public void verifyHCTest()76 {77 Assert.assertTrue(bandpyramidpage.verifyHCText());78 }79 80 @Test81 public void verifyTotalHCTest()82 {83 Assert.assertTrue(bandpyramidpage.verifyTotalHeadCount());84 }85 86 @Test87 public void verifyExcludeContractorsTest()88 {89 Assert.assertTrue(bandpyramidpage.verifyExcludeContractorstext());90 }91 92 @Test93 public void verifyBandPyramidChartTest()94 {95 Assert.assertTrue(bandpyramidpage.verifybandPyramidChart());96 }97 98 /*@AfterClass99 public void tearDown()100 {101 System.out.println("No of elements verified are: 7");102 103 System.out.println("Bench Pyramid tile execution ended");104 driver.quit();105 }*/106}...
Source:FTEbyContractCoadingTest.java
1package com.pld.Test;2import java.io.IOException;3import org.testng.Assert;4import org.testng.annotations.AfterClass;5import org.testng.annotations.AfterMethod;6import org.testng.annotations.AfterSuite;7import org.testng.annotations.BeforeClass;8import org.testng.annotations.BeforeMethod;9import org.testng.annotations.BeforeSuite;10import org.testng.annotations.Test;11import com.pld.Base.PldTestBase;12import com.pld.Pages.FTEbyContractCodingTile;13import com.pld.Pages.Loginpage;14import com.pld.Pages.UtilizationPage;15public class FTEbyContractCoadingTest extends PldTestBase {16 public Loginpage loginpage;17 public UtilizationPage utilizationpage;18 public FTEbyContractCodingTile ftebycontractcoading;19 public FTEbyContractCoadingTest() throws IOException {20 super();21 22 }23 24 /*@BeforeClass25 public void setUp() throws IOException26 {27 System.out.println("FTE By Contract Coading tile execution started");28 initialization();29 loginpage = new Loginpage();30 utilizationpage = loginpage.pldLogin(prop.getProperty("username"), prop.getProperty("password"));31 ftebycontractcoading = new FTEbyContractCodingTile();32 33 34 }*/35 36 @BeforeClass37 public void setUp() throws Exception38 {39 System.out.println("FTE By Contract Coading tile execution started");40 ftebycontractcoading = new FTEbyContractCodingTile();41 }42 @Test43 public void verifyFTETileTest()44 {45 Assert.assertTrue(ftebycontractcoading.verifyFTEbyContractCodingTile());46 }47 @Test48 public void verifyCAITest()49 {50 Assert.assertTrue(ftebycontractcoading.verifyCAIText());51 }52 @Test53 public void verifyCPTTest()54 {55 Assert.assertTrue(ftebycontractcoading.verifyCPTText());56 }57 @Test58 public void verifyDSandiXTest()59 {60 Assert.assertTrue(ftebycontractcoading.verifyDSandiXText());61 }62 @Test63 public void verifyADMITest()64 {65 ftebycontractcoading.verifyADMIText();66 }67 @Test68 public void verifyCAMSTest()69 {70 ftebycontractcoading.verifyCAMSText();71 72 }73 74 @Test75 public void verifyFTEByContractCodingDoughnutChartTest()76 {77 Assert.assertTrue(ftebycontractcoading.verifyFTEbyContractCodingDoughnutChart());78 79 }80 /*@AfterClass81 public void tearDown()82 83 {84 System.out.println("No of elements executed are: 6");85 System.out.println("FTE by Contract Coading tile execution ended");86 driver.quit();87 }*/88}...
Source:MultiBrowserTest.java
...3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.firefox.FirefoxDriver;6import org.openqa.selenium.ie.InternetExplorerDriver;7import org.testng.Assert;8import org.testng.annotations.AfterClass;9import org.testng.annotations.AfterMethod;10import org.testng.annotations.AfterTest;11import org.testng.annotations.BeforeClass;12import org.testng.annotations.BeforeMethod;13import org.testng.annotations.BeforeTest;14import org.testng.annotations.Listeners;15import org.testng.annotations.Parameters;16import org.testng.annotations.Test;17import org.testng.util.RetryAnalyzerCount;18import java.util.concurrent.TimeUnit;19public class MultiBrowserTest 20 21{ 22 public WebDriver driver;23 // Configure for multi browser drivers24 //@Parameters("browser")25 26 @BeforeTest27 public void beforeTest() {28 driver = new FirefoxDriver();29 30 System.out.println("In Beforetest");31 } 32 33 @Test(priority = 1) 34 public void googleLaunch() throws InterruptedException{35 //System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\chromedriver.exe");36 37 System.out.println("entering into test cases_GOOGLE");38 driver.get("http://google.com");39 System.out.println("google.com opened");40 41 42 Thread.sleep(2000);43 driver.findElement(By.id("gbqfq")).sendKeys("testing"); 44 //driver.quit();45 driver.findElement(By.id("gbqfb")).click();46 47 }48 49 @Test(priority = 2) 50 public void assertion2(){51 System.out.println("In failed test2");52 Assert.fail("Assert.fail2");53 54 }55 56 @Test(priority = 3) 57 public void assertion3(){58 System.out.println("In failed test3");59 Assert.fail("Assert.fail3");60 61 }62 @AfterTest63 public void aftertest() {64 65 System.out.println("In AfterTest1");66 driver.quit();67 System.out.println("In AfterTest2");68 69 }70}...
Assert
Using AI Code Generation
1import org.testng.Assert;2import org.testng.annotations.Test;3public class TestNGAssertTest {4 public void testAssertEquals() {5 String str1 = "TestNG is working fine";6 String str2 = "TestNG is working fine";7 Assert.assertEquals(str1, str2);8 }9 public void testAssertTrue() {10 String str = "TestNG is working fine";11 Assert.assertTrue(str.contains("TestNG"));12 }13 public void testAssertFalse() {14 String str = "TestNG is working fine";15 Assert.assertFalse(str.contains("JUnit"));16 }17 public void testAssertNull() {18 String str = null;19 Assert.assertNull(str);20 }21 public void testAssertNotNull() {22 String str = "TestNG is working fine";23 Assert.assertNotNull(str);24 }25}26TestNGAssertTest > testAssertEquals() PASSED27TestNGAssertTest > testAssertTrue() PASSED28TestNGAssertTest > testAssertFalse() PASSED29TestNGAssertTest > testAssertNull() PASSED30TestNGAssertTest > testAssertNotNull() PASSED31TestNGAssertTest > testAssertEquals() PASSED32TestNGAssertTest > testAssertTrue() PASSED33TestNGAssertTest > testAssertFalse() PASSED34TestNGAssertTest > testAssertNull() PASSED35TestNGAssertTest > testAssertNotNull() PASSED
Assert
Using AI Code Generation
1import org.testng.Assert;2import org.testng.annotations.Test;3import org.testng.asserts.SoftAssert;4public class TestNGSoftAssert {5 public void testSoftAssert() {6 SoftAssert softAssert = new SoftAssert();7 System.out.println("softAssert1");8 softAssert.assertEquals(12, 13, "softAssert1: values are not equal");9 System.out.println("softAssert2");10 softAssert.assertTrue(12>13, "softAssert2: condition is false");11 System.out.println("softAssert3");12 softAssert.assertFalse(12<13, "softAssert3: condition is true");13 System.out.println("softAssert4");14 softAssert.assertNotNull(null, "softAssert4: object is null");15 System.out.println("softAssert5");16 softAssert.assertNull("abc", "softAssert5: object is not null");17 System.out.println("softAssert6");18 softAssert.assertSame("abc", "xyz", "softAssert6: objects are not same");19 System.out.println("softAssert7");20 softAssert.assertNotSame("abc", "abc", "softAssert7: objects are same");21 System.out.println("softAssert8");22 softAssert.assertAll();23 }24 public void testHardAssert() {25 System.out.println("hardAssert1");26 Assert.assertEquals(12, 13, "hardAssert1: values are not equal");27 System.out.println("hardAssert2");28 Assert.assertTrue(12>13, "hardAssert2: condition is false");29 System.out.println("hardAssert3");30 Assert.assertFalse(12<13, "hardAssert3: condition is true
1static <T> T checkNotNull(T e) {2 if (e == null) {3 throw new NullPointerException();4 }5 return e;6}7
1import static com.googlecode.catchexception.apis.BDDCatchException.*;23@Test4public void testFooThrowsIndexOutOfBoundsException() {56 when(() -> foo.doStuff());78 then(caughtException()).isInstanceOf(IndexOutOfBoundsException.class);910}11
TestNG is a Java-based open-source framework for test automation that includes various test types, such as unit testing, functional testing, E2E testing, etc. TestNG is in many ways similar to JUnit and NUnit. But in contrast to its competitors, its extensive features make it a lot more reliable framework. One of the major reasons for its popularity is its ability to structure tests and improve the scripts' readability and maintainability. Another reason can be the important characteristics like the convenience of using multiple annotations, reliance, and priority that make this framework popular among developers and testers for test design. You can refer to the TestNG tutorial to learn why you should choose the TestNG framework.
You can push your abilities to do automated testing using TestNG and advance your career by earning a TestNG certification. Check out our TestNG certification.
Watch this complete tutorial to learn how you can leverage the capabilities of the TestNG framework for Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!