简体   繁体   English

assertTrue之后Java Selenium关闭浏览器

[英]Java Selenium close browser after assertTrue

I have a code with many classes. 我有很多类的代码。
There is a class which creates the driver - 有一个类可以创建驱动程序-

public class DriverDelegate {

    private String baseURL = "someLink";
    private WebDriver driver;
    private WebDriverWait wait;

    DriverDelegate(String url) {
        System.setProperty("webdriver.chrome.driver", "${directory}");
        driver = new ChromeDriver();
        driver.get(baseURL + url);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        wait = new WebDriverWait(driver, 5);
    }

    public WebDriver getDriver() {
        return driver;
    }

I create new driver for every test. 我为每个测试创建新的驱动程序。 And most of my lines are the ones containing assertTrue like this- 而且我的大部分行都是这样包含assertTrue的行-

public class UserInterfaceTests extends BaseTest{


    @Test
    public void headerClicker() throws java.lang.Exception {

        //Startup
        DriverDelegate driverDelegate = new DriverDelegate("url");
        WebDriver driver = driverDelegate.getDriver();

        //Some random assertTrue
        assertTrue("Test(HeaderClicker) - NoSuchElementException click", TestHelper.headerClicker(schedule, driver));

        //I hope that it is not neccessary to put up all helper classes like TestHelper or BaseTest

Now I launch my tests from a class called Startup - 现在,我从名为Startup的类中启动测试-

public class Startup{
    @Test
    public void HeaderClicker() throws Exception{ UserInterfaceTests UI = new UserInterfaceTests(); UI.headerClicker();}

My question here is how to close the browser after the assertTrue fails. 我的问题是在assertTrue失败后如何关闭浏览器。 Things like @AfterTest, @AfterSuite etc do not work because other methods can not use the same driver that was used in the test. 诸如@ AfterTest,@ AfterSuite等之类的东西不起作用,因为其他方法不能使用测试中使用的同一驱动程序。
Any ideas? 有任何想法吗?

Ok there are a few things I want to touch on here. 好的,我想在这里谈几件事。 First off, @shutdown -h now is correct in their comment that you shouldn't be programmatically creating test classes and running their @Test methods yourself. 首先,@ shutdown -h现在在他们的评论中是正确的,您不应该以编程方式创建测试类并自己运行其@Test方法。 Let the test running framework handle that (eg TestNG, JUnit, etc). 让测试运行框架处理该问题(例如TestNG,JUnit等)。

To the point of your actual question, though, is that you want pre-test and post-test methods to handle behavior that occurs before and / or after your actual test method. 不过,实际问题的关键在于,您想要预测试和后测试方法来处理在实际测试方法之前和/或之后发生的行为。 For these to work, though, you need to let the test framework handle the running of the tests. 但是,要使这些工作正常进行,您需要让测试框架处理测试的运行。 You mentioned @AfterTest and @AfterSuite as not being correct for your use case, though not for the reason you specified (entirely). 您提到@AfterTest@AfterSuite对于您的用例@AfterSuite是不正确的,尽管并非出于您完全指定的原因。 @AfterTest in TestNG only is executed once after all the test methods in all the classes inside of a <test> node specified in a suite. TestNG中的@AfterTest仅在套件中指定的<test>节点内的所有类中的所有测试方法之后执行一次。 @AfterSuite is only executed once after all the test methods in the entire suite. @AfterSuite在整个套件中的所有测试方法之后仅执行一次。 What you are looking for is the @AfterMethod annotation. 您正在寻找的是@AfterMethod批注。

Example: 例:

public class FooTest {

    private DriverDelegate driver;

    @BeforeMethod
    public void setup() {
        try {
            driver = new DriverDelegate("url");
        } catch (Exception ignore) { }
    }

    @AfterMethod
    public void tearDown() {
        try {
            driver.quit();
        } catch (Exception ignore) { }
        driver = null;
    }

    @Test
    public void foo() {
        // do test stuff
    }
}

In the above code, when TestNG runs this test class, each method annotated with @Test in this class will have a corresponding @BeforeMethod execution that initializes the driver and an @AfterMethod that closes the driver even if the test fails. 在上面的代码中,当TestNG运行此测试类时,此类中用@Test注释的每个方法将具有一个对应的@BeforeMethod执行(用于初始化驱动程序)和一个@AfterMethod用于在测试失败的情况下关闭该驱动程序)。 Couple of points to make about this type of setup with TestNG: 关于使用TestNG进行这种设置的几点要点:

(1) TestNG does not create separate instances of the test class so if you save state on the class object then you cannot run the test methods themselves in parallel within a class since you would have multiple methods trying to create new drivers and save them to the same variable, corrupting the state of the other tests that are running. (1)TestNG不会创建测试类的单独实例,因此,如果您将状态保存在类对象上,则您将无法在一个类内并行运行测试方法,因为您将有多个方法尝试创建新的驱动程序并将其保存到相同的变量,破坏正在运行的其他测试的状态。 You can run with parallel mode of per class (eg have 5 threads, each running a separate test class at the same time). 可以使用每个类的并行模式运行(例如,有5个线程,每个线程同时运行一个单独的测试类)。

(2) Why did I wrap the @BeforeMethod and @AfterMethod bodies in a try-catch block? (2)为什么将@BeforeMethod@AfterMethod主体包装在try-catch块中? Because TestNG takes a fail quickly on configuration method exceptions and can cause other tests that haven't run yet to be skipped so you need to deal with any code that could possibly fail. 由于TestNG在配置方法异常方面很快会失败,并且可能导致其他尚未运行的测试被跳过,因此您需要处理可能会失败的任何代码。 By wrapping the creating and closing of the web driver you can ignore the error and continue on running other tests. 通过包装Web驱动程序的创建和关闭,您可以忽略该错误并继续运行其他测试。 If the driver fails to be created, for instance, the driver variable will be null and that @Test method will fail but others might succeed. 例如,如果驱动程序创建失败,则driver变量将为null,该@Test方法将失败,但其他方法可能会成功。 Ideally, you should probably have some logging of the exception so that you can investigate why the driver failed to be created, for instance. 理想情况下,您可能应该对异常进行一些日志记录,以便例如调查驱动程序创建失败的原因

Also, if you need to use a driver in many test classes, you can make a base class that does the creation of the driver along with the closing of it after each method and have your test classes extend that. 另外,如果需要在许多测试类中使用驱动程序,则可以创建一个基类,该基类在每个方法之后创建驱动程序并关闭该驱动程序,并让您的测试类对此进行扩展。 If you have a class with a method annotated with @Test on it, it will run any @BeforeMethod methods on that test class AND on all of the super classes as well. 如果您的类的方法带有@Test注释,它将在该测试类以及所有超类上运行任何@BeforeMethod方法。 There is guaranteed ordering of the methods between classes (though not if you have multiple @BeforeMethod methods in the same class). 可以保证类之间方法的顺序(尽管如果同一类中有多个@BeforeMethod方法,则不能保证顺序)。

public abstract class A {
    @BeforeMethod
    public void setupA() { }
}

public class B extends A {
    @BeforeMethod
    public void setupB() { }

    @Test
    public void foo() { }
}

In the above, when foo is run, it will have run first setupA and then setupB . 在上面的代码中,运行foo时,它将先运行setupA ,然后运行setupB After methods work in the same way, but in the reverse order. After方法的工作方式相同,但顺序相反。

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

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