Implementing LogIn And Logout Functions In Selenium WebDriver

Right now, We are learning some commonly used functions which can actually helps us to minimize our webdriver test case code size. As you know, You need to perform some actions (Example : Comparing Strings, Comparing Integers, Compare Double, And More Like This) many times during your test case execution so repeating same code In every test case Is not a good practice. You can create and common function In some common class and then call that function whenever required.

Login and Logout from your application's admin panel or application account Is also this kind of repeating task which you need to perform very frequently during test execution. So why not create It once and then use It whenever required In your test? Let we try to Implement It.

Supposing you are testing facebook application and you wants to Implement LogIn and LogOut function on It. Then steps will looks like bellow.

Step 1 : Implement initData(), initBrowser(), and closeBrowser() functions In your CommonFunctions class as described In PREVIOUS POST.

Step 2 : Copy paste bellow given LogIn() and LogOut() functions In your CommonFunctions class.

public boolean isAlreadyLogIn=false;
     //Can accept userID and password as a string
  public void logIn(String userID, String password){
   //To check If already login previously then don't execute this function.
    //If Not login then login In to your account.
  public void logOut(){
   driver.findElement(By.xpath("//input[@value='Log Out']")).click();   

Step 3 : Now Its time to call LogIn() and LogOut() functions In your webdriver test as bellow. Create class with name FBLogin and copy paste bellow given lines In It. Replace dummy user Id and password with your actual user Id and Password before running test.

package Testng_Pack;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class FBLogin extends CommonFunctions{

 public void StartBrowser_NavURL() throws IOException {
 public void ClosingBrowser() {  
 public void OpenBrowserAndURL(){
  //Enter your real Userd ID and Password of FB bellow.
  logIn("YouruserID", "yourpassword");

Now run your test and observe result. It will login In your account and then logout from It. You can Include your test activity between logIn() and logOut() functions.

Now you can call logIn() and logOut() functions anywhere In your test or any of the test case. I hope you understand what I am trying to tell you.

No comments:

Post a Comment