Search code examples
pythonseleniumparameterized

(python) Using @parameterized lib with declaring a driver?


I'm writing a parameterized UI-tests for creating a new user. Previously I did it with @pytest.mark.parametrize(), but now I decided to try @parameterized . What my code looks like:

class TestUsers:

    @pytest.mark.usefixtures("open_users_page")
    @pytest.mark.usefixtures("login")
    @parameterized.expand([
        ('username', 'password', 'password_confirm', 'email'),  # test id

... and other parameters. After that:

@pytest.mark.usefixtures("fill_create_user_form")  # where i fill non-parameterized fields
def test_user(self, driver, name, password, password_confirm, email):
    users_page = UsersPage(driver)  # where driver is a geckodriver
    users_page.set_name(name)
    users_page.set_password(password)
    users_page.set_password_conf(passwordConf)
    users_page.set_email(email)
    users_page.click_create_new_user_button()

After running this I get the error: TypeError: test_user() missing 1 required positional argument: 'email' and we tried to rewrite it like this:

def test_user(self, *args, **kwargs)
    print(args, kwargs)
    users_page = UsersPage(driver)

-- tests printed the args and went to the next step before going to the next step and using the driver. I have to note that this test worked:

    def test_empty_fields(self, driver):
    """Try to create a new user with empty input fields - an errors should appear"""
    users_page = UsersPage(driver)
    users_page.click_add_user_button()
    users_page.click_create_new_user_button()
    errors = users_page.find_item_errors_on_page()
    errors_count = len(errors)
    assert errors_count == 9

So the question is: how can I use self and driver in this situation? Or should I just forget about the test class and @parameterized and use old @pytest.mark.parametrize? :)


Solution

  • Well, so after a couple of days of debugging we understood that when I have a couple of fixtures before the test class, then a couple of fixture in the class itself, some of them returns me the 'driver', then I have this code:

    @parameterized.expand([
            ("short password", 'username', 'password', 'password', 'email@email.com', '',
             pass_len_errs),
    ...)
    
    • parameterized probably thinks that first argument (which is "short password" - the name of the test) - is the driver itself. So this is probably a conflict of arguments.