Unable to call method which performing mouse actions to TestNG test case using selenium Java

Viewed 37
Main Block:
    public class test3 extends utility {
        public WebDriver driver;
        @BeforeTest
        public void invokeBrowser() throws IOException {
            driver = base();
            driver.get(resource.getProperty("url"));
    
        }
    
        @Test
        public class thirdPage {
            public void main() {
    
                actionTest test3 = new actionTest();
                test3.executeTest();
            }
        }
    
    }

Code performing mouse Actions:

public class actionTest {   
    
    public void executeTest() {
    Actions execute = new Actions();    
    execute.moveToElement(driver.findElement(By.id("header-search-input"))).click().keyDown(Keys.SHIFT).sendKeys("Cricket").build().perform();
    driver.findElement(By.id("header-desktop-search-button")).click();
    }
    
    

}

Above code results in "NullPointerException" stating driver = null while executing block test3.executeTest();

1 Answers

Restructuring your codebase might help you to fix this issue. Create a base test class. Add the code related to web driver initialization and other main things there. Then create another class for your tests. Inherit it from the base test class. Then you can use the initialized driver in your all test classes.

Related