Selenium: Automate Web Application Login
From last post, we have started writing basic selenium automation scripts. Now, let's take that journey forward and try to automate any web based application.
In this post, we will automate our Facebook login as a demo which can be used for any web based application with little modifications.
[Source: www.facebook.com ] |
Code with Explanation:
We will create one class named FacebookLogin in the package blog in eclipse which we have created during googleSearch automation script.
Here, we will import all the necessary external jars, we can give suppress warnings for jar which we are not using.
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
@SuppressWarnings("unused")
public class FacebookLogin {
public static void main(String[] args) throws InterruptedException {
WebDriver driver;
System.setProperty("webdriver.chrome.driver","C:\\Program Files\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://www.facebook.com");
Thread.sleep(1000);
driver.manage().window().maximize();
WebElement username=driver.findElement(By.id("email"));
WebElement password=driver.findElement(By.id("pass"));
// Enter your Facebook username here
username.sendKeys("********");
//Enter your Facebook password here
password.sendKeys("***********");
WebElement login=driver.findElement(By.id("loginbutton"));
login.click();
}
}
In the main method, first we are creating webdriver object and then instantiating it with google chrome driver using System.setProperty function. we need to store .exe file in local system and provide path of the same.
driver.get( ) is used to open specific URL. Thread.sleep( ) is used to provide wait time which will add throws declaration in class which is useful to caught exceptions.
Then we are finding Email and Password web elements using its id and sendkeys is used to provide input to that web elements.
Finally, we are finding Login button web element and clicking on that using Login.click( ).
Just like this, try to automate Login functionality of any web based application !
We will see more concepts and use of Selenium automation in coming posts.
Cheers !
Comments
Post a Comment