Elsid Elsid - 4 months ago 71
Java Question

How to pass called WebDriver driver from @BeforeMethod to @Test in TestNG Webdriver Java

I have this class SignIn:

package automationFramework;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import pageObject.devSplashScreenPage;
import utility.BrowserType;
import utility.Constant;
import appModule.SignIn_Action;

public class SignIn {

public WebDriver driver;



@BeforeMethod
@Parameters("browser")
public void SetUp(String browser) {

BrowserType.Execute(driver, browser);

}

@Test
public void signIn() {

// Call Sign In function
SignIn_Action.Execute(driver, Constant.StudentUsername, Constant.StudentPassword);
}

@AfterMethod
public void Teardown() {
driver.quit();

}

}


Where I am calling this code below which chooses the specific browser by the parameter that is passed. It works perfectly fine, it picks up the right browser and executes.

package utility;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class BrowserType {



@Test
public static void Execute(WebDriver driver, String browser) {

// Set Browsers
if(browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
}

else if (browser.equalsIgnoreCase("chrome")) {

{System.setProperty("webdriver.chrome.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/chromedriver.exe");}
driver = new ChromeDriver();
}

else if (browser.equalsIgnoreCase("ie")) {

{System.setProperty("webdriver.ie.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/IEDriverServer.exe");}
driver = new InternetExplorerDriver();
{DesiredCapabilities iecapabilities = DesiredCapabilities.internetExplorer();
iecapabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);}
}

// Implicit Wait and Maximize browser
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();

// Navigate to URL
driver.get(Constant.URL);


}

}


So everything executes perfectly fine in @BeforeMethod, the issue I have is the test stops because the driver doesn't pass from @BeforeMethod to @Test.

How can I get the driver that is initiated by running BrowserType.class into the @Test Sign_in.class. I guess how can i return the driver properly from browsertype and call it in Sign_in @Test.

Thanks

Answer

Solved like this:

BrowserType.java:

package utility;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class BrowserType {

    @Test
    public static WebDriver Execute(String browser) {

         // Set Browsers
         WebDriver driver = null;
         if(browser.equalsIgnoreCase("firefox")) {
         driver = new FirefoxDriver();
         }

         else if (browser.equalsIgnoreCase("chrome")) { 

         {System.setProperty("webdriver.chrome.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/chromedriver.exe");}
          driver = new ChromeDriver();        
          }

         else if (browser.equalsIgnoreCase("ie")) { 

              {System.setProperty("webdriver.ie.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/IEDriverServer.exe");}
              driver = new InternetExplorerDriver(); 
              {DesiredCapabilities iecapabilities = DesiredCapabilities.internetExplorer();
              iecapabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);}
              }

          // Implicit Wait and Maximize browser
          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
          driver.manage().window().maximize();

          // Navigate to URL
          driver.get(Constant.URL);

          return driver;

    } 

SignIn.java class:

package automationFramework;

    import java.util.concurrent.TimeUnit;

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.Parameters;
    import org.testng.annotations.Test;

    import pageObject.devSplashScreenPage;
    import utility.BrowserType;
    import utility.Constant;
    import appModule.SignIn_Action;

    public class SignIn {

    public WebDriver driver;



    @BeforeMethod
    @Parameters("browser")
    public void SetUp(String browser) {

        driver = BrowserType.Execute(browser);

    }

    @Test
    public  void signIn() {

        // Call Sign In function
        SignIn_Action.Execute(driver, Constant.StudentUsername, Constant.StudentPassword);    
     }  

    @AfterMethod
    public void Teardown() {
          driver.quit();

    } 

      }
Comments