繁体   English   中英

如何在Selenium WebDriver中为多个类创建同一实例

[英]How to create a same instance for multiple classes in selenium webDriver

我有一个基本的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);
    }

请使用如下的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");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM