We appreciate your visit to Implement a class LoginForm that simulates a login form commonly found on web pages The LoginForm must include the following public methods assume valid non. This page offers clear insights and highlights the essential aspects of the topic. Our goal is to provide a helpful and engaging learning experience. Explore the content and find the answers you need!

Implement a class `LoginForm` that simulates a login form commonly found on web pages. The `LoginForm` must include the following public methods (assume valid non-null strings are passed in for all parameters):

- `LoginForm(String expectedUsername, String expectedPassword)`: Constructor that takes in the expected username and password.
- `void input(String text)`: Takes the text as input for username if username doesn't already have a value; takes the text as input for password if username does have a value but password doesn't; ignores the text if both username and password already have a value.
- `void click(String button)`: If button is "Submit", submits the form; if button is "Reset", resets the form (the values for username and password); does nothing if button equals any other value.
- `boolean isLoggedIn()`: Returns true if the user is logged in; false otherwise.

Once a user has been successfully logged in by supplying a username and password that match the expected values and clicking submit, the `isLoggedIn` method returns true, and further input has no effect. If a user submits a wrong username and password combination, the form is reset.

Example:
```java
LoginForm login = new LoginForm("admin", "admin123");
login.input("test");
login.input("test123");
login.click("Submit");
login.isLoggedIn(); // returns false
login.input("admin");
login.input("admin123");
login.click("Reset");
login.isLoggedIn(); // returns false
login.click("Submit");
login.isLoggedIn(); // returns false
login.input("admin");
login.input("admin123");
login.input("test");
login.input("test123");
login.click("Submit");
login.isLoggedIn(); // returns true
login.input("test");
login.input("test123");
login.click("Submit");
login.isLoggedIn(); // returns true
```

**Problem 4: LoginFormTest**

Provide a JUnit test class `LoginFormTest` with five public test methods. Each method must test the described feature with at least one `assertEquals` method call:

- `void testCorrectUsernameIncorrectPassword()`: Tests that a correct username and an incorrect password do not allow the user to log in.
- `void testIncorrectUsernameCorrectPassword()`: Tests that an incorrect username and a correct password do not allow the user to log in.
- `void testCorrectCredentialsFirstTry()`: Tests that providing the correct username and password on the first try allows the user to log in.
- `void testIncorrectFirstCorrectSecond()`: Tests that the following steps allow the user to log in:
1. Provide incorrect credentials,
2. Submit,
3. Provide correct credentials,
4. Submit.
- `void testCorrectFirstIncorrectSecond()`: Tests that the user stays logged in after correct credentials are submitted, even if incorrect credentials are then submitted:
1. Provide correct credentials,
2. Submit,
3. Provide incorrect credentials,
4. Submit.

Answer :

The implementation of the LoginForm class:

The LoginForm Class

class LoginForm:

def __init__(self, expectedUsername, expectedPassword):

self.expectedUsername = expectedUsername

self.expectedPassword = expectedPassword

self.username = None

self.password = None

self.logged_in = False

def input(self, text):

if self.username is None:

self.username = text

elif self.password is None:

self.password = text

def click(self, button):

if button == "Submit":

if self.username == self.expectedUsername and self.password == self.expectedPassword:

self.logged_in = True

else:

self.username = None

self.password = None

elif button == "Reset":

self.username = None

self.password = None

def isLoggedIn(self):

return self.logged_in

And here's the LoginFormTest class with the required test methods:

import unittest

class LoginFormTest(unittest.TestCase):

def testCorrectUsernameIncorrectPassword(self):

login = LoginForm("admin", "admin123")

login.input("admin")

login.input("wrongpassword")

login.click("Submit")

self.assertFalse(login.isLoggedIn())

def testIncorrectUsernameCorrectPassword(self):

login = LoginForm("admin", "admin123")

login.input("wrongusername")

login.input("admin123")

login.click("Submit")

self.assertFalse(login.isLoggedIn())

def testCorrectCredentialsFirstTry(self):

login = LoginForm("admin", "admin123")

login.input("admin")

login.input("admin123")

login.click("Submit")

self.assertTrue(login.isLoggedIn())

def testIncorrectFirstCorrectSecond(self):

login = LoginForm("admin", "admin123")

login.input("wrongusername")

login.input("wrongpassword")

login.click("Submit")

login.input("admin")

login.input("admin123")

login.click("Submit")

self.assertTrue(login.isLoggedIn())

def testCorrectFirstIncorrectSecond(self):

login = LoginForm("admin", "admin123")

login.input("admin")

login.input("admin123")

login.click("Submit")

login.input("wrongusername")

login.input("wrongpassword")

login.click("Submit")

self.assertTrue(login.isLoggedIn())

if __name__ == '__main__':

unittest.main()

This code snippet furnishes the LoginForm class with necessary functions and imparts the defined test methods to the LoginFormTest class through the integration of the unittest framework.

Read more about login forms here:

https://brainly.com/question/10639375
#SPJ4

Thanks for taking the time to read Implement a class LoginForm that simulates a login form commonly found on web pages The LoginForm must include the following public methods assume valid non. We hope the insights shared have been valuable and enhanced your understanding of the topic. Don�t hesitate to browse our website for more informative and engaging content!

Rewritten by : Barada