java 如何在TestNG Webdriver Java中将调用的WebDriver驱动程序从@BeforeMethod传递到@Test

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29830482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:59:37  来源:igfitidea点击:

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

javaseleniumwebdrivertestng

提问by Elsid

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.

所以一切在@BeforeMethod 中执行得非常好,我遇到的问题是测试停止,因为驱动程序没有从@BeforeMethod 传递到@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.

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

Thanks

谢谢

采纳答案by Elsid

Solved like this:

是这样解决的:

BrowserType.java:

浏览器类型.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:

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

    } 

      }

回答by djangofan

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.

只需传递驱动程序对象(来自 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);    
    }

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 @BeforeMethodmethod, 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.

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

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

回答by segalaj

You should make your Executefunction return the driver:

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

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

In your test:

在您的测试中:

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

回答by Kimberley Ross

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