How to get the i value and perform click action? - java

.get(i) is not working
List<WebElement> prdct = driver.findElements(By.xpath("//h4[#class = 'product-name']"));
WebElement addto = driver.findElement(By.xpath("//button[#test() = 'ADD TO CART']"));
for (int i = 0; i < prdct.size(); i++) {
String Name = prdct.get(i).getText();
System.out.println(Name);
if (Name.contains("Pomegranate"))
{
addto.get
break;
}
}
I need to get the i index and perform click action but get() is working, please check the enclosed image for better understanding.
How to proceed with this?

From your posted code it seems you should do the get(i) on prdct that is the list of elements if you have to perform an action with that element.
The addto seems to be an 'ADD TO CART' button and there is only one, you can perform the action in it as well but it's not a list so there is no possibility to use get in it.
In code, you probably want prdct.get(i).click(). And maybe after that addto.click().

addto is not a List but a WebElement, hence you cannot use get(i) (WebElement interface does not extend List).

Related

My loop only prints out one object to my tableview. I´m using Java

private void displayGroupsInRanking() {
for (int i = 0; i < 4; i++)
{
RankingANames.setItems(FXCollections.observableArrayList(groupModel.getListA().get(i).getName()));
System.out.println(RankingANames);
}
RankingBNames.setItems(FXCollections.observableArrayList(groupModel.getListB()));
RankingCNames.setItems(FXCollections.observableArrayList(groupModel.getListC()));
RankingDNames.setItems(FXCollections.observableArrayList(groupModel.getListD()));
}
I´m trying to, to get a specific attribute from an arraylist into a new arraylist. This works fine, but the listview only shows one object?
[The output1
the Code
I'll not rewrite code from your screen to show you the right way to do this but I can tell you what is wrong here.
On every iteration you are creating new collection with exactly one item and then you are using it as items list for table.
That's clearly wrong.
To solve it, you have to first prepare full list of items and then pass it to setItems method.

Is there a way to check if a ComboBox has any items in JavaFX?

Is there a way to check if a ComboBox has any items in it or whether it is empty? I have an array of ComboBoxes and I need to go through each of them, if there are no items in the ComboBox, then I must hide it. This following code doesn't seem to work:
for (ComboBox cmb : comboBoxes) {
if (cmb.getItems().isEmpty()) {
cmb.hide();
}
}
The code for checking, if the ComboBox has no items is correct, you code for hiding the ComboBoxes is incorrect however.
ComboBox.hide only closes the popup showing the items, if it's open. It does not hide the ComboBox. To hide the ComboBox, you need to set the visibility:
for (ComboBox cmb : comboBoxes) {
if (cmb.getItems().isEmpty()) {
cmb.setVisible(false);
}
}
Alternatively to call a method to hide the ComboBoxes, you can bind the visibleProperty of the ComboBoxes to their own itemsProperty with a custom binding:
List<ComboBox<String>> comboBoxes = new ArrayList<>();
for(int i = 0; i< 10; i++) {
ComboBox<String> combo = new ComboBox<>();
combo.visibleProperty().bind(Bindings.createBooleanBinding(() -> !combo.getItems().isEmpty(),
combo.itemsProperty().get()));
comboBoxes.add(combo);
}
The advantage is, that you don't have to call any methods to hide your ComboBoxes, because the binding is evaluated automatically, therefore no one can see your empty combos.
The .getItems() method returns an ObservableList<T> so you can just check its .size(). This will tell you if it's empty.
for (ComboBox cmb : comboBoxes) {
if (cmb.getItems().size() <= 0) { // or cmb.getItems().isEmpty()
cmb.setVisible(false); }
}
If the ComboBox is populated by a List of its own, you could also just check if the list is empty with the same .size() call.

JAVA For loop tableview

This piece of code has to give me all the persons in the List but because the TaSpelers.setItems is in the for loop it only gives me the latest record he found. How do I fix it that my tableview is filled with all the items?
ArrayList < PersoonBag > perLijst = pdb.zoekSpelersPerPloeg(idPloeg);
for (PersoonBag r: perLijst) {
PersoonBag persoon = new PersoonBag(r.getId(), r.getNaam(), r.getVoornaam(), r.getMyDate(), r.getOpmerking(), r.isIsTrainer(), r.getPloeg());
ObservableList < PersoonBag > spelerLijst = FXCollections.observableArrayList(persoon);
spelerLijst.sorted();
System.out.println(spelerLijst);
taSpelers.setItems(spelerLijst);
}
Note: putting it ouside my for loop i can't acces my variables **
We can't see what type of object taSpelers is because you didn't include its declaration but I would guess that you want to use a method like "addItems" instead of "setItems".
thanks James D!
answer:
Create the (empty) list before the loop, add(...) the elements to the list in the loop, and call setItems(...) after the loop

WebDriverWait. Fast check element exist or not exist

I got a problem and I don't know how to solve it right. Situation: user enters login and password, and then he could be at one of two pages.
Question is: how to check correctly in which page we are?
1. I want to use WebDriverWait, so my implicitlyWait = 0 ms,
2. I use Page Object pattern, pages were initialized with AjaxElementLocatorFactory
a. So, if I'll do method for check some element like this:
#FindBy(id = "pushOutMessage")
private WebElement messageText;
public boolean pageIsPresent() {
return messageText.isDisplayed();
}
It will be not right because if page is wrong, then WebDriver will wait N seconds for this element. So it makes my simple tests slow, very-very slow.
b. If I will check element with "findElement" - my implicity waits is 0 ms, so if element was slowly-loaded pageIsPresent returns false, even if page is right.
I hope there is some other way to do this. Need your help!
There are multiple ways you can do that. but the easiest way will be to check the elemenet counts
I would rather do
#FindBy(id = "pushOutMessage")
private List<WebElement> elements;
public int pageIsPresent() {
return elements.size();
}
And, somewhere test the pageIsPresent() is 0 or greater. if greater than 0 we know the page element was returned
And, since you are using pageobject pattern and Java I would recommand you to create an overloading to the baseClass which will check for a selector everytime you instantiate a new pageobject. I have a git repo here with TestNG. that might help
After the user enters or clicks submit or login button you can do something like this:
public Object clickLogin() {
loginElement.click();
try{
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementedLocatedBy(By.id("pushOutMessage")));
return PageFactory.initElements(driver, FirstPage.class);
} catch (TimeoutException te) {
return PageFactory.initElements(driver, SecondPage.class);
}
}

