I am new to Selenium and I'm trying to create a small framework in which I login an application and then trying to verify some text inside the account.
But as soon as my script logged into account, the browser gets closed and I am unable to verify text string.
Some people are saying it is because driver is being null and that I need to import base class in LoginPage.
I tried this as well and got the same issue.
BASE CLASS
package testcases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import factory.BrowserFactory;
import factory.DataproviderFactory;
import pages.LoginPage;
public class baseClass {
WebDriver driver;
public void setUP()
{
driver=BrowserFactory.getBrowser("chrome");
driver.get(DataproviderFactory.getConfig().getApplicationurl());
}
public void loginIntoApplication()
{
LoginPage login=PageFactory.initElements(driver, LoginPage.class);
login.Login(DataproviderFactory.getExcel().getData(0, 0, 0), DataproviderFactory.getExcel().getData(0, 0, 1));
}
public void TearDown()
{
driver.quit();
}
}
LOGIN PAGE
package pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import testcases.baseClass;
public class LoginPage extends baseClass {
WebDriver driver;
public LoginPage(WebDriver ldriver)
{
this.driver=ldriver;
}
@FindBy(xpath="//input[@id='log']") WebElement username;
@FindBy(xpath="//input[@id='pwd']") WebElement password;
@FindBy(xpath="//input[@id='login']") WebElement LoginButton;
@FindBy(xpath="//li[@id='wp-admin-bar-my-account']/a") WebElement loginVerification;
public String loginText;
public String Exp="Howdy, AISHWARY";
public void Login(String uname,String pwd)
{
username.sendKeys(uname);
password.sendKeys(pwd);
LoginButton.click();
}
public String LoginVerification()
{
WebDriverWait wait=new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[@id='wp-admin-bar-my-account']/a")));
loginText=loginVerification.getText();
return loginText;
}
}
testCaseLoginPage
package testcases;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import pages.LoginPage;
public class testCaseLoginPage extends baseClass{
WebDriver driver;
baseClass base=new baseClass();
@BeforeTest
public void initial()
{
base.setUP();
}
@Test(priority=1)
public void login()
{
base.loginIntoApplication();
LoginPage login=PageFactory.initElements(driver, LoginPage.class);
login.LoginVerification();
System.out.println(login.loginText);
Assert.assertEquals(login.loginText, login.Exp,"Unable to verify login verification");
}
@AfterTest
public void close()
{
base.TearDown();
}
}
Error Log
[TestNG] Running:
C:\Users\Freak\AppData\Local\Temp\testng-eclipse--1783674419\testng-customsuite.xml
Starting ChromeDriver 2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9) on port 10357
Only local connections are allowed.
May 28, 2017 7:08:42 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
FAILED: login
java.lang.NullPointerException
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:770)
at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:96)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:71)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:45)
at pages.LoginPage.LoginVerification(LoginPage.java:42)
at testcases.testCaseLoginPage.login(testCaseLoginPage.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
at org.testng.TestNG.run(TestNG.java:1031)
I found the same error in appium as below
java.lang.NullPointerException
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:882)
at io.appium.java_client.TouchAction.<init>(TouchAction.java:52)
Cause-: Because I have initialised driver in child and parent class both as below.
Child class
public class AirAsia extends Base {
@SuppressWarnings("rawtypes")
private AndroidDriver driver;
/*
* Sign up Page Elements
*/
@AndroidFindBy(xpath = "//*[@text='Flights']")
private MobileElement flights;}
Base Class
public class Base {
@SuppressWarnings("rawtypes")
public AppiumDriver driver;
/*
* General App Elements
*/
@AndroidFindBy(xpath = "//androidx.appcompat.app.ActionBar.Tab[@content-desc=\"Home\"]/android.widget.TextView")
public MobileElement home;}
Solution-:
So now you remove the driver initialisation from child class as below Child class
public class AirAsia extends Base {
@SuppressWarnings("rawtypes")
private AndroidDriver driver;
/*
* Sign up Page Elements
*/
@AndroidFindBy(xpath = "//*[@text='Flights']")
private MobileElement flights;}
Hope it will help all to solve such type of errors in both Selenium and Appium using Java or any language. Just don't have to initialise driver if you are extending parent class and if yo have initialise driver in base or parent class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.