TransWikia.com

How to read the row number with having column data?

Software Quality Assurance & Testing Asked by user32519 on January 21, 2021

I’m automating an app and using winappdriver tool with java language for the same. I am selecting a row and click on that row in GUI. Here i am deleting a row , so I am having sequence number and with that sequence number I need to delete the row.

This is the snap of UI Spy :

enter image description here

Here sequence number value is 5, but I want to click the row 3.

//here loopng through each row by row..
String seqNoData=alEvents.get(i).sequenceNumber; //data (sequcenceNumber) from db
List<WebElement> pbp_insert = mainEntrySession.findElementByName("DataGridView").findElements(By.tagName("./*[contains(@LocalizedControlType, 'table')]"));                                     
                    List<WebElement> pbp_insert_grid_cells = pbp_insert.get(0).findElements(By.tagName("./*[contains(@LocalizedControlType, 'item')]"));

          //    pbp_insert_grid_cells--contains all cellvalues  
                    for(int cellIndex = 14;cellIndex < pbp_insert_grid_cells.size();cellIndex=cellIndex+17) //14 th column is sequence number
                    {

                        rowSequenceNo = pbp_insert_grid_cells.get(cellIndex).getText(); //read sequence number from gui

                        if(seqNoData==rowSequenceNo)
                        {

//sequcence number 5==5 mtching, how to get the sequcen number row 3 and row 3 parent
//here how to fetch parent from child to click on that row..
}

One Answer

There is no simple way to find the element's parent if you already have a child in WebElement when you use WinAppDriver. However you can use the work-around the idea of which I demonstrate in the below example:

public static void main(String[] args) throws MalformedURLException {
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("app", "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");
    WindowsDriver driver = new WindowsDriver(new URL("http://127.0.0.1:4723"), capabilities);
    WebElement element = driver.findElementByAccessibilityId("num8Button");
    WebElement parentElement = findParent(element, driver);
    System.out.println(parentElement.getAttribute("Name"));
    driver.quit();
}

static WebElement findParent(WebElement child, WebDriver driver){
    String automationId = child.getAttribute("AutomationId");
    return driver.findElement(By.xpath("//*[./*[@AutomationId='" + automationId + "']]"));
}

Code explanation:

Basically, here you introduce helper method that takes your element, extracts AutomationId attribute value and builds xpath query starting from the top of the dom. That query is built in the way to find all the elements which have at least one child element having the given AutomationId. So if you have the structure like this:

root
|
+-- el1
|    |
|    +-- el2 [AutomationId = 'blahblahId']
|
+-- el3

And if you pass el2 to that method, then it extracts value blahblahId and build the following xpath query: //*[./*[@AutomationId='blahblahId']]. This query will return element el1

Answered by Alexey R. on January 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP