Eclipse not showing run as java application, only showing run configurations - java

This is my code.
Still building it.
But just wanted to check if the syso works.
package mySeleniumProjects;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ReadExcelExample {
static WebDriver driver = new FirefoxDriver();
static String username;
static String passwd;
static String baseURL = "http://www.guru99.com";
int rowNum;
int colNum;
public void main (String[] args) throws IOException{
File excel = new File("Gave File path here");
FileInputStream fis = new FileInputStream(excel);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet ws = wb.getSheet("Sheet1");
rowNum = ws.getLastRowNum();
colNum = ws.getRow(0).getLastCellNum();
System.out.println(rowNum);
System.out.println(colNum);
}
}
When I try to run it the only option I am getting is "Run Configurations".
Why I am not getting run as java application option?
I don't know how to choose run time configuration.
Can somebody help?

Method signature is incorrect:
You mentioned:
public void main (String[] args) throws IOException
It should be:
public static void main (String[] args) throws IOException

Write click on your java file. then on the next window search for 'java application', then click on 'New launch configuration' and select your java file and click on run. It will run your java program successfully.

Related

Nullpointer Exception While Using Trying to Read Excel in Selenium

Hi guys i Searched Every Where Solution For But Can't Find. Why Am Getting Null Pointer Exception For This i Dunno. Please Sort Me This Out. It is Showing as Path is Only Wrong But i Specified it Correctly only.
My Code :
package UsingExcel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.sun.rowset.internal.Row;
public class Demo
{
public void ReadExcel(String filepath,String filename,String Sheetname) throws IOException
{
File file = new File(filepath); // line 21
FileInputStream stream = new FileInputStream(file);
Workbook Mybook = null;
String FileExtensionnname = filename.substring(filename.indexOf("."));
if(FileExtensionnname.equals(".xlsx"))
{
Mybook = new XSSFWorkbook(stream);
}
else if(FileExtensionnname.equals(".xls"))
{
Mybook = new HSSFWorkbook(stream);
}
Sheet filesheet = Mybook.getSheet(Sheetname);
int rowcount = filesheet.getLastRowNum()-filesheet.getFirstRowNum();
for(int i=0;i<rowcount+1;i++)
{
org.apache.poi.ss.usermodel.Row row =filesheet.getRow(i);
for(int j=0;j<row.getLastCellNum();j++)
{
System.out.println(row.getCell(j).getStringCellValue()+ "||");
}
System.out.println();
}
}
public static void main(String[] args) throws IOException
{
Demo excelfile = new Demo();
String filepath = System.getProperty("E:\\Mybook.xlsx");
excelfile.ReadExcel(filepath, "Mybook.xlsx", "DemoExcel");
}
}
My Error is :
Exception in thread "main" java.lang.NullPointerException
at java.io.File.<init>(Unknown Source)
at UsingExcel.Demo.ReadExcel(Demo.java:21)
at UsingExcel.Demo.main(Demo.java:61)
Hope You Have Understood My Problem, Please Sort This out. But When am Testing a Login Page Using Excel That No Problem Will Be Coming, Now i Try To Print on The
Console it is Not Working.
Your filepath should just be
String filepath = "E:\\Mybook.xlsx", don't use System.getProperty.
From docs :
Gets the system property indicated by the specified key
A null is being passed to your method ReadExcel(...), because there is no System property defined as E:\Mybook.xlsx

How to click on an image using selenium WebDriver

