尽管使用了软断言,测试仍然失败

时间:2018-03-06 11:11:29

标签: java testng assertions

我正在尝试使用第一个断言。我只想在条件为true

时状态为PASSGRACEfalse时声明FAIL
ExcelDataConfig edc = new ExcelDataConfig("C:\\Users\\COM959\\Desktop\\Report\\All_City.xlsx");

            //Configuring cell and font style
            CellStyle style1 = edc.configFontPass();
            CellStyle style2 = edc.configFontGrace();
            CellStyle style3 = edc.configFontFail();

            //Counting the number of rows in the sheet
            int rows=edc.getRowCount();

            //Declaring boolean variable to be used for assertions
            boolean b = false;

            //Declaring SoftAssertion object
            SoftAssert softAssertion= new SoftAssert();

            for(int i=1;i<=rows;i++) {

                hs.clickSearchBar();

                //Retrieving city name from excel
                String str = edc.getExcelData(i, 0);

                //Defining point to start assertion
                test = extent.createTest("Test for city: "+str);

                //Clearing data from the search bar
                ss.clearSearchInput();

                //Entering city name in the search bar
                ss.enterSearchInput(str);

                driver.hideKeyboard();

                Thread.sleep(2000);

                //Add all text fields shown on the screen to the List
                By mySelector = By.xpath("//android.widget.TextView");
                List<MobileElement> myElements = driver.findElements(mySelector);
                //Counting the occurrence of text fields
                int count = 0;
                //Traversing the List elements
                for(WebElement e : myElements) {
                    count++;
                    //Comparing the city names of excel and auto suggest list
                    if(e.getText().equalsIgnoreCase(str)) {
                        //Calculating index
                        int index = (int) Math.ceil((count-2)/2);
                        //Defining the criteria for status 
                        if(index<=3) {
                            //Pass status for when index value is less than or equal to 3
                            edc.setExcelDataString(i, 1, "PASS");
                            //Changing font Style for pass condition
                            edc.setFontStyle(i, 1, style1);
                        }
                        else if(index>3) {
                            //Status= Grace when index value is greater than 3
                            edc.setExcelDataString(i, 1, "GRACE");                          
                            //Changing font style for grace condition
                            edc.setFontStyle(i, 1, style2);
                        }
                        //Writing index value to the excel sheet
                        edc.setExcelDataInt(i, 2, index);

                        //Clicking on the first matched string 
                        e.click();
                        //Retrieving place name and eloc from the screen
                        try {
                            edc.setExcelDataString(i, 3, sr.getPlaceName());
                            edc.setExcelDataString(i, 4, sr.getEloc());
                        }
                        catch(NoSuchElementException ex) {
                            ex.getMessage();
                        }
                        //Pressing 'X' button adjacent to the search bar 
                        sr.clickXButton();

//WANT TO ASSERT TRUE HERE FOR WHEN THE STATUS IS PASS OR GRACE
                        softAssertion.assertTrue(true);

                        //break the List element loop if matched string if found
                        break;
                    }
                    else{
//WANT TO ASSERT FALSE HERE FOR WHEN CONDITION FAILS

                        softAssertion.assertTrue(false);

                        //If no matches are found in the auto suggest list then Status = Fail
                        edc.setExcelDataString(i, 1, "FAIL");
                        //Changing font style for fail condition
                        edc.setFontStyle(i, 1, style3);
                    }
                    //Writing date and time in the excel file
                    edc.setExcelDataString(i, 5, DateConfig.getDate());


                    softAssertion.assertAll();

                }
                //Opening FileOutputStream to write the date in the excel sheet
                edc.writeExcelData();
            }
            //Closing the workbook
            edc.closeWorkbook();

0 个答案:

没有答案