Encountering a null pointer exception when attempting to declare a WebElementFacade within a page object

While attempting to implement the page object model in Serenity BDD, I encountered a null pointer exception when declaring WebElementFacade in my page object.

Below is the code for my page object class:

package PageObjects;

import net.serenitybdd.core.annotations.findby.FindBy;
import net.serenitybdd.core.pages.WebElementFacade;
import net.thucydides.core.pages.PageObject;

public class LoginPage extends PageObject {

    @FindBy(css ="#search_query_top") public WebElementFacade searchField;

    public void login() {

        open();
        searchField.typeAndEnter("Blouse");
    }
}

This is the step file associated with it:

public class Loginsteps {

    LoginPage loginPage=new LoginPage();

    @Step
    public void DoLogin(){
        loginPage.login();
    }
}

Here is the test class I am working with:

@RunWith(SerenityRunner.class)
public class LoginTest {

    @Steps
    Loginsteps loginsteps;

    @Title("Executing login test")
    @Test
    public void DoLoginTest(){
        loginsteps.DoLogin();
    }
}

The console log output includes the following error message:

DoLoginTest
--------------------------------------------------------------------------------
[main] INFO [driver information]
Starting ChromeDriver and more...

java.lang.NullPointerException at designated lines.

Error messages are displayed to assist...

Please help me pinpoint the root cause of this issue regarding the null pointer.</p>

Answer №1

There are various factors that could potentially be causing the issue at hand. One suggestion would be to consider incorporating Page Factory into your LoginPage, as it is essential for proper POM implementation.

public LoginPage() {
        PageFactory.initElements(Driver.instance(), this);
    }

Although I cannot guarantee that this syntax will work flawlessly with serenity projects, it is worth giving it a try.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Fixture in Py.test: Implement function fixture within scope fixture

I've encountered a small issue with pytest fixtures and I could really use some assistance. Below are some function fixtures without their implementation details for brevity. @pytest.fixture() def get_driver(): pass @pytest.fixture() def login( ...

The error message "AttributeError: 'module' does not contain a 'document' attribute" indicates that the specified module does not have a

I'm trying to use Python (2.7.10) and Selenium to input a username and password, but I keep encountering the following error. Can anyone help me fix it? CODE:- from selenium import webdriver import selenium driver = webdriver.Chrome("/Users/username ...

Jenkins encountered a critical error: FATAL - The build script located at C:selenium tests for moveonmoveon4tests1uild.xml could not be found

My current setup involves using Jenkins and Ant to execute my selenium tests successfully on my local machine. However, I recently encountered an issue when trying to run the tests on a Jenkins server located in a different location. The problem arose be ...

What are some effective techniques for developing a script to automate a straightforward task with selenium?

Hey there! I'm just starting out on my coding journey and I have a cool project idea. I heard about this guy who wrote a script to send a text to his wife when he was working late, and I want to do something similar. Basically, I need a script that c ...

Can we enhance the filtering capabilities of the Elements Collection?

When using the following code, I am able to retrieve one table row from a page: ElementsCollection letters = $$("#myIdName tr").filterBy(Condition.text(email)) Now, my goal is to select and click on a td href element within that row. How can I accomplish ...

Selenium encounters difficulties in extracting data from a table

I am having trouble extracting data from the table located at . The code snippet I am using seems to be unable to access it for some reason. Can anyone suggest why the table scraping is not working? from bs4 import BeautifulSoup from selenium import webd ...

Verify that the select field has been chosen using Protractor

Currently, I am in the process of developing a test that places importance on whether a select field has already been populated. it('should automatically select a field if it hasn't been selected previously', function() { var usersField = ...

Understanding the Select MethodThe process of choosing the most

I'm just starting out with Java and Selenium, trying to expand my knowledge. I'm currently facing a challenge in extracting names from a Dropdown Menu using the Select class. To tackle this issue, I've set up a String Array variable with th ...

Using Selenium Webdriver to upload a file to a website

What is the process for automating jpeg uploads on a UI? Currently, I have the image stored in my repository under resources and using this code: WebElement element = driver.findElement(By.id("mypicId")); File file = new File(ClassLoader.getSystemResourc ...

Utilizing Python with Selenium to automate clicking on a button depending on its type, value, and class attributes

Here is the structure of the HTML code: <button type="submit" name="page" value="2" class="_btn _btng">Next →</button> <button type="submit" name="page" value="1" class="_btn">← Back</button> I am trying to click on the "Next ...

Is the text "abc" present within the text "abcd" using Selenium's IsTextPresent method?

const string inputSearch = "try this example"; Sel.Type("//*[@id='textBox']", inputSearch); Sel.Click("//*[id='searchButton']"); string matchingTermsInResults = Sel.GetText("//*[@id='matchedTerm']"); //displays "example" Lo ...

Issue with clicking the OK button in Selenium WebDriver using Java due to the presence of a span tag within the button tag

This is the HTML code snippet that I am working with: <div id="sieb-ui-popup-mvg-selected" class="AppletStylePopup"> <form onsubmit="return false;" action="/ecom_enu/start.swe" method="post" name="SWEForm4_0"> <div class="siebui ...

Executing multiple test classes with TestNG while utilizing page factory

I have developed two classes for locating elements on two web pages within the same package, named LoginPage.java and AddEmployee.java. Additionally, I have created two test classes in a separate package to correspond with the aforementioned classes, named ...

Is there a way to specify a custom Content-Type for a jax-rs client?

Running JAX-RS resources declared in the following way: public interface MyEntityServiceV2 { @PUT @Consumes({"application/myentity-v2+json"}) @Produces({"application/myentity-v2+json"}) @Path("/{uuid:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA- ...

Tips for inputting a phone number into a form using Python

Can someone help with automating the completion of the application form below using Python? enter image description here I am struggling to fill out the phone number field in the form. The last name, first name, and email are randomly populated from the ...

Ways to verify the content within two separate p elements

<p> expanded state:</p> <P> true </p> Merging the text from both tags into a single output. Final Output: expanded state: true ...

What is the best way to verify that a page has successfully reloaded after clicking a link that navigates to the current page?

I have encountered a peculiar issue that I am attempting to replicate through testing. While I have identified the source of the problem and know how to resolve it, I am struggling to create an effective test case for it. Here is the scenario: I have my ...

What is the best way to retrieve the second to last element in a list

When using Protractor, you have convenient methods like .first() and .last() on the ElementArrayFinder: var elements = element.all(by.css(".myclass")); elements.last(); elements.first(); But what about retrieving the element that comes right before the ...

Tips for locating and interacting with a specific element using Selenium

I am delving into the realm of Python and honing my skills by creating practical applications for daily use. I recently embarked on automating a routine task, but hit a roadblock when attempting to utilize selenium to click on a specific element that dynam ...

I am encountering difficulties signing in to Ancestry using Python Selenium

I have been using selenium and the chromedriver to log in to ancestry.com, but I keep getting an error message saying that it cannot find the ID for the login information. The browser opens up and goes to the page, but it doesn't fill in the login det ...