Sriram
Sriram

Reputation: 449

Calling a method from class to another using Java for Selenium Webdriver

I am new to Selenium and Java. The basic idea about my code is, I have created two classes namely first and second under my java project. My first class consists of all the methods (opening a browser, login, logout) and my second class consists of the main program to inherit all the methods from the first.

I have created an excel stating which test case (login , logout) should run based on the excel sheet. The test case will be executed based on "Y" or "N" under Execute column in Excel sheet

When I tried to run my second class it showed an error. Please find my code below and the error and excel sheet as image from where the test case ought to run.

This is my Second class (Main program)

package asdf;

import java.io.File;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.SkipException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class second {                   

    @DataProvider(name = "DP1")
    public Object[][] createData1() throws Exception {
        Object[][]retObjArr=getTableArray("Resource\\Testdata.xls","credentials","TD");
        return(retObjArr);
    }

    @Test(dataProvider = "DP1")
    public void execution(String TCname, String TCDescription, String Execute) {
        first r=new first();
        if(Execute.equalsIgnoreCase("Y")) { 
            if(TCname.equalsIgnoreCase("TC01")) {
                r.login();
            } else if(TCname.equalsIgnoreCase("TC02")) {
                r.login();
            r.logout();
        }
        } else {
        throw new SkipException("Skipping this test case: "+TCname);
        }
    }       

    public String[][] getTableArray(String xlFilePath, String sheetName, String tableName) throws Exception {
        String[][] tabArray=null;
        Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
        Sheet sheet = workbook.getSheet(sheetName);
        int startRow,startCol, endRow, endCol, ci, cj;
        Cell tableStart=sheet.findCell(tableName);
        startRow = tableStart.getRow();
        startCol = tableStart.getColumn();
        Cell tableEnd = sheet.findCell(tableName, startCol+1, startRow+1, 100, 64000,  false);
        endRow = tableEnd.getRow();
        endCol=tableEnd.getColumn();

        System.out.println("startRow="+startRow+",  endRow="+endRow+", " + "startCol="+startCol+", endCol="+endCol);
        tabArray = new String[endRow-startRow-1][endCol-startCol-1];
        ci=0;

        for (int i=startRow+1;i<endRow;i++,ci++) {
            cj=0;
            for (int j=startCol+1;j<endCol;j++,cj++) {
                tabArray[ci][cj]=sheet.getCell(j,i).getContents();
            }
        }
        return(tabArray);
    }
}        

**This is my First class (which contains all the methods and should return these methods to Second class)** 
package asdf;

import java.io.File;    
import org.openqa.selenium.WebDriver;    
import org.openqa.selenium.By;    
import org.openqa.selenium.WebDriver;    
import org.openqa.selenium.firefox.FirefoxDriver;    
import org.openqa.selenium.ie.InternetExplorerDriver;    
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeMethod;    
import org.testng.annotations.Test;

public class first {

    public static WebDriver driver;
    public String baseUrl;
    static{
     driver =new InternetExplorerDriver();
     String baseUrl = "http://newtours.demoaut.com/";

    }

public void login()

{

    driver.get(baseUrl);

    driver.findElement(By.name("Email")).sendKeys("mercury");

    driver.findElement(By.name("Passwd")).sendKeys("mercury");

    driver.findElement(By.name("signIn")).click();
    }

public void logout()

{

    driver.findElement(By.xpath(".//*[@id='gb_71']"));

}

}

The exception it throws is given below

> [TestNG] Running:
  C:\Users\SRI\AppData\Local\Temp\testng-eclipse--2072559575\testng-customsuite.xml

startRow=0, endRow=3, startCol=0, endCol=4
FAILED: execution("TC01", "login", "Y")
java.lang.ExceptionInInitializerError
    at asdf.second.execution(second.java:28)
    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:80)
    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:128)
    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:1203)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
    at org.testng.TestNG.run(TestNG.java:1036)
    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.IllegalStateException: The path to the driver executable must be set by the webdriver.ie.driver system property; for more information, see http://code.google.com/p/selenium/wiki/InternetExplorerDriver. The latest version can be downloaded from http://code.google.com/p/selenium/downloads/list
    at com.google.common.base.Preconditions.checkState(Preconditions.java:176)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:105)
    at org.openqa.selenium.ie.InternetExplorerDriverService.access$1(InternetExplorerDriverService.java:1)
    at org.openqa.selenium.ie.InternetExplorerDriverService$Builder.build(InternetExplorerDriverService.java:177)
    at org.openqa.selenium.ie.InternetExplorerDriver.setupService(InternetExplorerDriver.java:111)
    at org.openqa.selenium.ie.InternetExplorerDriver.setup(InternetExplorerDriver.java:104)
    at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:51)
    at asdf.first.<clinit>(first.java:19)
    ... 25 more

FAILED: execution("TC02", "logout", "N")
java.lang.NoClassDefFoundError: Could not initialize class asdf.first
    at asdf.second.execution(second.java:28)
    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:80)
    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:128)
    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:1203)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
    at org.testng.TestNG.run(TestNG.java:1036)
    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: 2, Failures: 2, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Failures: 2, Skips: 0
===============================================

[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@9fa12f: 16 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@1cc0a0f: 0 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@e43b44: 0 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@109062e: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@1d7e4d6: 31 ms

Kindly requesting your help on this.

Screen shot of Excel sheet:

TD    TCname        TCDescription          Execute  

       TC01                login                 Y  

       TC02                logout                N  

                                                               TD

NEW ERROR REPORT AFTER ADDING THE IE PATH

[TestNG] Running:
  C:\Users\SRI\AppData\Local\Temp\testng-eclipse--824275806\testng-customsuite.xml

startRow=0, endRow=3, startCol=0, endCol=4
FAILED: execution("TC01", "login", "Y")
java.lang.ExceptionInInitializerError
    at asdf.second.execution(second.java:28)
    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:80)
    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:128)
    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:1203)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
    at org.testng.TestNG.run(TestNG.java:1036)
    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.IllegalStateException: The driver executable does not exist: C:\Selenium\iexploredriver.exe
    at com.google.common.base.Preconditions.checkState(Preconditions.java:176)
    at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:117)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:112)
    at org.openqa.selenium.ie.InternetExplorerDriverService.access$1(InternetExplorerDriverService.java:1)
    at org.openqa.selenium.ie.InternetExplorerDriverService$Builder.build(InternetExplorerDriverService.java:177)
    at org.openqa.selenium.ie.InternetExplorerDriver.setupService(InternetExplorerDriver.java:111)
    at org.openqa.selenium.ie.InternetExplorerDriver.setup(InternetExplorerDriver.java:104)
    at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:51)
    at asdf.first.<clinit>(first.java:21)
    ... 25 more

FAILED: execution("TC02", "logout", "N")
java.lang.NoClassDefFoundError: Could not initialize class asdf.first
    at asdf.second.execution(second.java:28)
    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:80)
    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:128)
    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:1203)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
    at org.testng.TestNG.run(TestNG.java:1036)
    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: 2, Failures: 2, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Failures: 2, Skips: 0
===============================================

[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 15 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@9fa12f: 78 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@1cc0a0f: 0 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@e43b44: 47 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@109062e: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@1d7e4d6: 110 ms

Upvotes: 1

Views: 17788

Answers (2)

Pradeep
Pradeep

Reputation: 21

Try below code , may b it resolve the issue -

public class first {

{

    System.setProperty("webdriver.ie.driver", "D:\\Pradeep\\pradeep\\IEDriverServer.exe");
         // your location of driver


}

WebDriver driver = new InternetExplorerDriver();

public String baseUrl = "http://newtours.demoaut.com/";

...... then your code

@test ...

Upvotes: 1

slashdot
slashdot

Reputation: 786

You are missing the path to executable IE driver. Try this:

File file = new File("C:/Selenium/iexploredriver.exe"); // your location of driver
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();

Hope that helps.

Upvotes: 1

Related Questions