MarketSquare / robotframework-seleniumlibrary-java

Java port of the Python based SeleniumLibrary for Robot Framework
Apache License 2.0
23 stars 16 forks source link

prefs not correctly handled as experimentaloptions #80

Closed ractoc closed 5 years ago

ractoc commented 5 years ago

Currently I'm having some problems setting the experimental options. In Python, everything works fine (through the Create Webdriver keyword). When I try and do the same for the Java variant, I'm not getting the same results. I am setting the same prefs.

I put both the Python version and the Java version here for reference. What I do is, I cal leither of these to open a Chrome browser. Then I browse to a URL containing a PDF document. With the Python settings, the PDF is downloaded directly, to the folder I set in prefs. With the Java variant, the download popup shows using the default download folder.

I already tweaked the BrowserManagement class in the library to convert the prefs JSONObject to a Java Map. This didn't fix the issue. It looks like only the one prefs property is honered through Java for some reason.

Start Chrome For Python
    [Arguments]    ${URL}
    ${chrome options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
    ${prefs}    Create Dictionary    prompt_for_download=false  download.default_directory=${OUTPUT_DIR}${/}${download}    plugins.always_open_pdf_externally=${TRUE}
    Call Method    ${chrome options}    add_experimental_option    prefs    ${prefs}
    Create Webdriver    Chrome    chrome_options=${chrome options}
    Go To   ${URL}

Start Chrome For Java
    [Arguments]    ${URL}
    ${downloadDir}    Replace String    ${OUTPUT_DIR}${/}${download}    \\    /    
    ${browserOptions}=    Catenate   
    ...    {
    ...        "args": [  ],
    ...        "extensions": [  ],
    ...        "prefs":
    ...            {
    ...                "prompt_for_download": false,
    ...                "download.default_directory": "${downloadDir}",
    ...                "plugins.always_open_pdf_externally": true
    ...            }
    ...    }
    Log    ${browserOptions}
    Open Browser    ${URL}    Chrome    browserOptions=${browserOptions}
Hi-Fi commented 5 years ago

I think it works fine for me, expect the default download dir, which might relate to SeleniumHQ/selenium#5292

Hi-Fi commented 5 years ago

Now I got this to work with Windows. I think it's matter of escaping things, and because of that some more logic is needed if tests are running with both Linux and Windows.

Working Chrome part is:

Download automatically
    WebDriver Manager Setup    Chrome
    ${downloadDir}    Replace String    ${OUTPUT_DIR}${/}${download}    \\    \\\\
    Log    ${downloadDir}
    ${browserOptions}=    Catenate   
    ...    {
    ...        "args": [  ],
    ...        "extensions": [  ],
    ...        "prefs":
    ...            {
    ...                "plugins.always_open_pdf_externally": true
    ...                "download": {
    ...                    "default_directory": "${downloadDir}",
    ...                    "prompt_for_download": false
    ...                }
    ...            }
    ...    }
    Log    ${browserOptions}
    Open Browser    ${URL}    Chrome    browserOptions=${browserOptions} 
    Capture Page Screenshot    #Needed because otherwise incomplete download is made. Basically any browser command probably would be OK.

So no changes needed to the library itself I think.