Quantcast
Channel: Active questions tagged config - Stack Overflow
Viewing all articles
Browse latest Browse all 5049

Cucumber Selenium - Java - PageFactory : Step Definition: java.lang.NullPointerException

$
0
0

Hi community: I'm facing the next issue: I'm working with Cucumber, Selenium WebDriver using Page Factory. The error getting here is from the Step Definition side:

java.lang.NullPointerException
at pages.page_First.startNavigation(page_First.java:33)
at stepdefs.stepdefs_First.iGoToGoogle(stepdefs_First.java:20)
at ✽.I go to Google (file:src/test/resources/features/first.feature:10)

This is the part of the code (StepDefinition) where issue occurs:

@Given("I go to Google")
    public void iGoToGoogle() {

        page_first.startNavigation();
    }

From the Page side, this is the code:

public class page_First extends BasePage {


    public page_First()  {
        PageFactory.initElements(driver, this);
    }


    ///////////////////////WEB ELEMENTS///////////////////////

    @FindBy(name = "q")
    private WebElement searchText;

    @FindBy(name="btnK")
    private WebElement searchButton;


    //////////////////////BASE METHODS//////////////////////

    public void startNavigation()  {

        driver.get(PropertyManager.getInstance().getURL());
    }

    public void search(String search)  {

        setTextAs(searchText, search);
    }

    public void enterButton (){

        clickElement(searchButton);
    }
}

The feature file:

Scenario Outline: Search google.com to verify google search is working

    Given I go to Google
    When I query for "<search>" cucumber spring selenium
    And click search
    Then google page title should become the first page

    Examples:
    | search            |
    | Cucumber Selenium |

This is my Browser class:

    public class Browser {

    // Take the instance of WebDriver
    public static WebDriver driver;

    public WebDriver initializeBrowser() throws IOException {

        //Properties taken from config.properties
        String browser = PropertyManager.getInstance().getBrowser();

        if(browser.equals("chrome")) {
            WebDriverManager.chromedriver().setup();
            driver = new ChromeDriver();

        } else if(browser.equals("firefox")) {
            WebDriverManager.firefoxdriver().setup();
            driver = new FirefoxDriver();

        } else if(browser.equals("ie")) {
            WebDriverManager.iedriver().setup();
            driver = new InternetExplorerDriver();

        } else if(browser.equals("edge")) {
            WebDriverManager.edgedriver().setup();
            driver = new EdgeDriver();

        } else if(browser.equals("opera")) {
            WebDriverManager.operadriver().setup();
            driver = new OperaDriver();

        } else {
            System.setProperty("webdriver.safari.driver","/usr/bin/safaridriver");
            driver = new SafariDriver();
        }
        System.out.println("-----> Proceed to initialize driver <-----");
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        return driver;
    }
}

and here is my configuration property reader class:

public class PropertyManager {

    private static PropertyManager instance;
    private static final Object lock = new Object();
    private static String propertyFilePath = System.getProperty("user.dir") + "//src//main//resources//data//config.properties";

    ///////////////////////////// DATA IN PROPERTIES //////////////////////////

    private static String url;
    private static String browser;

    //Create a Singleton instance. We need only one instance of Property Manager.
    public static PropertyManager getInstance () {
        if (instance == null) {
            synchronized (lock) {
                instance = new PropertyManager();
                instance.loadData();
            }
        }
        return instance;
    }

    //Get all configuration data and assign to related fields.
    private void loadData() {

        //Declare a properties object
        Properties prop = new Properties();

        //Read config.properties file
        try {
            prop.load(new FileInputStream(propertyFilePath));
        } catch (IOException e) {
            System.out.println("Configuration properties file cannot be found");
        }

        //Get properties from config.properties
        url = prop.getProperty("url");
        browser = prop.getProperty("browser");
    }

    public String getURL () {
        return url;
    }

    public String getBrowser () {
        return browser;
    }
}

I was forgetting this, my Step Definition class:

    public class stepdefs_First {

    private page_First page_first = PageFactory.initElements(Browser.driver, page_First.class);



    @Given("I go to Google")
    public void iGoToGoogle() {

        page_first.startNavigation();
    }

    @When("I query for {string} cucumber spring selenium")
    public void iQueryForCucumberSpringSelenium(String search) throws Exception {

        page_first.search(search);
    }

    @And("click search")
    public void clickSearch() {

        page_first.enterButton();

    }

    @Then("google page title should become the first page")
    public void googlePageTitleShouldBecomeTheFirstPage() {

        System.out.println("All OK");
    }
}

By the way, this is my config.properties

browser = firefox
url = https://www.google.cl

Please I need your help.


Viewing all articles
Browse latest Browse all 5049

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>