Reputation: 611
Using Selenium and Cucumber, I'm trying to setup a method to store WebElements through args. Then with a second method, I'm trying to check the visibility of all elements on the list.
I have some elements located this way:
@FindBy(how = How.XPATH, using = "//*
[@id=\"content\"]/section/fieldset/form/div[1]/div[2]/input")
public WebElement formName;
@FindBy(how = How.CSS, using = "//*
[@id=\"content\"]/section/fieldset/form/div[2]/div[2]/input")
public WebElement formPassword;
@FindBy(how = How.ID, using = "remember")
public WebElement rememberMe;
@FindBy(how = How.CLASS_NAME, using = "button principal")
public WebElement loginButton;
Then I wrote this method to add WebElements in a list:
public void crearListaWebElement(WebElement... elements){
List<WebElement> webElementList = new ArrayList<>();
for (WebElement element : elements){
webElementList.add(elements);
}
}
Getting this error on .add(elements) 1º issue:
add (java.util.Collection) in List cannot be applied to (org.openqa.selenium.WebElement[])
Cannot figure out why i can't add these same-type elements to my List
Then, here is the second method to check visibility:
public void estaVisible(WebDriver(tried WebElement as well) visibleWebElements) {
boolean esvisible = driver.findElement(visibleWebElements).isDisplayed();
Assert.assertTrue(esvisible);
return esvisible;
}
Getting this error on "visibleWebElement":
findElement (org.openqa.selenium.By) in WebDriver cannot be applied to (org.openqa.selenium.WebDriver)
I understand the conflict between the 2 different types of object, but, aren't the objects found by WebDriver stored as WebElements?
Could someone put some light overhere please? Thanks in advance.
Upvotes: 2
Views: 15708
Reputation: 314
This function would return a boolean value and check whether list elements are visible or not.
public boolean isListElementsVisible(WebDriver driver, By locator) {
boolean result = false;
try {
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator));
result = true;
} catch (Exception e) {
System.out.println(e.getMessage());
result = false;
}
return result;
}
Upvotes: 0
Reputation: 1439
Cannot figure out why i can't add these same-type elements to my List -
There is, most probably a typo in your code. It should be element
instead of elements
:
for (WebElement element : elements){
webElementList.add(element);
}
Getting this error on "visibleWebElement" -
driver.findElement()
method accepts a By
object as an argument, which is a selector. But you are passing visibleWebElements
which is a WebElement
object and that is why your code is giving an error. For ex, if you want to locate an element by xpath, this is the correct way to use it:
WebElement your_element = driver.findElement(By.xpath("//whatever xpath"));
You can directly use the isDisplayed()
method on your WebElement:
public void estaVisible(WebElement visibleWebElements) {
boolean esvisible = visibleWebElements.isDisplayed();
Assert.assertTrue(esvisible);
return esvisible;
}
Upvotes: 1
Reputation: 1128
When you try to check the list of Web elements you have to check with the visibility of all elements.Try with the below code
WebDriverWait for list of web elements
Store all the elements in a list
List<WebElement> listOfAllWebElements = new ArrayList<WebElement>();
listOfAllWebElements.add(elementOne);
listOfAllWebElements.add(elementTwo);
listOfAllWebElements.add(elementThree);
WebDriverWait listWait = new WebDriverWait(driver,10);
listWait.until(ExpectedConditions.visibilityOfAllElements(listOfAllWebElements));
WebDriverWait for single web element
WebElement element = driver.findElement(By.id("element"));
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOf(element));
Upvotes: 4