Horizontal scrolling in selenium web driver till element is not visible - java

I have a website that has a long horizontal table. I have Xpath that extracts column headers of a table that stretches more than the screen width (3X of the screen width). Now I have to extract all column heads with their respective index as my table has show columns filters from where user can select or deselect the rows. So I need to dynamically fetch the columns with the index. Now I when I try to fetch the column heads for the first time then system fetch me the column heads till it is visible on the first screen without scroll but now when the system tries to get the column head which is not visible on chrome then code returns a null value.
I have tried to get the total number of columns and checked that scrolling right by 8 columns will do my job but then with this, I am not able to reuse this function on other web tables where there are lesser columns as the system does not find the i+8 column and shows the exception.
I tried to scroll right every time by one column when I get column head as blank. But with this, my code has to check one if condition each time when it does get blank and this is making my method very slow which I can't afford as I need to get at least 35 columns and that too more than 2-3 time in bigger methods.
Now, what I know here is I know which is the last column head was visible to me so I need to scroll right till column head which I had is not visible to me on a screen which other way means I have scrolled maximum to right without hiding the column which I haven't covered. How can I do it?
This is what I tried for this scrolling by 8 columns by seeing the general width of columns. This is working for me but restricting the reusability of function.
if (colHead.equalsIgnoreCase("")) {
WebElement elementToBeScrolled1 = driver.findElement(By.xpath("//th["+i+"+8]"));
By elementToBeScrolled = getByLocator(elementToBeScrolled1);
do {
keyPress(Keys.ARROW_RIGHT);
} while (waitForElement(elementToBeScrolled, 5, WaitType.invisibilityOfElementLocated));
colHead = driver.findElement(By.xpath("//th[" + i + "]"))
.getText();

Related

POI Excel chart - set property to - 'Don't move or size with cells'

I have an excel template which has the table and charts already setup, just need to add data. There is a collapsible table (using group row) which has a variable number of rows. Beneath this table are two bar charts at the same vertical level.
After filling the table data the two charts are shifted down so when the table is expanded there is no overlapping between the table and charts.
To enable this the chart properties are set to 'Move but dont size with cells'. This works but when any columns or rows are resized the two charts start to move. This can be fixed by using the 'Dont move or size with cells' option.
I have tried the below code but am getting NPE with the following code. The getGraphicFrame() returns null.
dbSheet.getDrawingPatriarch().getCharts().get(0).getGraphicFrame().getAnchor().setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);
Can someone advise how this can be done? Thanks.

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 to change JTable column width depending on data?

How would I resize the column for it to just fit the largest string in that column. For example in one column I might have a word like "Monday" and I want this column to be shorter than the "Wednesday" column.
The data changes often as it shows the next 3 days, so I would want Java to automatically change it depending on the input data rather than me having to manually set the column width.
My JTable is within a JScrollPane if that changes anything
Hope that makes sense. Please point me in the right direction.

wierd behavior on Scrolling selenium

I'm working on ui automation and recently discovered one interesting behavior.
Sometimes I need to scroll to some particular element and I've been using both, JS function to scroll to element and actions.moveToElement(WebElement)
This seem to work for any checkboxes, textboxes, buttons types of elements, but when i need to move to the table cell () of the table which has multiple columns - see that table is being shift to the left, this happens almost every time. Let say there is a table 5x5 and i'm only looping over 5th column, when i do scroll2element via either of those 2 functions - the whole table is getting messed up. all the previous columns would disappear and my 5th column is shifting all the way to the left.
I found the workaround - instead of scrolling to specific table cell i scroll to the button that is located on top of that table, but just out of curiosity, if anyone experienced such an issue and what did they do? Thanks
I think you problem could be with selectors.
Try using CSS selectors over the XPath, from my life experience XPath doesnt work as well ass CSS.
Also i'm always using JS and never had any problems.
try that
WebElement x = driver.findElement(by);
((JavascriptExecutor) driver).executeScript(
"arguments[0].scrollIntoView();", x);

Using NatTable for large data which is slow to retrieve

I got a question about the NatTable SWT datagrid widget which I want to use to display a huge log file.
As the log file can be several GB in size, I cannot load it in memory entirely. The application should use as little heap space as possible. Thus, my IDataProvider implements its getDataValue method to read a log file line on the fly, with the help of a little caching mechanism and a cache for raw line positions within the file. When opening a file, almost nothing is known about its contents - not even the amount of rows the table will eventually have to display.
This leads to several problems which currently freak me out:
1) I have to estimate the total row count (using average line length) until the file has been scanned entirely and a maximum line count is really known for sure. As soon this is the case my getRowCount method returns the actual, correct number of rows. This seems to freak out the NatTable widget, it loses its current position entirely and jumps to row # 1!
2) When doing something intuitive as double-clicking on the border of a column (to auto-size the column, just like in Excel), my application freezes completely as suddenly ALL rows cells contents is queried using my provider and the whole concept is abused! I did not want anything to trigger a full file read!
Does anybody have some hints for me? The documentation is so little and so bad...
Any hint is greatly appreciated! How can I prevent my application from freezing??
Thanks!
1) Sounds like the selection isn't preserved when you change the row count - a row count change implies a backing data change hence it's probably playing safe and setting the selection to the first row. You could query the selected row before changing the row count and then reset the selection after you change the row count.
2) In order to figure out what the max-width of the column is, the table has to query each value for that cell in every row... Either you have to implement a paging mechanism such that you return something resulting in zero width when the row isn't visible, or you'll probably want to raise a bug against NatTable to request a feature for an auto-resize mode based on only the visible columns!

Categories