如何在循环内的方法中使用数组

时间:2019-06-21 22:59:07

标签: java arrays selenium-webdriver

我正在使用循环列表中的URL进行Selenium中的测试。对于每个特定的URL,我想为测试中要调用的可重用方法定义单独的字符串。它们都必须不同,否则我的数据将相互覆盖。

public class LogIn extends ReusableMethod {

// here is my lists of URLS

@Test void test()throws Exception {

     String[] Urls;
     Urls = new String[3];

     Urls[0] = "site.com/us/en";
     Urls[1] = "site.com/ca/en";
     Urls[2] = "site.com/au/en";


     for (int i=0; i<Urls.length; i++) {
         openbrowser(Urls[i]);

     //openbrowser is calling a reusable method to open a browser and go to 
     each //URL and the test loops through the URLs defined above



  //------- sign in from the sign in page--------//
     RegisteredSignIn();

     CheckTitle();

// My check title method calls 
/*
public void CheckTitle () {
    String ActualTitle = driver.getTitle();


String[]  Title = new String[7];

    Title[0] = "My Site Account | Site US";
    Title[1] = "My Site Account | Site CA";
    Title[2] = "My Site Account | Site AU";

    for (int i=0; i<Title.length; i++) {

    if(ActualTitle.equals(Title[i])){
        System.out.println("Page"+ Title[i] + " is up + Registered Log on 
have passed");
    }
    else {System.err.println( Title[i] + " Page + Registerd Log on have 
FAILED");   
    }
    }
}*/


      quitbrowser();

    }

}   
}

我可以获取页面的单个标题并将其与预期标题进行比较。.但是,如何更改方法中使用的字符串并对其进行循环以保持我的Test循环进行更改?

3 个答案:

答案 0 :(得分:1)

只需包装整个方法以接受单个参数url,然后在测试用例中遍历您的url。

答案 1 :(得分:0)

我不确定我是否完全理解您的问题,但是听起来您需要以与存储需要测试的URL相似的方式存储期望的标题。这是一个示例:

public class LogIn {

    @Test
    void test() throws Exception {
        String[] urls = new String[3];

        urls[0] = "site.com/us/en";
        urls[1] = "site.com/ca/en";
        urls[2] = "site.com/au/en";

        String[] titles = new String[3];
        titles[0] = "Site Title - US";
        titles[1] = "Site Title - CA";
        titles[2] = "Site Title - AU";

        for (int i = 0; i < urls.length; i++) {
            checkTitle(urls[i], titles[i]);
        }
    }

    void checkTitle(String url, String expected) {
        openbrowser(url);

        //------- sign in from the sign in page--------//
        RegisteredSignIn();

        CheckTitle(expected);

        quitbrowser();
    }
}

答案 2 :(得分:0)

谢谢@ordonezalex我像soo那样使用您的解决方案。

public class LogIn extends ReusableMethod { 

    Test void test()throws Exception {

     String[] Urls;
     Urls = new String[3];

        Urls[0] = "https://www.nixon.com/us/en";
        Urls[1] = "https://www.nixon.com/ca/en";

        String[] Titles;
        Titles = new String[3];
        Titles[0] = "My Nixon Account | Nixon US";
        Titles[1] = "My Nixon Account | Nixon CA";

     for (int i=0; i<Urls.length; i++) {
        System.out.println(Urls.length);

        openbrowser(Urls[i]);

        RegisteredSignIn();

        CheckTitle(Titles[i]); ....