如何在TestNG Webdriver Java中将@BeforeMethod中的WebDriver驱动程序传递给@Test

时间:2015-04-23 17:29:35

标签: java selenium webdriver testng

我有这个班级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();

} 

  }

我在下面调用此代码的地方,通过传递的参数选择特定的浏览器。它工作得非常好,它可以选择合适的浏览器并执行。

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


    } 

    }

所以在@BeforeMethod中一切都完美无缺,我遇到的问题是测试停止,因为驱动程序没有从@BeforeMethod传递到@Test。

如何将通过运行BrowserType.class启动的驱动程序放入@Test Sign_in.class中。我想如何从browsertype正确返回驱动程序并在Sign_in @Test中调用它。

由于

4 个答案:

答案 0 :(得分:0)

你的工作方式可以大大改善。

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

只需传递驱动程序对象(来自DataProvider)。我假设您在DataProvider方法中生成驱动程序实例,因为您的测试方法已经参数化并且需要驱动程序。

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

我在上面展示的代码不会编译,但我在这里要传达的是,您需要使用@BeforeMethod方法Object[]的可选TestNG arg,它可以让您访问传递给测试方法的对象,在调用测试方法之前,例如访问"驱动程序帮助程序"在DataProvider工厂中创建,然后在运行测试之前对其进行一些功能设置。

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

答案 1 :(得分:0)

您应该让Execute函数返回驱动程序:

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

在你的测试中:

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

答案 2 :(得分:0)

像这样解决:

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类:

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

    } 

      }

答案 3 :(得分:0)

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