Issue
Can I write a method for WebDriver in a pageObject class, and then call the method in my testcase class?
public class setupMethods {
static WebDriver driver;
public static void setup(String browser) throws Exception{
//Check if parameter passed from TestNG is 'firefox'
if(browser.equalsIgnoreCase("firefox")){
//create firefox instance
System.setProperty("webdriver.gecko.driver", "U:\\path\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
}
//Check if parameter passed as 'chrome'
else if(browser.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "U:\\selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
}
//Check if parameter passed as 'Edge'
else if(browser.equalsIgnoreCase("IE")){
System.setProperty("webdriver.ie.driver","U:\\path\\IEDriverServer.exe");
InternetExplorerDriver driver;
driver = new InternetExplorerDriver();
}
else{
//If no browser passed throw exception
throw new Exception("Browser is not correct");
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
}
This is a method that I would like to call in my test case class, but when I run the test it starts Chrome (or any other browser), but then returns java.lang.NullPointerException
when it comes to the line of code where the driver needs to maximize the window driver.manage().window().maximize();
Solution
Here is the Answer to your Question:
Answering straight, Yes, you can write a method for WebDriver in a pageObject class, and then call the method in your testcase class
You need to consider certain changes in your code as follows:
As you need to
call the method in my testcase
you need to get back theWebDriver
instance as a return. So change the method frompublic static void setup(String browser)
topublic static WebDriver setup(String browser)
As you have already declared the instance of the
WebDriver
as instatic WebDriver driver;
, so you no more need to mention the Class nameWebDriver
again while initializing thedriver
as inWebDriver driver = new FirefoxDriver();
Functionally, this is causing you NullPointerException
in your code.
There is no best practice but I would prefer to check the passed parameter
String browser
inif
loops only.Once you have completed all the actions within this class, return the
WebDriver
instance.Your own code with some simple tweaks will look like:
public class setupMethods { static WebDriver driver; public static void setup(String browser) throws Exception { //Check if parameter passed from TestNG is 'firefox' if(browser.equalsIgnoreCase("firefox")) { //create firefox instance System.setProperty("webdriver.gecko.driver", "U:\\path\\geckodriver.exe"); driver = new FirefoxDriver(); } //Check if parameter passed as 'chrome' if(browser.equalsIgnoreCase("chrome")) { System.setProperty("webdriver.chrome.driver", "U:\\selenium\\chromedriver.exe"); driver = new ChromeDriver(); } //Check if parameter passed as 'Edge' if(browser.equalsIgnoreCase("IE")) { System.setProperty("webdriver.ie.driver","U:\\path\\IEDriverServer.exe"); InternetExplorerDriver driver; driver = new InternetExplorerDriver(); } else { //If no browser passed throw exception throw new Exception("Browser is not correct"); } driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); return driver; } }
Let me know if this Answers your Question.
Answered By - DebanjanB