Reputation: 95
I'm doing some automated testing and I'm trying to save some items from a web element list to a string array, there should be some parsing involved but I'm not sure how, see code snippet below
public void I_should_see_the_following_folders(DataTable expectedData) throws Throwable {
int tbl1; int tbl2;
List<List<String>> featureTable = expectedData.raw();
WebElement folders = driver.findElement(By.id("folders"));
List <WebElement> emailFolders = folders.findElements(By.className("folder-name"));
List<String> foo = new ArrayList<String>();
for (List<String> featuresList : expectedData.raw())
foo.add(featuresList.get(0));
tbl1 = emailFolders.size();
tbl2 = featureTable.size();
List<String> webList = new ArrayList<String>();
for(int i=0;i<tbl1;i++){
webList.add(emailFolders.get(0));
}
}
what I'm trying to do is take a datatable list of items, convert it into a string array and then take a list of items from the webpage and also store it into a string array and then compare each array to determine if the elements are present and are the same in not particular order.
I think I got the data table array list to work, but need some help with the web elements array list.
Thanks!!!
Upvotes: 0
Views: 32480
Reputation: 175
There may be an easier way to convert a List of WebElements to String[] array, but this generic method works -
// iterate through all elements adding to array we are returning
public static String[] listToStringArray(List<WebElement> inList) {
String[] outArray = new String[inList.size()]; // instantiate Array
for (int i = 0; i < inList.size(); i++) {
outArray[i] = inList.get(i).getText();
}
}
return outArray;
}
Upvotes: 0
Reputation: 30136
Change this:
for (int i=0; i<tbl1; i++)
webList.add(emailFolders.get(0));
To this:
for (WebElement emailFolder : emailFolders)
webList.add(emailFolder.getAttribute(XXX));
Then, replace XXX
with the name of the attribute which stores the actual folder name.
A few examples:
<tag><class="folder-name" id="folder1"></tag>
, replace XXX
with "id"
<tag><class="folder-name" value="folder1"></tag>
, replace XXX
with "value"
<tag><class="folder-name">folder1</tag>
, replace XXX
with "innerHTML"
BTW, instead of getAttribute("innerHTML")
, you can simply use getText()
.
Upvotes: 0
Reputation: 1115
Please try using following sample code, change xpath/data accordingly
List<WebElement> resultList = _driver.findElements(By.xpath("//img[contains(@src,'tab-close.png')]/preceding-sibling::span[@class='tab-name ng-binding']"));
for (WebElement resultItem : resultList){
String tabname=resultItem.getText();
if(resultItem.getText().equalsIgnoreCase("Dashboard")){
GlobalFunctions.clickOnButtonCustomised(false, _driver, By.xpath("//span[@class='tab-name ng-binding' and contains(text(),'"+tabname+"')]/following-sibling::img[contains(@src,'tab-close.png')]"), "");
}
}
Upvotes: 0