Selenium Java, class to run same setup across tests - java

I had started at this original post
This has been very enlightening to read. I am in the same situation and have been trying to use this as a guide. Unfortunately, I am getting a java.lang.NullPointerException which I know why, its because the page isn't getting loaded but I am not sure why.
Here is my base test class:
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class IRIS_Firefox_Test_Base {
static WebDriver driver;
static String baseURL;
#Before
public void setup() throws IOException{
baseURL = "http://localhost:3006";
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get(baseURL);
}
#After
public void closeFireFox(){
driver.quit();
}
}
And my test:
import java.io.IOException;
import org.junit.Test;
import pageObjects.Login_Page;
public class POM_TC extends IRIS_Firefox_Test_Base {
#Test
public void login() throws IOException{
super.setup();
Login_Page.loginInput(driver).sendKeys("scornell");
Login_Page.passwordInput(driver).sendKeys("password");
Login_Page.loginBtn(driver).click();
}
}
I'm not really a well versed java programmer, my background is more JavaScript and C#(not a lot there either though). Maybe I'm missing something simple.
Here is my stack trace:
FAILED: login
java.lang.NullPointerException
at pageObjects.Login_Page.loginInput(Login_Page.java:13)
at com.hiiq.qa.testing.gen2.Login_Tests.POM_TCTest.login(POM_TCTest.java:11)
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:84)
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:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
thanks to all for their input.

I would add a page load time out:
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
This allows the page to load, giving the program time to catch up and verify that certain elements are available on the page. I would make your setup look like:
#Before
public void setup() throws IOException{
baseURL = "http://localhost:3006";
driver = new FirefoxDriver();
driver.get(baseURL);
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
}
Remove the implicit wait, and add the pageLoadTimeout, also I moved the maximize portion until after the URL is called.

Related

Getting Null pointer exception when running Selenium through TestNG

I have Test base class which calls a method from LandingPage class which has the locator for the page under test. When I execute this project, I'm getting Null pointer exception. I'm sure it has something to do with testNG annotations, but I'm unable to find out the reason.
package com.xyz.tests;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.xyz.pageObjects.LandingPage;
import com.xyz.pageObjects.LoginPage;
import com.xyz.utils.Utils;
import engine.Engine;
public class LandingPageTest {
public WebDriver driver;
//Engine engine = new Engine(driver);
LandingPage landingPage;
LoginPage loginPage;
#BeforeTest
public void setUp(){
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.go.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
#Test
public void clickOnSignInLink() {
landingPage.SignIn().click();
}
Below is my LandingPage class
package com.xyz.pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class LandingPage {
private WebDriver driver;
public LandingPage(WebDriver driver) {
this.driver = driver;
}
//By signIn= By.xpath(".//*[#id='pageContainerInner']/div[2]/div[1]/div/div[2]/div[1]/a");
By signInBtn = By.linkText("Sign In or Create Account");
//By signInBtn2 = By.cssSelector("css=a.signIn");
By closeCrisisMessage = By.xpath(".//*[#id='closeCrisisMessageBtn']");
public WebElement SignIn(){
return driver.findElement(signInBtn);
}
}
Following is the error message
C:\Users\ad\AppData\Local\Temp\testng-eclipse--957796922\testng-customsuite.xml
FAILED: clickOnSignInLink
java.lang.NullPointerException
at com.disney.tests.LandingPageTest.clickOnSignInLink(LandingPageTest.java:36)
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:84)
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:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Any suggestions will be appreciated.
I'm not sure on what line your NullPointerException is thrown, but I would expect this to fail because your landingPage has not been initialized when the #Test method is run:
landingPage = new LandingPage(driver);
I see, you didn't instantiate landingPage & loginPage anywhere. That's why you are getting a null pointer exception. To get around this issue, you have to instantiate those references (mainly, landingPage here). Instantiate it either in place of declaration or in #BeforeClass annotation.

java.lang.NullPointerException - selenium java testng

yesterday my test works ok, bust i want to use testng and then appeared a problem.
My main class leadTest:
public class leadTest {
WebDriver driver;
#Test
public void f() {
..
...
...
/*------------------------Go to leads page-------------------------------*/
LeadsPage ldsP = new LeadsPage(driver);
dbp.gotoLeadsPage();
ldsP.findLeadByName("nameToFind");
}
#BeforeClass
public void beforeClass() {
driver = new FirefoxDriver();
driver.get("http://getbase.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
and my class LeadsPage:
public class LeadsPage {
WebDriver driver;
String expStatus = "new";
#FindBy(id="leads-new")
WebElement addLead;
#FindBy(className = "lead-status")
WebElement UserStatus;
public void addNewLead(){
addLead.click();
}
public void checkUsrStat(){
String stat = UserStatus.getText().toLowerCase();
Assert.assertEquals(stat, expStatus.toLowerCase());
}
public void findLeadByName(String leadName){
driver.findElement(By.partialLinkText(leadName)).click();
}
public LeadsPage(WebDriver driver){
PageFactory.initElements(driver, this);
}
I have problem with this part from above:
public void findLeadByName(String leadName){
driver.findElement(By.partialLinkText(leadName)).click();
}
I have error
AILED: f
java.lang.NullPointerException
at leadTest.LeadsPage.findLeadByName(LeadsPage.java:38)
at leadTest.leadTest.f(leadTest.java:101)
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:84)
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:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
When i use
#FindBy(partialLinkText="nameToFind")
WebElement ntf;
and
ntf.click();
its works, but i cant do that, becouse in place nameToFind will be variable.
And i dont know what is wrong... Please help me
Driver variable not initialized: you try to call findElement() method on a null object.
One way to confirm that is to print the driver value:
System.out.println("driver=" + driver); just before the exception is thrown
public void findLeadByName(String leadName){
System.out.println("driver=" + driver);
driver.findElement(By.partialLinkText(leadName)).click();
}
also i am noticing ,
LeadsPage ldsP = new LeadsPage(driver);
dbp.gotoLeadsPage();
ldsP.findLeadByName("nameToFind");
gotoLeadsPage(); and findLeadByName(); both functions are in same class LeadsPage but using different objects dbp and ldsp
May be below changes will work.
in LeadsPage class change your findLeadByName(); function as follows,
public void findLeadByName(WebDriver driver, String leadName){
this.driver.findElement(By.partialLinkText(leadName)).click();
}
and in your class leadTest call function findLeadByName() as follows,
LeadsPage ldsP = new LeadsPage(driver);
ldsP.findLeadByName(driver ,"nameToFind");

How can I pass this code successfully? Java Webdriver TestNg

I want to pass this code to my other classes so I don't have to keep pasting it.
This is the class containing the code:
package utility;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class BrowserType {
public static WebDriver driver;
#Parameters("browser")
#Test
public static void CallBrowser(String browser) {
if(browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
// If browser is IE, then do this
}else if (browser.equalsIgnoreCase("chrome")) {
// Here I am setting up the path for my IEDriver
{System.setProperty("webdriver.chrome.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/chromedriver.exe");}
driver = new ChromeDriver();
driver.get(Constant.URL);
}
}
}
I want to pass CallBrowser to SetUp which is below:
package automationFramework;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import utility.Constant;
import appModule.SignIn_Action;
public class SignIn {
public WebDriver driver;
#BeforeMethod
#Parameters("browser")
public void SetUp(String browser) {
if(browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
// If browser is Chrome, then do this
}else if (browser.equalsIgnoreCase("chrome")) {
{ System.setProperty("webdriver.chrome.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/chromedriver.exe");}
driver = new ChromeDriver();
{ driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);}
{driver.manage().window().maximize();}
driver.get(Constant.URL);
}
}
#Test
public void signIn() {
SignIn_Action.Execute(driver, Constant.DevStudentUsername, Constant.DevStudentPassword);
}
#AfterMethod
public void Teardown() {
driver.quit();
}
}
I tried just calling the static class, but then #BeforeMethod is throwing a configuration error, I'm sure I am just doing it wrong with passing driver, and parameters.
Can someone please explain the changes I need to make to both classes to make it work correctly?
These are the errors:
FAILED CONFIGURATION: #AfterMethod Teardown
java.lang.NullPointerException
at automationFramework.SignIn.Teardown(SignIn.java:38)
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:84)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:786)
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:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
FAILED: signIn
java.lang.NullPointerException
at appModule.SignIn_Action.Execute(SignIn_Action.java:27)
at automationFramework.SignIn.signIn(SignIn.java:33)
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:84)
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:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================
===============================================
Suite
Total tests run: 1, Failures: 1, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================
This is how i am trying to call the code:
public WebDriver driver;
#BeforeMethod
#Parameters("browser")
public void SetUp(String browser) {
BrowserType.CallBrowser(browser);
}
Your issue is that your driver is not initialized when you call teardown. You need to make sure it is initialized in all cases. I would go through your code with a debugger and check to make sure that when you hit the setup and teardown method that the driver is in fact initialized at setup and exists at tear down. You also have two drivers present when things run. One is your static driver that you declare in the first part of your code, and then you have another non-static driver located with your actual tests. I would fix that as it may be causing issues.
The easiest way to solve your issue is to make sure the driver is ALWAYS initialized during setup, even if the string does not match either option.
public void SetUp(String browser) {
if(browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
// If browser is Chrome, then do this
}else {
System.setProperty("webdriver.chrome.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/chromedriver.exe");
driver = new ChromeDriver();
{ driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);}
{driver.manage().window().maximize();}
driver.get(Constant.URL);
}
}

org.openqa.selenium.NoSuchElementException

