Passing the single instance of the driver object t

2019-05-17 01:51发布

问题:

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

sample class

    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 file

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

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

回答1:

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

Common driver class

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;

        }

    }


回答2:

It is very easy to use with extends keyword same as Java. Just you need to create common webdriver class where you will open required browser and application url with the help of TestNG annotations as below code.

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

    }

Another Class:

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");

    }

Like this way you can pass webdriver instance to all other clases. I found the solution from this site.

Here I have used @BeforeTest annotation because my application url and browser should be open only once before starting my Test cases execution with @Test annotation.