mardi 9 février 2016

not able to execute second Class from Testng.xml file

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 mu 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. please help:

Following is my testng XML file:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://ift.tt/19x2mI9">

<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>






Following are my code that I included in  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 Following are code that included in 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();
      }

}

Aucun commentaire:

Enregistrer un commentaire