如何在Selenium ExtentReport中打印表头和表数据?

时间:2018-03-28 08:42:57

标签: selenium webdriver extentreports selenium-extent-report

我尝试使用以下代码将WebTable数据打印到ExtentReport日志中,但它不会打印正确的数据。我能够打印数据但th不打印在仅打印td的表格中。如果我从0开始第一次循环,那么它会抛出异常java.lang.ArrayIndexOutOfBoundsException。你能帮帮我吗.....

enter image description here

环境详情:

  • Selenium-Java:3.8.1
  • 范围报告:3.1.3
  • Java:1.8

HTML code:

<html>
<head>
    <title>This is sample page</title>
</head>
<body>
    <table border=1>
        <tr>
            <th>Sr. no.</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Address</th>
        </tr>
        <tr>
            <td>01.</td>
            <td>Steve</td>
            <td>Smith</td>
            <td>London, United Kingdom</td>
        </tr>
        <tr>
            <td>02.</td>
            <td>Peter</td>
            <td>Parker</td>
            <td>Seattle, United State</td>
        </tr>
        <tr>
            <td>03.</td>
            <td>Rock</td>
            <td>Handsome</td>
            <td>NY, United State</td>
        </tr>
        <tr>
            <td>04.</td>
            <td>John</td>
            <td>Matrin</td>
            <td>New Jersy, United State</td>
        </tr>
    </table>
</body>
</html>

我的Selenium代码是:

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.aventstack.extentreports.markuputils.MarkupHelper;

public class sampleClass {

    public static void main(String[] args) {
        driver.get("webdriver.chrome.driver","path/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("pathOfFile/sample.html");
        String[][] data = null;
        List<WebElement> rows = driver.findElements(By.tagName("tr"));
        int rowSize = rows.size();
        for(int i=1; i<rowSize; i++)
        {
             WebElement exampleTableRow = rows.get(i);
             List<WebElement> cells = exampleTableRow.findElements(By.tagName("td"));
             int cellSize = cells.size();
             if(data == null)
             {
                  data = new String[rowSize][cellSize];
             }
                  for(int j=0; j<cellSize; j++)
             {
                  data[i][j] = cells.get(j).getText();
             }
         }
      childTest.info(MarkupHelper.createTable(data));
    }    
}

0 个答案:

没有答案