如何逐个运行TestNG测试

时间:2015-06-23 17:48:26

标签: selenium selenium-webdriver testng

我有一个测试套件,我想逐个运行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(); } 

AbstractTest

    public class AbstractTest {
     WebDriver driver = new FirefoxDriver();
     WebDriverWait wait = new WebDriverWait(driver, 10);
     public void openBrowserFireFoxAllTcs() { 
    driver.get("some URL"); 
    } 

3 个答案:

答案 0 :(得分:0)

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

<强>的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回购可用here

答案 1 :(得分: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>

答案 2 :(得分:0)

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

@ AfterTest     public void quit(){     driver.close()     driver.quit(); }