Set selected item in ComboBox by Code

I want to selected the first item of a comboBox by code if the value list have only one item. I've tried this:
Comboitem item = new Comboitem();
for(Empresa e : empresasList){
item.setValue(e.getEmpId());
item.setLabel(e.getEmpNombre());
item.setParent(cb_empresa);
}
if(empresasList.size()==1){
idEmpresa = empresasList.get(0).getEmpId();
//cb_empresa.setSelectedIndex(0);
cb_empresa.setSelectedItem(item);
}
But it does not work. I also tried the commented line:
//cb_empresa.setSelectedIndex(0);
Any help?
Thanks!
I don't know whether the item.setParent(cb_empresa); has an immediately effect to the view.
You can try setting the index later, or after refresh the cb_empresa.
First of all, your code is buggy. you have to create as many Comboitems as empresasList.size(). I guess something like
for(Empresa e : empresasList){
Comboitem item = new Comboitem();
item.setValue(e.getEmpId());
item.setLabel(e.getEmpNombre());
cb_empresa.appendItem(item);
}
if(cb_empresa.getItemCount()==1){
cb_empresa.setSelectedIndex(0);
}
Should work. However, there was a bug in ZK that the setSelectedIndex() had to be postponed, as Aloong mentioned. As far as I remember, this bug has been fixed. If not, you can use Event.echoEvent() as a workaround.
for(Empresa e : empresasList){
Comboitem item = new Comboitem();
item.setValue(e.getEmpId());
item.setLabel(e.getEmpNombre());
cb_empresa.appendChild(item);
}
if(cb_empresa.getItemCount()==1){
cb_empresa.setSelectedIndex(0);
}

Categories

Resources