简体   繁体   English

Selenium和TestNG都在一个浏览器中进行测试

[英]Selenium and TestNG all test in one browser

I have abstract class, where initialize webdriver. 我有抽象类,初始化webdriver。 All classes with the implementation of the tests are inherited from him. 所有执行测试的类都是从他继承的。 I want to reduce the time of test, open a browser for all tests. 我想减少测试时间,为所有测试打开浏览器。

class AbstractClassCase {
    public static WebDriver driver;

    @BeforeClass    
    @Parameters({"webDriver", "applicationHost", "applicationPort", "driverHost", "driverPort",  "username", "password"})
    public void setUp(
        String webDriverIdentifier,
        String applicationHost,
        @Optional String applicationPort,
        @Optional String driverHost,
        @Optional String driverPort,
        @Optional String username,
        @Optional String password){      

        driver = new FirefoxDriver();
        driver.get("localhost");
        login(username, password)

    }

    @AfterСlass
    public void tearDown() {   
        driver.quite();
    }     

}

public class TestButton extends AbstractClassCase {
    @Test
    public void testClickButtonNo() {
        WebElement button = driver.findElement(By.id("button-no"));
        button.click();
        WebElement status = driver.findElement(By.id("button-status"));
        Assert.assertEqual("Cancel", status.getText());
   }
}

and other test class in the same spirit. 和其他测试课一样的精神。

How can I reconfigure this class, so that the browser opened once? 如何重新配置​​此类,以便浏览器打开一次?

If you want to Open Browser at the start once and run all the methods and then close it. 如果要在开始时打开浏览器并运行所有方法,然后关闭它。

There are 2 steps i would recommend to follow. 我建议遵循两个步骤。

Step 1 : Include browser invocation code in methods with @BeforeSuite && @AfterSuite for closing the browser/driversession. 步骤1 :在@BeforeSuite && @AfterSuite的方法中包含浏览器调用代码,以关闭浏览器/ driversession。 This makes sure these tests are run once before and after test suite. 这确保了这些测试在测试套件之前和之后运行一次。

protected static WebDriver Browser_Session;

@BeforeSuite
public void beforeSuite() {
    Browser_Session=new FirefoxDriver();
    Browser_Session.manage().timeouts().implicitlyWait(30000, TimeUnit.MILLISECONDS);
}

@AfterSuite
public void afterSuite() {
    Browser_Session.quit();
}

Step2 : Open testng.xml and include all such classes (test methods) under a single Suite, Thus making sure the browser (via Selenium) is invoked first and then rest all methods are run in the same browser. 步骤2 :打开testng.xml并在单个Suite下包含所有这些类(测试方法),从而确保首先调用浏览器(通过Selenium),然后在同一浏览器中运行所有方法。 在此输入图像描述

Here " Class Init " contains Browser Initiating code and ClassA & ClassB are subclassess of Init. 这里“ 类Init ”包含浏览器启动代码,ClassA和ClassB包含Init的子类。

Hope this helps 希望这可以帮助

If you want to run all test in one instance, i wrote my own WebDriverFactory so here is some examples for help: 如果你想在一个实例中运行所有测试,我编写了自己的WebDriverFactory,所以这里有一些帮助示例:

    public static WebDriver getDriver(){
       if (driver == null) {
            return new FireFoxDriver();
        } else {
          return driver;
        }
   }

Now remove AfterClass and add this to your class it will shutdown your browser in the end 现在删除AfterClass并将其添加到您的类中它最终将关闭您的浏览器

static {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                try {
                    dismissDriver();
                } catch (Exception e) {
                }
            }
        });
}

 public static void dismissDriver() {
        if (driver != null) {
            try {
                driver.quit();
                driver = null;
            } catch (Throwable t) {
            }
        }
    }

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

相关问题 如何在testng for selenium中使浏览器的打开(在一个浏览器中全部测试)静态 - How to make the opening of browser(all test in one browser) static in testng for selenium testng在一个线程中运行所有测试类 - testng run all test classes in one thread 在TestNG(Selenium,Appium)的一项测试中类之间的转换中的NPE - NPE in the transition between classes in one test in TestNG (Selenium, Appium) 如何使用Selenium和TestNG对多个站点运行一个测试 - How to run one test against multiple sites using Selenium and TestNG 使用testng的跨浏览器和测试套件 - Cross browser and test suite with testng 在Chrome浏览器Testng上运行测试 - Running test on Chrome Browser Testng Selenium RC:所有测试都只有一个浏览器? - Selenium RC : Only one browser for all the tests? 使用testng在一个浏览器中跨多个类运行Java Selenium Webdriver测试 - Running java selenium webdriver tests across many classes in one browser using testng 依次执行 Selenium 测试,无需通过 testng 重新启动浏览器 - Executing Selenium tests one after the other without re launching the browser through testng Maven - 带有 TestNG 的黄瓜。 通过标签启动黄瓜测试后 - 所有 testng 测试@Test 与黄瓜一同时启动 - Maven - Cucumber with TestNG. After starting cucumber test by tag - all testng tests @Test are started at same time with cucumber one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM