Error passing WebDriver instance to another method

Viewed 102

everyone, I'm struggling with the following situation. Returned driver is not recognized. I want to make a method to used it for rest of the methods for a test suite but

package acceptanceTesting

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeSuite;

public class LoginTestingStep {

    @BeforeSuite
    public static WebDriver driver() {
        System.setProperty(WebDriverPage.webDriverAdress, WebDriverPage.webDriverPath);
            WebDriver driver = new ChromeDriver();
                driver.get("https://evernote.com/");
    return driver;
    }
    public static void WaitForElementVisible(String option) {
        WebDriverWait wait = new WebDriverWait(driver(), 5);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(option)));
    }
    @Test
    public static void UnauthotisedLogin() {
                driver.manage().window().maximize();
                driver.findElement(By.cssSelector(LoginTestingPage.loginButton)).click();
                driver.findElement(By.cssSelector(LoginTestingPage.emailAdreesField)).sendKeys("spacesiatat@yahoo.com");
                WaitForElementVisible(LoginTestingPage.continueButton);
                driver.findElement(By.cssSelector(LoginTestingPage.continueButton)).click();
//              TimeUnit.SECONDS.sleep(5);
//              driver.close();
    }
    public static void AuthotisedLogin() {
}
}
1 Answers

Simple advice first: Do not make return anything when using TestNG annotations (like @BeforeSuite and etc.) Because everytime you want to call this method as parameter it will open new Chrome Browser. Since you are creating driver in the inside of driver() method No any other methods can see that there is driver already defined. Insted use it like global variable, not inside method.

public class LoginTestingStep {
WebDriver driver; //declare as global
@BeforeSuite
public static void driver() {
System.setProperty(WebDriverPage.webDriverAdress,                        
WebDriverPage.webDriverPath);
driver = new ChromeDriver(); //then create instance 
wait = new WebDriverWait(driver, Duration.ofSeconds(5));

driver.get("https://evernote.com/");
}


public static void WaitForElementVisible(String option) {
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(option)));
}
@Test
public static void UnauthotisedLogin() {
            driver.manage().window().maximize();
            driver.findElement(By.cssSelector(LoginTestingPage.loginButton)).click();
            driver.findElement(By.cssSelector(LoginTestingPage.emailAdreesField)).sendKeys("spacesiatat@yahoo.com");
            WaitForElementVisible(LoginTestingPage.continueButton);
            driver.findElement(By.cssSelector(LoginTestingPage.continueButton)).click();
}

}

**What I changed in your code? **

  1. Made driver() method to be return type void instead of WebDriver return type
  2. Deleted line that include: return driver;
  3. Made WebDriverWait global to enable using it in other methods as well and avoiding repeating creating instance of it every time
  4. Added 'DurationOfSeconds' to 'WebDriverWait' constructor parameter since raw usage of second is deprecated
Related