Skip to main content

You are here

Automate the log in steps for this website.

The following would be the manual steps required to log on to the test user account for "TestLogin". I have created this account for this exercise.

Username: TestLogin
Password: TestPassword

The manual steps are:

1.) Load Firefox browser
2.) Load the website (http://www.logicandtricks.com)
2.1.1) optionally put an assert here to check if the right website is loaded.
3.) Click on the main menu option for "Login/Create Account"
4.) Click on the "Log in" tab.
5.) Enter your user name in the "Username" text box (use "TestLogin")
6.) Enter your password in the "Password" text box (use "TestPassword")
7.) Click on the "Log in" button.

Essentially, we would need to write code to execute each of the manual steps above.

Before doing that, lets extract the XPATHS for the web elements referenced in the steps.

Login/Create Account = //*[@id='main-menu']/li[5]/a

Log in tab = //*[@id='content']/div[1]/ul/li[2]/a

Username text box = //*[@id='edit-name']

Password text box = //*[@id='edit-pass']

Log in button = //*[@id='edit-submit']

Now for the code itself. You will need to have set reference to Selenium, of course.

import java.util.concurrent.TimeUnit;

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

public class LogOnToLogiandtricks {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.navigate().to("http://www.logicandtricks.com");

driver.findElement(By.xpath("//*[@id='main-menu']/li[5]/a")).click(); //click the Login menu option
driver.findElement(By.xpath("//*[@id='content']/div[1]/ul/li[2]/a")).click(); // click the Log in tab
driver.findElement(By.xpath("//*[@id='edit-name']")).sendKeys("TestLogin"); // locate username text box and type in user id
driver.findElement(By.xpath("//*[@id='edit-pass']")).sendKeys("TestLogin"); // locate password text box and type in password
driver.findElement(By.xpath("//*[@id='edit-submit']")).click();  //click log in button
}

}

The next thing will be to add the code to Log Out. I will leave the reader to add that bit. smiley

Category: 

Language: 

0
No votes yet

Add new comment

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.

Disclaimer

Note that the codes and ideas expressed on this site are the authors personal views. The content of this website are the personal notes based on the learning experience of the author. The author will not be responsible for any error or damages resulting from you using the content of this website.

Premium Drupal Themes by Adaptivethemes