I am new to selenium, unable to click on image named "Register" which is highlighted in attached screenshot.Can anyone let me know why the web element is not being identified on run time and what can be done to identify and click on it?
import java.io.FileInputStream;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.annotations.Test;
public class Registration {
public WebDriver driver1;
public String exepath="C:\\Users\\ADMIN\\Downloads\\chromedriver.exe";
public String filepath="C:\\Users\\ADMIN\\Desktop\\Book1tetsts.xls";
public FileInputStream file;
public String userID;
public String password;
public String Fname;
public String Lname;
private Object wait;
#Test
void formfilling() throws BiffException, IOException, InterruptedException
{
file = new FileInputStream(filepath);
Workbook wb = Workbook.getWorkbook(file);
Sheet sh = wb.getSheet(0); // this is to get the access to Sheet1.
userID= sh.getCell(0,0).getContents();
password= sh.getCell(1,0).getContents();
Fname= sh.getCell(2,0).getContents();
Lname=sh.getCell(3,0).getContents();
System.setProperty("webdriver.chrome.driver", exepath);
driver1= new ChromeDriver();
driver1.get("http://www.esevaonline.telangana.gov.in");
synchronized (driver1) {
driver1.wait(15000);
}
driver1.findElement(By.xpath("//*[#id='lhsNav']/a/img[#src='images/register2.gif']")).click();
//UserID
driver1.findElement(By.xpath("/html/body/center/form/table/tbody/tr[2]/td/div/center/table/tbody/tr[1]/td[2]/input")).sendKeys(userID);
//Password
driver1.findElement(By.xpath("/html/body/center/form/table/tbody/tr[2]/td/div/center/table/tbody/tr[2]/td[2]/input")).sendKeys(password);
//Re-Type Password
driver1.findElement(By.xpath("/html/body/center/form/table/tbody/tr[2]/td/div/center/table/tbody/tr[3]/td[2]/input")).sendKeys(password);
driver1.findElement(By.xpath("/html/body/center/form/table/tbody/tr[2]/td/div/center/table/tbody/tr[5]/td[2]/input")).sendKeys(Fname);
//LastName
driver1.findElement(By.xpath("/html/body/center/form/table/tbody/tr[2]/td/div/center/table/tbody/tr[6]/td[2]/input")).sendKeys(Lname);
//DOB
}
}
Screenshot
The given "Register" img is inside frame . So you need to switch frame first then performe any event on webelement which are inside that frame.
Please add below line of code in ur code.
driver1.switchTo().frame("mainFrame"); // switch frame
driver1.findElement(By.xpath("//*[#id='lhsNav']/a/img[#src='images/register2.gif']")).click();
//UserID
// other operation
Hope this help you :)

Selenium Webdrier using Java: Exception while reading from Excel

