Is there a way to write a function to call multiple times in java selenium webdriver

Viewed 39
@Test(priority = 0)
public void platformAdminLogin() {
    // login as platform admin with correct credentials
    try {
        driver.findElement(By.id("emailId")).sendKeys("platform.admin@rotic.com");
        driver.findElement(By.id("password")).sendKeys("Test@123");
        driver.findElement(By.className("loginButton")).click();
        System.out.println("Sucessful login as Platform admin");
        //Logout
        Thread.sleep(500);
        driver.findElement(By.id("headerIconLogout")).click();
        driver.findElement(By.id("logOutYesIcon")).click();
    } catch (Exception e) {
        System.out.println("Error while login as Platform admin = " + e);
    }

How to write a function for logout and how to call multiple times

3 Answers

You can call your function using any iterator such as while or for loops and set your calling limit.

You can call your function with a time duration using the java scheduler schedulers-in-java

Write method like this below with name as "logOut", then in you Test function you can call this method when you want script to logout

public void logOut(){
        Thread.sleep(500);
        driver.findElement(By.id("headerIconLogout")).click();
        driver.findElement(By.id("logOutYesIcon")).click();

}
@Test(priority = 0)
public void platformAdminLogin() {
    // login as platform admin with correct credentials
    try {
        driver.findElement(By.id("emailId")).sendKeys("platform.admin@rotic.com");
        driver.findElement(By.id("password")).sendKeys("Test@123");
        driver.findElement(By.className("loginButton")).click();
        System.out.println("Sucessful login as Platform admin");
        
//Calling Logout method Here       
       logOut();

    } catch (Exception e) {
        System.out.println("Error while login as Platform admin = " + e);
    }
Related