简体   繁体   English

在不同的类中访问@BeforeTest和@AfterClass(TestNG)中的变量?

[英]Access variable in @BeforeTest and @AfterClass (TestNG) across separate classes?

I am writing some selenium automated UI tests for my company using Java and the TestNG framework. 我正在使用Java和TestNG框架为我的公司编写一些selenium自动UI测试。 I am defining the driver in a Base class, and I want to actually initialize the driver in an @BeforeTest and quit it in a @AfterTest method. 我在Base类中定义驱动程序,我想在@BeforeTest初始化驱动程序并在@AfterTest方法中退出它。 What is the Java way to do that, assuming they are in different classes? Java的方法是什么,假设它们在不同的类中? I know how to make it work in the same class, but not over separate classes. 我知道如何让它在同一个类中工作,但不能在单独的类中工作。 Here is my Base.java file: 这是我的Base.java文件:

public class Base {

        public static WebDriver driver = null;
        public WebDriver getDriver() {
            driver = new ChromeDriver();
            return driver;
        }
}

Now, I want to have a separate Setup class and a separate Teardown class. 现在,我想要一个单独的Setup类和一个单独的Teardown类。 If I was going to define all of this in the same @Test , I would do it this way: 如果我要在同一个@Test定义所有这些,我会这样做:

@Test
public void testOne() {

    Base b = new Base();
    WebDriver driver = b.getDriver();

    // Do test-y things here. 

    driver.quit();
}

How would I set this up? 我该如何设置? Trying to learn the right way to do this, and not hack something together. 试图学习正确的方法,而不是一起破解。 I can also provide more information if needed. 如果需要,我还可以提供更多信息。 Thanks! 谢谢!

Use inheritance. 使用继承。

public class TestBase {

    protected WebDriver driver;

    @BeforeClass
    public void setUp(){
        System.out.println("I am in setUp method.");

        //WebDriver instantiation etc.
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized", "--disable-cache");
        driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @AfterClass
    public void tearDown(){
        System.out.println("I am in tearDown method.");

        //You can clean up after tests.
        driver.close();
    }
}

And then inheritance can be used. 然后可以使用继承。 Pay attention to the extends keyword: 注意extends关键字:

public class ParticularTest extends TestBase {

   @Test
   public void testMethod() {
       System.out.println("I am in testMethod.");

       //Your driver from TestBase is accessible here.
       //Your assertions come here.
   }
}

Later on you can just execute ParticularTest.java . 稍后您可以执行ParticularTest.java Output: 输出:

I am in setUp method.
I am in testMethod.
I am in tearDown method.

I would suggest that you please DONOT resort to this mechanism of instantiating webdrivers in your tests. 我建议你请不要诉诸于在你的测试中实例化webdrivers的机制。 @BeforeTest by definition is expected to be invoked ONLY ONCE per <test> tag. 根据定义, @BeforeTest预计仅在每个<test>标签上调用一次 So if your TestNG suite xml file has something like this 所以,如果你的TestNG套件xml文件有这样的东西

<test name="MyTests">
    <classes>
        <class name="com.foo.Test1"/>
        <class name="com.foo.Test2"/>
    </classes>
</test>

and if both com.foo.Test1 and com.foo.Test2 extend your TestBase class, then the @Test methods within your com.foo.Test2 class are bound to hit a NullPointerException when accessing your driver object because your @BeforeTest annotated setup() method within TestBase will be executed only once (for Test1 ) and it will NOT be executed for Test since both the classes extend from the same base class. 如果com.foo.Test1com.foo.Test2扩展了你的TestBase类,那么com.foo.Test2类中的@Test方法在访问你的driver对象时会遇到NullPointerException ,因为你的@BeforeTest注释setup()内方法TestBase将只执行一次(对于Test1 ),它不会对被执行Test ,因为两个类从相同的基类延伸。

I would suggest that you please resort to something like this (this is my blog) to do web driver instantiations. 我建议你请求助于像这样 (这是我的博客)做网络驱动程序实例。

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

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