提问者:小点点

如何在测试电子邮件报告中获取断言错误?


下面是我的代码

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;

                    }
            }

有了这段代码,我得到了test58通过的testng报告,我给了try catch(我想这样做是因为我希望测试即使某些断言失败也能运行)。但是testng报告并没有显示所有断言都失败了。Asser. assertTrue(BottomTable1.包含(库存清单),库存清单“未找到证券交易所”);

我想打印测试报告中失败的断言。请帮帮我


共1个答案

匿名用户

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

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

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