Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

1 like 0 dislike
4.5k views
by Contributing Tester (78 points)
retagged by
Suppose, if i used assertion to verify the condition, its verified and then status is shown as 'failed'. But it will not execute next test. I used soft assertion, its verified and its executed to next test. But its status is shown as 'Passed'. How to verify the condition.

String actualTitle = driver.getTitle();

String expectedTitle = "Dashboard";

Thread.sleep(3000);

wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Register a Patient")));

softAssert.assertEquals(actualTitle, expectedTitle);

1 Answer

0 like 0 dislike
by Master (1.2k points)
selected by
 
Best answer

softAssert will only do the comparison part and show up the result on the console.

Say,

If you execute below code,

SoftAssert softAssertion= new SoftAssert();

System.out.println("softAssert Method Was Started");

softAssertion.assertTrue(false);

System.out.println("softAssert Method Was Executed");

Above will give below output.

softAssert Method Was Started
softAssert Method Was Executed

To make sure suite fail, use 

softAssertion.assertAll();

in your tearDown method with annotation @After.

by Contributing Tester (78 points)
This is my code:
When the condition is failed it will continue and status should be 'Failed'
try{
String actualTitle = driver.getTitle();
String expectedTitle = "Dashboard";
softAssert.assertEquals(actualTitle, expectedTitle);
} catch (Exception e)
{
softAssert.assertTrue(false);
softAssert.assertAll();
}
@AfterTest
public void tearDown() throws Exception {
driver.close();
}
   
    }
I got this result in console
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
by Master (1.2k points)
Can you use softAssert.assertAll(); in @AfterTest?

@AfterTest
public void tearDown() throws Exception {


driver.close();
softAssert.assertAll();
}

try that and let us know.
by Contributing Tester (78 points)
Sir,
I got this result. @AfterTest is failed.Here Signin should be failed
PASSED: Signin

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
    Configuration Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================
by Master (1.2k points)
Can you post your complete code? You can remove sensitive information like password and internal app URL.
by Contributing Tester (78 points)
This is my code. After view the code, Please give some input to enhance myself.Is this code correct .Give some procedural way to create automation script.Is this code is Ok?

package mytestpackage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
//import com.google.common.base.Verify;
//import com.google.common.base.Function;
import org.testng.asserts.SoftAssert;

public class Login {
    WebDriver driver;
    WebDriverWait wait;
    XSSFWorkbook workbook;
    SoftAssert softAssert = new SoftAssert();
    XSSFSheet sheet;
    XSSFCell cell;

    @BeforeTest
    public void TestSetup() {
        // Set the path of the Firefox driver.
        //System.setProperty("webdriver.gecko.driver", "D:\\BALA\\geckodriver.exe");
        driver = new FirefoxDriver();
        driver.manage().window().maximize();   
        // driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        wait = new WebDriverWait(driver, 25);
    }
    private XSSFCell getCellValue(int row, int column) {
        XSSFCell cell = sheet.getRow(row).getCell(column, Row.CREATE_NULL_AS_BLANK);
        return cell;
    }
    public void ExcelRead() throws IOException {
        File src = new File("D:\\BALA\\TestData1.xlsx");
        FileInputStream finput = new FileInputStream(src);
        workbook = new XSSFWorkbook(finput);
    }

    @Test(priority = 1)
    public void Signin() throws IOException, InterruptedException {
        ExcelRead();
        sheet = workbook.getSheetAt(0);
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            try {

                cell = getCellValue(i, 1);
                String cellValue = getCellValueAsString(cell);
                wait.until(ExpectedConditions.elementToBeClickable(By.id("LG")));
                driver.findElement(By.id("LG")).clear();
                driver.findElement(By.id("LG")).sendKeys(cellValue);
                cell = getCellValue(i, 2);
                String cellValue1 = getCellValueAsString(cell);
                // String rawValue = cell.getRawValue();
                // driver.findElement(By.id("PWD")).clear();
                driver.findElement(By.id("PWD")).sendKeys(cellValue1);
                driver.findElement(By.id("btnSubmit")).click();
                String actualTitle = driver.getTitle();
                String expectedTitle = "IntouchOnDemand: Menu";
                softAssert.assertEquals(actualTitle, expectedTitle);           
                 } catch (Exception e) {
                softAssert.assertTrue(false);
                            }
           
        }
       

    }

    public static String getCellValueAsString(Cell cell) {
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
            return cell.getRichStringCellValue().getString();
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                return cell.getDateCellValue().toString();
                // return cell.getDateCellValue().toString();
            } else {
                return cell.getNumericCellValue() + "";
                // return cell.getNumericCellValue();
            }
        case Cell.CELL_TYPE_BOOLEAN:
            return ((Boolean) cell.getBooleanCellValue()).toString();
        case Cell.CELL_TYPE_FORMULA:
            return cell.getCellFormula().toString();
        }
        return null;
    }

    /*@Test(priority = 2)
    public void Logout() throws IOException, InterruptedException {
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.dropdown-toggle.ng-binding")));
        driver.findElement(By.cssSelector("a.dropdown-toggle.ng-binding")).click();
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("span.glyphicon.glyphicon-off")));
      driver.findElement(By.cssSelector("span.glyphicon.glyphicon-off")).click(); }*/
    @AfterTest
    public void tearDown() throws Exception {

        driver.close();
        softAssert.assertAll();
       

    }

}
by Master (1.2k points)
I see in your previous comment that softAssert.assertAll() actually marked test as failed and you can see configuration failure message:

===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================

To mark test as failed,

You can simply initiate your SoftAssert inside you test and call softAssert.assertAll() at the end of your @Test

E.g.

@Test(priority = 1)
    public void Signin() throws IOException, InterruptedException {
    SoftAssert softAssert = new SoftAssert(); //declare it here
        ExcelRead();
        sheet = workbook.getSheetAt(0);
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                try {

                    cell = getCellValue(i, 1);
                    String cellValue = getCellValueAsString(cell);
                    wait.until(ExpectedConditions.elementToBeClickable(By.id("LG")));
                    driver.findElement(By.id("LG")).clear();
                    driver.findElement(By.id("LG")).sendKeys(cellValue);
                    cell = getCellValue(i, 2);
                    String cellValue1 = getCellValueAsString(cell);
                    // String rawValue = cell.getRawValue();
                    // driver.findElement(By.id("PWD")).clear();
                    driver.findElement(By.id("PWD")).sendKeys(cellValue1);
                    driver.findElement(By.id("btnSubmit")).click();
                    String actualTitle = driver.getTitle();
                    String expectedTitle = "IntouchOnDemand: Menu";
                    softAssert.assertEquals(actualTitle, expectedTitle);//Make sure that this condition fails or any exception occures
                } catch (Exception e) {
                    softAssert.assertTrue(false);
                }
       
            
        }
    //softAssert.assertTrue(false); //or uncomment this line to make sure that no matter what, your test fails.
        softAssert.assertAll();

    }
@AfterTest
    public void tearDown() throws Exception {

        driver.close();
        //softAssert.assertAll(); //This is not required.
        

    }
by Contributing Tester (78 points)
Thank you sir.Its working.Thank for you for your support.


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

...