# 5.1. Form Authentication

![](https://2241592081-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M_xhg-ilL7O4aNQofMy%2F-MaRlXhIcTeTVyv_65lf%2F-MaRm39vGVPZbKO6TbcH%2FScreen%20Shot%202021-05-24%20at%2012.10.36%20PM.png?alt=media\&token=49f78b94-289a-41c6-9b21-6e95a7082c80)

## Inspect html

```markup
<form name="login" id="login" action="/authenticate" method="post" >
     <div class="row">
      <div class="large-6 small-12 columns">
        <label for="username">Username</label>
        <input type="text" name="username" id="username"/>
      </div>
    </div>
    <div class="row">
      <div class="large-6 small-12 columns">
        <label for="password">Password</label>
        <input type="password" name="password" id="password"/>
      </div>
    </div>
      <button class="radius" type="submit">
          <i class="fa fa-2x fa-sign-in"> 
            Login
          </i>
        </button>
  </form>
```

{% hint style="info" %}
**Brainstorm questions**

* What is tag name?
* What are attributes which has specified meaningful value?
* What is tex?
  {% endhint %}

## Java Code

```java
public class FormAuthenticationTest {
    WebDriver driver;

    @BeforeClass
    void setup() {
        driver = new ChromeDriver();
    }


    @BeforeMethod
    void load() {
        driver.get("https://the-internet.herokuapp.com/login");
    }

    @Test
    void validCredential() {

        driver.findElement(By.id("username")).sendKeys("tomsmith");
        driver.findElement(By.id("password")).sendKeys("SuperSecretPassword!");

        driver.findElement(By.xpath("//*[@type='submit']")).click();

        Assert.assertEquals(driver.getCurrentUrl(), "https://the-internet.herokuapp.com/secure");

        Assert.assertTrue(driver.findElement(By.id("flash-messages")).isDisplayed()); //You logged into a secure area!

    }

    @Test
    void invalidCredential() {

        driver.findElement(By.id("username")).sendKeys("tomsmith");
        driver.findElement(By.id("password")).sendKeys("SuperSecretPassword");

        driver.findElement(By.xpath("//*[@type='submit']")).click();


        Assert.assertEquals(driver.getCurrentUrl(), "https://the-internet.herokuapp.com/login");
        Assert.assertTrue(driver.findElement(By.className("error")).isDisplayed());

    }

    @AfterClass
    void tearDown() {
        driver.quit();

    }

}
```

�
