MarketSquare / robotframework-browser

Robot Framework Browser library powered by Playwright.
Apache License 2.0
513 stars 104 forks source link

httpCredentials Raises RobotNotRunningError: Cannot access execution context #3346

Closed gitkatsi closed 8 months ago

gitkatsi commented 9 months ago

Describe the bug

new_context(httpCredentials={'
username': "$user", 'password': "$pass"})`

Raises Error: BuiltIn.py", line 86, in _get_context raise RobotNotRunningError('Cannot access executioncontext')`

To Reproduce Code

from Browser import Browser 
br = Browser()
br.new_context(httpCredentials={'username': "$name}", 'password': "$password"})

Raises BuiltIn.py", line 86, in _get_context raise RobotNotRunningError('Cannot access execution context')

Code

from Browser import Browser
br = Browser()
br.new_context(httpCredentials={'username': "name", 'password': "password"})

Raises \librarycomponent.py", line 215, in resolve_secret raise ValueError( ValueError: Direct assignment of values or variables as 'httpCredentials' is not allowed. Use special variable syntax ($var instead of ${var}) to prevent variable values from being spoiled.

Expected behavior Use httpcredentials for basic http authentication

Desktop (please complete the following information):

Additional context I cannot find a proper syntax to pass httpcredentials in new_context. if i use $ it seems to bypass the Direct Assignment ValueError, but EXECUTION_CONTEXTS.current in builtin is always None and self._contexts an empty array. Tried other execution methods like new browser, new_context without params, but EXECUTION_CONTEXTS.current is always None and self._contexts again an empty array.

aaltat commented 8 months ago

@allcontributors please add @gitkatsi bugs.

It seems that we try to resolve variables from RF side when RF is not running. But in this case we should not do it.

allcontributors[bot] commented 8 months ago

@aaltat

I've put up a pull request to add @gitkatsi! :tada:

aaltat commented 8 months ago

I think this will not work:

br.new_context(httpCredentials={'username': "$name}", 'password': "$password"})

because if Robot is not running, then we can not resolve variables from Robot context. In this case raising an exception is the way to go. We should discuss is the raised exception correct one or should it be changed? I do not have strong opinion in either direction.

But I think we should allow, when running without RF, this one:

br.new_context(httpCredentials={'username': "name", 'password': "password"})

Because in this case logging is/can be controlled by user more accurately. Also it is more convenient to do from Python side.

Also should check with test that resolving environment variables do work:

br.new_context(httpCredentials={'username': "%name", 'password': "%password"})

without Robot context.

gitkatsi commented 8 months ago
br.new_context(httpCredentials={'username': "%name", 'password': "%password"})

prints

Given variable placeholder could not be resolved.
Given variable placeholder could not be resolved.

and resutls again in

ValueError: Direct assignment of values or variables as 'httpCredentials' is not allowed. Use special variable syntax ($var instead of ${var}) to prevent variable values from being spoiled.
aaltat commented 8 months ago

Yes, it needs some code changes to work correctly. But because https://robocon.io/ is near and I need to stuff for it, it is likely that fixing will go after the conference.