如何在testng emailable报告中获得断言错误?

时间:2017-11-21 05:23:14

标签: selenium selenium-webdriver

以下是我的代码

public void test58() throws FileNotFoundException{

        for(int i=16; i<65; i++){
                    News_details nd=PageFactory.initElements(driver, News_details.class); 
                    nd.Stock_Exchange_List();
                    //click on edit stocklist
                    driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[3]/table/tbody/tr/td/a/span")).click();

                    driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[3]/form/div[2]/div[1]/table/tbody/tr[5]/td[4]/a/img")).click();
                    driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[3]/form/div[2]/div[1]/table/tbody/tr[6]/td[4]/a/img")).click();
                    driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[3]/form/div[2]/div[1]/table/tbody/tr[7]/td[4]/a/img")).click();
                    driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[3]/form/div[2]/div[1]/table/tbody/tr[8]/td[4]/a/img")).click();
                    driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[3]/form/div[2]/div[1]/table/tbody/tr[9]/td[4]/a/img")).click();
                    driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[3]/form/div[2]/div[1]/table/tbody/tr[10]/td[4]/a/img")).click();
                    driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[3]/form/div[2]/div[1]/table/tbody/tr[11]/td[4]/a/img")).click();
                    driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[3]/form/div[2]/div[1]/table/tbody/tr[12]/td[4]/a/img")).click();
                    List<WebElement> els = driver.findElements(By.xpath("//input[@type='checkbox']"));
                        for( WebElement el : els ) {
                            if ( el.isSelected() ) {
                                el.click();
                            }
                        }
                    try{
                    //Select an stock exchange
                    driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[3]/form/div[2]/div[1]/table/tbody/tr["+i+"]/td[2]/input")).click();

                    }
                    catch(org.openqa.selenium.NoSuchElementException error)
                    {
                            continue;
                    }
                    //save
                    driver.findElement(By.id("navpanel_fwd")).click();
                    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
                    String Stocklist=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[3]/div[2]/div[2]/strong")).getText();
                    driver.navigate().to(url);

                    BottomTable1=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[3]/form/div[2]/div[1]/div[3]/table/tbody/tr/td/table[2]/tbody/tr[12]/td[2]")).getText();
                    try{

                        Assert.assertTrue(BottomTable1.contains(Stocklist),Stocklist+ "Stock Exchange Not Found");

                    }
                    catch(AssertionError err){
                        err.printStackTrace();
                        System.out.println(err);
                        //System.setOut(new PrintStream(new FileOutputStream("d://output.txt")));
                        //Reporter.log("PASS/FAIL");
                        //throw err; 
                        continue;

                    }
            }

使用这段代码,我得到testng报告,test58传递了sice我给了try catch(我想这样做是因为我希望测试运行,即使某些断言失败)。但是testng报告没有显示所有断言都失败了。 Assert.assertTrue(BottomTable1.contains(Stocklist),Stocklist+ "Stock Exchange Not Found");

我想在testng报告中打印失败的断言。请帮帮我

1 个答案:

答案 0 :(得分:1)

使用 SoftAssert - 它会在@Test期间收集错误(不会抛出异常)。

SoftAssert s_assert = new SoftAssert();
s_assert.assertTrue(BottomTable1.contains(Stocklist),Stocklist+ "Stock Exchange Not Found");

不要使用Assert.assertTrue。这是一个坚定的断言。它将立即抛出AssertException,测试被标记为失败,失败的消息将打印在堆栈跟踪中,而不是在报告中。