How to resolve java.lang.NullPointerException in Page Object Model in Selenium? - selenium

TestBase Class:-
public class TestBase {
public static WebDriver driver;
public static Properties prop;
// TestBase class constructor is used to initialize the properties object to
// fetch config variables from config.properties file.
public TestBase() {
try {
// File src = new File("./src/main/java/com/config/config.properties");
File src = new File(".\\src\\main\\java\\com\\config\\config.properties");
FileInputStream fs = new FileInputStream(src);
prop = new Properties();
prop.load(fs);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void initialization() throws InterruptedException {
String browserName = prop.getProperty("Browser");
if (browserName.equals("chrome")) {
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
} else {
System.out.println("Oops! Exception has Caught");
}
driver.manage().window().maximize();
driver.get(prop.getProperty("URL"));
}
}
HomePage Class:-
public class HomePage extends TestBase {
TestUtil testUtil;
#FindBy(xpath = "//a[#title='Update your profile, personal settings, and more']")
WebElement updateProfile;
#FindBy(xpath = "//a[#title='Create a New Post']")
WebElement createNewPost;
#FindBy(xpath="(//span[#class='count'])[2]")
WebElement drafts;
#FindBy(xpath="//a[#class='button masterbar__recent-drafts-see-all is-compact is-borderless']")
WebElement seeAll;
public HomePage() {
PageFactory.initElements(driver, this);
}
public ProfilePage updateYourProfile() throws AWTException, InterruptedException {
updateProfile.click();
Thread.sleep(3000);
return new ProfilePage();
}
public NewPostPage createNewPost() throws InterruptedException {
createNewPost.click();
Thread.sleep(3000);
return new NewPostPage();
}
public DraftsPage draftsPage() throws InterruptedException {
createNewPost.click();
Thread.sleep(3000);
drafts.click();
Thread.sleep(3000);
seeAll.click();
Thread.sleep(3000);
return new DraftsPage();
}
}
DraftsPage Class:-
public class DraftsPage extends TestBase {
#FindBy(xpath="(//button[#title='Toggle menu'])[1]")
WebElement toggleMenu;
#FindBy(xpath="(//button[#role='menuitem'])[2]")
WebElement trash;
public void toggleMenu() throws InterruptedException {
toggleMenu.click();
Thread.sleep(3000);
trash.click();
Thread.sleep(3000);
}
}
DraftsPageTest Class:-
public class DraftsPageTest extends TestBase {
ProfilePage myProfile;
LoginPage loginpage;
HomePage homepage;
NewPostPage newPostPage;
DraftsPage draftspage;
public DraftsPageTest() {
super();
}
#BeforeMethod
public void setUp() throws InterruptedException {
initialization();
loginpage = new LoginPage();
loginpage.login();
Thread.sleep(3000);
loginpage.userName(prop.getProperty("username"));
Thread.sleep(3000);
homepage = loginpage.password(prop.getProperty("password"));
Thread.sleep(3000);
myProfile = new ProfilePage();
Thread.sleep(3000);
newPostPage = new NewPostPage();
Thread.sleep(3000);
draftspage = new DraftsPage();
Thread.sleep(3000);
}
#Test
public void DraftsPageTest() throws InterruptedException {
homepage.draftsPage();
Thread.sleep(3000);
draftspage.toggleMenu();
Thread.sleep(3000);
}
#AfterMethod
public void close() {
driver.close();
}
}
Error Message:-
FAILED: DraftsPageTest
java.lang.NullPointerException
at com.pages.DraftsPage.toggleMenu(DraftsPage.java:18)
at com.testCases.DraftsPageTest.DraftsPageTest(DraftsPageTest.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Description:-
I am trying to develop TestNG Framework with the help of Selenium.
I have developed HomePage, LoginPage, ProfilePage, NewPostPage, DraftsPage. While I was running the DraftsPageTest.java, #BeforeMethod was working fine
and the moment control comes to #Test, homepage.draftsPage(); is also working fine. But the moment control comes to draftspage.toggleMenu(); it is throwing
java.lang.NullPointerException.
Any help will be highly appreciated.
Thanks in advance:)

There's a bug in your code.
Please add a constructor which calls PageFactory.initElements(driver, this); to your com.pages.DraftsPage class [ similar to the HomePage constructor in your shared code ]. That should fix the problem.
The reason behind the NullPointerException is because, there's no call to PageFactory.initElements() the WebElement toggleMenu is not being initialised.

Related

Selenium 4.X : EventFiringDecorator throws error when using JavascriptExecutor to click webelement

public class DummyClass {
WebDriver driver;
#Test(priority = 1)
public void testNewFeature() {
WebDriverManager.chromedriver().setup();
System.setProperty("webdriver.chrome.silentOutput", "true");
WebDriverListener customListener = new DriverEventListener();
driver = new EventFiringDecorator(customListener).decorate(new ChromeDriver());
driver.get("http://www.google.com");
WebElement button = driver.findElement(By.xpath(".//a[text()='Sign in']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", button);
}
}
public class DriverEventListener implements WebDriverListener {
public void beforeExecuteScript(WebDriver driver, String script, Object[] args) {
System.out.println("beforeExecuteScript ");
}
public void afterExecuteScript(WebDriver driver, String script, Object[] args, Object result) {
System.out.println("afterExecuteScript ");
}
}
Throws error as
java.lang.IllegalArgumentException: Argument is of an illegal type:
com.sun.proxy.$Proxy15 at
org.openqa.selenium.remote.internal.WebElementToJsonConverter.apply(WebElementToJsonConverter.java:85)
at
java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at
java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at
java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at
java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at
java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
at
java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at
java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
at
org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:550)
at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564) at
org.openqa.selenium.support.decorators.WebDriverDecorator.call(WebDriverDecorator.java:228)
at
org.openqa.selenium.support.decorators.DefaultDecorated.call(DefaultDecorated.java:48)
at
org.openqa.selenium.support.decorators.WebDriverDecorator.lambda$createProxy$0(WebDriverDecorator.java:287)
at com.sun.proxy.$Proxy14.executeScript(Unknown Source)
I had the same issue, it is stated that it will be fixed with the next version. Ticket - https://github.com/SeleniumHQ/selenium/issues/9483
I encountered this issue with selenium 4.0.0-beta-3

Getting java.lang.NullPointerException in Selenium WebDriver

I am getting java.lang.NullPointerException when I run the testcase in Eclipse. Can somebody help me in pointing out the error I have made.
Error # Line 17: WebElement in LoginPage.Java.
# Line 12: LoginPage in TC_LoginTest_001.java.
**LoginPage.Java**
package com.internetBanking.pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class LoginPage {
WebDriver driver;
public LoginPage (WebDriver driver) {
this.driver = driver;
}
WebElement usrname = driver.findElement(By.name("uid"));
WebElement pwd = driver.findElement(By.name("password"));
WebElement login = driver.findElement(By.name("btnLogin"));
public void setUsrname(String uname) {
usrname.sendKeys(uname);
}
public void setPwd(String pswd) {
pwd.sendKeys(pswd);
}
public void login() {
login.click();
}
}
**TC_LoginTest_001.java**
package com.internetBanking.testCases;
import org.testng.annotations.Test;
import com.internetBanking.pageObjects.LoginPage;
public class TC_LoginTest_001 extends BaseClass {
#Test
public void LoginTest() {
driver.get(baseURL);
logger.info("URL is opened");
LoginPage loginPage = new LoginPage(driver);
loginPage.setUsrname(username);
logger.info("Username is entered");
loginPage.setPwd(password);
logger.info("Password is entered");
loginPage.login();
logger.info("Login button is clicked");
}
}
When I run the test case, I am getting the below errors:
java.lang.NullPointerException
at com.internetBanking.pageObjects.LoginPage.(LoginPage.java:17)
at com.internetBanking.testCases.TC_LoginTest_001.LoginTest(TC_LoginTest_001.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Consider this code:
public class MainTest {
public static void main(String[] args) {
new Test(new Object());
}
}
class Test {
InitMe initMe = new InitMe();
public Test(Object o){
System.out.println("Test instance has been created");
}
}
class InitMe{
public InitMe(){
System.out.println("Init me instance has been created");
}
}
If you run it you will see the following output:
Init me instance has been created
Test instance has been created
which means that fields are initialized before the constructor is executed. So in your case you have the following structure:
public class MainTest {
public static void main(String[] args) {
new Test(new Object());
}
}
class Test {
Object o;
String oStr = o.toString();
public Test(Object o){
this.o = o;
}
}
where your fields are initialized through the reference that has not been yet initialized itself (because the constructor has not yet been called).
Your particular solution would be to change this:
WebDriver driver;
public LoginPage (WebDriver driver) {
this.driver = driver;
}
WebElement usrname = driver.findElement(By.name("uid"));
WebElement pwd = driver.findElement(By.name("password"));
WebElement login = driver.findElement(By.name("btnLogin"));
to this:
WebDriver driver;
WebElement usrname;
WebElement pwd;
WebElement login;
public LoginPage (WebDriver driver) {
this.driver = driver;
usrname = driver.findElement(By.name("uid"));
pwd = driver.findElement(By.name("password"));
login = driver.findElement(By.name("btnLogin"));}
}

Running selenium, cucumber and Page Factory. Second step definition fail to run

I wrote a cucumber framework that has two feature files and two stepdefinitions which are glued to the feature files. When i run the test together, it ran for the first stepDefinition and fail to enter the second stepDefinition. I have initialize my pages and yet couldn't get it to work.
Error and Codes below
java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy19.click(Unknown Source)
at Pages.HomePage.CreateANewOrderPage.createOrderLink(CreateANewOrderPage.java:35)
at StepDefinitions.CreateOrderStep.user_click_on_create_a_new_order(CreateOrderStep.java:24)
at ✽.When user click on create a new order(CreateOrder.feature:5)
public class CreateANewOrderPage {
WebDriver driver;
public CreateANewOrdePage(WebDriver driver){
this.driver=driver;
PageFactory.initElements(driver, this);
}
#FindBy (linkText= "Create a new order")
public WebElement createOrderLink;
public void createOrderLink(){
createOrderLink.click();
}
public class SigninPage {
WebDriver driver;
public SigninPage(WebDriver driver) {
this.driver=driver;
PageFactory.initElements(driver, this);
}
#FindBy(xpath="//*[#id=\"userName\"]")
public WebElement usernameField;
#FindBy(name="password")
public WebElement passwordField;
#FindBy(id="buttonSubmitLogin")
public WebElement submitBtn;
public void loginDetails(String uname, String psw) {
usernameField.sendKeys(uname);
passwordField.sendKeys(psw);}
public void clickLogin(){
submitBtn.click();
}
}
public class SigninStep {
WebDriver driver;
SigninPage logIn = new SigninPage(driver);
#Given("^user navigates to mySite$")
public void userNavigatesToMysite() throws Throwable {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\mypc\\Documents\\Automation\\drivers\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("www.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
#And("^user enter \"([^\"]*)\" and \"([^\"]*)\"$")
public void userEnterValidCredentials(String validuname, String validpsw) throws Throwable {
SigninPage logIn = new SigninPage(driver);
logIn.loginDetails("jdjdjdj","jjdjdj");
}
#When("^user click on Sign in$")
public void userClickSignIn() throws Throwable {
SigninPage logIn = new SigninPage(driver);
logIn.clickLogin();
}
}
public class CreateOrderStep {
WebDriver driver;
CreateANewOrderPage ordercreate;
#When("^user click on create a new order$")
public void user_click_on_create_a_new_order() throws Throwable {
ordercreate= PageFactory.initElements(driver,CreateANewOrderPage.class);
ordercreate.createOrderLink();
}
#RunWith(Cucumber.class)
#CucumberOptions (features = "src\\test\\java\\Features\\",
glue ={"StepDefinitions"},
tags={"#Signin, #CreateOrder"}
//format = {"pretty", "html:target/Destination.."}
// format={"json:target/Destination/cucumber.json"
)
public class SigninRunner {
}

Error while executing Selenium script - java.lang.NullPointerException - TestNG

I'm trying to run a script via testNG. But getting java.lang.NullPointerException. I have put in the driver initialisation code in BeforeMethod and rest of the code under Test. Please let me know what's wrong in my script due which Im getting the error.Thanks in Advance.
My Script:
public class NewTest {
WebDriver driver = null;
#BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", Constants.Chrome_Driver);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://" + Constants.auth_Username + ":" + Constants.auth_Password + "#" + Constants.URL);
}
#Test
public void f() throws InterruptedException {
actions.Admin_Login.txbx_aUsername(driver).sendKeys(Constants.aUsername);
actions.Admin_Login.txbx_aPassword(driver).sendKeys(Constants.aPassword);
actions.Admin_Login.btn_login(driver).click();
actions.create(driver).click();
Actions action = new Actions(driver);
action.moveToElement(actions.create.list(driver)).build().perform();
actions.create_list.list_category(driver).click();
actions.create_list.list_ceate_category(driver).click();
actions.create_list.txtbx_Cat_tile(driver).sendKeys(Constants.list_title);
actions.create_list.btn_Cat_Save(driver).click();
System.out.println("List Created Successfully");
}
}
I am using PageObjects to get the data. This script is running fine without testNG. But when I convert the test into testNG and execute, it throws error.
Error I am facing:
java.lang.NullPointerException
at actions.Admin_Login.txbx_aUsername(Admin_Login.java:13)
at testScripts.NewTest.f(NewTest.java:43)
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:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Constants Class :
public class Constants {
public static final String URL_Shika = "url";
public static final String list_Title = "Test";
public static final String aUsername = "Test";
public static final String aPassword = "abcd123";
public static final String uUsername = "Test01";
public static final String uPassword = "abcd123";
public static final String vUsername = "test321";
public static final String vPassword = "abcd123";
public static final String auth_Username = "admin";
public static final String auth_Password = "admin#123";
public static final String FF_Driver = "/path/geckodriver";
public static final String Chrome_Driver = "/path/chromedriver";
}
Action Class:
public class Admin_Login {
private static WebElement element = null;
public static WebElement txbx_aUsername (WebDriver driver) {
element = driver.findElement(By.id("username"));
return element;
}
public static WebElement txbx_aPassword (WebDriver driver) {
element = driver.findElement(By.id("password"));
return element;
}
}
The driver object you are initialising in #BeforeMethod is not visible in your test method. Try following:
public class NewTest {
WebDriver driver = null;
#BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", Constants.Chrome_Driver);
//Pay close attention this below line. Notice how we are not creating a new WebDriver variable
//but we are just initialising the class level data member here
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://" + Constants.auth_Username + ":" + Constants.auth_Password + "#" + Constants.URL);
}
#Test
public void f() throws InterruptedException {
Admin_Login.txbx_aUsername(driver).sendKeys(Constants.aUsername);
Admin_Login.txbx_aPassword(driver).sendKeys(Constants.aPassword);
Admin_Login.btn_login(driver).click();
actions.create(driver).click();
Actions action = new Actions(driver);
action.moveToElement(actions.create.list(driver)).build().perform();
//Create a Course Category
actions.create_list.list_category(driver).click();
actions.create_list.list_ceate_category(driver).click();
actions.create_list.txtbx_Cat_tile(driver).sendKeys(Constants.list_title);
actions.create_list.btn_Cat_Save(driver).click();
System.out.println("List Created Successfully");
}
}
Please try to wait for the text box to appear, before entering any data using following code:
public static WebElement txbx_aUsername (WebDriver driver) {
WebDriverWait w = new WebDriverWait(driver, 30);
w.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
return driver.findElement(By.id("username"));
}
Let me know, if you still face any issue.

Exception in thread "main" java.lang.NullPointerException in Selenium Webdriver

class Login as the following method Kreato_Login():-
public void Kreato_Login(){
driver = new FirefoxDriver();
baseUrl = "https://testrugtn.kreatocrm.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit. SECONDS);
driver.manage().window().maximize();
driver.get(baseUrl + "/");
driver.findElement(By. id("Login_txtUserName")).clear();
driver.findElement(By. id("Login_txtUserName")).sendKeys( "saravana");
driver.findElement(By. id("Login_txtPassword")).clear();
driver.findElement(By. id("Login_txtPassword")).sendKeys( "5678");
driver.findElement(By. id("Login_btnLogin")).click();
}
Class Lead as the following method "Lead_MandatoryCheck()":-
LoginLogout leadInstance=new LoginLogout();
public void Lead_MandatoryCheck() throws InterruptedException{
leadInstance.Kreato_Login();
driver1.findElement(By. xpath("//a[contains(text(),'Customers')]")).click();
driver1.findElement(By. linkText("Leads")).click();
//Add New
driver1.findElement(By. cssSelector( "#ctl00_ContentPlaceHolder1_cbpSubContent_imgAddNew_CD > span.dx-vam" )).click();
Thread.sleep(3000);
//Save
driver1.findElement(By. cssSelector("#ctl00_ContentPlaceHolder1_cbpAssociationNew_btnNewItemTopCreationSave_CD > span.dx-vam" )).click();
String mandatoryPopup= driver1.switchTo().alert().getText();
driver1.switchTo().alert().accept();
System.out.println(mandatoryPopup);
}
I try to call the above methods from the class Trigger as follows:-
public class TriggerClass {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
/*LeadCreation lc= new LeadCreation();
lc.setUp();
lc.testLeadCreation();
lc.tearDown();*/
LeadModule Lm=new LeadModule();
Lm.Lead_MandatoryCheck();
}
when i run the "Trigger.class"
Exception in thread "main" java.lang.NullPointerException
at workflow.LeadModule.Lead_MandatoryCheck(LeadModule.java:132)
at workflow.TriggerClass.main(TriggerClass.java:13)
This is how you should structure your class, members and functions:
TriggerClass.java:
public class TriggerClass {
public static WebDriver driver;
public static void main(String[] args) throws Exception {
driver = new FirefoxDriver();
LeadModule Lm=new LeadModule();
Lm.Lead_MandatoryCheck();
}
LeadModule.java:
public class LeadModule {
public WebDriver driver;
public LeadModule() {
this.driver = TriggerClass.driver;
}
public void Lead_MandatoryCheck() throws InterruptedException{
LoginLogout leadInstance = new LoginLogout();
leadInstance.Kreato_Login();
driver.findElement(By.xpath("//a[contains(text(),'Customers')]")).click();
driver.findElement(By.linkText("Leads")).click();
//Add New
driver.findElement(By.cssSelector( "#ctl00_ContentPlaceHolder1_cbpSubContent_imgAddNew_CD > span.dx-vam" )).click();
Thread.sleep(3000);
//Save
driver.findElement(By. cssSelector("#ctl00_ContentPlaceHolder1_cbpAssociationNew_btnNewItemTopCreationSave_CD > span.dx-vam" )).click();
String mandatoryPopup= driver.switchTo().alert().getText();
driver.switchTo().alert().accept();
System.out.println(mandatoryPopup);
}
LoginLogout.java:
public class LoginLogout extends LeadModule {
public void Kreato_Login(){
baseUrl = "https://testrugtn.kreatocrm.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit. SECONDS);
driver.manage().window().maximize();
driver.get(baseUrl + "/");
driver.findElement(By.id("Login_txtUserName")).clear();
driver.findElement(By.id("Login_txtUserName")).sendKeys("saravana");
driver.findElement(By.id("Login_txtPassword")).clear();
driver.findElement(By.id("Login_txtPassword")).sendKeys("5678");
driver.findElement(By.id("Login_btnLogin")).click();
}
}
Your Java/Selenium basics are not clear. Please read through the documentation or view related tutorials on YouTube.