Search code examples
seleniumintellij-ideapageobjectswebautomation

Selenium - Can't access page object model properties


I'm using the Boni Garcia 'webdrivermanager'. https://github.com/bonigarcia/webdrivermanager/blob/master/README.md

My issue: I'm not able to 'get' my initialized driver properties from 'BaseSwag' to 'home.page' in order to launch Chrome and go to the desired URL. Here is my set up as follows. What can I do to fix this?

src/main/java/swaglogin/BaseSwag

package swaggerLogin;

import io.github.bonigarcia.wdm.WebDriverManager;
import io.github.bonigarcia.wdm.managers.ChromeDriverManager;
import io.github.bonigarcia.wdm.managers.FirefoxDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class BaseSwag {

  public WebDriver driver;
  public Properties prop;

  public WebDriverManager initializeDriver() throws IOException {
    // Create global property file
    prop = new Properties();
    FileInputStream fis = new FileInputStream(
        "//Users/rad/WebTest/src/main/resources/data.properties");

    prop.load(fis);
    String browserName = prop.getProperty("browser");
    System.out.println(browserName);

    if (browserName.equals("chrome")) {
      WebDriverManager.chromedriver().setup();
      driver = new ChromeDriver();
    } else if (browserName.equals("firefox")) {
      WebDriverManager.firefoxdriver().setup();
      driver = new FirefoxDriver();
    }
    return driver;
  }
}

resources/data.properties

url = http://qaclickacademy.com/
apiKey = 1234563333random
browser = chrome

test/java/home.page

package home.page;

import java.io.IOException;
import org.testng.annotations.BeforeTest;
import swaggerLogin.BaseSwag;

public class SwaggerLoginDev extends BaseSwag {

  @BeforeTest

  public void initialize() throws IOException {
    driver = initializeDriver();
    driver.get(prop.getProperty("url"));

  }

}

Stack Trace

Error:(32, 12) java: cannot find symbol
  symbol:   method driver()
  location: class resources.Base

Solution

  • Your driver variable should be an instance of WebDriver, not WebDriverManager.

    You can consider WebDriverManager as utility class, that only manage (download, setup, etc...) your drivers for different browsers. Once you call .setup() method for the desired type of browser, you can create an instance of it:

    public class BaseSwag {
    
      public WebDriver driver;
      public Properties prop;
    
      public WebDriver initializeDriver() throws IOException {
        // Create global property file
        prop = new Properties();
        FileInputStream fis = new FileInputStream(
            "//Users/rad/WebTest/src/main/resources/data.properties");
    
        prop.load(fis);
        String browserName = prop.getProperty("browser");
        System.out.println(browserName);
    
        if (browserName.equals("chrome")) {
          WebDriverManager.chromedriver().setup();
          driver = new ChromeDriver();
        } else if (browserName.equals("firefox")) {
          WebDriverManager.firefoxdriver().setup();
          driver = new FirefoxDriver();
        }
        return driver;
      }
    }
    

    Now, you should be able to call .get(...) method on your driver.