-1

I have written a simple automated test to test the login functionality of a Web-based application. I have a question about the if clause at the bottom and the assert True statement within it. Is it unnecessary? My intention with the login check is to confirm that the bot is redirected to the home screen after successful login by having it look for an element I know is on the home page. loginCheck is a Boolean, and it will return true if the Expected Conditions are met, so do I really need to assert True a Boolean I already know must be true?

Or am I overthinking this?

I would appreciate any suggestions on how to fortify my code to make debugging easier.

    def test1(self):
        #register and login
        driver = self.driver
        driver.get("http://localhost:3000")
        self.assertIn("React To-Do List with Registration and Login", driver.title)

        #try to log in
        driver.find_element(By.XPATH, Tags.nameField).send_keys("rogera")
        driver.find_element(By.XPATH, Tags.passField).send_keys("cde34rfv")
        driver.find_element(By.XPATH, Tags.loginButton).click()

        #check for error message
        failureCheck = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, Tags.errorMessage)))

        #register if login attempt fails
        if failureCheck:
            driver.find_element(By.XPATH, Tags.registerButton).click()
            time.sleep(3)
            driver.find_element(By.XPATH, Tags.firstNameField).send_keys("Roger")
            driver.find_element(By.XPATH, Tags.lastNameField).send_keys("Allen")
            driver.find_element(By.XPATH, Tags.usernameField).send_keys("rogera")
            driver.find_element(By.XPATH, Tags.passwordField).send_keys("cde34rfv")
            driver.find_element(By.XPATH, Tags.registerPageButton).click()
            time.sleep(3)
            successCheck = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, Tags.successMessage)))

            #login after registration
            if successCheck:
                driver.find_element(By.XPATH, Tags.nameField).send_keys("rogera")
                driver.find_element(By.XPATH, Tags.passField).send_keys("cde34rfv")
                driver.find_element(By.XPATH, Tags.loginButton).click()
                time.sleep(3)
                loginCheck = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, Tags.logoutButton)))

                if loginCheck:
                    assert True
                
        else:
            AssertionError("Something went wrong with user registration")
1
  • 1
    No, there's no point in assert True. It cannot possibly fail. Commented Jul 5 at 16:48

1 Answer 1

0

In the above code there is no need of assert True. What you asked is correct.

Generally speaking. assert is used for debugging purpose i.e to check whether code is working correctly.

Just an additional info: Not just assert, Even for other Errors: of course it is possible to check the code with an if statement say if condition: print('Value Error'). But in most of the cases, we use try.. except method for the sake of readability purpose. Using try.. except will make the code more structurized and easy to understand.

Not the answer you're looking for? Browse other questions tagged or ask your own question.