0

I have piece of code as this in python3

context.execute_steps(
    """
                Given user is on the landing page
                When he clicks the LOGIN button
                And he provides the correct credential
                And clicks the login button
                Then he should be get into the main page
                """.format(
        button_color="red"
    )
)
...

And when I was to run a "pre-commit run --all-files" to in order to have code check in git/Master, I get this kind of failure:

  ..\testcase.py error: Not all arguments converted during string formatting

, that complains this piece of code was not good. What could I do to make it through ?

Thanks,

Jack

user3595231
  • 711
  • 12
  • 29
  • 1
    Why are you calling `.format(button_color="red")` when you're not referencing `{button_colior}` in the text? – larsks Nov 09 '19 at 03:40
  • @larsks `.format()` is different from `%` — there is no problem in not using any parameters in string. Try `"test".format(button_color="red")` yourself. – phd Nov 09 '19 at 13:55
  • The Q to the OP: are you sure the problem is in the code? Please show us more code and the entire traceback. – phd Nov 09 '19 at 13:56
  • @phd you'll now I did not suggest it was the cause of the error! But thank you for the lesson. – larsks Nov 09 '19 at 14:06

1 Answers1

0

Thanks all for the feedback.

1) I was trying to python a case to have a user to click a tab from the browser. There could be 2 scenarios: A) user has already login-in, and he may do the click directly. B) He may need to login himself first, then to click that tab. If it was the later case, I want to make a call to the login code which is defined somewhere else, by using this context.execute_steps() call.

2) So here is my code.

@given("make sure the user log-in")
def step_given_user_has_logged_in(context):
    if context.holder.logged_in is True:
        context.logger.info("user already login.")
        return
    else:
        context.logger.info("user need to do login first.")
        context.execute_steps(
            """
                Given user is on the landing page
                When he clicks the LOGIN button
                And he provides the correct credential
                And clicks the login button
                Then he should be get into the extender main page
                """.format(button="red")
        )
        context.holder.logged_in = True
        return

this context.execute_steps() can be referenced from this link: https://behave.readthedocs.io/en/latest/api.html, and somehow when i do the pre-commit, it gives me such messages:

testcase.py:29: error: Not all arguments converted during string formatting

What I have just realized now is I may work around by calling in this way:

context.execute_steps(
            """
                Given user is on the landing page
                When he clicks the LOGIN button
                And he provides the correct credential
                And clicks the login button
                Then he should be get into the extender main page
                """)

It still works for me for user login, and it by-pass the pre-commit checking failure.

In my environment, I am using python 3.7, pre-commit version 1.2.0, and it was running on Ubuntu 16.0.4

user3595231
  • 711
  • 12
  • 29