将驱动程序对象的单个实例传递给所有其他类(Testng框架) [英] Passing the single instance of the driver object to all other classes (Testng framework)

查看:109
本文介绍了将驱动程序对象的单个实例传递给所有其他类(Testng框架)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在类示例中初始化的驱动程序对象.我也想将该驱动程序对象传递给其他类,但是我得到了一个空指针异常.我的代码是

I have a driver object initialized in class sample.I want to pass the driver object to other classes also but i get a null pointer exception. My code is

示例课程

    public class sample {

    WebDriver driver ;


    @Test(priority=1)

    public void openbrowser(){


        System.setProperty("webdriver.chrome.driver",
                "/home/ss4u/Desktop/Vignesh/jars/chromedriver");

        driver = new ChromeDriver();

        driver.get("http://www.google.com");

        System.out.println(driver instanceof WebDriver);


    }
   @Test(priority=2)
   public void maximize(){

      driver.manage().window().maximize();

   }
   @Test(priority=3)
   public void transfer_instance(){

       sampleone obj=new sampleone(driver);


   }

}

sampleclassone

public class sampleone {

    WebDriver driver;

    public sampleone(WebDriver driver){

        this.driver=driver;

        System.out.println(driver instanceof WebDriver);

        System.out.println(this.driver instanceof WebDriver);

        System.out.println("constructor2");


    }

  public sampleone(){

        System.out.println("Default constructor called");


    }


    @Test(priority=1)

     public void gettitle(){

          System.out.println(this.driver instanceof WebDriver);

          System.out.println(driver instanceof WebDriver);

          String title=this.driver.getTitle();

          System.out.println(this.driver instanceof WebDriver);

          System.out.println(title);

          Assert.assertEquals(title, "Google");

        }

    @Test(priority=2)

    public void navigate(){

        this.driver.get("https:in.yahoo.com");

    }

}

Testng xml文件

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="TestNG" verbose="1" >

    <test name="sample test">
    <classes>
      <class name="testsample.sample" />
    </classes>
    </test>

    <test name="sample testone">
    <classes>
      <class name="testsample.sampleone" />
    </classes>
    </test>

</suite>

发生此问题是因为iam不使用创建的对象而是使用testng.xml文件调用类,是否有任何可能的方法来创建新的Java实例(所有类通用)或在所有类中使用现有实例

This issue occurs as iam calling the class not using the object created but using the testng.xml file is there any possible way to create a new java instance(common for all classes) or use the existing instance in all classes

推荐答案

我自己找到了一个解决方案...当我详细了解testng时,我发现testng xml文件调用了xml中指定的所有类的默认构造函数. file.so,即使我们将对象传递给另一个类,也无法通过该对象执行操作,因此会发生空指针异常....我发现了两个解决方案:第一个解决方案是使用pagefactory,第二个解决方案是使用公共驱动程序类为您的测试套件...以便我们可以在所有类中使用相同的驱动程序实例

I found a solution myself...When i read about the testng in detail i found that testng xml file calls the default constructor of all classes specified in the xml file.so even if we pass the object to another class we cannot perform the action through the object so null pointer exception occurs....i found two solutions first one is to use a pagefactory and second one is to use a common driver class for your Test suite...so that we can use the same driver instance in all classes

通用驱动程序类

public class Driver {

    public static WebDriver driver=null;



    public static WebDriver startdriver(String browser){


        if(browser.equalsIgnoreCase("Chrome")){

        System.setProperty("webdriver.chrome.driver", "/home/vicky/Documents/Jars/chromedriver");

        driver=new ChromeDriver();

        }else if(browser.equals("Firefox")){

        driver=new FirefoxDriver();

        }
        return driver;

        }

    }

这篇关于将驱动程序对象的单个实例传递给所有其他类(Testng框架)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