在Java 7中关联一对字符串并循环它们

时间:2014-07-01 14:57:38

标签: java selenium junit

给出以下代码: -

//setup code and import statements, including: 

private static String baseURL = Environment.getTestWebsiteURL();

public static String articleOneName = ArticleCreationTest.getArticleOneName();
public static String articleTwoName = ArticleCreationTest.getArticleTwoName();
public static String articleThreeName = ArticleCreationTest.getArticleThreeName();

public static String articleOnePath = ArticleCreationTest.getArticleOnePath();
public static String articleTwoPath = ArticleCreationTest.getArticleTwoPath();
public static String articleThreePath = ArticleCreationTest.getArticleThreePath();

public static String[] articlesPathArray = {articleOnePath, articleTwoPath, articleThreePath}


    @BeforeClass
    public static void setup() {
    driver = Driver.getURL();

    for (String s : articlesArray) {
        if (s == null) {
        //tell me which articles could not be found
            System.out.println("Could not find an article for: " + s + " , perhaps it wasn't created in the prior test");
        } else {
       //assuming array holds some path values, append to the baseURL
            driver.get(baseURL + s);

        }

}

@Test...

//run some test assertions against the baseURL + path website page that is returned

我需要代码在路径变量保存值并运行测试的任何地方循环。在先前的ArticleCreationTest无法生成文章的情况下,当前解决方案没有用处,因为该变量只包含null。所以文本是:“无法找到一篇文章:null,也许它不是在先前的测试中创建的”。

我真正需要的是将articleName与articlePath相关联,因此消息类似于:“找不到ArticleOne:可能未创建”,然后针对所有创建的对象运行测试。也许是某种hashmap或2D数组?

1 个答案:

答案 0 :(得分:0)

根据给出的代码,

public static String articleOneName = ArticleCreationTest.getArticleOneName();
public static String articleTwoName = ArticleCreationTest.getArticleTwoName();
public static String articleThreeName = ArticleCreationTest.getArticleThreeName();

public static String articleOnePath = ArticleCreationTest.getArticleOnePath();
public static String articleTwoPath = ArticleCreationTest.getArticleTwoPath();
public static String articleThreePath = ArticleCreationTest.getArticleThreePath();

public static String[] articlesPathArray = {articleOnePath, articleTwoPath, articleThreePath}

好像,它是一个articleNames和articlePaths的列表

List<String> acticleNames = new ArrayList<String>();
List<String> acticlePaths = new ArrayList<String>();

List将包含要检查的字符串,可用于测试。

  

我需要代码循环遍历路径变量所在的位置   价值和运行测试

您可以通过检查当前正在检查的(s != null)来检查此情况   if (s == null)