# 5.1. Form Authentication

![](/files/-MaRm39vGVPZbKO6TbcH)

## 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();

    }

}
```

�


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tvn.gitbook.io/selenium-java/examples/form-authentication.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
