1
votes

I have a Table created using <div> tags and with set of rows and each row has columns say col1, col2, col3, col4, col5 and col6.

col1 to col5 have some values and col6 is edit button.

Based on Col1 to col4 values sent as parameters , i have to click on Edit button and then do some action.

I have idea how to handle this through Selenium WebDriver.

Sample code will be as below:

    // Find Table Web Element
WebElement tableEle = driver.findElement(By.xpath("\\div[@class='ui-grid-canvas']")

     // Using Table WebElement find the list of Rows
    List<WebElement> tableRows = tableEle.findElements(By.xpath("\\div[@ui-grid-row='row']"))

//Loop through each Row 
    for(int rnum=0;rnum<tableRows.size();rnum++)
        {
        //Get the Columns for each row
        List<WebElement> tableColumns=tableRows.get(rnum).findElements(By.xpath("div[@role='gridcell']\div"));
         count=0;
         //loop through each column
        for(int cnum=0;cnum<tableColumns.size();cnum++)
        {
            //verify the each column value with req values array , if matches increase the count otherwise come out of columns for loop
if((columns.get(cnum).getText()).equalsIgnoreCase(ReqValues[cnum]))
               {
                    count++;
               }
               else
                {
                    break;
                }
            // count is equal to column size -1 then matched all records click on the last column where edit icon exist and come out of rows for loop
           if(count ==( tableColumns.size()-1))
           {
                columns.get((tableColumns.size())-1).click();
                   break;
           }
        }
        }

I want to know, how the same code we can write using robot framework.

1
your first for loop will be as is and the 2nd for loops could go into a robotframework keyword which will be called inside first for loop. rest looks pretty much possible in robot. - Waman
first one is same, i can use Get Element keyword but next onwards based on the element, how we can use Get Elements keyword?, i think that Get Elements keywords will be like driver.findElements but not on webElement.findElements, there i stuck - Sarada Akurathi
have you tried using get table cell? github.com/robotframework/SeleniumLibrary/blob/master/test/… here's example of its usage - Waman
Hi waman, those methods will work, if the table created using table tag but in my case, it created using <div> tag, so it is not working - Sarada Akurathi
any luck with the other ways? like getting elements and iterating over it? - Waman

1 Answers

3
votes

I solved this issue by creating one python keyword and then using this in robot framework code.

below is the robot framework code:

    @{elemnts}=    Get Webelements    ${table_rows_xpath}

    #iterate for each row
    : FOR    ${row}    IN    @{elemnts}
    \    @{columns}=    CustomLibrary.Get Webelements By Element    ${row}    ${from_parent_row_to_columns_xpath}
    \    ${status}=    Traverse through columns        @{columns}
    \    Run Keyword If    ${status}=="SUCCESS"    Click on Edit icon and Exit Loop    @{columns}


Traverse through columns
    [Arguments]    @{columns}
    ${length}    Get Length    ${verify_values}
    Set Test Variable    ${count}    0
    ${index}    Set Variable     0
    : FOR    ${col}    IN    @{columns}
    \    ${value}=    Get Text    ${col}
    \    Run Keyword If    '${value}'== '${verify_values[${index}]}'    Increment Count    ELSE    Exit For Loop
    \    ${index}=    Evaluate    ${index}+1
    \    Run Keyword If    ${length} == ${count}   Set Status SUCCESS and Exit 


Click on Edit icon and Exit Loop
    [Arguments]    @{columns}
    ${length}    Get Length    ${columns}
    ${length}=    Evaluate    ${length}-1
    Click Element    @{columns}[${length}]
    Exit For Loop    

python code

def get_webelements_by_element(webElement, locator):
values = locator.split("=",1)

    locateBy = values[0]
    locator = values[1]

    if(locateBy =="name"):
        ele = webElement.find_elements_by_name(locator)
    elif(locateBy =="link"):
        ele = webElement.find_elements_by_link_text(locator)
    elif(locateBy =="partial link"):
        ele = webElement.find_elements_by_partial_link_text(locator)
    elif(locateBy =="tag"):
        ele = webElement.find_elements_by_tag_name(locator)
    elif(locateBy =="css"):
        ele = webElement.find_elements_by_css_selector(locator)
    elif(locateBy =="xpath"):
        ele = webElement.find_elements_by_xpath(locator)    

    return ele