Java 对多个数组元素执行JUnit断言-当一个数组元素失败时如何处理?

Java 对多个数组元素执行JUnit断言-当一个数组元素失败时如何处理?,java,selenium,selenium-webdriver,webdriver,junit4,Java,Selenium,Selenium Webdriver,Webdriver,Junit4,我正在尝试为内容管理系统编写一个webdriver测试。该测试采用早期测试中创建的不同“文章”,旨在依次检查它们。 这是我的阵列: public static String[] articlesArray = {articleOnePath, articleTwoPath, articleThreePath, articleFourPath,}; …其中“path”是附加到基本URL的相对URL 然后我使用@BeforeClass设置测试,如下所示: @课前 public static voi

我正在尝试为内容管理系统编写一个webdriver测试。该测试采用早期测试中创建的不同“文章”,旨在依次检查它们。
这是我的阵列:

public static String[] articlesArray = {articleOnePath, articleTwoPath, articleThreePath, articleFourPath,};
…其中“path”是附加到基本URL的相对URL

然后我使用@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");
        }
    }

}
然后,我运行类似于以下内容的测试断言: @试验 公共作废twitterShareButtonPresent(){

列出twitterShareButton=articlePage.checkTwitterShareButton();
如果(twitterShareButton.size()==0){
失败(“在页面上找不到Twitter共享按钮!:“+”“+baseURL+”/“+s”);
}否则;
}
问题是:

  • 如果在先前的测试中,比如articleFour,未能创建一个项目,那么articleThreePath将等于null。此测试在到达for循环中的articleThreePath时将爆炸,并且不会运行ArticleThree的测试
  • 我需要能够打印到控制台,这篇文章失败了,但是使用当前的方法,在这里我使用's'字符串名称,我只得到一个'null'
  • 我很感激有一种更好的方法可以做到这一点——请大家提出建议

    编辑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();
    
            }
        } 
    
    publicstatichashmap articlesHashMap=newhashmap();
    @课前
    公共静态无效设置(){
    driver=driver.get();
    articlePage=新的articlePage(驱动程序);
    articlesHashMap.put(articleOneName,articleOnePath);
    articlesHashMap.put(articleTwoName,articleTwoPath);
    对于(articlesHashMap){
    获取(baseURL+??);
    如果(?==null){
    系统输出打印项数(??);
    }
    articlePage.articlePageHasLoaded();
    }
    } 
    
    我建议在Junit中使用@Parameterized循环浏览不同的页面并运行该测试。这是因为@Before和@Test在没有它的情况下只运行一次。@Parameterized表示法将允许您在此测试中获得4个不同的结果。这也将更恰当地为每个打开的页面分离web元素引用

    现在看来,您将创建一个ArtialPage实例,然后打开4个浏览器窗口,然后尝试引用该页面的webElements。如果存在4个窗口,selenium将不知道您尝试引用哪个窗口。您要么在最后打开的页面上运行一个测试,要么得到一个过时的web元素引用


    如果您最终要处理多个浏览器窗口,则需要处理它们,以便selenium可以引用它们。Selenium有这样做的方法,如.getWindowHandle()、.switchTo()等。所有这些方法都允许您引用它们并切换焦点。焦点决定了它在何处查找您正在使用的web元素。

    这里的解决方案很简单。您必须将初始数组修改为字典/哈希:……这是因为您需要知道两件事:文章的名称(大概)和文章路径。然后,在执行任何其他操作之前,您需要修改
    for
    循环,以在第一步检查文章路径是否为空。如果为空,则打印文章的名称。因此,第1步,修改你的文章列表,以获取文章的名称。@Arran-非常感谢。您是对的,打印文章名(在另一个测试中创建和捕获并导入到这个类中)会很有用。请原谅我的无知,但我只是在学习,还没有使用哈希。我已经用edit1升级了OP-我不确定如何使用HashMap访问for循环中的每个文章名。
    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();
    
            }
        }