将驱动程序对象的单个实例传递给所有其他类(Testng框架)

问题描述 投票:3回答:2

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

样本班

    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实例(所有类通用)或使用所有类中的现有实例

selenium-webdriver testng
2个回答
4
投票

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

常见的司机类

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;

        }

    }

0
投票

使用与Java相同的extends关键字非常容易。只需要创建一个通用的webdriver类,您将在TestNG注释的帮助下打开所需的浏览器和应用程序URL,如下面的代码所示。

WebDriver Common Class:

public class Seleniumlinktext {

    public WebDriver driver;
    String baseurl = "http://www.google.co.in";     

    @BeforeTest
    public void openBrowser(){

        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);

    }

另一类:

public class WebDriverTest extends Seleniumlinktext {   

    @Test(priority=1)
    public void linkText(){
        //images hyperlink      
        driver.findElement(By.linkText("Images")).click();      
        System.out.println("Click on Images hyperlink");

    }

像这样,您可以将webdriver实例传递给所有其他类。我从qazxsw poi找到了解决方案。

这里我使用了@BeforeTest注释,因为在使用@Test注释开始我的测试用例执行之前,我的应用程序URL和浏览器应该只打开一次。

© www.soinside.com 2019 - 2024. All rights reserved.