0
votes

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

4

4 Answers

0
votes

You should make your Execute function return the driver:

public static WebDriver Execute(String browser) {
    ...
    return driver;
}

In your test:

public void SetUp(String browser) {
    driver = BrowserType.Execute(browser);
}
0
votes

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

    } 

      }
0
votes

The way your doing things can be greatly improved.

public class BrowserTest extends TestBase{
    @Test(dataProvider="test1")
    public static void execute(WebDriverHelper helper, String browser) {
         // Set Browsers
         driver.get(url);

Just pass the driver object (coming from the DataProvider). I assume your generating the driver instance within the DataProvider method since your test method is already parameterized and takes the driver.

public class TestBase {
    private WebDriver driver;
    ...
    @BeforeMethod
    @Parameters("browser")
    public void setUp(Object[] params) {
        driver = (WebDriverHelper)params.get(1);
        browserName = (String)params.get(2);
        this.setTestName( params.get(0) + "-" + browserName;
        driver.navigateTo(startUrl);    
    }

This code I show above wont compile but what I am trying to convey here is that you need to use the optional TestNG arg to the @BeforeMethod method, which is Object[] , and it gives you access to objects passed to test methods, BEFORE the test method is called, such as getting access to a "driver helper" created in the DataProvider factory, and then doing some Capabilities setup on that before the test is ran.

@DataProvider(name = "test1")
public Object[][] createData1() {
 return new Object[][] { 
   { "Cedric", new WebDriverHelper(), "firefox" },
   { "Anne", new WebDriverHelper(), "chrome"}
 }; 
} 
0
votes
public class TestSuiteDriver {

    private static WebDriver driver;

    @BeforeClass
    public static void setUp(){
        System.setProperty("webdriver.chrome.driver", "/Users/Kimberleyross/chromedriver");
        driver = new ChromeDriver();
    }

    public static WebDriver getDriver() {
        return TestSuiteDriver.driver;
    }
}