not able to execute second Class from Testng.xml file

痴心易碎 提交于 2019-12-26 01:32:09

问题


In the Base class I initiate browser as static WebDriver driver = new FirefoxDriver();

Then do I need to include same static WebDriver driver = new FirefoxDriver(); in the second class ?

1 Case: Where I included static WebDriver driver = new FirefoxDriver(); in the second class as well however, during the execution second class in not executed. I used @BeforeMethod and @AfterMethod TestNG annotation as well in my both class. However, it is not working out for me. Please help me if I am doing anything wrong here.

In the below case I want to continue my test execution with second class from where I stopped my first Class test:

For example in the first class I logged into application and after login I am on Home page now. So now I want to further execute my second class from Home page onward. However, after executing first class and login into application, second class is not executing.

After Test suite execution in the "Result of running suite" tab show following error message for class 2:

java.lang.NullPointerException
    at com.proweb.Web2.navigation(ProviderInformation.java:32)
    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:497)
    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.access$000(SuiteRunner.java:37)
    at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:368)
    at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

Here is my testNG XML file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="2" parallel="tests" >
  <test name="AllTest">
    <classes>
      <class name="com.proweb.web1" />
      <class name="com.proweb.web2" />
    </classes>
  </test>
</suite>

Here is my Base class:

package com.proweb;
import java.io.File;
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.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class Web1 {

    public WebDriver driver;
    Workbook wb;
    Sheet sh1;
    int numrow;
    String username;
    String password;

    @BeforeMethod
    public void oneTimeSetUp() {

    static WebDriver driver = new FirefoxDriver();  
    driver.manage().window().maximize();
    driver.get("http://example.com");
    Thread.sleep(1000);
    driver.findElement(By.xpath("//*[@id='app']/div/main/section/ul/li[1]/a")).click();
    Thread.sleep(1000);

    }


    @Test(dataProvider="testdata")
    public void testFireFox(String uname,String password1) throws InterruptedException

    {

    driver.findElement(By.xpath("//input[@name='username']")).clear();
    Thread.sleep(1000);
    driver.findElement(By.xpath("//input[@name='username']")).sendKeys(uname);
    Thread.sleep(1000);
    driver.findElement(By.xpath("//input[@name='password']")).clear();
    Thread.sleep(1000);
    driver.findElement(By.xpath("//input[@name='password']")).sendKeys(password1);
    Thread.sleep(1000);
    driver.findElement(By.xpath("//button[@name='loginButton']")).click();
    Thread.sleep(1000);
    }

    @DataProvider(name="testdata")
    public Object[][] TestDataFeed(){

    try {

    // load workbook
    wb=Workbook.getWorkbook(new File("C://File//Book2.xls"));

    // load sheet in my case I am referring to first sheet only
    sh1= wb.getSheet(0);

    // get number of rows so that we can run loop based on this
    numrow=  sh1.getRows();
    }
    catch (Exception e)

    {
    e.printStackTrace();
    }

    // Create 2 D array and pass row and columns
    Object [][] logindata=new Object[numrow][sh1.getColumns()];

    // This will run a loop and each iteration  it will fetch new row
    for(int i=0;i<numrow;i++){

    // Fetch first row username
        logindata[i][0]=sh1.getCell(0,i).getContents();
    // Fetch first row password
        logindata[i][1]=sh1.getCell(1,i).getContents();

    }

    // Return 2d array object so that test script can use the same
    return logindata;
    }

      @AfterMethod

      public void afterMethod() {

          // Close the driver

          driver.quit();
      }
}

And here is my second Class:

package com.proweb;

