Implementing LogIn And Logout Functions In Selenium WebDriver

Right now, We are learning unopen to unremarkably used functions which tin truly helps us to minimize our webdriver seek illustration code size. As yous know, You take away to perform unopen to actions (Example : Comparing Strings, Comparing Integers, Compare Double, And More Like This) many times during your test case execution thence repeating same code In every seek illustration Is non a expert practice. You tin do in addition to common function In unopen to mutual shape in addition to and then telephone phone that business office whenever required.

Login in addition to Logout from your application's admin panel or application draw of piece of job organisation human relationship Is likewise this form of repeating chore which yous take away to perform real oft during seek execution. So why non do It ane time in addition to and then utilization It whenever required In your test? Let nosotros attempt to Implement It.

Supposing yous are testing facebook application in addition to yous wants to Implement LogIn in addition to LogOut business office on It. Then steps volition looks similar bellow.

Step 1 : Implement initData(), initBrowser(), and closeBrowser() functions In your CommonFunctions shape every bit described In PREVIOUS POST.

Step ii : Copy glue bellow given LogIn() in addition to LogOut() functions In your CommonFunctions class.

public boolean isAlreadyLogIn=false;        //Can conduct maintain userID in addition to password every bit a string   populace void logIn(String userID, String password){    //To cheque If already login previously in addition to then don't execute this function.    if(!isAlreadyLogIn){     //If Not login in addition to then login In to your account.     driver.findElement(By.xpath("//*[@id='email']")).sendKeys(userID);     driver.findElement(By.xpath("//*[@id='pass']")).sendKeys(password);     driver.findElement(By.xpath("//*[@id='loginbutton']")).click();     isAlreadyLogIn=true;    }   }      populace void logOut(){    driver.findElement(By.xpath("//div[@id='userNavigationLabel']")).click();    driver.findElement(By.xpath("//input[@value='Log Out']")).click();       isAlreadyLogIn=false;   }

Step iii : Now Its fourth dimension to telephone phone LogIn() in addition to LogOut() functions In your webdriver seek every bit bellow. Create shape amongst name FBLogin in addition to re-create glue bellow given lines In It. Replace dummy user Id in addition to password amongst your actual user Id in addition to Password earlier running test.

package Testng_Pack;  import java.io.IOException; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test;  populace shape FBLogin extends CommonFunctions{     @BeforeTest  populace void StartBrowser_NavURL() throws IOException {   initData();    }    @AfterTest  populace void ClosingBrowser() {     closeBrowser();    }    @Test  populace void OpenBrowserAndURL(){   initBrowser();   driver.get("https://www.facebook.com/");   //Enter your existent Userd ID in addition to Password of FB bellow.   logIn("YouruserID", "yourpassword");   logOut();  } }

Now run your seek in addition to notice result. It volition login In your draw of piece of job organisation human relationship in addition to and then logout from It. You tin Include your seek activity betwixt logIn() in addition to logOut() functions.

Now yous tin telephone phone logIn() in addition to logOut() functions anywhere In your seek or whatsoever of the seek case. I promise yous sympathize what I am trying to say you.

More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post