I am using SeleniumWebDriver using Java to automation one of the portal applications. As part of this, I want to read username and password from Excel and written below code. But seeing Exception in thread "main" java.lang.NoClassDefFoundError: org/openxmlformats/schemas/drawingml/x2006/main/ThemeDocument Below is the code
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.Random;
public class BankingFaceLift {
static WebDriver driver = null;
public static void main(String[]args){
driver = new FirefoxDriver();
driver.get("https://obsit.enbduat.com/obweb/common/login.jsf?faces-redirect=true");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
try
{
File file = new File("TestData.xlsx");
FileInputStream iFile = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(iFile);
XSSFSheet sheet = wb.getSheet("Sheet1");
int rowCount = sheet.getLastRowNum();
System.out.println("the no of rows are : " + rowCount);
for (int row=1; row<=rowCount; row++)
{
String Username = sheet.getRow(row).getCell(0).getStringCellValue();
String Password = sheet.getRow(row).getCell(1).getStringCellValue();
driver.findElement(By.id("username")).sendKeys(Username);
driver.findElement(By.id("j_idt49")).sendKeys(Password);
driver.findElement(By.id("submit")).click();
I have imported poi-xxx.jar and poi-ooxml.jar1.
Kindly advice Thanks!
You need to import poi-ooxml-schemas jar as well. You can download the jar from here

Argument type mismatch in JUnit

I'm fairly new to Selenium webdriver and Java, previously I used Selenium IDE. I'm trying to read testdata from an Excel sheet. Which is then written to Eclipse console, which works, and should be used to execute the actual test, which doesn't work. The actual test is not executed because I get an error argument type mismatch. The code looks like this:
package Test_Excel;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
#RunWith(Parameterized.class)
public class TestWithExcelData {
// Our two parameters
private final int input;
private final int resultExpected;
// Constructor
public TestWithExcelData(int input, int result) {
this.input = input;
this.resultExpected = result;
}
#Parameters
public static Iterable<Object []> data() throws BiffException, IOException
{
String FilePath = "C://Selenium//workspace//testproject//src//testdata//TestData.xls";
FileInputStream fs = new FileInputStream(FilePath);
Object[][] object = new Object[6][2];
Workbook wb = Workbook.getWorkbook(fs);
//locate the excel file in the local machine
Sheet sheet = wb.getSheet("IOResult");
int i=1; //avoid header row
while(!(sheet.getCell(0, i).getContents().equals("end"))) //read data till it reaches the cell whose text is ‘end’
{
object[i-1][0]=sheet.getCell(0, i).getContents();
object[i-1][1]=sheet.getCell(1, i).getContents();
System.out.print(sheet.getCell(0, i).getContents() + "\t");
System.out.print(sheet.getCell(1, i).getContents() + "\t");
System.out.println();
i++;
}
return Arrays.asList(object);
}
#Test
public void testSquareOff(){
Assert.assertEquals(resultExpected, MathUtils.square(input));
}
}
Is there somebody who can point me in the right direction?
Thanks in advance
The method sheet.getCell(column, row).getContents() is returning a String, but your constructor expects int.
You may modify the two lines in the data() method:
object[i-1][0] = Integer.valueOf(sheet.getCell(0, i).getContents());
object[i-1][1] = Integer.valueOf(sheet.getCell(1, i).getContents());

Crystal report SDK JRC Passing parameter giving error Error code:-2147217394 Error code name:missingParameterValueError

When I am trying pass paramaters from crystal report SDK and export my report to PDF. It is stand alone application so I can't user crystalreportviewer options but it is keep giving me error
com.crystaldecisions.sdk.occa.report.lib.ReportSDKParameterFieldException: InternalFormatterException---- Error code:-2147217394 Error code name:missingParameterValueError My code is given below please help
import com.crystaldecisions.reports.sdk.DatabaseController;
import com.crystaldecisions.reports.sdk.ReportClientDocument;
import com.crystaldecisions.reports.sdk.ParameterFieldController;
import com.crystaldecisions.sdk.occa.report.data.IConnectionInfo;
import com.crystaldecisions.sdk.occa.report.exportoptions.ExportOptions;
import com.crystaldecisions.sdk.occa.report.exportoptions.ReportExportFormat;
import com.crystaldecisions.sdk.occa.report.lib.ReportSDKException;
public class Test {
public static void main(String args[]) throws ReportSDKException, SQLException, FileNotFoundException, IOException{
try{
ReportClientDocument reportClientDoc = new ReportClientDocument();
reportClientDoc.open("Report.rpt",0);
ParameterFieldController paramController = reportClientDoc.getDataDefController().getParameterFieldController();
paramController.setCurrentValue("","P_DP",new Integer(22));
//Here I was calling switch database code
ByteArrayInputStream byteArrayInputStream = (ByteArrayInputStream)reportClientDoc.getReportSource().export(ReportExportFormat.PDF);
IOUtils.copy(byteArrayInputStream, new FileOutputStream("new.pdf"));
reportClientDoc.close();
}
}
it starts working by just moving setting parameter code to above line. we need to set parameter before opening the report.
import com.crystaldecisions.reports.sdk.DatabaseController;
import com.crystaldecisions.reports.sdk.ReportClientDocument;
import com.crystaldecisions.reports.sdk.ParameterFieldController;
import com.crystaldecisions.sdk.occa.report.data.IConnectionInfo;
import com.crystaldecisions.sdk.occa.report.exportoptions.ExportOptions;
import com.crystaldecisions.sdk.occa.report.exportoptions.ReportExportFormat;
import com.crystaldecisions.sdk.occa.report.lib.ReportSDKException;
public class Test {
public static void main(String args[]) throws ReportSDKException, SQLException, FileNotFoundException, IOException{
try{
ReportClientDocument reportClientDoc = new ReportClientDocument();
ParameterFieldController paramController = reportClientDoc.getDataDefController().getParameterFieldController();
paramController.setCurrentValue("","P_DP",new Integer(22));
reportClientDoc.open("Report.rpt",0);
//Here I was calling switch database code
ByteArrayInputStream byteArrayInputStream = (ByteArrayInputStream)reportClientDoc.getReportSource().export(ReportExportFormat.PDF);
IOUtils.copy(byteArrayInputStream, new FileOutputStream("new.pdf"));
reportClientDoc.close();
}
}

Categories