Steerpike
Steerpike

Reputation: 1843

Performing JUnit assertions against multiple array elements - how to handle when one fails?

I'm trying to write a webdriver test for a content management system. The test takes different 'articles' created in earlier tests, and aims to check them in turn.
Here is my array:

public static String[] articlesArray = {articleOnePath, articleTwoPath, articleThreePath, articleFourPath,};

...where 'path' is a relative URL appended to a base URL.

I then set the test up with a @BeforeClass like so: @BeforeClass

public static void setup() {
    driver = Driver.get();
    articlePage = new ArticlePage(driver);

    for (String s : articlesArray) {  
        driver.get(baseURL + s);

        articlePage.articlePageHasLoaded();

        if(s == null){
            System.out.println(s + " could not be found");
        }
    }

}

I then run my test assertions similar to the following: @Test public void twitterShareButtonPresent() {

    List<WebElement> twitterShareButton = articlePage.checkTwitterShareButton();
    if (twitterShareButton.size() == 0) {
        fail("No Twitter share button was found on the page!:" + " " + baseURL + "/" + s);
    } else ;

}

The problems:

  1. If one article fails to be created in prior tests, say articleFour, then articleThreePath will equal null. This test will bomb when it gets to articleThreePath in the for loop and will not run tests for articleFour
  2. I need to be able to print to console, which article has failed, but using the current methods, where I use the 's' string name, I get only a 'null'.

I appreciate there is probably a far better way of doing this - can people please suggest.

EDIT 1:

public static HashMap articlesHashMap = new HashMap<String, String>();

@BeforeClass

public static void setup() {
    driver = Driver.get();
    articlePage = new ArticlePage(driver);

    articlesHashMap.put(articleOneName, articleOnePath);
    articlesHashMap.put(articleTwoName, articleTwoPath);


    for (articlesHashMap) {
        driver.get(baseURL + ??);

        if(?? == null){
            System.out.println(???);
        }

        articlePage.articlePageHasLoaded();

        }
    } 

Upvotes: 0

Views: 169

Answers (1)

nwill001
nwill001

Reputation: 745

I would recommend using @Parameterized in Junit to cycle through the different pages and run that test. This is because the @Before and @Test will run only once without it. The @Parameterized notation will allow you to have 4 different results for this test. This will also more appropriately separate out the web element references for each page opened.

Right now it appears you will be creating a single instance of articalPage, then opening 4 browser windows, and then attempt to reference webElements of that page intance. If 4 windows exist selenium will not know which one you are trying to reference. You will either end up running a single test on the very last opened page or end up getting a stale web element reference.

If you do end up dealing with multiple browser windows you will need to handle them so selenium can reference them. Selenium has methods to do this like .getWindowHandle(), .switchTo(), etc. All of these methods allow you to reference them and switch focus. The focus determines where it looks for the web elements you are working with.

Upvotes: 1

Related Questions