import java.io.File;
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.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class Web2 {

    public WebDriver driver;
    Workbook wb;
    Sheet sh2;
    int numrow;
    String firstname;
    String middlename;
    String lastname;

    @BeforeMethod
    public void SetUp() {
        static WebDriver driver = new FirefoxDriver();

        driver.findElement(By.xpath("//*[@id='main-navigation']/span[1]")).click();
        Thread.sleep(1000);
        driver.findElement(By.xpath("//*[@id='app']/div/header/nav[1]/div/ul/li/ul/li[2]/a")).click();
        Thread.sleep(1000);
        driver.findElement(By.xpath("//*[@id='app-main']/main/div/div[1]/div/a/span[2]")).click();
        Thread.sleep(1000);
        driver.findElement(By.xpath("//*[@id='-selector_input']")).sendKeys("xyz123xyz");
        Thread.sleep(1000);
        driver.findElement(By.xpath("//*[@id='selector_listbox__option__0']")).click();
        Thread.sleep(1000);
        driver.findElement(By.xpath("//*[@id='app-main']/main/div/div[3]/button")).click();
        Thread.sleep(3000);
        }




    @Test(dataProvider="testdata")
    public void testProvidername(String fname,String mname,String lname) throws InterruptedException

    {

    driver.findElement(By.xpath("//*[@id='nameFirst']")).clear();
    Thread.sleep(1000);
    driver.findElement(By.xpath("//*[@id='nameFirst']")).sendKeys(fname);
    Thread.sleep(1000);
    driver.findElement(By.xpath("//*[@id='nameMiddle']")).clear();
    Thread.sleep(1000);
    driver.findElement(By.xpath("//*[@id='nameMiddle']")).sendKeys(mname);
    Thread.sleep(1000);
    driver.findElement(By.xpath("//*[@id='nameLast']")).click();
    Thread.sleep(1000);
    driver.findElement(By.xpath("//*[@id='nameLast']")).sendKeys(lname);
    Thread.sleep(1000);
    }

    @DataProvider(name="testdata")
    public Object[][] TestDataFeed(){

    try {

    // load workbook
    wb=Workbook.getWorkbook(new File("C://File//Book2.xls"));

    // load sheet in my case I am referring to first sheet only
    sh2= wb.getSheet(1);

    // get number of rows so that we can run loop based on this
    numrow=  sh2.getRows();
    }
    catch (Exception e)

    {
    e.printStackTrace();
    }

    // Create 2 D array and pass row and columns
    Object [][] Peinformationdata=new Object[numrow][sh2.getColumns()];

    // This will run a loop and each iteration  it will fetch new row
    for(int i=0;i<numrow;i++){

    // Fetch first row username
        Peinformationdata[i][0]=sh2.getCell(0,i).getContents();
    // Fetch first row password
        Peinformationdata[i][1]=sh2.getCell(1,i).getContents();

        Peinformationdata[i][2]=sh2.getCell(2,i).getContents();

    }

    // Return 2d array object so that test script can use the same
    return Peinformationdata;
    }

      @AfterMethod

      public void afterMethod() {

          // Close the driver

          driver.quit();
      }

}

回答1:


As per provided example, you initiated webdriver in each class and quitting the same in that class. If you what to use same webdriver in two classes, then its good to initiate webdriver in one separate class and then extends this class to your test cases classes(nothing but yours base and second class).

let see one simple example, i will create a class config where i initiate my webdriver

 public class config{

static WebDriver driver;

@BeforeSuite
public void setup(){

    driver=new FirefoxDriver();

}

@AfterSuite
public void tearDown(){
    driver.quit();
}

}

Now, i will extend this class to my test cases/classes

   public class NewTest1 extends config{

   @Test
   public void test1() {
   driver.get("http://www.google.com");
   }
  }

for another class

  public class NewTest2 extends config{

@Test
public void MyTesting() {
         driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
  }
}

Now, execute these two classes from tesng.xml please note to use preserve-order="true" to execute test cases/classes as specified order

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
 <suite name="Suite" parallel="false" preserve-order="true">

  <test name="TestA">
    <classes>
      <class name="com.test.NewTest1"/>
      <class name="com.test.NewTest2"/>
    </classes>
  </test> <!-- Test -->
 </suite> <!-- Suite -->

Thank You, Murali



来源:https://stackoverflow.com/questions/35296953/not-able-to-execute-second-class-from-testng-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!