Bernd
Bernd

Reputation: 657

How to send username, password and click submit button

I want to send my username, password and click my submit button that is within a nested div class element.

So does my HTML code look like:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>

    <link rel="stylesheet" type="text/css" href="/css/bootstrap.min.css"/>
    <link rel="stylesheet" type="text/css" href="/css/main.css"/>
    <title>Matilda Login</title>
</head>
<body>
    <div class="vertical-center">
        <div class="container">
            <div class="row justify-content-center align-items-center">
                <div class="col-md-6 col-md-offset-3">
                    <h4>LOGIN</h4>
                    <form action="/login" method="post" autocomplete="off"><input type="hidden" name="_csrf" value="e497ff0a-993c-41ad-bf03-54c79a7756e9"/>


                        <div class="form-group">
                            <label for="username" >Benutzername</label>:
                            <input type="text"
                                   id="username"
                                   name="username"
                                   class="form-control"
                                   autofocus="autofocus"
                                   placeholder="Username"/>
                        </div>
                            <div class="form-group">
                                <label for="password" >Passwort</label>:
                                <input type="password"
                                   id="password"
                                   name="password"
                                   class="form-control"
                                   placeholder="Password"/>
                        </div>                <div class="form-group">

                            <div class="row">
                                <div class="col-sm-6 col-sm-offset-3">
                                    <input type="submit"
                                           name="login-submit"
                                           id="login-submit"
                                           class="form-control btn btn-info"
                                           value="Log In"/>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

<script type="text/javascript" src="/webjars/jquery/3.2.1/jquery.min.js/"></script>
<script type="text/javascript" src="/webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>

</body>
</html>

This is my Integration test but I do not know how to input the user ADMIN and my password and then click the submit button.

List<WebElement> elements = driver.findElements(By.cssSelector("div.row input[class='col-sm-6 col-sm-offset-3']"));
for(WebElement el : elements) 
{
    driver.findElement(By.id("username")).sendKeys("ADMIN");
    driver.findElement(By.id("password")).sendKeys("password"); 
}

Maybe someone could help me please.

Thanks in advance.

Upvotes: 0

Views: 2608

Answers (1)

Ukrainis
Ukrainis

Reputation: 534

Don't understand, why you are using for cycle. In easiest form you need this kind of code:

driver.findElement(By.id("username")).sendKeys("ADMIN");
driver.findElement(By.id("password")).sendKeys("password");
driver.findElement(By.id("login-submit")).click();

With PageObjects it will be some different.

Upvotes: 1

Related Questions