What is the best method for combining several variables into a single one using a parameter?

On the webpage, there is a table containing 11 checkboxes.

I am looking to create a keyword that allows testers to input just 1, 2, or 3 to select a specific checkbox without needing multiple lines of element variables.

Here are the elements:

xpath=(//input[@class="ant-checkbox-input"])[1]
xpath=(//input[@class="ant-checkbox-input"])[2]
xpath=(//input[@class="ant-checkbox-input"])[3]

Current script:

*** Settings ***
Library    Browser

*** Variables ***
${Checkbox-1}   xpath=(//input[@class="ant-checkbox-input"])[1]
${Checkbox-2}   xpath=(//input[@class="ant-checkbox-input"])[2]
${Checkbox-2}   xpath=(//input[@class="ant-checkbox-input"])[3]

*** Test Cases ***
   Click    ${Checkbox-1}
   Click    ${Checkbox-2}
   Click    ${Checkbox-3}

Expected script:

    *** Settings ***
    Library    Browser
    
    *** Variables ***
    ${Checkbox-1}   xpath=(//input[@class="ant-checkbox-input"])[1]
    ${Checkbox-2}   xpath=(//input[@class="ant-checkbox-input"])[2]
    ${Checkbox-2}   xpath=(//input[@class="ant-checkbox-input"])[3]
    ${CustomCheckbox}   Constructing this variable
    
    *** Test Cases ***
    User selects checkboxes 1 and 2
      [Arguments]    ${not sure}  
       Click    ${CustomCheckbox}   1     2

Outer HTML of the first row in the table:

<tr data-row-key="1" class="ant-table-row ant-table-row-level-0">
    <td class="ant-table-cell ant-table-selection-column">
        <label class="ant-checkbox-wrapper">
            <span class="ant-checkbox">
                <input type="checkbox" class="ant-checkbox-input" value="">
                    <span class="ant-checkbox-inner"/>
                </span>
            </label>
        </td>
        <td class="ant-table-cell">1</td>
        <td class="ant-table-cell">101-39s5mb.mp4</td>
    </tr>

Answer №1

Implementing Keywords Below.

*** Variables ***
${checkbox_xpath}    xpath=(//input[@class="ant-checkbox-input"])

*** Test Cases ***
Test Case
    Log To Console    \nClick 1
    Perform Action With Names    1
    Log To Console    Clicking 2 and 3
    Perform Action With Names    2    3

*** Keywords ***
Perform Action With Names
    [Arguments]    @{index}
    
    FOR    ${key}    IN    @{index}
        Log To Console    ${checkbox_xpath}\[${key}\]
    END

Output:

==============================================================================
Main Execution
==============================================================================
Test Case
Click 1
.xpath=(//input[@class="ant-checkbox-input"])[1]
.Clicking 2 and 3
.xpath=(//input[@class="ant-checkbox-input"])[2]
xpath=(//input[@class="ant-checkbox-input"])[3]
Test Case                                                           | PASS |
------------------------------------------------------------------------------
Main Execution                                                     | PASS |
1 test case executed, 1 passed, 0 failed
==============================================================================

Answer №2

To accomplish this task, utilize the Evaluate function.

*** Settings ***

Library    Browser
Library    SeleniumLibrary

*** Variables ***

${Checkbox}   xpath=(//input[@class='ant-checkbox-input'])[{0}]

*** Test Cases ***
Test
    Click Checkbox    1  3  9

*** Keywords ***
Click Checkbox 
    [Arguments]  @{Values}
    FOR     ${value}    IN    @{Values}
        ${final xpath}    Evaluate    "${Checkbox}".format("${value}")
        Click Element    ${final xpath}
    END

The final xpath will vary depending on the input values provided

(//input[@class='ant-checkbox-input'])[1], (//input[@class='ant-checkbox-input'])[3]....(//input[@class='ant-checkbox-input'])[9]

Alternatively, you can follow this method

*** Settings ***
Library    Browser
Library    SeleniumLibrary

*** Variables ***

${Checkbox}   xpath=//input[@class='ant-checkbox-input']

*** Test Cases ***

Click Checkbox 

*** Keywords ***

Click Checkbox 
     # If the number of elements is unknown, retrieve all elements and click them one by one.
     ${list of elements}  Get Webelements  ${Checkbox}
     For    ${element}   in  ${list of elements}
        Click Element    ${element}
          

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it better to apply the font-family to the body or to the html element?

Is it more appropriate to specify the font-family CSS property on the html element or the body element? html { font-family: sans-serif; } or body { font-family: sans-serif; } I've looked into this issue online, but there doesn't seem to ...

Center-align the text in mui's textfield

What I'm looking for is this: https://i.stack.imgur.com/ny3cy.png Here's what I ended up with: https://i.stack.imgur.com/vh7Lw.png I attempted to apply the style to my input props, but unfortunately, it didn't work. Any suggestions? Than ...

Troubleshooting Problems with Cascading Style Sheets

Struggling with a layout issue on my website. I need to display an image with text at the beginning of the site-wrap div under the name background-image, with the text inside that div. Adding line-height to the text div adjusts its height, but I want to m ...

Running a Selenium project on a WebLogic server

I am interested in developing a selenium project for conducting Sanity testing on one of our client applications. The plan is to create a Dynamic Web Project and deploy this selenium project on the server so that anyone can easily launch and execute test c ...

Utilize CSS Grid to adjust the size of an image

Hi there! I'm currently working on resizing an image using a CSS grid layout. Here's the CSS code I have so far: /* Reset */ * { padding: 0; margin: 0; } html { height: 100%; background-image: url("/assets/images/background.p ...

What is the best way to prevent text-decoration from being applied to icons when utilizing font-awesome inside a hyperlink?

I recently began using FontAwesome and it's been going well. However, I have a question about using it with an anchor tag that has text-decoration:none, and on hover text-decoration:underline. Whenever I hover over the link, the icon also receives the ...

Utilize JavaScript to extract an image from an external URL by specifying its attributes (id, class)

Is it possible to fetch an image from an external website by using its CSS id or class in conjunction with JavaScript/jQuery's get methods? If so, could someone provide guidance on how to achieve this task? ...

Is the website blocking me because of certain missing parameters? (Using selenium for web scraping)

I am currently working on practicing web scraping and the specific page I am focusing on is the following URL. My goal is to scrape the income statement chart located at the bottom of the page. import time from PIL import Image import time from selenium ...

How to customize JavaFx context menu appearance using CSS to achieve a full-width background color

I am trying to customize my context menu, but I'm having trouble with the white space between the menu item and the context menu border. Here is an example: view the rendered context menu with CSS styles What I want is for the color to fill the entir ...

Styling content in a CSS file and then rendering it as a

My current project involves converting a webpage into a PDF file and I am using @page @bottom-left to insert text in the bottom left corner. @page {size: portrait A4; @bottom-left {content: "Some text to display at the bottom left. Some additional text ...

Modifying the color of a GIF animated image using CSS

I'm currently working on an Angular application and I have a transparent GIF Loader image that I downloaded. I've been trying to figure out how to change the color of the GIF image, but so far, I've only been successful in resizing it. I sea ...

During the execution, the WebElement turns null

Running the code below is causing a null pointer exception in this section. Despite storing as we=dr.findElement(By.id("email"));, why does WebElement 'we' get reset to null when I run all these steps together using Cucumber and TestNG? @When("^ ...

What's the best way to constrain a draggable element within the boundaries of its parent using right and bottom values?

I am currently working on creating a draggable map. I have successfully limited the draggable child for the left and top sides, but I am struggling to do the same for the right and bottom sides. How can I restrict the movement of a draggable child based o ...

The most efficient method for interacting with externally generated HTML in Rails

For my Rails page help documentation, I utilized the HelpNDoc help authoring tool to generate a folder containing HTML pages and additional folders for JavaScript, images, stylesheets, and libraries. Now I am seeking the most efficient way to integrate thi ...

Switch between light and dark mode on a webpage using HTML

I am looking to create a script using JavaScript, HTML, and CSS that can toggle between dark mode and night mode. Unfortunately, I am unsure of how to proceed with this task. https://i.sstatic.net/xA3Gq.png https://i.sstatic.net/v5vq5.png My goal is to ...

Guide on automatically logging in to a specific website using Selenium with Kakao, Google, or Naver credentials

I encountered an issue with my selenium login code for a particular website, where the keys seem to be generating errors. https://i.stack.imgur.com/wJ3Nw.png Upon clicking each button, it brings up the login tab. However, I keep encountering this error ...

The Boostrap card-deck appears to be see-through and malfunctioning

This problem has been puzzling me for quite some time now, and I just can't seem to find the solution! Kindly disregard the other language and code - the card is located under the jumbotron. (To access the link, remove spaces: https://code pen.io/ano ...

What are some ways to enhance the appearance of the initial two items in a list through CSS?

Is it possible to enhance the appearance of the first 2 elements in a list using CSS for IE8 compatibility? Here is an example of the format: <ul id="list1" style="margin-left:-20%;font-weight:bold" > <li class="xyz"> Element1 </li> ...

What is the process for building a grid system similar to Bootstrap's 12-column grid using the new CSS Grid Layout?

Is there a way to implement a 12-column grid system similar to Bootstrap using CSS Grid? I am struggling to understand this new technology and would greatly appreciate it if someone could provide a demo. My goal is to create a simple grid structure resem ...

Tips for Customizing a Bootstrap input-group to Resemble a form-group

I'm struggling to match the appearance of the last row with the others, but I want to include the icon in the text box. Adjusting the width has been a challenge! (I'm sorry for the inconvenience, the code snippet may not display correctly on thi ...