<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Dataprovider & TestNG XML:Selenium 中的參數化(示例) > 原文: [https://www.guru99.com/parameterization-using-xml-and-dataproviders-selenium.html](https://www.guru99.com/parameterization-using-xml-and-dataproviders-selenium.html) 在創建軟件時,我們始終希望它對不同的數據集可以有所不同。 對于同一軟件的[測試](/software-testing.html),我們僅用一組數據進行測試就不公平。 同樣,在這里,我們需要驗證我們的系統正在采用它希望支持的所有組合。 圖中是參數化。 為了在運行時將多個數據傳遞給應用程序,我們需要參數化測試腳本。 我們通過參數化實現的概念稱為**數據驅動測試。** 在本教程中,您將學習- * [TestNG-中的參數化類型-](#1) * [帶 Testng.xml 的參數注釋](#2) * [故障排除](#3) * [使用 Dataprovider](#4) 的參數 * [從不同的類](#5)調用 DataProvider * [Dataprovider](#6) 中參數的類型 ## TestNG-中的參數化類型 為了使參數化更加清晰,我們將在 Selenium Webdriver 最受歡迎的框架之一中瀏覽參數化選項- **TestNG** 。 **有兩種方法**,我們可以通過它們在 TestNG 中實現參數化 1. With the help of?**Parameters**?**annotation**?and?**TestNG XML**?file. ![TestNG: Parameterization using XML & DataProvider in Selenium](https://img.kancloud.cn/88/3b/883b938cd6f163795487d954a88eb29c_239x18.png "Parameterization using XML and DataProviders: Selenium") 2. With the help of?**DataProvider**?annotation. ![TestNG: Parameterization using XML & DataProvider in Selenium](https://img.kancloud.cn/09/a5/09a59871882ddc5d454de326ef8da0e6_266x19.png "Parameterization using XML and DataProviders: Selenium") ![TestNG: Parameterization using XML & DataProvider in Selenium](https://img.kancloud.cn/56/4a/564a63488f39d14148f23d98eec68827_624x359.png "Parameterization using XML and DataProviders: Selenium") 來自 Testng.xml 的參數可以是套件級別或測試級別 來自 DataProvider 的參數可以采用 Method 和 ITestContext 作為參數。 讓我們詳細研究它們- ## 帶 Testng.xml 的參數注釋 當您確實想處理復雜性&時,請使用注釋選擇參數化,輸入組合的數量會減少。 讓我們看看這是如何工作的 測試場景 步驟 1)啟動瀏覽器&轉到 Google.com 步驟 2)輸入搜尋關鍵字 ![TestNG: Parameterization using XML & DataProvider in Selenium](https://img.kancloud.cn/95/a2/95a23b9c4af791a41d68edc7d54ac15d_370x248.png "Parameterization using XML and DataProviders: Selenium") 步驟 3)確認輸入的值與我們的測試數據提供的值相同 步驟 4)重復 2 & 3 直到輸入所有值 <colgroup><col> <col></colgroup> | 測試作者 | 搜索關鍵字 | | 大師 99 | 印度 | | 克里希納 | 美國 | | 布普什 | 中國 | 這是沒有參數的情況下的示例 ``` package parameters; import org.testng.annotations.Test; import org.testng.AssertJUnit; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class NoParameterWithTestNGXML { String driverPath = "C:\\geckodriver.exe"; WebDriver driver; @Test public void testNoParameter() throws InterruptedException{ String author = "guru99"; String searchKey = "india"; System.setProperty("webdriver.gecko.driver", driverPath); driver= new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://google.com"); WebElement searchText = driver.findElement(By.name("q")); //Searching text in google text box searchText.sendKeys(searchKey); System.out.println("Welcome ->"+author+" Your search key is->"+searchKey); System.out.println("Thread will sleep now"); Thread.sleep(3000); System.out.println("Value in Google Search Box = "+searchText.getAttribute("value") +" ::: Value given by input = "+searchKey); //verifying the value in google search box AssertJUnit.assertTrue(searchText.getAttribute("value").equalsIgnoreCase(searchKey)); } } ``` 一個研究,上面的例子。 試想一下,當我們對 3 個輸入組合執行此操作時,代碼將變得多么復雜 現在,讓我們使用 TestNG 參數化它 為此,您需要 * 創建一個將存儲參數的 XML 文件 * 在測試中,添加注釋@Parameters ![TestNG: Parameterization using XML & DataProvider in Selenium](https://img.kancloud.cn/c4/00/c400983ebf98997773d19e87b840c839_513x322.png "Parameterization using XML and DataProviders: Selenium") 這是完整的代碼 **測試級別 TestNG.xml** ``` <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="TestSuite" thread-count="3" > <parameter name="author" value="Guru99" /> <parameter name="searchKey" value="India" /> <test name="testGuru"> <parameter name="searchKey" value="UK" /> <classes> <class name="parameters.ParameterWithTestNGXML"> </class> </classes> </test> </suite> ``` **ParameterWithTestNGXML.java 文件** ``` package parameters; import org.testng.AssertJUnit; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Optional; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class ParameterWithTestNGXML { String driverPath = "C:\\geckodriver.exe"; WebDriver driver; @Test @Parameters({"author","searchKey"}) public void testParameterWithXML( @Optional("Abc") String author,String searchKey) throws InterruptedException{ System.setProperty("webdriver.gecko.driver", driverPath); driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://google.com"); WebElement searchText = driver.findElement(By.name("q")); //Searching text in google text box searchText.sendKeys(searchKey); System.out.println("Welcome ->"+author+" Your search key is->"+searchKey); System.out.println("Thread will sleep now"); Thread.sleep(3000); System.out.println("Value in Google Search Box = "+searchText.getAttribute("value") +" ::: Value given by input = "+searchKey); //verifying the value in google search box AssertJUnit.assertTrue(searchText.getAttribute("value").equalsIgnoreCase(searchKey)); } } ``` 有關運行腳本,選擇 XML 文件和“以 Test NG Suite 運行”的說明 **右鍵單擊.xml 文件->運行方式-> [測試版](/all-about-testng-and-selenium.html)套件(注意:套件)** ![TestNG: Parameterization using XML & DataProvider in Selenium](https://img.kancloud.cn/2c/1a/2c1a987158a4ff69bb8857c216dd5962_624x159.png "Parameterization using XML and DataProviders: Selenium") 現在,可以在 2 個級別定義參數 1. 套件級別– TestNG XML 文件的<套件>標記內的參數將為套件級別參數。 2. 測試級別-測試 XML 文件的<測試>標記內的參數將為測試級別參數。 這是帶有套件級別參數的相同測試 ![TestNG: Parameterization using XML & DataProvider in Selenium](https://img.kancloud.cn/c1/6f/c16f2158a46e719147cb9a0e18c90398_436x189.png "Parameterization using XML and DataProviders: Selenium") ***注意:*** 如果套件級別和測試級別中的參數名稱相同,則測試級別參數將優先于套件級別。 因此,在那種情況下,該測試級別內的所有類將共享重寫的參數,而測試級別外的其他類將共享套件級別的參數。 ![TestNG: Parameterization using XML & DataProvider in Selenium](https://img.kancloud.cn/32/b2/32b2e90034110bdcbc7866d1f3988d62_536x202.png "Parameterization using XML and DataProviders: Selenium") ### 故障排除 ***問題#1*** 無法將 testng.xml 中的參數值類型轉換為相應的測試方法的參數,這將引發錯誤。 考慮下面的例子 ![TestNG: Parameterization using XML & DataProvider in Selenium](https://img.kancloud.cn/5c/99/5c993b99f544b2ebd1dcfdad399ea243_624x287.png "Parameterization using XML and DataProviders: Selenium") 在這里,“作者”屬性等于“ Guru99”,它是一個字符串,在相應的測試方法中,它期望一個整數值,因此我們將在這里得到一個例外。 ***問題#2*** 您的@Parameters 在 testing.xml 中沒有相應的值。 您可以通過在測試方法的相應參數中添加 **@optional** ,**批注**來解決這種情況。 ![TestNG: Parameterization using XML & DataProvider in Selenium](https://img.kancloud.cn/da/ef/daef5b4423e8641240fda835e5da92fd_613x128.png "Parameterization using XML and DataProviders: Selenium") ***問題#3* :**您要使用 Testng.xml 測試同一參數的多個值 簡單的答案是這不能完成! 您可以有多個不同的參數,但是每個參數只能有一個值。 這有助于防止將值硬編碼到腳本中。 這使代碼可重用。 可以將其視為腳本的配置文件。 如果要為參數使用多個值,請使用 DataProviders ## 使用 Dataprovider 的參數 @Parameters 批注很容易,但是要測試多組數據,我們需要使用 Data Provider。 要使用我們的測試框架填充數千個 Web 表單,我們需要一種不同的方法,該方法可以在一個執行流程中為我們提供非常大的數據集。 這個數據驅動的概念是通過 TestNG 中的 **@DataProvider** 注釋實現的。 ![TestNG: Parameterization using XML & DataProvider in Selenium](https://img.kancloud.cn/df/41/df41ce0837addb4f6f20cff85cc2c4d0_517x153.png "Parameterization using XML and DataProviders: Selenium") 它只有一個**屬性'name'**。 如果未指定 name 屬性,則 DataProvider 的名稱將與相應的方法名稱相同。 數據提供者將**二維 JAVA 對象**返回到測試方法和測試方法,將在 M * N 類型的對象數組中調用 M 次。 例如,如果 DataProvider 返回一個 2 * 3 對象的數組,則相應的測試用例將每次使用 3 個參數被調用 2 次。 ![TestNG: Parameterization using XML & DataProvider in Selenium](https://img.kancloud.cn/a9/1e/a91e0b9fb43cfb62501c696bf1544082_517x153.png "Parameterization using XML and DataProviders: Selenium") **完整示例** ![TestNG: Parameterization using XML & DataProvider in Selenium](https://img.kancloud.cn/00/22/0022d02f75d85ab8f83e07bb1b9a9973_390x147.png "Parameterization using XML and DataProviders: Selenium") ``` package parameters; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.BeforeTest; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class ParameterByDataprovider { WebDriver driver; String driverPath = "C:\\geckodriver.exe"; @BeforeTest public void setup(){ //Create firefox driver object System.setProperty("webdriver.gecko.driver", driverPath); driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://google.com"); } /** Test case to verify google search box * @param author * @param searchKey * @throws InterruptedException */ @Test(dataProvider="SearchProvider") public void testMethod(String author,String searchKey) throws InterruptedException{ { WebElement searchText = driver.findElement(By.name("q")); //search value in google searchbox searchText.sendKeys(searchKey); System.out.println("Welcome ->"+author+" Your search key is->"+searchKey); Thread.sleep(3000); String testValue = searchText.getAttribute("value"); System.out.println(testValue +"::::"+searchKey); searchText.clear(); //Verify if the value in google search box is correct Assert.assertTrue(testValue.equalsIgnoreCase(searchKey)); } } /** * @return Object[][] where first column contains 'author' * and second column contains 'searchKey' */ @DataProvider(name="SearchProvider") public Object[][] getDataFromDataprovider(){ return new Object[][] { { "Guru99", "India" }, { "Krishna", "UK" }, { "Bhupesh", "USA" } }; } } ``` ### 從不同的類調用 DataProvider 默認情況下,DataProvider 駐留在測試方法所在的類或其基類中。 要將其放在其他類中,我們需要將數據提供者方法設為靜態,在測試方法中,我們需要在 **@Test** 批注中添加屬性 **dataProviderClass** 。 ![TestNG: Parameterization using XML & DataProvider in Selenium](https://img.kancloud.cn/de/72/de72687bd336ba3de9a28a04483d0400_437x159.png "Parameterization using XML and DataProviders: Selenium") **代碼示例** ![TestNG: Parameterization using XML & DataProvider in Selenium](https://img.kancloud.cn/87/c4/87c43c63c28f2ae40b2d2d3a68e756d1_404x168.png "Parameterization using XML and DataProviders: Selenium") **TestClass ParameterDataproviderWithClassLevel.java** ``` package parameters; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class ParameterDataproviderWithClassLevel { WebDriver driver; String driverPath = "C:\\geckodriver.exe"; @BeforeTest public void setup(){ System.setProperty("webdriver.gecko.driver", driverPath); driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://google.com"); } @Test(dataProvider="SearchProvider",dataProviderClass=DataproviderClass.class) public void testMethod(String author,String searchKey) throws InterruptedException{ WebElement searchText = driver.findElement(By.name("q")); //Search text in google text box searchText.sendKeys(searchKey); System.out.println("Welcome ->"+author+" Your search key is->"+searchKey); Thread.sleep(3000); //get text from search box String testValue = searchText.getAttribute("value"); System.out.println(testValue +"::::"+searchKey); searchText.clear(); //verify if search box has correct value Assert.assertTrue(testValue.equalsIgnoreCase(searchKey)); } } ``` **DataproviderClass.java** ``` package parameters; import org.testng.annotations.DataProvider; public class DataproviderClass { @DataProvider(name="SearchProvider") public static Object[][] getDataFromDataprovider(){ return new Object[][] { { "Guru99", "India" }, { "Krishna", "UK" }, { "Bhupesh", "USA" } }; }} ``` ### Dataprovider 中的參數類型 DataProvider 方法支持兩種類型的參數。 **方法**-如果 **SAME** 數據提供程序在使用不同的測試方法時應具有不同的行為,請使用 Method 參數。 ![TestNG: Parameterization using XML & DataProvider in Selenium](https://img.kancloud.cn/16/b8/16b85c7a964abb797451e8104097b566_624x254.png "Parameterization using XML and DataProviders: Selenium") 在以下示例中, * 我們檢查方法名稱是否為 testMethodA。 * 如果是,則返回一組值 * 否則返回另一組值 ``` package parameters; import java.lang.reflect.Method; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.BeforeTest; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class ParameterByMethodInDataprovider{ WebDriver driver; String driverPath = "C:\\geckodriver.exe"; @BeforeTest public void setup(){ System.setProperty("webdriver.gecko.driver", driverPath); driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://google.com"); } @Test(dataProvider="SearchProvider") public void testMethodA(String author,String searchKey) throws InterruptedException{ WebElement searchText = driver.findElement(By.name("q")); //Search text in search box searchText.sendKeys(searchKey); //Print author and search string System.out.println("Welcome ->"+author+" Your search key is->"+searchKey); Thread.sleep(3000); String testValue = searchText.getAttribute("value"); System.out.println(testValue +"::::"+searchKey); searchText.clear(); //Verify if google text box is showing correct value Assert.assertTrue(testValue.equalsIgnoreCase(searchKey)); } @Test(dataProvider="SearchProvider") public void testMethodB(String searchKey) throws InterruptedException{ { WebElement searchText = driver.findElement(By.name("q")); //Search text in search box searchText.sendKeys(searchKey); //Print only search string System.out.println("Welcome ->Unknown user Your search key is->"+searchKey); Thread.sleep(3000); String testValue = searchText.getAttribute("value"); System.out.println(testValue +"::::"+searchKey); searchText.clear(); //Verify if google text box is showing correct value Assert.assertTrue(testValue.equalsIgnoreCase(searchKey)); } } /** * Here DataProvider returning value on the basis of test method name * @param m * @return **/ @DataProvider(name="SearchProvider") public Object[][] getDataFromDataprovider(Method m){ if(m.getName().equalsIgnoreCase("testMethodA")){ return new Object[][] { { "Guru99", "India" }, { "Krishna", "UK" }, { "Bhupesh", "USA" } };} else{ return new Object[][] { { "Canada" }, { "Russia" }, { "Japan" } };} } } ``` 這是輸出 ![TestNG: Parameterization using XML & DataProvider in Selenium](https://img.kancloud.cn/8e/34/8e3453cfd14ef58f843eebb3b2415477_414x356.png "Parameterization using XML and DataProviders: Selenium") **ITestContext** -它可以用于基于組為測試用例創建不同的參數。 在現實生活中,您可以使用 ITestContext 根據測試方法,測試主機,配置更改參數值。 ![TestNG: Parameterization using XML & DataProvider in Selenium](https://img.kancloud.cn/f2/67/f267c4a29b21fc811c34b2117a614456_624x377.png "Parameterization using XML and DataProviders: Selenium") 在下面的代碼示例中 * 我們有 2 組 A & B * 每種測試方法都分配給一個組 * 如果組的值為 A,則返回特定的數據集 * 如果組的值為 B,則返回另一個數據集 ``` package parameters; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.ITestContext; import org.testng.annotations.BeforeTest; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class ParameterByITestContextInDataprovider { WebDriver driver; String driverPath = "C:\\geckodriver.exe"; @BeforeTest(groups={"A","B"}) public void setup(){ System.setProperty("webdriver.gecko.driver", driverPath); driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://google.com"); } @Test(dataProvider="SearchProvider",groups="A") public void testMethodA(String author,String searchKey) throws InterruptedException{ { //search google textbox WebElement searchText = driver.findElement(By.name("q")); //search a value on it searchText.sendKeys(searchKey); System.out.println("Welcome ->"+author+" Your search key is->"+searchKey); Thread.sleep(3000); String testValue = searchText.getAttribute("value"); System.out.println(testValue +"::::"+searchKey); searchText.clear(); //verify correct value in searchbox Assert.assertTrue(testValue.equalsIgnoreCase(searchKey)); } } @Test(dataProvider="SearchProvider",groups="B") public void testMethodB(String searchKey) throws InterruptedException{ { //find google search box WebElement searchText = driver.findElement(By.name("q")); //search a value on it searchText.sendKeys(searchKey); System.out.println("Welcome ->Unknown user Your search key is->"+searchKey); Thread.sleep(3000); String testValue = searchText.getAttribute("value"); System.out.println(testValue +"::::"+searchKey); searchText.clear(); //verify correct value in searchbox Assert.assertTrue(testValue.equalsIgnoreCase(searchKey)); } } /** * Here the DAtaProvider will provide Object array on the basis on ITestContext * @param c * @return */ @DataProvider(name="SearchProvider") public Object[][] getDataFromDataprovider(ITestContext c){ Object[][] groupArray = null; for (String group : c.getIncludedGroups()) { if(group.equalsIgnoreCase("A")){ groupArray = new Object[][] { { "Guru99", "India" }, { "Krishna", "UK" }, { "Bhupesh", "USA" } }; break; } else if(group.equalsIgnoreCase("B")) { groupArray = new Object[][] { { "Canada" }, { "Russia" }, { "Japan" } }; } break; } return groupArray; } } ``` 注意:如果直接運行您的 testng 類,它將首先調用 dataprovider,由于組不可用,該數據提供程序無法獲取組信息。 但是,相反,如果您通過 testng.xml 調用此類,它將在 ITestContext 中提供可用的組信息。 使用以下 XML 調用測試 ``` <!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" > <suite name="test-parameter"> <test name="example1"> <groups> <run> <include name="A" /> </run> </groups> <classes> <class name="parameters.ParameterByITestContextInDataprovider" /> </classes> </test> <test name="example2"> <groups> <run> <include name="B" /> </run> </groups> <classes> <class name="parameters.ParameterByITestContextInDataprovider" /> </classes> </test> </suite> ``` **摘要**: * **必須進行參數化**才能創建**數據驅動測試**。 * TestNG 支持兩種參數化,使用 **@ Parameter + TestNG.xml** 和使用 **@DataProvider** * In **@Parameter+TestNG.xml** parameters can be placed in suite level and test level. If 在兩個地方都聲明了相同的參數名稱; 測試級別參數將優先于西服級別參數。 * 使用@ Parameter + TestNG.xml 一次只能設置一個值,但是@DataProvider 返回**一個對象**的二維數組。 * 如果 DataProvider 存在于不同的類中,那么測試方法所在的類為 **DataProvider** 應該是**靜態方法**。 * **DataProvider** 支持兩個參數,即**方法**和 **ITestContext。**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看