如何在 TestNG Webdriver 中将调用的 WebDriver 驱动程序从 @BeforeMethod 传递到 @Test Java
How to pass called WebDriver driver from @BeforeMethod to @Test in TestNG Webdriver 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) {
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。我想我怎样才能 return 从 browsertype 正确地调用驱动程序并在 Sign_in @Test.
中调用它
谢谢
你做事的方式可以大大提高。
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);
}
我在上面显示的代码无法编译,但我想在这里传达的是,您需要将可选的 TestNG arg 用于 @BeforeMethod
方法,即 Object[]
,它给出在调用测试方法之前访问传递给测试方法的对象,例如访问在 DataProvider 工厂中创建的 "driver helper",然后在测试 运行 之前对其进行一些功能设置.
@DataProvider(name = "test1")
public Object[][] createData1() {
return new Object[][] {
{ "Cedric", new WebDriverHelper(), "firefox" },
{ "Anne", new WebDriverHelper(), "chrome"}
};
}
您应该让 Execute
函数 return 成为驱动程序:
public static WebDriver Execute(String browser) {
...
return driver;
}
在你的测试中:
public void SetUp(String browser) {
driver = BrowserType.Execute(browser);
}
这样解决的:
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();
}
}
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;
}
}
我有这个 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) {
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。我想我怎样才能 return 从 browsertype 正确地调用驱动程序并在 Sign_in @Test.
中调用它谢谢
你做事的方式可以大大提高。
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);
}
我在上面显示的代码无法编译,但我想在这里传达的是,您需要将可选的 TestNG arg 用于 @BeforeMethod
方法,即 Object[]
,它给出在调用测试方法之前访问传递给测试方法的对象,例如访问在 DataProvider 工厂中创建的 "driver helper",然后在测试 运行 之前对其进行一些功能设置.
@DataProvider(name = "test1")
public Object[][] createData1() {
return new Object[][] {
{ "Cedric", new WebDriverHelper(), "firefox" },
{ "Anne", new WebDriverHelper(), "chrome"}
};
}
您应该让 Execute
函数 return 成为驱动程序:
public static WebDriver Execute(String browser) {
...
return driver;
}
在你的测试中:
public void SetUp(String browser) {
driver = BrowserType.Execute(browser);
}
这样解决的:
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();
}
}
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;
}
}