Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串数组方法不返回数组对象-Selenium WebDriver_Java_Selenium Webdriver_Testng_Pageobjects - Fatal编程技术网

Java 字符串数组方法不返回数组对象-Selenium WebDriver

Java 字符串数组方法不返回数组对象-Selenium WebDriver,java,selenium-webdriver,testng,pageobjects,Java,Selenium Webdriver,Testng,Pageobjects,请找到我的代码,它不返回数组对象值,它只返回一个数组对象 public String[] verify_userRole(String[] Expected_role) { String[] actual_role = new String[4]; String first; WebElement role_table = driver.findElement(By .xpath("//*[@id='tblListV

请找到我的代码,它不返回数组对象值,它只返回一个数组对象

public String[] verify_userRole(String[] Expected_role) {
        String[] actual_role = new String[4];
        String first;
        WebElement role_table = driver.findElement(By
                .xpath("//*[@id='tblListView']/tbody[1]"));
        List<WebElement> allRows = role_table.findElements(By.tagName("tr"));

        for (WebElement row : allRows) {
            List<WebElement> cells = row.findElements(By.tagName("td"));

            for (WebElement cell : cells) {
                first = cell.getText().toString();
                actual_role = new String[] {first};

                }
        }
        return actual_role;
    }
public String[]验证用户角色(String[]应为用户角色){
字符串[]实际角色=新字符串[4];
先串;
WebElement role_table=driver.findElement(按
.xpath(“/*[@id='tblListView']/tbody[1]”);
List allRows=role_table.findElements(按.tagName(“tr”));
for(WebElement行:所有行){
列表单元格=行.findElements(按.tagName(“td”));
for(WebElement单元:单元){
first=cell.getText().toString();
实际角色=新字符串[]{first};
}
}
返回实际角色;
}
第一个变量包含四个值(“名称”、“名称1”、“名称2”、“名称3”) 将此字符串值转换为数组(实际的_角色)后,其仅返回一个值(“名称”)


请澄清上述代码的问题所在

在循环的每个步骤上重新初始化字符串数组

你应该只做一次

   ArrayList<String> actual_role = new ArrayList<String>( )
   for (WebElement row : allRows) {
        List<WebElement> cells = row.findElements(By.tagName("td"));

        for (WebElement cell : cells) {
            first = cell.getText().toString();
            actual_role.add(first);

        }
    }

    return (String[]) actual_role.toArray( new String[ actual_role.size() ] );
ArrayList实际角色=新建ArrayList()
for(WebElement行:所有行){
列表单元格=行.findElements(按.tagName(“td”));
for(WebElement单元:单元){
first=cell.getText().toString();
实际_角色。添加(第一个);
}
}
返回(字符串[])实际角色.toArray(新字符串[实际角色.size());
顺便说一句,我已经将您的示例转换为使用中间ArrayList,因为您不知道实际的数据大小,而且动态重新初始化数组很容易出错


如果您正在实现的方法的签名不是由外部框架指定的,我建议您使用
List
作为返回类型,而不是
String[]

您总是在循环中实例化数组:

actual_role = new String[] {first};
请尝试:

actual_role[i] = first;
当i是当前索引时