我有一个Base WebDriver类,如果我在类中创建一个构造函数,我已经创建了一个接口,我在其中创建了所有实现
public class Base {
public InterfaceClass driver;
public void setDriver(InterfaceClass driver){
this.driver = driver;
}
}
public class Seleniumclass implements InterfaceClass {
private WebDriver driver;
public void initiTest(){
Browser initialisation
}
}
public interface InterfaceClass {}
两个类:第一个打开的实例必须通过第二个类传递 - 如何传递它?
public class firstclass extends HomePageComponents {
@BeforeClass
public void setup() throws IOException {
driver = initiTest(this.getClass().getSimpleName());
}
public class SecondClass extends HomePageComponents {
public SecondClass(ActionEngine driver) {
// TODO Auto-generated constructor stub
System.out.println(BaseClass.driver);
driver = BaseClass.driver;
}
@BeforeClass
public void setup() throws IOException {
SecondClass ASA= new SecondClass(BaseClass.driver);
}
答案 0 :(得分:0)
请使用如下的Singleton类:
public class TestBotApp
{
private static volatile TestBotApp ourInstance;
private static final Object mutex = new Object();
private WebDriver webDriver;
//public final HelperClass helperClass;
private TestBotApp() {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"/Chrome/chromedriver");
ChromeOptions opt = new ChromeOptions();
opt.addArguments("disable-extensions");
opt.addArguments("--start-maximized");
webDriver = new ChromeDriver(opt);
//webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
webDriver.navigate().to("http://localhost:4200/web");
webDriver.manage().window().maximize();
//helperClass = new HelperClass();
}
public WebDriver getWebDriver() {
return webDriver;
}
public void closeWebDriver() {
webDriver.close();
webDriver.quit();
}
public static TestBotApp getInstance() {
TestBotApp result = ourInstance;
if (result == null) {
synchronized (mutex) {
result = ourInstance;
if (result == null)
ourInstance = result = new TestBotApp();
}
}
return result;
}
}
对于创建对象:
public class TestConfig {
@BeforeSuite
public void testBeforeSuite() {
WebDriver webDriver = TestBotApp.getInstance().getWebDriver();
System.out.println("testBeforeSuite()");
}
@AfterSuite
public void testAfterSuite() {
System.out.println("testAfterSuite()");
//TestBotApp.getInstance().closeWebDriver();
}
@BeforeTest
public void testBeforeTest() {
System.out.println("testBeforeTest()");
}
@AfterTest
public void testAfterTest() {
System.out.println("testAfterTest()");
}
@BeforeMethod
public void beforeTestMethod() {
System.out.println("Test");
}
}