I am trying to import login credential like: username and password using xsl sheet, by using below code but I got NoSuchElementException exception while running,
my xsl sheet looks like:
username | password
--------------------------
jan30selenium | selenium
am using:
eclipse
webdriver.iostream
selenium tool
Login.java:
package Iostream;
import java.io.FileInputStream;
import jxl.Sheet;
import jxl.Workbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class Login {
public WebDriver driver;
#Test
public void f()throws Exception {
FileInputStream fi=new FileInputStream("E:\\workspace1\\SeleniumAutomation\\test data\\login.xls");
Workbook w= Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);
driver.findElement(By.id("f_id")).sendKeys(s.getCell(0,1).getContents());
driver.findElement(By.id("f_pwd")).sendKeys(s.getCell(1,0).getContents());
Thread.sleep(5000);
driver.findElement(By.linkText("input.signin")).click();
Thread.sleep(3000);
}
#BeforeTest
public void beforeTest() {
System.setProperty("webdriver.chrome.driver","\\E:\\lib\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("http://www.gmail.com");
}
#AfterTest
public void afterTest() {
}
}
Exception is:
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=C:\Users\User\AppData\Local\Temp\scoped_dir5352_17443}, rotatable=false, locationContextEnabled=true, version=33.0.1750.154, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, applicationCacheEnabled=false, takesScreenshot=true}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:348)
at org.openqa.selenium.By$ById.findElement(By.java:216)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)
at Iostream.Login.f(Login.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
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:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Where is the problem?
You are getting that issue, because when you get("http://gmail.com/") the id's ("f_id" and "f_pwd" are not there.
If your intention is to log in, then you should probably be more specific with your URL, and you should use the following code:
#Test
public void f()throws Exception {
FileInputStream fi=new FileInputStream("E:\\workspace1\\SeleniumAutomation\\test data\\login.xls");
Workbook w= Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);
driver.findElement(By.id("Email")).sendKeys(s.getCell(0,1).getContents());
driver.findElement(By.id("Passwd")).sendKeys(s.getCell(1,0).getContents());
Thread.sleep(5000);
driver.findElement(By.cssSelector("input#signIn")).click();
Thread.sleep(3000);
}
#BeforeTest
public void beforeTest() {
System.setProperty("webdriver.chrome.driver","\\E:\\lib\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://accounts.google.com/ServiceLogin?service=mail");
}
Also, you'll note when clicking the login button, you were looking for a link with the TEXT "input.signin" I think what you meant to do, is a CSS selector input with a class of signin but that element doesn't exist either.

Cannot instantiate class

iam facing a problem i have written a method navback, which i need to use regularily to navigate back. when iam running it is throwing a error.
below is the Code.
package Examples;
import java.util.concurrent.TimeUnit;
//import org.junit.BeforeClass;
import org.testng.annotations.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Flipkart {
public static WebDriver driver;
Actions action = new Actions(driver);
//String ddd;
// public Example2() {
// super();
// }
#BeforeClass
public void beforeClass()
{
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30000, TimeUnit.MILLISECONDS);
}
#Test
public void mailSend() throws InterruptedException
{
driver.get("https://www.flipkart.com/");
driver.manage().window().maximize();
navback();
driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/ul/li/div/div[2]/div/ul/li[2]/a")).click();
driver.navigate().back();
navback();
driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/ul/li/div/div[2]/div/ul/li[3]/a")).click();
driver.navigate().back();
driver.navigate().refresh();
}
public void navback()
{
WebElement we = driver.findElement(By.xpath("//html/body/div/div/div[2]/div/div/ul/li/a/span"));
action.moveToElement(we).build().perform();
}
#AfterClass
public void tear()
{
// driver.quit();
}
}
Below is the Error.
org.testng.TestNGException:
Cannot instantiate class Examples.Flipkart
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:186)
at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
at org.testng.TestRunner.initMethods(TestRunner.java:409)
at org.testng.TestRunner.init(TestRunner.java:235)
at org.testng.TestRunner.init(TestRunner.java:205)
at org.testng.TestRunner.<init>(TestRunner.java:160)
at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:141)
at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:561)
at org.testng.SuiteRunner.init(SuiteRunner.java:157)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
... 21 more
Caused by: java.lang.NullPointerException
at org.openqa.selenium.interactions.Actions.<init>(Actions.java:41)
at Examples.Flipkart.<init>(Flipkart.java:18)
... 26 more
Please some one help me, iam not able to proceed further.
Thanks
You don't initialize driver, so it is null, but you pass it to Actions.
public static WebDriver driver;
Actions action = new Actions(driver);
That throws a NullPointerException.
Caused by: java.lang.NullPointerException
at org.openqa.selenium.interactions.Actions.<init>(Actions.java:41)
Initialize driver.
Note the lifecycle. Before JUnit runs your #BeforeClass or #Before methods, it has to create the Flipkart instance. The instance field initialization expression runs at that point.
Rethink your design. Initialize action after driver has been initialized.

Categories

Resources