如何逐一运行 TestNG 测试

问题描述 投票:0回答:4

我有一个测试套件,我想逐一运行 XML 文件中的测试。但是当我运行它时,它会同时运行所有 TC(我同时打开了 3 个浏览器)。

我的 XML:

    <suite name="TestingSuite" preserve-order="true" parallel="false" verbose="10">
      <test name="Test1">
         <classes>
            <class name="guiAndFunctianal.LoginFail" />
            <class name="guiAndFunctianal.LoginAsManager" />
            <class name="guiAndFunctianal.CreateUserTest" />        
          </classes>
      </test>
    </suite>  

我的 TC 看起来像这样:

    public class LoginFail extends AbstractTest{ 
    @ BeforeTest 
    public void openBrowser() {
     openBrowserFireFoxAllTcs();
     } 
    @ Test
     public void main (){   
    }
    @ AfterTest 
    public void quit() { 
    driver.quit(); } 

抽象测试

    public class AbstractTest {
     WebDriver driver = new FirefoxDriver();
     WebDriverWait wait = new WebDriverWait(driver, 10);
     public void openBrowserFireFoxAllTcs() { 
    driver.get("some URL"); 
    } 
selenium selenium-webdriver testng
4个回答
0
投票

我假设你想要顺序执行。打开浏览器>完成测试>关闭浏览器,然后对其他浏览器重复相同的操作。如果是这种情况,我会执行以下操作:

TestNG.XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" junit="false" parallel="false" configfailurepolicy="skip" thread-count="5" skipfailedinvocationcounts="false" data-provider-thread-count="10" group-by-instances="false" preserve-order="true" allow-return-values="false">
   <test name="firefox" junit="false" skipfailedinvocationcounts="false" preserve-order="true" group-by-instances="false" allow-return-values="false">
      <parameter name="browser" value="firefox" />
      <classes>
         <class name="com.github.tests.GitHubHomePageTests" />
      </classes>
   </test>
   <test name="ie" junit="false" skipfailedinvocationcounts="false" preserve-order="true" group-by-instances="false" allow-return-values="false">
      <parameter name="browser" value="ie" />
      <classes>
         <class name="com.github.tests.GitHubHomePageTests" />
      </classes>
   </test>
   <test name="chrome" junit="false" skipfailedinvocationcounts="false" preserve-order="true" group-by-instances="false" allow-return-values="false">
      <parameter name="browser" value="chrome" />
      <classes>
         <class name="com.github.tests.GitHubHomePageTests" />
      </classes>
   </test>
</suite>

我如何实例化驱动程序:

package com.github.tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.*;
public class BaseTest {
    public WebDriver driver;
    String url = "https://github.com/";

    @Parameters("browser")
    @BeforeClass
    public void SetUpTests(String browser) {
        if (browser.equalsIgnoreCase("firefox")) {
            driver = new FirefoxDriver();
        } else if (browser.equalsIgnoreCase("ie")) {
            System.setProperty("webdriver.ie.driver", ".\\drivers\\IEDriverServer.exe");
            driver = new InternetExplorerDriver();
        } else if (browser.equalsIgnoreCase("chrome")) {
            System.setProperty("webdriver.chrome.driver", ".\\drivers\\chromedriver.exe");
            driver = new ChromeDriver();
        }
        //Navigate to url
        driver.navigate().to(url);
        //Maximize the browser window
        driver.manage().window().maximize();
    }
    @AfterClass
    public void CleanUpDriver() throws Exception {
        // Quit current driver instance.
        try {
            driver.quit();
        } catch (Exception ex) {
            throw ex;
        }
    }

如何试运行

package com.github.tests;
import com.github.pageobjects.GitHubLandingPage;
import org.testng.Assert;
import org.testng.annotations.Test;
public class GitHubHomePageTests extends BaseTest {@Test
    public void ExploreLinkTest() {
        String explorePageTitle = new GitHubLandingPage(driver)
            .clickGitHubExplorePage()
            .getGitHubExplorerPageTitle().trim();
        System.out.println(explorePageTitle);
        Assert.assertEquals(explorePageTitle, "Explore GitHub");
    }
}

可在此处

获取全球 Github 存储库

0
投票

试试这个:

<suite name="TestingSuite" preserve-order="true" thread-count="1" verbose="10">
    <test name="Test1">
        <classes>
            <class name="guiAndFunctianal.LoginFail" />
        </classes>
    </test>
    <test name="Test2">
        <classes>
            <class name="guiAndFunctianal.LoginAsManager" />
        </classes>
    </test>
    <test name="Test3">
        <classes>
            <class name="guiAndFunctianal.CreateUserTest" />
        </classes>
    </test>
</suite>

0
投票

退出前关闭你的驱动程序:

@测试后 公共无效退出(){ 驱动程序.close() 驱动程序.quit(); }


0
投票

从 testng.xml 执行顺序测试

<suite name="Validation Suite" preserve-order="true" thread-count="1" verbose="10" parallel="false" configfailurepolicy="skip"
allow-return-values="false">
 <listeners>
    <listener class-name="ios.AllureListener"/>
 </listeners>
  <test name="Pricing Page Validations" skipfailedinvocationcounts="false" preserve-order="true" group-by-instances="false" allow-return-values="false">
    <classes>
      <class name="ios.PricingTierPageValidations"/>
    </classes>
  </test> <!-- Test -->
  <test name="Application Page Validations" skipfailedinvocationcounts="false" preserve-order="true" group-by-instances="false" allow-return-values="false">
    <classes>
      <class name="ios.ApplicationPageValidations"/>
    </classes>
  </test> 
</suite>
© www.soinside.com 2019 - 2024. All rights reserved.