【问题标题】:how to get assertion error in testng emailable report?如何在 testng 电子邮件报告中获取断言错误?
【发布时间】:2017-11-21 05:23:13
【问题描述】:

下面是我的代码

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 已通过,因为我已经给出了 try catch(我想这样做是因为即使某些断言失败,我也希望测试运行)。但是 testng 报告没有显示所有断言都失败了。 Assert.assertTrue(BottomTable1.contains(Stocklist),Stocklist+ "Stock Exchange Not Found");

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

【问题讨论】:

    标签: selenium selenium-webdriver


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 2013-02-18
      • 2018-01-27
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 2017-07-06
      相关资源
      最近更新 更多