Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

I have defined an object of Web driver in one class as

Static WebDriver driver;

and want to use that instance into another class to automate browser. Is it possible? Or I will have to define drivers every time in each class.

I tried by defining a webdriver in another class as:

Static webDriver AnotherDriver;

and then assigning it the value of other driver as:

AnotherDriver = ClassName.driver; But I am getting nullPointer Exception when I using AnotherDriver.

And If I will have define new driver in each class to do so then I will get stuck as i have defined few methods which I will be using in other class file. and all of them uses the driver defined in that class.

Any kind of suggestion is highly appreciated.

share|improve this question
    
You haven't described how do you initialize original driver variable. Uninitialized variables may cause NullPointerException. –  dzieciou Jul 7 at 12:44
    
okay. Actually what I am doing is, I have defined a startup function in one class and want to use that in other classes containing test cases. So my test cases are not running. Do I need to initialize webdriver with some value? –  razizcool4all Jul 7 at 13:06
    
Can you provide a minimal working code sample demonstrating what you did? That would ease. –  dzieciou Jul 7 at 13:19
    
@razizcool4all If you're talking about Java/JUnit, this is where you'll want to use inheritance; if your test class inherits from a base class, and that base class defines a setup method, that setup method will be run before the test as well. –  Yamikuronue Jul 7 at 19:54
    
What is business case to have two separate webdriver instances? Seems a bit strange for me. Separate instances of same webdriver (using same browser) will likely share cookies and authorization, and will not be independent. My test are based on PageObject design pattern, and each pageobject shares webdriver. It works fine so far, just curious why you need separate webdrivers. –  Peter Masiar Jul 9 at 15:52

1 Answer 1

up vote 1 down vote accepted

Approach 1

  • Create a class, for example Hull.
  • In the cass is a protected variable WebDriver driver.
  • In the class is a method setup() with a @Before annotation.
  • In the method you has a condition: Is WebDriver null, initialize; else do nothing.
  • Classes with test cases extends from Hull.
  • Classes with test cases can use the variable driver.

Approach 2

  • Create a class, for example Hull.
  • In the cass is a private variable WebDriver driver.
  • In the class is a method getDriver().
  • In the method you has a condition: Is WebDriver null, initialize; else do nothing.
  • Classes with test cases extends from Hull.
  • Classes with test cases can use the getDriver() method to use the WebDriver. Tip: Use always this method if you interact with the WebDriver, because so you are save that the WebDriver will be initialized.
share|improve this answer
    
is it possible that I open browser with a driver and access web contents with the another driver by defining it as: SecondDriver = classname.firstDriver; –  razizcool4all Jul 7 at 13:00
2  
@razizcool4all I do not know. Try it! Print the value of firstDriver and secondDriver. If they have the same value, it should be possible. But why you want to do something like that? –  Twaldigas Jul 7 at 13:14
    
I want to define the link to test in one function and open that link first whenever I have to run test cases. So here i came up with a problem that if I initiate browser with one driver we can't access it with other driver. So to resolve this I have to use another instance of the same driver. –  razizcool4all Jul 8 at 7:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.