I created a class which has page objects defined in it. Then I am trying to use that class in a test class by using a constructor. However when I run the test class using JUnit I am getting a NullPointerException error.
Page_Objects Class:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.Select;
public class Page_Objects {
private WebDriver driver;
WebElement Username = driver.findElement(By.id("username"));
// ERROR: Caught exception [Error: locator strategy either id or name must be specified explicitly.]
WebElement Password = driver.findElement(By.id("password"));
WebElement Login_Button = driver.findElement(By.id("Login"));
WebElement Logout_Button = driver.findElement(By.linkText("Logout"));
}
Test Class:
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Salesforce_Test {
private WebDriver driver1;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
Page_Objects pageobjects = new Page_Objects();
#Before
public void setUp() throws Exception {
driver1 = new FirefoxDriver();
baseUrl = "some url for testing/";
driver1.navigate().to(baseUrl);
driver1.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void Login_Logout() throws Exception {
pageobjects.Username.sendKeys("someusername");
// ERROR: Caught exception [Error: locator strategy either id or name must be specified explicitly.]
pageobjects.Password.sendKeys("somepassword");
pageobjects.Login_Button.click();
pageobjects.Logout_Button.click();
}
#After
public void tearDown() throws Exception {
driver1.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver1.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver1.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver1.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
When I run the above class I get a null pointer exception.
java.lang.NullPointerException
at Page_Objects.<init>(Page_Objects.java:20)
at Salesforce_Test.<init>(Salesforce_Test.java:16)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
I see two issues here. First is that the Page_Objects class is called before you initiated the driver. That's why when you're calling
Page_Objects pageobjects = new Page_Objects();
you're getting a NullPointerException.
So you want to initiate the Page_Objects after you've initiated the driver.
Second, you need to pass it the instance of the driver you've initiated, because right now it has its own Private driver which isn't initiated and therefore it's null.
So in short:
Initiate Page_Objects after you've initiated driver (after the driver1 = new FirefoxDriver(); line)
Call it with the driver you've initiated: Page_Objects pageobjects = new Page_Objects(driver1 );
Related
This is the link I wanted to automate. But I am not being able to click on countryName. I wanted to enter "na" in the searchField and then click on "Nepal" using the list attribute i.e list.get(i).click() but I have not been able to. Please help
package autoComplete;
import java.time.Duration;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
public class AutoCompleteCountry {
WebDriver driver;
String url = "https://practice-cybertekschool.herokuapp.com/autocomplete";
By countryField = By.id("myCountry");
By countryList = By.xpath("//input[#type='hidden']");
#BeforeTest
public void getUrl() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));
driver.get(url);
}
#Test
public void autoCompleteTest() throws InterruptedException {
driver.findElement(countryField).sendKeys("N");
List<WebElement> listOfCountry = driver.findElements(countryList);
// driver.findElement(nepalClick).click();
for (int i = 0; i < listOfCountry.size(); i++) {
// System.out.println(list);
String searchText = listOfCountry.get(i).getAttribute("value");
System.out.println(searchText);
if (searchText.equals("Nepal")) {
listOfCountry.get(i).click();
}
}
}
#AfterTest
public void tearDown() throws InterruptedException {
Thread.sleep(5000);
driver.quit();
}
}
This is the error screenshot:
Error Screenshot
You needn't iterate through the whole list if you just want to click the "Nepal" option. Does the following help at all?
driver.findElement(By.CSS_SELECTOR, 'input[value="Nepal"]').click()
you can use Webdriver wait with a customExpected Condition like this below
Instead of using implicit wait use WebDriverwait for Fluentwait
private static ExpectedCondition<Boolean> waitForDropdownElement(By by, String value) {
return ( driver -> {
List<WebElement> listOfCountry = driver.findElements(by);
for (int i = 0; i < listOfCountry.size(); i++) {
String searchText = listOfCountry.get(i).getText();
if (searchText.equals(value)) {
listOfCountry.get(i).click();
return true;
}
}
return false;
});
}
Also modified below locator like this, as you will get element not clickable exception on using that
By countryList = By.xpath("//div[#id='myCountryautocomplete-list']//div");
Full code below which is working for me
import io.github.bonigarcia.wdm.WebDriverManager;
import java.time.Duration;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class AutoCompleteCountry {
WebDriver driver;
String url = "https://practice-cybertekschool.herokuapp.com/autocomplete";
By countryField = By.id("myCountry");
By countryList = By.xpath("//div[#id='myCountryautocomplete-list']//div");
#BeforeTest
public void getUrl() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));
driver.get(url);
}
#Test
public void autoCompleteTest() throws InterruptedException {
driver.findElement(countryField).sendKeys("N");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(60));
wait.until(waitForDropdownElement(countryList, "Nepal"));
}
#AfterTest
public void tearDown() throws InterruptedException {
Thread.sleep(5000);
driver.quit();
}
private static ExpectedCondition<Boolean> waitForDropdownElement(By by, String value) {
return (driver -> {
List<WebElement> listOfCountry = driver.findElements(by);
for (int i = 0; i < listOfCountry.size(); i++) {
String searchText = listOfCountry.get(i).getText();
if (searchText.equals(value)) {
listOfCountry.get(i).click();
return true;
}
}
return false;
});
}
}
package com.qa.base;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.qa.util.TestUitl;
public class TestBase {
public static WebDriver driver;
public static Properties prop;
// Creating a Constructor
public TestBase() {
prop = new Properties();
try {
File file = new File("/APIAutomation/CRMTest/src/main/java/com/qa/config/config.properties");
FileInputStream fis = new FileInputStream(file);
prop.load(fis);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void initializtion() {
String browser = prop.getProperty("browser");
if(browser.equals("chrome")) {
System.setProperty("webdriver.chrome.driver", "C:/APIAutomation/chromedriver.exe");
driver = new ChromeDriver();
}
else
{
System.out.println("Quit Running Script");
}
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(TestUitl.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(TestUitl.IMPLICIT_WAIT, TimeUnit.SECONDS);
driver.get(prop.getProperty("url"));
}
}
Page Factory
package com.qa.pages;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.qa.base.TestBase;
public class loginPage extends TestBase{
//Page Factory
#FindBy(name="email")
WebElement email;
#FindBy(name="password")
WebElement password;
#FindBy(xpath=".//div[#class='ui fluid large blue submit button']")
WebElement loginButton;
#FindBy(xpath="//div[#class ='column']/div[2][#class='ui message']")
WebElement OldLogin;
//Initialization the Page Object
public loginPage() {
PageFactory.initElements(driver, this);
}
//Actions
public String validateLoginPageTitle() {
return driver.getTitle();
}
public String validateUserName() {
return OldLogin.getText();
}
public HomePage login(String user, String pass) {
email.sendKeys(user);
password.sendKeys(pass);
loginButton.click();
return new HomePage();
}
}
Page Factory HomePage
package com.qa.pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.qa.base.TestBase;
public class HomePage extends TestBase{
//PageFactory
#FindBy(xpath = ".//div[#id='main-nav']/a[#href='/home']")
WebElement Homepage;
#FindBy(xpath =".//div/span[#class='user-display']")
WebElement username;
#FindBy(xpath =".//span[contains(text(),'Contacts')]")
WebElement contactlink;
#FindBy(xpath="//a[5][contains(#href,'/deals')]")
WebElement dealslink;
//Initializing Page Object
public HomePage() {
PageFactory.initElements(driver, this);
}
//Actions
public String validateHomePage() {
return driver.getTitle();
}
public boolean validateHomePageDisplayed() {
return Homepage.isDisplayed();
}
public String validateUser() {
return username.getText();
}
public boolean verifyUsername() {
return username.isDisplayed();
}
public ContactPage clickContactLink() {
contactlink.click();
return new ContactPage();
}
public DealsPage clickDeals() {
dealslink.click();
return new DealsPage();
}
}
Test Case Class
package com.crm.qa.TestCases;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.qa.base.TestBase;
import com.qa.pages.ContactPage;
import com.qa.pages.HomePage;
import com.qa.pages.loginPage;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
public class HomePageTest extends TestBase {
loginPage loginpage;
HomePage homepage;
ContactPage contactpage;
//Create a constructor of HomePageTest and call the super class i.e the TestBase class constructor will be called
public HomePageTest() {
super();
}
// test case should be separated - independent with each other
// before each test cases - launch the browser and login
// after each test cases - close the browser
#BeforeMethod
public void setUp() {
initializtion();
loginpage = new loginPage();
//Login method is returning homepage object
homepage = loginpage.login(prop.getProperty("username"), prop.getProperty("password"));
}
#Test
public void verifyHomePageTitleTest() {
String homepageTitle = homepage.validateHomePage();
System.out.println("Print HomePage Title:::::::::::::::::" +homepageTitle);
Assert.assertEquals(homepageTitle, "Cogmento CRM","Homepage title is not matching");
}
#Test
public void verifyUsernameTest() {
Assert.assertTrue(homepage.verifyUsername(), "Username is not matching");
}
#Test
public void verifyUserTest() {
String Username = homepage.validateUser();
Assert.assertEquals(Username, "Gokul Kuppusamy","Username text is not matching");
}
#Test
public void verifyContactPageTest() {
System.out.println("Clicking Contact Page");
contactpage = homepage.clickContactLink();
}
#AfterMethod
public void tearDown() {
driver.quit();
}
}
package com.crm.qa.TestCases;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.qa.base.TestBase;
import com.qa.pages.ContactPage;
import com.qa.pages.HomePage;
import com.qa.pages.loginPage;
public class ContactPageTest extends TestBase {
loginPage loginpage;
HomePage homepage;
ContactPage contactpage;
public ContactPageTest() {
super();
}
#BeforeMethod
public void setUp() throws Exception {
initializtion();
loginpage = new loginPage();
// Login method is returning homepage object
homepage = loginpage.login(prop.getProperty("username"), prop.getProperty("password"));
driver.manage().timeouts().implicitlyWait(5000, TimeUnit.SECONDS);
Thread.sleep(3000);
homepage.validateHomePageDisplayed();
/*
* Thread.sleep(3000); homepage.clickContactLink(null);
*/
}
#Test
public void verifyContactsPageNewButtonTest() {
Assert.assertTrue(contactpage.verifyNewButton());
}
#Test(priority = 1)
public void verifyContactsPageLableTest() {
driver.manage().timeouts().implicitlyWait(5000, TimeUnit.SECONDS);
Assert.assertTrue(contactpage.verifyContactlable(), "Contacts is missing on the page");
}
#Test(priority = 2)
public void selectContactDetailsTest() {
contactpage.selectContactDetails();
}
#AfterMethod
public void tearDown() {
driver.quit();
}
}
java.lang.NullPointerException
at com.crm.qa.TestCases.ContactPageTest.verifyContactsPageNewButtonTest(ContactPageTest.java:49)
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)
initializtion();
loginpage = new loginPage();
//below line is missing please add in HomePageTest class
**homepage=new Homepage();**
Please confirm whether you are using only one driver instance for all pages, Basically Null point exception occurring for this , The solution uses a global variable for driver instance
public static Webdrivr driver;
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I am new to the automated tests so please, bear with me
I've been trying to execute this Selenium-generated test, but with no success. It seems that there is a problem with the Firefox Driver instantiation process. Here is a copy-paste of my Stack, followed by the test itself.
testUntitledTestCase caused an error: java.lang.NullPointerException
java.lang.NullPointerException
at SomaTeste.tearDown(SomaTeste.java:47)
It also seems that it never reaches the "driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS)". It proceeds to the "driver.quit()"(line 47) right after executing "driver = new FirefoxDriver()".
By analyzing a breakpoint placed on line 47 (which is the "driver.quit()" one), I've confirmed that the "driver" variable is set to null, which explains why i'm getting the "NullPointerException", but why is it not being initialized in the first place ? I can't seem to find an answer to this.
Does anyone have any idea on what might be causing this?
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.junit.Test;
import static org.junit.Assert.*;
public class SomaTeste{
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
baseUrl = "https://www.katalon.com/";
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void testUntitledTestCase() throws Exception {
driver.get("http://localhost:8080/SomarParcelas/");
driver.findElement(By.name("p1")).click();
driver.findElement(By.name("p1")).clear();
driver.findElement(By.name("p1")).sendKeys("21");
driver.findElement(By.name("p2")).click();
driver.findElement(By.name("p2")).clear();
driver.findElement(By.name("p2")).sendKeys("12");
driver.findElement(By.name("calcular")).click();
driver.findElement(By.xpath("//h1")).click();
assertEquals("O resultado foi 33", driver.findElement(By.xpath("//h1")).getText());
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
I cannot comment, but it works fine for me when I ran your code in Eclipse on Windows with the chrome driver. However, when I use the firefox driver, it does exactly what you are saying. It turns out to be the selenium version I used. I was using 3.3.1, but 3.9.1 works fine.
I am learning selenium and Java.
Trying to create a test script to book hotel, then view the itinerary and finally perform logout operation on a test site.
After clicking "book_now" button it takes some time to load the next frame/page where the itinerary button exists.
I explicitly added "waitForFrameToLoad" command as the test case was failing on IDE stating "my_Itinerary" element not found.
After addition test case passed on IDE and when exported using junit/webdriver option,I see the below note:
package com.example.tests;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class CompleteBooking {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.adactin.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void testCompleteBooking() throws Exception {
driver.get(baseUrl + "/HotelAppBuild2/");
driver.findElement(By.id("username")).sendKeys("pranu86”);
driver.findElement(By.id("password")).sendKeys("test”);
driver.findElement(By.id("login")).click();
new Select(driver.findElement(By.id("hotels"))).selectByVisibleText("Hotel Sunshine");
driver.findElement(By.id("Submit")).click();
driver.findElement(By.id("radiobutton_0")).click();
driver.findElement(By.id("continue")).click();
driver.findElement(By.id("first_name")).sendKeys("pranu");
driver.findElement(By.id("cc_num")).sendKeys("1234567891234567");
new Select(driver.findElement(By.id("cc_type"))).selectByVisibleText("VISA");
driver.findElement(By.id("book_now")).click();
// ERROR: Caught exception [ERROR: Unsupported command [waitForFrameToLoad | http://www.adactin.com/HotelAppBuild2/BookingConfirm.php | 10000]]
// ERROR: Caught exception [Error: locator strategy either id or name must be specified explicitly.]
driver.findElement(By.id("my_itinerary")).click();
// ERROR: Caught exception [unknown command [clickAndWait ]]
driver.findElement(By.linkText("Click here to login again")).click();}
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);}}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;}}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;}}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
How to fix this issue/Error note? Please help
How to use this code :
public boolean isElementPresent(By by)
When I export my test script there will be automatically in the test script, but most of the method below is not used, so there will warnings stated that I'm not using that method..and my test script will be failed.
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class cheesecake {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "https://www.google.com.my/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void testCheesecake() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("gbqfq")).clear();
driver.findElement(By.id("gbqfq")).sendKeys("cheesecake");
driver.findElement(By.cssSelector("a.q.qs")).click();
driver.findElement(By.cssSelector("span.mn-dwn-arw")).click();
driver.findElement(By.xpath("//a[contains(text(),'News')]")).click();
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alert.getText();
} finally {
acceptNextAlert = true;
}
}
}
Can I know what is the use of all these methods and how to use it? Because, if I deleted them, my test script can run but even though I put a wrong password also can log in to the system.
You are right, this is auto generated by the IDE but you have incorrectly edited it:
public boolean isElementPresent(By by) {
try {
driver.findElements(by);
return true;
} catch (org.openqa.selenium.NoSuchElementException e) {
return false;
}
}
Then call it with:
isElementPresent(By.id("J_idt16:J_idt30"));
It is there to make it easier to determine whether an element is visible on the page. Sometimes you want to have that element and do something with it, other times you just want to know if it's present or not. This is there to make it easy to do so.
If you are having problems with your test script, please post the code you are using and the HTML you are running against or try and reproduce it with a different public facing website.