Simple Login / Logout script using Selenium

Today we will continue further and see  simple scripts.
Here is a simple login logout script
1.How to invoke browser.
2.How do we pass on the arguments.
3.How do we run a test.
4.What are the web elements we encounter in general.
Let me start with various webeleemts and the tags that you should be familiar while working with HTML
HTML tags are common things that as a Selenium QA you keep bumping around every now and then.
The basic tags that you use in this case are the text filed, buttons,check box, radio button, hyper link,
images,table data scrolling, pagination.
in this exercise i am opening a website and asserting the page title of the page.
In this case its the home page.
Web driver driver = new FirefoxDriver(); //Driver object is created using web driver interface for Mozilla firefox.
driver.navigate().to(“website”); // this is one the ways of opening a link
navigate to method helps user in performing browser operation like back/forward/refresh etc.
driver.get(“website”); // this is again another way to open a link
An added wait after the website invocation code is passed, in this case we have used implicit wait.
In future articles types of wait will be discussed in length and breadth.
We should verify whether we are in right page here we are asserting by checking the title of the page.
Which is done using the equals method, later using testing another method will be introduced for the moment lets continue with this assertion method.
Here we have a very simple login and logout scenario with Xpath used on most of the occasions for element identification.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Firstclass {
/**
* @param args
*/
// Code to assert the page
public static boolean Assertpage(String exp,String act)
{
if(exp.equals(act))
{
System.out.println(“You are in Right page :page title is ” +act);
return true;
}
else
{
System.out.println(“There is some error, check for network connection and get back”);
}
return false;
}
//Main function where the actual execution begins
public static void main(String[] args) {
// TODO Auto-generated method stub
//Browser invocation
String expectedpage =”KanbanFlow – Lean project management, simplified”;
WebDriver driver = new FirefoxDriver();
//Welcome page
   //driver.get(“https://kanbanflow.com/“);
driver.navigate().to(“https://kanbanflow.com/“);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(4,TimeUnit.SECONDS);
//Assertion of exact page
String actualpage = driver.getTitle();
boolean flag =false;
flag = Assertpage(expectedpage,actualpage);
if(flag)
{
driver.findElement(By.xpath(“//a[contains(text(),’Login’)]”)).click();
//Login Page
driver.manage().timeouts().implicitlyWait(2,TimeUnit.SECONDS);
driver.findElement(By.xpath(“//input[contains(@id,’email’)]”)).sendKeys(“nandan.stc@gmail.com“);
driver.findElement(By.xpath(“//input[contains(@id,’password’)]”)).sendKeys(“automation123”);
driver.findElement(By.tagName(“button”)).click();
driver.manage().timeouts().implicitlyWait(2,TimeUnit.SECONDS);
}
else
{
System.out.println(“error we are in wrong page”);
}
//Homepage
driver.findElement(By.xpath(“//a/span[contains(text(),’Nandan’)]”)).click();
driver.findElement(By.xpath(“//li/a[contains(text(),’Log out’)]”)).click();
flag = Assertpage(expectedpage,actualpage);
if(flag)
{
System.out.println(“logged out successfully”);
}
else
{
System.out.println(“Unable to logout”);
}
driver.quit();
}
}
There are 3 ways to close the browser however i suggest using driver.quit();
Various types of browser closure are,
 webDriver.Close() – Close the browser window that the driver has focus of
 webDriver.Quit() – Calls dispose
 webDriver.Dispose() Closes all browser windows and safely ends the  session
 We will be back on other webelements and concepts shortly!
 Till then happy automation!

Leave a comment