If you lot know, nosotros own got already acquire almost testng difficult assertions (Example assertEquals, assertNotEquals, etc..) which nosotros tin john role In our webdriver test for software spider web application. Hard assertion examples links are given on THIS PAGE. View all those examples i yesteryear i to acquire difficult assertions. Let me tell you lot i affair almost difficult assertions, When difficult assertion volition neglect Inside whatever attempt method, remaining execution of that specific attempt method volition endure aborted. Now If you lot wants to proceed remaining attempt business office execution fifty-fifty If assertion fails as well as too you lot wants to study assertion failure In testng trial study as well as then you lot tin john role testng soft assertion method.
Soft Assertions In Selenium WebDriver Software Testing Tool
To role testng soft assertion, you lot own got to role testng SoftAssert class. This degree volition helps to non throw an exception on assertion failure as well as recording failure. If you lot volition role soft assertion as well as then your software spider web application's attempt execution volition rest proceed fifty-fifty If whatever assertion fails. Another most Important affair Is your assertion failure volition endure reported In study hence that you lot tin john stance It at terminate of test. You tin john role soft assertion when you lot are using multiple assertions In same attempt method as well as you lot wants to execute all of them fifty-fifty If whatever i In betwixt fails.
Let us human face at really uncomplicated instance of testng difficult assertion as well as soft assertion to run across the divergence betwixt both of them. In bellow laissez passer example, hard_assert_text() and soft_assert_text() each own got iv assertions. Used difficult assertions In hard_assert_text() method as well as soft assertions In soft_assert_text() method for software spider web application to depict divergence betwixt both of them.
Run bellow given instance In your eclipse amongst testng framework.
Run bellow given instance In your eclipse amongst testng framework.
package Testng_Pack; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.testng.asserts.SoftAssert; world degree Soft_Assert { //Created object of testng SoftAssert degree to role It's Properties. SoftAssert s_assert = novel SoftAssert(); String Actualtext; WebDriver driver = novel FirefoxDriver(); @BeforeClass world void load_url(){ driver.manage().window().maximize(); driver.get(" "); } @Test //In this method, If whatever assertion fails as well as then execution volition endure aborted. world void hard_assert_text() { Actualtext = driver.findElement(By.xpath("//h2/span")).getText(); //Text on expected side Is written Incorrect intentionally to acquire neglect this assertion. Assert.assertEquals(Actualtext, "Tuesday, 01 Jan 2014", "1st assert failed."); System.out.println("Hard Assertion -> 1st pagetext assertion executed."); Assert.assertEquals(Actualtext, "Tuesday, 28 Jan 2014", "2nd assert failed."); System.out.println("Hard Assertion -> 2d pagetext assertion executed."); driver.findElement(By.xpath("//input[@value='Show Me Alert']")).click(); String Alert_text = driver.switchTo().alert().getText(); driver.switchTo().alert().accept(); Assert.assertEquals(Alert_text, "Hi.. is warning message!", "Alert Is InCorrect"); System.out.println("Hard Assertion -> 1st warning assertion executed."); Assert.assertEquals(Alert_text, "Hi.. This is warning message!", "Alert Is Correct"); System.out.println("Hard Assertion -> 2d warning assertion executed."); } @Test //In this method, Test execution volition non abort fifty-fifty If whatever assertion fail. Full Test volition endure executed. world void soft_assert_text() { Actualtext = driver.findElement(By.xpath("//h2/span")).getText(); //Text on expected side Is written Incorrect intentionally to acquire neglect this assertion. s_assert.assertEquals(Actualtext, "Tuesday, 01 Jan 2014", "1st assert failed."); System.out.println("Soft Assertion -> 1st pagetext assertion executed."); s_assert.assertEquals(Actualtext, "Tuesday, 28 Jan 2014", "2nd assert failed."); System.out.println("Soft Assertion -> 2d pagetext assertion executed."); driver.findElement(By.xpath("//input[@value='Show Me Alert']")).click(); String Alert_text = driver.switchTo().alert().getText(); driver.switchTo().alert().accept(); //Alert expected text Is written Incorrect intentionally to acquire neglect this assertion. s_assert.assertEquals(Alert_text, "Hi.. is warning message!", "Alert Is InCorrect"); System.out.println("Soft Assertion -> 1st warning assertion executed."); s_assert.assertEquals(Alert_text, "Hi.. This is warning message!", "Alert Is Correct"); System.out.println("Soft Assertion -> 2d warning assertion executed."); s_assert.assertAll(); } @Test world void wait_and_click(){ WebDriverWait hold off = novel WebDriverWait(driver, 15); wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='submitButton']"))); driver.findElement(By.xpath("//input[@id='submitButton']")).click(); } @AfterClass world void Closebrowser(){ driver.quit(); } }
On execution completion, you lot volition acquire bellow given trial In console.
Soft Assertion -> 1st pagetext assertion executed. Soft Assertion -> 2d pagetext assertion executed. Soft Assertion -> 1st warning assertion executed. Soft Assertion -> 2d warning assertion executed. PASSED: wait_and_click FAILED: hard_assert_text
As per console result, soft_assert_text() method Is executed sum as well as printed all statements fifty-fifty failed ii assertions. On other side, hard_assert_text() method has non printed whatever disputation In console because software attempt execution aborted on get-go assertion failure.
Now If you lot human face at testng attempt trial report, both failures of soft assertion has been reported In testng study (You tin john stance this post service to run across HOW TO VIEW TESTNG TEST RESULT REPORT). That way your assertion failure has been reported without attempt abortion. Look at bellow laissez passer our testng attempt trial report.