Question

I have a page that contains 50 odd WebElements that I'd like to assert have the correct text. I am using Page Objects so the Test is seperate from the object and the Test is where the assertions must live.

I don't want to have 50 seperate @Test's for each element, so I need some kind of collection, List or array to hold the text values and then assert against each one. However, I don't want the test to fall over if one text value is wrong (a likely scenario with a page that is updated frequently and contains 50 values). This points to seperate @Test's for each assertion?

So I need to do a driver.findElement(By.....for each element, in the PageObject.

I then need to pass a the value of the WebElement as a String(s), using .getText()), over to the Test Class, preferably in one method.

I need to assert the text is correct for each of the 50 values in the Test Class, preferably under one @Test, but not have the test fail if one value is incorrect.

I've been wrestling with this for some hours, sorry if this is unclear and apologies for the chunk of text; I'm something of a beginner - if I can clarify please ask. There is obviously a common approach to this that I am missing.

Thanks

Was it helpful?

Solution

If you're using JUnit 4.0+, you could use parameterized tests.

  • Test class with annotation @RunWith(Parameterized.class)
  • Static method with annotation @Parameters (this returns a nested array of params)
  • Tests in the class will be run once for each set of params

Example from the wiki link above:

@RunWith(Parameterized.class)
public class FibonacciTest {
    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {

                 { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 },{ 6, 8 }  
           });
    }

    private int fInput;

    private int fExpected;

    public FibonacciTest(int input, int expected) {
        fInput= input;
        fExpected= expected;
    }

    @Test
    public void test() {
        assertEquals(fExpected, Fibonacci.compute(fInput));
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top