Login a form with Selenium 2 (Webdriver)

Now we are going to automate these steps.

  1. Open url: 'http://honnuicao.blogspot.com/2014/02/login.html'
  2. Enter 'Jason' to username textbox
  3. Enter 'mindgeek' to password textbox
  4. Click Login button
Verify that the system redirects to a page with title contains 'welcome'.

Example:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import static org.fest.assertions.api.Assertions.assertThat;

/**
 * Created by nhien.truong on 3/3/14.
 */
public class FirstSampleWithWebDriver {
    public static void main(String[] args) throws InterruptedException {
        // Launch firefox browser
        WebDriver driver = new FirefoxDriver();

        // Open url
        driver.get("http://honnuicao.blogspot.com/2014/02/login.html");

        //Get username textbox
        WebElement username_textbox = driver.findElement(By.id("txtUsername"));

        //Get password textbox
        WebElement password_textbox = driver.findElement(By.id("txtPassword"));

        //Get login button
        WebElement login_button = driver.findElement(By.id("btnSubmit"));

        //Enter username
        username_textbox.sendKeys("Jason");
        //Enter password
        password_textbox.sendKeys("mindgeek");
        //Click login button
        login_button.click();

        //Verify the page title contains 'welcome'
        String newTitle = driver.getTitle();
        assertThat(newTitle)
                .containsIgnoringCase("welcome");


        driver.close();
    }

}

Không có nhận xét nào:

Đăng nhận xét