Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Please Find my code, its not Returning the Array object value, its return only one array object

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;
    }

variable first its contain Four values ("name","name1","name2","name3") after convert this string value into an array (actual_role) then its return only one value ("name")

Please clarify what is the Problem for the above code

share|improve this question

You re-initialize string array on every step in your loop.

You should do it only once.

   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() ] );

BTW, I've converted your example to use intermediary ArrayList, because you don't know the actual data size, and it's error-prone to reinitialize arrays on the fly.

If the signature of the method, you are implementing, is not dictated by external framework, I suggest you use List<String> as a return type instead of String[].

share|improve this answer

You always instantiate your array inside the loop:

actual_role = new String[] {first};

try instead:

actual_role[i] = first;

When i is the current index.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.