I wanted to try and implement Page Object Model in my code. The first page would be responsible for opening url, and giving username and password. Currently my code opens the url and loads the webpage without locating the element, then opens a new blank window and repeats this three or four times. Crashes while trying to locate element. Can anyone tell me where I am making mistake. I have stored below details in Config .I am checking code again and again but not able to find issue
public class Base {
public static WebDriver driver;
public static Properties prop;
public Base(){
try {
prop = new Properties();
FileInputStream ip = new FileInputStream("C:\\config\\config.properties");
prop.load(ip);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void initilization()
{
String browsername = prop.getProperty("browser");
if (browsername.equals("chrome")){
System.setProperty("webdriver.chrome.driver","C:\\Users\\chromedriver.exe");
driver= new ChromeDriver();
}
driver.manage().window().maximize();
driver.get(prop.getProperty("url"));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(70));
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(70));
}
}
Login page :
public class LoginPage extends Base {
@FindBy(name = "username")
WebElement username;
@FindBy(name = "password")
WebElement password;
@FindBy(xpath = "//input[@type='submit']")
WebElement loginbutton;
@FindBy(xpath = "//button[contains(text(),'Sign Up')]")
WebElement SignUpButton;
@FindBy(xpath= "//img[contains(@class='img-responsive')]")
WebElement crmlogo;
public void LoginPage()
{
PageFactory.initElements(driver,this);
}
public boolean logo()
{
return crmlogo.isDisplayed();
}
public String validatetitleofpage()
{
return driver.getTitle();
}
public HomePage login(String un,String psd)
{
username.sendKeys(un);
password.sendKeys(psd);
loginbutton.click();
return new HomePage();
}
}
This is LoginTest from where I am calling all methods :
public class LoginPageTest extends Base {
LoginPage LoginPage;
HomePage HomePage;
public LoginPageTest()
{ super();
}
@BeforeMethod
public void setup()
{
initilization();
LoginPage = new LoginPage();
}
@Test (priority = 1)
public void loginPageTitleTest()
{
String title = LoginPage.validatetitleofpage();
Assert.assertEquals(title,"title details");
}
@Test (priority = 2)
public void logoTest()
{
boolean logotrue=LoginPage.logo();
Assert.assertTrue(logotrue);
}
@Test(priority = 3)
public void logintest()
{
HomePage= LoginPage.login(prop.getProperty("unme"),prop.getProperty("pwd"));
}
@AfterMethod
public void teardown()
{
driver.quit();
}
}
Below is the error
java.lang.NullPointerException
at com.crm.qa.pages.LoginPage.logo(LoginPage.java:31 undefined)
at com.crm.qa.Testcases.LoginPageTest.logoTe0st(LoginPageTest.java:37 undefined)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at `enter code
here`java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566 undefined)
at`org.testng.internal.MethodInvocationHelper.invokeMethod`(MethodInvocationHelper.java:133)
at `org.testng.internal.TestInvoker.invokeMethod`(TestInvoker.java:598 undefined)
at org.testng.internal.TestInvoker.invokeTestMethod
(TestInvoker.java:173)
at org.testng.internal.MethodRunner.runInSequence
(MethodRunner.java:46)
at
`org.testng.internal.TestInvoker$MethodInvocationAgent.invoke`(TestInvoker.java:824)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146 undefined)
Config file
url = https://classic.crmpro.com/
unme = abcd
pwd = Abcd
browser = chrome**