Error: locator.waitFor: Error: strict mode violation: locator

I need help. I'm using the Browser Library robot framework to automate a work application. However, I encountered the following challenge, trying to validate the names of existing columns in a simple way works:

Get Text    //span[text()='CPF']            contains    CPF
Get Text    //span[text()='CNS']            contains    CNS
Get Text    //span[text()='Nascimento']     contains    Nascimento
Get Text    //span[text()='Nome']           contains    Nome
Get Text    //span[text()='Nome da mãe']    contains    Nome da mãe
Get Text    //span[text()='UF']             contains    UF
Get Text    //span[text()='Município']      contains    Município
Get Text    //span[text()='Carteiras']      contains    Carteiras

Wanting to make something more technical, I tried to implement it as follows:

${element}    Set Variable    css=thead tr td span

Wait For Elements State    ${element}    
...    visible    5

Get Text    ${element}    contains    CPF
Get Text    ${element}    contains    CNS
Get Text    ${element}    contains    Nascimento
Get Text    ${element}    contains    Nome
Get Text    ${element}    contains    Nome da mãe
Get Text    ${element}    contains    UF
Get Text    ${element}    contains    Município
Get Text    ${element}    contains    Carteiras

Then the following error is displayed:

Error: locator.waitFor: Error: strict mode violation: locator('div .novaListagem table thead tr td span') resolved to 8 elements: 1) CPF aka getByRole('cell', { name: 'CPF', exact: true }).locator('span') 2) CNS aka getByText('CNS') 3) Nascimento aka getByText('Nascimento', { exact: true }) 4) Nome aka getByRole('cell', { name: 'Nome', exact: true }).locator('span') 5) Nome da mãe aka getByText('Nome da mãe') 6) UF aka getByText('UF') 7) Município aka getByText('Município') 8) Carteiras aka getByText('Carteiras')

Screenshot I want to check

The expected result is that in validation it checks whether it contains the columns CPF, CNS, Birth, Name, Mother's name, State, Municipality, Licenses.

Upvotes: 0

Views: 166

Answers (1)

Pramesh
Pramesh

Reputation: 343

The reason you are getting the error is explained by rasjani above. One way to verify the column names is:

  1. Create a list variable @{EXPECTED_HEADERS}, that lists your expected column names
  2. Save the 8 webelements as a list, using the Get Elements keyword. For example: ${elements} Get Elements ${element}
  3. You can then use the FOR loop over the ${elements} to Get Text from each ${webelement}. For example, ${header} Browser.Get Text ${header_element}
  4. Either, you can create empty list using Create List and Append To List. Then compare two lists outside the loop using the Lists Should Be Equal keyword
  5. OR, without creating an empty list, you can use the List Should Contain Value inside the loop. For example, List Should Contain Value ${EXPECTED_HEADERS} ${header}

Here is one example table and below is the code that checks if the name of the header/column exists in the @{EXPECTED_HEADERS} list.

*** Settings ***
Documentation    Test to verify column names
Library          Collections
Library          Browser


*** Variables ***
${URL}                   https://www.worldometers.info/world-population/world-population-by-year
${BROWSER}               chromium
${TABLE_HEADERS}         thead > tr > th:nth-child(n)
@{EXPECTED_HEADERS}      Year    World Population    Yearly Change    Net Change    Density(P/Km²)


*** Test Cases ***
Verify the column name
    [Documentation]                      Test case to verify the column names
    Open Site And Consent
    Browser.Wait For Elements State      text="Year"    visible    timeout=5s
    ${header_elements}                   Browser.Get Elements    css=${TABLE_HEADERS}
    FOR  ${header_element}  IN  @{header_elements}
        ${header}        Browser.Get Text    ${header_element}
        Log To Console    Header name is: ${header}
        Collections.List Should Contain Value    ${EXPECTED_HEADERS}    ${header}
    END


*** Keywords ***
Open Site And Consent
    [Documentation]                    Opens the example site
    Browser.New Browser                browser=${BROWSER}    headless=False
    Browser.New Context                viewport={'width': 1920, 'height': 1080}
    Browser.New Page                   ${URL}
    Browser.Wait For Elements State    xpath=//button[contains(., 'Consent')]    visible    timeout=5s
    Browser.Click                      xpath=//button[contains(., 'Consent')]

Output:

==============================================================================
PS C:\Development\robot-scripts\Test_Test> robot -d results ./testdemo6.robot
==============================================================================
Testdemo6 :: Test to verify texts
==============================================================================
Verify the column name :: Test case to verif the column names         ...
Header name is: Year
Header name is: World Population
Header name is: Yearly
Change
Verify the column name :: Test case to verif the column names         | FAIL |
[ Year | World Population | Yearly Change | Net Change | Density(P/Km²) ] does not contain value 'Yearly
Change'.
------------------------------------------------------------------------------
Testdemo6 :: Test to verify texts                                     | FAIL |
1 test, 0 passed, 1 failed
==============================================================================
Output:  C:\Development\robot-scripts\Test_Test\results\output.xml
Log:     C:\Development\robot-scripts\Test_Test\results\log.html
Report:  C:\Development\robot-scripts\Test_Test\results\report.html

Hope this helps.

Upvotes: 0

Related Questions