Watir::Wait.until not working with frames - watir

I'm automating an internal tool that is rife with frames, using Watir. I am able to manipulate all the various elements so I know I am identifying the frames correctly, but any time I attempt to use a Wait statement for any of these elements it fails. Tracking back through the error message, it always hits the activesupport gem in core_ext/time/calculations and it looks like it can't get the duration value, it gets set to false, and then the operation fails because it is expecting a Float. Is this a bug?
Using Ruby 1.8.7 and Watir 1.6.7
My code is:
require 'rubygems'
require 'watir/testcase'
require 'main_setup'
require 'win32ole'
require 'common'
class Smoketest < Watir::TestCase
include CommonCode
def test_AddEdit_Endpoint
Watir::Wait.until { #b.link(:id,"lbShowEndpointForm").exists? }
end
end
Error is the following:
test_basic_smoke(Smoketest):
TypeError: can't convert false into Float
C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.9/lib/active_support/core_ext/time/calculations.rb:278:in `plus_without_duration'
C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.9/lib/active_support/core_ext/time/calculations.rb:278:in `+'
C:/Ruby187/lib/ruby/gems/1.8/gems/commonwatir-1.6.7/lib/watir/wait.rb:15:in `until'
C:/qa/trunk/CCAdmin/Automation/CCAdmin/lib/smoketest.rb:27:in `test_basic_smoke'

So, which line is the C:/qa/trunk/CCAdmin/Automation/CCAdmin/lib/smoketest.rb:27?

I thought the correct usage for the command was wait_until, unless it's changes from Watir 1.6.5, http://wtr.rubyforge.org/rdoc/1.6.5/classes/Watir/Waiter.html

Related

open() throwing mypy issues which reading an image file

I have the following line of code which reads an image (which is fed into a POST request):
files = {"image": (image_path, open(image_path, "rb"))}
While trying to run this through mypy, it keeps throwing the following error:
Argument 1 to "open" has incompatible type "Optional[str]"; expected "Union[Union[str, bytes, PathLike[str], PathLike[bytes]], int]"
I've tried searching this, but I've not found a solution for similar problems.
Is there a different way to read filepaths in order to avoid these issues?
Not the correct answer but if you want to temporarily make it go away to move ahead:
# type: ignore
at the end of the erroring line should work.

Can multiple object attributes be patched at the same time?

I am looking to reduce nesting in my tests, I was looking at with patch.multiple() for the likes of below, but can't figure out a way to get it to work.
Where mock_task_instance, mock_dag_run and success_mock_task_instance are defined in the test function.
So I was wondering if there is a way that multiple object attributes be patched at the same time?
with patch.object(
mock_task_instance, "xcom_pull", side_effect=[self.file_name, self.config]
):
with patch.object(
mock_dag_run, "get_task_instances", return_value=[success_mock_task_instance]
):
with patch.object(
success_mock_task_instance, "current_state", return_value=State.SUCCESS
):
_my_func()
If you are looking for a way to cleanup nested with blocks, I recommend checking out contextlib.ExitStack() as this can help with than.
Here is the example from the docs:
with ExitStack() as stack:
files = [stack.enter_context(open(fname)) for fname in filenames]
# All opened files will automatically be closed at the end of
# the with statement, even if attempts to open files later
# in the list raise an exception
See: https://docs.python.org/3/library/contextlib.html#contextlib.ExitStack
For your code, I think that might look a bit like:
with contextlib.ExitStack() as stack:
stack.enter_context(patch.object(mock_task_instance, "xcom_pull", side_effect=[self.file_name, self.config]))
stack.enter_context(patch.object(mock_dag_run, "get_task_instances", return_value=[success_mock_task_instance]))
stack.enter_context(patch.object(success_mock_task_instance, "current_state", return_value=State.SUCCESS))
_my_func()

Throws error when passing argument with space in JAVA_OPTS in Linux

I am passing command line parameters to gatling script.
This works and executes my test in Windows operating system:
set JAVA_OPTS="-DuserCount=2 -DflowRepeatCount=3 -DdefinitionId=102168 -DtestServerUrl=https://someURL -DenvAuthenticationHeaderFromPostman="Basic UWRZm9aGwsxFsB1V7RXK0OlB5cmZvcm1hbmNldGVzdDE="
It works and takes input which is passed
**********************INPUT*************************************
User Count ====>> 2
Repeat Count ====>> 3
Definition ID ====>> 102168
Environment URL ====>> https://someURL
Authentication Header ====>> Basic UWRZm9aGwsxFsB1V7RXK0OlB5cmZvcm1hbmNldGVzdDE=
***********************************************************
I want to do this same thing on Linux System.
While if I use this command in Linux then it throws error or takes Null or Binary values as input
(Passing arguments with ./gatling.sh)
JAVA_OPTS="-DuserCount=2 -DflowRepeatCount=3 -DdefinitionId=102168 -DtestServerUrl='https://someURL' -DenvAuthenticationHeaderFromPostman='Basic UWRZm9aGwsxFsB1V7RXK0OlB5cmZvcm1hbmNldGVzdDE='" ./gatling.sh
Gives this error,
GATLING_HOME is set to /opt/gatling-charts-highcharts-2.0.3 Error:
Could not find or load main class
UWRZm9aGwsxFsB1V7RXK0OlB5cmZvcm1hbmNldGVzdDE='
Here the problem is the space given in argument of -DenvAuthenticationHeaderFromPostman='Basic UWRZm9aGwsxFsB1V7RXK0OlB5cmZvcm1hbm='.
What is the solution?
The problem is that the $JAVA_OPTS variable is probably not surrounded by quotes. See this question: Passing a space-separated System Property via a shell script doesn't work
The gatling guys clearly forgot to do that.
I would file a bug and/or just edit gatling.sh.
Ideally though you might just want to consider seeing if Gatling takes a properties file or some other way to configure.

Equivalent of String.Format in a Chef/Bash Recipe

looking for something similar to .Net string format in a chef recipe ie.
string phone = String.format("phone: {0}",_phone);
I have a Chef recipe where I need to build up a command string with 30 of these params so hoping for a tidy way to build the string, in principle Im doing this
a=node['some_var'].to_s
ruby_block "run command" do
block do
cmd = shell_out!("node mycommand.js #{a}; exit 2;")
end
end
When I try this I get the error
Arguments to path.join must be strings any tips appreciated
Chef runs in two phases:
Compile and Execute (see https://www.chef.io/blog/2013/09/04/demystifying-common-idioms-in-chef-recipes/ for more details).
Your variable assignment to a happens at compile time, e.g. when chef loads all recipes. The ruby block will be execute in execution mode at converge time and cannot access the variable a.
So the easiest solution might be putting the attribute into the ruby block:
ruby_block "run command with argument #{node['some_var']}" do
block do
shell_out!("node mycommand.js #{node['some_var']}")
end
end
However:
If you don't need to execute Ruby code, consider using the execute or bash resource instead.
Keep in mind, that you must have a unique resource name, if you're building some kind of loop around it. An easy way is to put something unique into the name ruby_block "something unique per loop iteration" do ... end
What I really don't understand is your exit code 2. This is an error code. It will make chef throw an exception each time. (shell_out! throws an exception if exit code != 0, see https://github.com/chef/chef/blob/master/lib/chef/mixin/shell_out.rb#L24-L28)
The resource will be executed on each chef run. This is probably not in your interest. Consider adding a guard (test), to prevent unnecessary execution, see https://docs.chef.io/resource_common.html#guards

How to see exactly what went wrong in Behave

We recently started using Behave (github link) for BDD of a new python web service.
Question
Is there any way we can get detailed info about the failure cause as tests fails? They throw AssertionError, but they never show what exactly went wrong. For example the expected value and the actual value that went into the assert.
We have been trying to find an existing feature like this, but I guess it does not exist. Naturally, a good answer to this question would be hints and tips on how to achieve this behavior by modifying the source code, and whether this feature exists in other, similar BDD frameworks, like jBehave, NBehave or Cucumber?
Example
Today, when a test fails, the output says:
Scenario: Logout when not logged in # features\logout.feature:6
Given I am not logged in # features\steps\logout.py:5
When I log out # features\steps\logout.py:12
Then the response status should be 401 # features\steps\login.py:18
Traceback (most recent call last):
File "C:\pro\venv\lib\site-packages\behave\model.py", line 1037, in run
match.run(runner.context)
File "C:\pro\venv\lib\site-packages\behave\model.py", line 1430, in run
self.func(context, *args, **kwargs)
File "features\steps\login.py", line 20, in step_impl
assert context.response.status == int(status)
AssertionError
Captured stdout:
api.new_session
api.delete_session
Captured logging:
INFO:urllib3.connectionpool:Starting new HTTP connection (1): localhost
...
I would like something more like:
Scenario: Logout when not logged in # features\logout.feature:6
Given I am not logged in # features\steps\logout.py:5
When I log out # features\steps\logout.py:12
Then the response status should be 401 # features\steps\login.py:18
ASSERTION ERROR
Expected: 401
But got: 200
As you can see, the assertion in our generic step clearly prints
`assert context.response.status == int(status)`
but I would rather have a function like
assert(behave.equals, context.response.status, int(status)
or anything else that makes it possible to generate dynamic messages from the failed assertion.
Instead of using "raw assert" statements like in your example above, you can use another assertion provider, like PyHamcrest, who will provide you with desired details.
It will show you what went wrong, like:
# -- file:features/steps/my_steps.py
from hamcrest import assert_that, equal_to
...
assert_that(context.response.status, equal_to(int(status)))
See also:
http://jenisys.github.io/behave.example/intro.html#select-an-assertation-matcher-library
https://github.com/jenisys/behave.example
According to https://pythonhosted.org/behave/tutorial.html?highlight=debug,and This implementation is working for me.
A “debug on error/failure” functionality can easily be provided, by using the after_step() hook. The debugger is started when a step fails.
It is in general a good idea to enable this functionality only when needed (in interactive mode). This is accomplished in this example by using an environment variable.
# -- FILE: features/environment.py
# USE: BEHAVE_DEBUG_ON_ERROR=yes (to enable debug-on-error)
from distutils.util import strtobool as _bool
import os
BEHAVE_DEBUG_ON_ERROR = _bool(os.environ.get("BEHAVE_DEBUG_ON_ERROR", "no"))
def after_step(context, step):
if BEHAVE_DEBUG_ON_ERROR and step.status == "failed":
# -- ENTER DEBUGGER: Zoom in on failure location.
# NOTE: Use IPython debugger, same for pdb (basic python debugger).
import ipdb
ipdb.post_mortem(step.exc_traceback)
Don't forget you can always add an info message to an assert statement. For example:
assert output is expected, f'{output} is not {expected}'
I find that use pyhamcrest assertions yields much better error reporting than standard Python assertions.

Resources