0

I have to assert whether the page has button enabled or not but each time I run my code I get the mentioned error. Can anyone please have a look at this ?

Here is my class that has selector for the button.

public class DynamicProperties {

    WebDriver driver;
    //Constructor that will be automatically called as soon as the object of the class is created
    public DynamicProperties(WebDriver driver) {
        this.driver = driver;
    }
    
    //Locators for the page title and the logout button
    By vis = By.xpath("//button[@id=enableAfter']");
    WebElement visibleAfterFive = driver.findElement(vis);
    Boolean button = visibleAfterFive.isDisplayed();
    
    public Boolean getButton() {
        return button;
    }
}

And here is my Test Case Running Class:

public class TestCase9_DynamicProperties {

    @Test(priority=1, description = "Testing the button") 
    public void checkEnableAfter() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "Link to chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://demoqa.com/dynamic-properties");
        
        DynamicProperties DP = new DynamicProperties(driver);
        Thread.sleep(5000);
        
        boolean button = DP.getButton();
        boolean actualValue = true;
        Assert.assertEquals(button,actualValue);
        driver.quit();
    }
}
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • Are you sure the automatically invoked constructor of `DynamicProperties` always gets an initialized `WebDriver` passed? – deHaar Sep 06 '22 at 14:35
  • @Muhammad Kamran, you have kept the following code at class level. I suggest you to move that into a method. As when you initialize the object, these lines get executed before the constructor is called. `By vis = By.xpath("//button[@id=enableAfter']"); WebElement visibleAfterFive = driver.findElement(vis); Boolean button = visibleAfterFive.isDisplayed();` – ketanvj Sep 06 '22 at 15:10
  • @deHaar yeah, I think so. – Muhammad Kamran Sep 07 '22 at 10:54

0 Answers0