0

I am using Pageobjects to run my scripts. I declared two packages - one for Pageobject definition and the other for TestNGclasses. Supposed If I have 3 classes in Package-1 then I would correspondingly have 3 test classes in package-2.

As of now I have 2 classes ( Login and home) in Package-1 and test classes for the same in Package-2

I declared the driver in LoginTest.java in package-2 as follows;

public static WebDriver driver = new FirefoxDriver();    

@BeforeMethod
public void Setup(){

    driver.get(StringUrl);
    driver.manage().window().maximize();
}

Now When I call HomeTest.java it shows Null Pointer Exception; How can I declare the webdriver once and use it's instance in multiple test classes?

public class searchPOTest {
private WebDriver driver;

public void HomePOTest(WebDriver driver){

    this.driver = driver;
}

@Test(priority=1)
public void testvalidsearchnInputs() throws Exception {
  //homePagePO.doaSearch("Google");
  homePagePO homeTosearch  = PageFactory.initElements(driver, homePagePO.class);
  searchResultPO toSearch = homeTosearch.search("Google");
}
djangofan
  • 28,471
  • 61
  • 196
  • 289
user3573671
  • 3
  • 1
  • 5

1 Answers1

0

You can do it by extending a class. For example, in the selenium framework found here..

There is a setup like this:

class AutomationTest {
  public AutomationTest() {
    driver = new Driver();
  }
}


class MyTest extends AutomationTest {
  @Test
  public void doThis() {
    driver.findElement... // driver will not be null now.
  }
}

You could of course put the @Before method instead of the constructor.

ddavison
  • 28,221
  • 15
  • 85
  • 110
  • I have a question here, what if I have multiple test classes? How do I extend it? If I do class MyTest extends AutomationTest, every time a new browser window will be opened. please help – user3573671 May 07 '14 at 16:31
  • that's exactly what you want.. to make sure that your tests are independent and quick, you need them to open their own browser each time. – ddavison May 07 '14 at 17:09
  • ACtually, I have a flow that is continuous flow, from Login-> Search-> Search Result -> logout etc. All needs to be done in only one browser session. – user3573671 May 07 '14 at 17:16
  • Then do that. `driver.findElement(By.cssSelector("#username")).sendKeys("username")...driver.findElement(By.cssSelector("searchTerm")).sendKeys("search term")...driver.findElement(By.cssSelector("#logoutLink")).click()` – ddavison May 07 '14 at 17:28