selenium, tutorial,

Working with form authentication

Ha Do Ha Do Follow Feb 01, 2019 · 1 min read
Working with form authentication
Share this

Form Authentication is basic page to login a site. So we are using selenium to create a test automated for this site.

Test Case description

Steps:

  • Open browser
  • Navigate to https://the-internet.herokuapp.com/login
  • Fill in username with tomsmith
  • Fill in the password with SuperSecretPassword!
  • Click on Login button
  • And the home page is appear

Writing code

We using two dependencies

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.3.0</version>
    <scope>test</scope>
</dependency>

Tạo Test class

 @Test
    void validCredentials(){
        WebDriver driver = new ChromeDriver();
        driver.get("https://the-internet.herokuapp.com/login");

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

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

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

    }
Ha Do
Written by Ha Do Follow
Hi, I am Ha, the author of Tvn education center site