2

I am trying to figure out if something is possible (or if I am being a bit silly.)

I have a very simple Excel sheet - 2 columns, one column is a list of search terms, the second column is a list of expected URLS. I run this via selenium, it will navigate to google, open the Excel sheet, search for the term and if the expected result appears pass the test. It does this for the three rows in the sheet. All good. However, I was hoping to @Test each of the rows - but I can't quite figure out how to achieve this.

Below is the test code, like I said I can't quite get this to work - at present it runs but appears as a single test which has had 3 different searches.

@Test
@Severity(SeverityLevel.CRITICAL)
public void driveDatData() throws InterruptedException, BiffException, IOException {
    parameters = WebDriverSteps.currentDriver.toString();
    steps.openWebPage("http://www.google.co.uk");

    FileInputStream fi = new FileInputStream("C:\\temp\\sites.xls");
    Workbook w = Workbook.getWorkbook(fi);
    Sheet s = w.getSheet("Sheet1");

    for (int i=1;i<=s.getRows(); i++) 
    {
        if (i > 1) 
        {
            steps.goToURL("http://www.google.co.uk");
        }

        steps.search(s.getCell("A" + i).getContents());
        Assert.assertTrue("Check the " + s.getCell("A" + i).getContents() + " link is present", steps.checkForTextPresent(s.getCell("B" + i).getContents()));
    }

}
4

4 回答 4

0

对于任何好的测试,代码复杂度都应该是 1。任何循环都应该被参数化测试代替。请查看https://github.com/junit-team/junit/wiki/Parameterized-tests

于 2015-03-20T09:09:47.600 回答
0

我建议您将Feed4JUnit添加到您的项目中。它是高度可配置的,并且是我所知道的唯一一个可以通过 Excel 支持开箱即用地进行参数化 JUnit 和 TestNG 测试的库。

Feed4Junit

@RunWith(Feeder.class)
public class AddTest {

    @Test
    @Source("http://buildserv.mycompany.com/wiki/myproject/tests/add.xls")
    public void testAdd(int param1, int param2, int expectedResult) {
        int result = MyUtil.add(param1, param2);
        assert result == expectedResult;
    }
}

此示例直接来自 Feed4Junit 站点。

重要的是要注意参数是从左到右读取的。

每一行都是一个测试,并且必须在每一列中具有有效值,即如果一列对于 3 行具有相同的值,则它需要出现在每一行中。

于 2015-03-23T03:56:06.833 回答
0

几件事:

我认为将测试数据放在外部 Excel 表中对您来说有意义吗?否则,更常见的方法是将测试数据作为测试资源保留在项目中。此外,我认为有各种框架可以帮助您从 excel 文件中检索测试数据。

说了这么多:

更改代码以将测试数据填充到数据结构中@Before,编写不同@Test的 s 来测试不同的事物。这还将测试数据的检索与实际测试分开(这在可维护性和责任方面是一件好事)。如果文件读取/性能是一个问题,您可能希望@BeforeClass每个测试类只使用一次。

@Before
// read file, store information into myTestData

@Test
// tests against myTestData.getX

@Test
// tests against myTestData.getY
于 2015-03-19T20:38:38.950 回答
0

经过一番努力,使用@RunWith 设法在 JUnit 中工作。找到了一些示例,虽然不是我想要的,但它们提供了足够的洞察力,让我对 JUnit 感到满意。

于 2015-03-24T06:00:38.873 回答