org.openqa.selenium.NoSuchElementException - java

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.

Related

IEDriver always showing localhost title instead of given URL title

When driver launching the browser it is always getting open with local host
and then navigating to given URL but driver is not getting the current page
title instead always showing local host title "This page can’t be
displayed". I am posting my code and error i am getting, please tell me
where i am wrong.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class TestIEBrowser {
public WebDriver driver;
#BeforeClass
public void setUp() {
System.out.println("*******************");
System.out.println("launching IE browser");
System.setProperty("webdriver.ie.driver", "C:\\IEDriverServer_Win32_2.39.0\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
}
#Test(priority=1)
public void testGooglePageTitleInIEBrowser() {
driver.navigate().to("http://www.google.com");
driver.manage().timeouts().implicitlyWait( 30, TimeUnit.SECONDS );
String strPageTitle = driver.getTitle();
System.out.println("Page title: - "+strPageTitle);
Assert.assertTrue(strPageTitle.equalsIgnoreCase("Google"),
"Page `title doesn't match");`
}
#AfterClass
public void tearDown() {
if(driver!=null) {
System.out.println("Closing IE browser");
driver.quit();
}
} }
Error
launching IE browser
Started InternetExplorerDriver server (32-bit) 2.39.0.0
Listening on port 47579
Jul 31, 2017 5:43:08 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Page title: - This page can’t be displayed
Closing IE browser
FAILED: testGooglePageTitleInIEBrowser
java.lang.AssertionError: Page title doesn't match expected [true] but found [false]
at org.testng.Assert.fail(Assert.java:93)
at org.testng.Assert.failNotEquals(Assert.java:512)
at org.testng.Assert.assertTrue(Assert.java:41)
at TestIEBrowser.testGooglePageTitleInIEBrowser(TestIEBrowser.java:30)
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:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
Try to use driver.get() instead of driver.navigate().
Also you could try to use
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(title.isDisplayed)

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.

Selenium Java, class to run same setup across tests

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.

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);
}
}

Selenium Webdriver unable to locate element from webtable

I am trying to access a webtable which is quite static and get the values from each row.
I am getting an error, the code is:
import java.util.List;
import java.util.concurrent.TimeUnit;
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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TS1_TC2 {
WebDriver driver=null;
String btn1="html/body/form/div[1]/center/table/tbody/tr/td[1]/input";
String table="html/body/form/table/tbody/tr[2]/td/div/center/table";
#BeforeTest
public void browserCheck()
{
driver=new FirefoxDriver();
driver.get("http://demo.borland.com/gmopost/");
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
#Test
public void TS1_TC2()
{
driver.findElement(By.xpath(btn1)).click();
//html/body/form/table/tbody/tr[2]/td/div/center/table/tbody
//html/body/form/table/tbody/tr[2]/td/div/center/table
WebElement table=driver.findElement(By.xpath("html/body/form/table/tbody/tr[2]/td/div/center/table"));
List<WebElement> rows=table.findElements(By.tagName("tr"));
//WebDriverWait wait = new WebDriverWait(driver, 100, 15);
System.out.println(rows.size());
for(int rownum=2;rownum<rows.size();rownum++)
{
List<WebElement> cols=rows.get(rownum).findElements(By.tagName("td"));
System.out.println(cols.size());
for(int colnum=1;colnum<cols.size();colnum++)
{
//System.out.println(cols.get(colnum).getText());
//wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("html/body/form/table/tbody/tr[2]/td/div/center/table/tbody/tr["+rownum+"]/td["+colnum+"]")));
System.out.println("html/body/form/table/tbody/tr[2]/td/div/center/table/tbody/tr["+rownum+"]/td["+colnum+"]");
System.out.println(cols.get(colnum).findElement(By.xpath("html/body/form/table/tbody/tr[2]/td/div/center/table/tbody/tr["+rownum+"]/td["+colnum+"]")).getText());
System.out.println("--");
}
System.out.println();
}
}
}
The Exception thrown is as follows:
FAILED: TS1_TC2
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"html/body/form/table/tbody/tr[2]/td/div/center/table/tbody/tr[2]/td[1]"}
Command duration or timeout: 20.08 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.40.0', revision: 'fbe29a9', time: '2014-02-19 20:55:11'
System info: host: 'Vihaan-PC', ip: '192.168.1.3', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_51'
Session ID: 15f51aea-04cd-4ea4-bdae-0bdd47312bd7
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=false, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=29.0.1}]
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.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:573)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
at org.openqa.selenium.remote.RemoteWebElement.findElement(RemoteWebElement.java:171)
at org.openqa.selenium.remote.RemoteWebElement.findElementByXPath(RemoteWebElement.java:244)
at org.openqa.selenium.By$ByXPath.findElement(By.java:357)
at org.openqa.selenium.remote.RemoteWebElement.findElement(RemoteWebElement.java:167)
at TS1_TC2.TS1_TC2(TS1_TC2.java:57)
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)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"xpath","selector":"html/body/form/table/tbody/tr[2]/td/div/center/table/tbody/tr[2]/td[1]"}
Build info: version: '2.40.0', revision: 'fbe29a9', time: '2014-02-19 20:55:11'
System info: host: 'Vihaan-PC', ip: '192.168.1.3', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_51'
Driver info: driver.version: unknown
at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/Vihaan/AppData/Local/Temp/anonymous7820392430389922124webdriver-profile/extensions/fxdriver#googlecode.com/components/driver_component.js:8904)
at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/Vihaan/AppData/Local/Temp/anonymous7820392430389922124webdriver-profile/extensions/fxdriver#googlecode.com/components/driver_component.js:396)
Kindly help , because I see the the xpath from firepath is exactly the same as the xpath fetched in the for loop.
Thanks
rajee
try using
driver.findElement(By.xpath("html/body/form/table/tbody/tr[2]/td/div/center/table/tbody/tr["+rownum+"]/td["+colnum+"]")).getText()
instead of
cols.get(colnum).findElement(By.xpath("html/body/form/table/tbody/tr[2]/td/div/center/table/tbody/tr["+rownum+"]/td["+colnum+"]")).getText()
It should work.
But I dont understand why you used the above line. The following code achieves the same thing.
WebDriver driver = null;
#BeforeTest
public void browserCheck() {
driver = new FirefoxDriver();
driver.get("http://demo.borland.com/gmopost/");
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
#Test
public void TS1_TC2() {
driver.findElement(By.name("bSubmit")).click();
WebElement table = driver
.findElement(By
.xpath("//form[#name='Items']/table/tbody/tr[2]/td/div/center/table"));
List<WebElement> rows = table.findElements(By.tagName("tr"));
for (int rownum = 2; rownum < rows.size(); rownum++) {
List<WebElement> cols = rows.get(rownum).findElements(
By.tagName("td"));
for (int colnum = 1; colnum < cols.size(); colnum++) {
System.out.println(cols.get(colnum).getText());
}
System.out.println("\n");
}
}
Let me know if this helps you.

Categories

Resources