简体   繁体   English

如何在使用 selenium 运行 testng 套件中的所有测试后关闭浏览器

[英]How to close browser after runnning all tests in testng suite with selenium

I have a TESTNG file like below: Where tests in credentials and checkHome are executed first before runnning DatacheckTest .我有一个TESTNG文件,如下所示:在运行 DatacheckTest 之前首先执行credentialscheckHome中的DatacheckTest

DatacheckTest.xml DatacheckTest.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
    <suite name="Suite">
        <suite-files>
            <suite-file
                path="./credentials.xml"></suite-file>
            <suite-file path="./checkHome.xml"></suite-file>
        </suite-files>
    
        <test thread-count="5" name="Test">
            <classes>
                <class name="DatacheckTest" />
            </classes>
        </test> <!-- Test -->
    </suite> <!-- Suite -->

All the Testng class file below extend Base which gets driver information.下面的所有Testng class 文件都扩展了获取驱动程序信息的Base I want to close the browser session after running all the tests in the suite.我想在运行套件中的所有测试后关闭浏览器 session。 for example when I run DatacheckTest , the Base class afterSuite methhod annotated with AfterSuite , closes browser and ends the session soon after running credentials.xml .例如,当我运行DatacheckTest时,使用 AfterSuite 注释的Base AfterSuite afterSuite方法关闭浏览器并在运行credentials.xml后立即结束 session。

  1. How can I make the browser close after running all tests in the suite.在套件中运行所有测试后,如何关闭浏览器。
  2. When we are running multiple suites, close browser after everything is finished.当我们运行多个套件时,一切都完成后关闭浏览器。
public class Credentials extends Base {
    @Test
    void test1() {
    }
}

public class CheckHome extends Base {
    @Test
    void test2() {
    }
}

public class DatacheckTest extends Base {
    @Test
    void test3() {
    }
}

public class Base extends {
    @AfterSuite
    public void afterSuite() {
        driver.close();
    }
}

@AfterSuite will not work for you, since you refer to multiple suite files and this will be invoked multiple times. @AfterSuite对你不起作用,因为你引用了多个套件文件,这将被多次调用。

IExecutionListener in TestNG TestNG 中的 IExecutionListener

The IExecutionListener listener monitors the start and the end of the whole TestNG execution process. IExecutionListener监听器监听整个TestNG执行过程的开始和结束。

It's the most generic listener, which allows executing some code before everything and after everything .它是最通用的侦听器,允许在一切之前之后执行一些代码。

The IExecutionListener contains two methods: IExecutionListener包含两个方法:

  • onExecutionStart
  • onExecutionFinish

You have to implement a custom ExecutionListener.您必须实现自定义 ExecutionListener。

import org.testng.IExecutionListener

class ExecutionListener implements IExecutionListener {

    @Override
    public void onExecutionStart() {
        // launch driver
        System.out.println("onExecutionStart invoked");
    }

    @Override
    public void onExecutionFinish() {
        // close driver
        System.out.println("onExecutionFinish invoked");
    }
}

and apply it on Base class:并将其应用于 Base class:

import org.testng.annotations.Listeners

@Listeners(ExecutionListener.class)
class Base {
    // some implementation
}

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

相关问题 在TestNG(带有Java的Selenium)中运行测试套件后,如何收集所有System.out.print(“”)结果 - How to Collect all System.out.print(“”) results after running a test suite in TestNG (Selenium with Java) 为什么浏览器实例在 Selenium 和 TestNG 中执行后没有关闭/退出? - Why browser instances not getting close/quit after execution in Selenium and TestNG? 有没有办法找到在 TestNG 套件中禁用的所有测试? - Is there anyway to find all tests that are disabled in a TestNG suite? 在使用TestNG进行硒测试期间,如何关闭打开的驱动程序 - How can I close opened drivers during Selenium tests with TestNG 依次执行 Selenium 测试,无需通过 testng 重新启动浏览器 - Executing Selenium tests one after the other without re launching the browser through testng 在同一个浏览器中运行所有测试 - Running all tests in the same browser webdriver testng 我要在硒TestNG Java中执行Suite后发送电子邮件吗? - I want to send an email after Suite is executed in selenium TestNG java? 如何关闭尝试关闭浏览器后出现的Selenium弹出窗口 - How to Close a popup in Selenium that appear after trying to close the browser TestNG 未在测试套件中运行测试 - TestNG not running tests in testing suite Selenium和TestNG都在一个浏览器中进行测试 - Selenium and TestNG all test in one browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM