Accessing elements located by custom locator strategy with Javascript

限于喜欢 提交于 2019-12-02 17:09:29

问题


I have following issue:

  • I have a checkbox which is hidden and there is another layer used to make it nice and shiny
  • I can easily change its value by accessing its ID however I also need to access this element using my custom locator which uses xpath (that has to stay variable)

So, here's my script:

Custom Select Checkbox    id=my_checkbox        #that works fine
Custom Select Checkbox    customLocatorStrat    #that doesn't work at all

*** Keywords ***
Custom Select Checkbox
    [Arguments]    ${locator}    ${timeout}=${global_timeout}
    Execute Javascript    document.getElementById("${resultLocator}").checked = true;
    OR
    ${el}.checked = true;     #need to get the ${el} variable if the ${locator is not an id}


My Custom Locator
    [Arguments]    ${criteria}    ${tag}    ${constraints}
    ...     #assembling xpath
    ${el}=    Get Webelements    xpath=${path}
    [Return]    ${el}

There is no issue with the locator strategy - it workes fine. I only need to use it/force it in my Custom Select Checkbox keyword. I need to get the ${el} variable and considering there's more than one Custom Locator keyword calling it directly will not work for me. Any idea how to do this please? Many thanks in advance.


回答1:


Custom locators in the selenium2library require activation through the Add Location Strategy [Doc]. This seems to me to be the missing from your example.

*** Test Cases ***
Test Case
    Add Location Strategy   custom  Custom Locator Strategy
    Page Should Contain Element custom=my_id

*** Keywords ***
Custom Locator Strategy 
    [Arguments]    ${browser}    ${criteria}    ${tag}    ${constraints}
    ${retVal}=    Execute Javascript    return
window.document.getElementById('${criteria}');      
    [Return]    ${retVal}



回答2:


${el}=     Run Keyword If  '${locator}'!='my_checkbox'    My Custom Locator   ${criteria}    ${tag}    ${constraints}

You can get the variable before executing ${el}.checked = true;



来源:https://stackoverflow.com/questions/45737729/accessing-elements-located-by-custom-locator-strategy-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!