Selenium WebDriver: Random click a row button - java

How to click a row in table by randomly?
I know how to using Random class to iterate the List but don't know how to click the button of the specific row after randomize due to xpath different.
Please provide me some idea and guideline on how to solve this.
ps: I got some idea, the button is located at the last column, so i just click on the last column button.

Here is simple way to get random number from 1 to 10
int min=1;
int max=9;
int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1);
System.out.println(randomNum);
If we think its row number, then, need to click on last column of that row.
i am imaging last column as td[6] then path will looks like
"//table/tbody/tr["+randomNum+"]/td[6]/button"

Related

I can't do selenium java random click [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 25 days ago.
Improve this question
enter image description here
I want to do a rondom click with selenium, but I can't. I searched but couldn't find it. I want to choose the sizes in the photo randomly. Because the sizes may be out of stock and my test will fail.
I ask for your support. I tried with list methods and I didn't.
As you have not shared the HTML structure, can't suggest you on the actual code, however see the algorithm below:
Solution1: Assuming the dropdown has select tag:
Get the dropdown values into a Select object, call it dropdownValues
Select dropdownValues= new Select(driver.findElement(By.name("name of the element")));
Create an instance(object) of Random class and generate a random number in the dropdown index range
Select the random index from the dropdownValues
dropdownValues.selectByIndex(randomElement);
Solution2: Assuming the dropdown does not have select tag:
Get the dropdown values into a List, example below
List<WebElement> dropdownValues = driver.findElements(By.xpath("enter xpath here"));
Create an instance(object) of Random class like below
Random rand = new Random();
Select the random index from the List dropdownValues
dropdownValues.get(rand.nextInt(dropdownValues .size()));
2 approach for this.
1.
WebElement InputBox = driver.findElement(by.xpath("xpath of input box"));`
InputBox.click();
WebElement DropDownAttribute = driver.findElement(by.xpath("//*[text()='Xs']"));
DropDownAttribute.click();
2.With Loop
List<WebElement> list = driver.findElements(by.xapth("Xpath of all the dropdown entity"));
for(WebElement ele:list){
if(ele.getText().equals("Xs")){
ele.click();
}
}
The easiest way to do this is to count the dropdown selections and then generate a random number between 1 and the max number of selections. NOTE: You don't want to include 0 because of the "Size" label inside the dropdown.
Random random = new Random();
By selectLocator = By.id("someId"); // the locator for the Size dropdown
Select dropdown = new Select(driver.findElement(selectLocator)); // get the Size dropdown element and convert it to a Select element to make it easier to work with
Integer size = dropdown.getOptions().size() + 1; // get the number of OPTIONs and add 1 to avoid selecting the "Size" OPTION
dropdown.selectByIndex(random.nextInt(size)); // select an OPTION based on the generated random number

Does NatTable need a manual refresh after repaintCell?

I have the following code snippet.
int index = getEventList().indexOf(myObj);
SelectionLayer selectionLayer = getGlazedListsGridLayer()
.getBodyLayerStack().getSelectionLayer();
getNatTable().repaintCell(0,selectionLayer.getRowIndexByPosition(index));
When I run the code shown above, the affected cells only get repainted after I click on the table displayed in the GUI. If I comment out that code and use getNatTable().refresh(); it repaints without me having to first click on the table.
Is there a way to have a cell repainted without having to click on the table displayed in the GUI? I would hate to have to call refresh() for a large table where this code may be executed many times.
No you don't need to perform some additional step in order to trigger the repainting. The issue in your code is the usage of wrong index values. You have a grid so column 0 is the row header from the NatTable perspective. I suppose you want to redraw the first column of the body, which is index 1 from the NatTable point of view. Also the row index is incorrect, as you calculate the index in the SelectionLayer but actually need the index in the table, which is at minimum +1 if you only have a column header row.
Actually your code should work by adding 1 on the index if you have one row header column and one column header row in the grid.
getNatTable().repaintCell(1, selectionLayer.getRowIndexByPosition(index) + 1);

How do I make a JButton move to an empty tile?

So I got an assignment in Java to make an 8 piece puzzle, and I chose to use JButtons for tiles (don't know if that's even possible). I've got the buttons to appear in a random order when starting it, but I have no idea how to make the tiles able to move when you click on them. So I'm wondering if someone could just point me in the right direction? I've come to conclusion that I need to use Actionlistners and saw an old user here getting tip of doing Actionlistners on every button, but I am having trouble with knowing what to writ, and do I put this in another class?
Any help is very much appreciated!
The product so far:
To make it easier, you could add ninth button which is transparent and not clickable. Every but should has his id, which would also tell hes position.
11 12 13
21 22 22
31 32 33
So for every button you already assign his random number. Now just like you use actionlistners to detect which button is clicked. And when is clicked you check if button that is neighbor is transparent. If transparent you would swap there values, one would become visible,other transparent and not clickable.
To get button neighbors you would use + and - operations.
int leftNeighbour = id - 1;
int rightNeighbour = id + 1;
int topNeighbour = id - 10;
int bottomtNeighbour = id + 10;
I assume that all buttons are saved in an array so you just go like this:
for(Button tempButton : Buttons)
{
if(leftNeighbour > 0 && leftNeighbour == tempButton.id) //we check first if button id is OK, then we compere it with tempButtons id
{
int tempButtonValue = tempButton.value;
tempButton.value = currentButton.value;
currentButton.value = tempButtonValue ;
makeButtonTransparent(tempButton);
break; //we found over neighbor so we can stop for loop
}
//then you check conditions for other neighbor id's. Butt first condition is allays different
}
I hope that I didn't make it over complicated, but this is idea that I got in few minutes, when I was thinking how I would make it with buttons. There is probably some better way to set ID's to buttons, and check if they are neighbors.

Editing a table in JavaFx

I have created a table in JavaFx. I used reflection to populate the table with numbers 1 to 100. This table contains a zone number and a description. There are 100 zones. I want the table to be editable. I have used the following code to make the cells editable.
zonesTable.setEditable(true);
zone.setEditable(true);
zone.setCellFactory(TextFieldTableCell.<Zones>forTableColumn());
description.setEditable(true);
description.setCellFactory(TextFieldTableCell.<Zones>forTableColumn());
zone.setCellValueFactory(new PropertyValueFactory<Zones, String>("rZoneNumber"));
description.setCellValueFactory(new PropertyValueFactory<Zones, String>("rDescription"));
for(int i = 0; i < 100; i++){
data.add(new Zones(i + "", ""));
}
zonesTable.setItems(data);
At the moment, this code adds numbers to the zone column and makes the zone and description column editable. However, after I type a value into the column and click the next row, my values that I input into the table disappear. I have no idea why. What do I need to do to cause my typed values to stay visible in the table after I select a different row than the one I am editing? Thanks in advance!
However, after I type a value into the column and click the next row,
my values that I input into the table disappear.
It doesn't work because there's a severe, embarrassing bug in JavaFX that Oracle refuses to fix.
The solution for you would be to press enter before you click the next row. But of course you can't request that from your users.
You may find a workaround here.
If you want to upvote the fix and comment on the bug, here's the issue.
Not enough code to determine how you work with the table.
You could tableColumn.setOnEditCommit(new CustomEventHandler)
You could tableColumn.setCellFactory(TextFieldTableCell.forTableColumn(new StringConverter() { your logic});
Those still only commit changes in the cell when Enter is pressed. So you will still need your own implementation based on which event you consider to commit edition.

JTable how to save the new Row index

I have the following Problem.
Let's Say I have a JTable with the folowing Values:
1
2
3
4
5
Now I select "3" and press a button which saves the index of the row and prints "3"
I have another Button let's call it the "next button", when I press it it will print "4"
Now I sort (Or in this Case just randomise) the whole Table and it's now like this:
2
5
4
1
3
When I now press the "next button" I want it to print "1" because it's the value after "4".
How can I manage this to work ?
It already works when I'm not sorting the Table than I can just print the saved row +1...
Is there something like a sort Listener so I can overwrite the saved row with the new value?
Cheers Timothy
I suggest you dive into the world of TableModel. When programming in Swing it is usually best to split the model from the component straight away. Add a column to the table to handle order, but don't show it.
A simpler method, but a bit of a dead end, is to make the the object stored in the table of a type of your own devising. Just override toString to return the display representation.
As i understand that the problem is with the last index , in ur problem that when it's 4 the next one should be 1 . this problem can be done by makeing if condition .
fore example if i suppose u save the index in a varible rowIndex = 4;
if(rowIndex ==jTable.getRowCount()-1)
nextRowinsex = 1 ; // which is the first index

Categories