データプロバイダーと TestNG XML: パラメータ化 Selenium(例)
パラメータ化 Selenium
パラメータ化 Selenium 実行時に複数のデータをアプリケーションに渡すために、テスト スクリプトをパラメータ化するプロセスです。 これは、異なる値を使用してテスト ケースを複数回自動的に実行する実行戦略です。 テスト スクリプトをパラメータ化することで実現される概念は、 データ駆動型テスト.
パラメータ化のタイプ TestNG-
パラメータ化をより明確にするために、最も一般的なフレームワークのパラメータ化オプションを見ていきます。 Selenium ウェブドライバー – TestNG.
全 二つの方法 これによりパラメータ化を実現できます。 TestNG
Testng.xmlからのパラメータはスイートまたはテストレベルにすることができます
DataProvider からのパラメーターは、メソッドと ITestContext をパラメーターとして受け取ることができます。
それらを詳しく調べてみましょう –
パラメータの注釈 TestNG
パラメータの注釈 TestNG .xml ファイルを使用してテスト メソッドに値を引数として渡すために使用されるメソッドです。 ユーザーは、実行時に値をテスト メソッドに渡すことが要求される場合があります。 @Parameters アノテーション メソッドは、@Test、@Before、@After、または @Factory アノテーションを持つ任意のメソッドで使用できます。
Testng.xml によるパラメータ注釈
複雑な処理が必要で、入力の組み合わせの数が少ない場合は、注釈を使用したパラメータ化を選択します。
これがどのように機能するかを見てみましょう
テストシナリオ
ステップ 1) ブラウザを起動し、Google.com にアクセスします。
ステップ2) 検索キーワードを入力します
ステップ 3) 入力された値がテストデータによって提供された値と同じであることを確認します。
ステップ 4) すべての値を入力するまで 2 と 3 を繰り返します。
| テスト作成者 | 検索キー |
|---|---|
| Guru99 | インド |
| Krishna | USA |
| ブペシュ | China |
パラメーターを使用せずにこれを行う方法の例を次に示します。
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。の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>
パラメータありTestNGXML.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 ファイルを選択し、テスト NG スイートとして実行する手順
.xml ファイルを右クリック -> 「実行」 -> テスト スイート(注:スイート)
パラメータを 2 つのレベルで定義できるようになりました
- スイート レベル – スイート内のパラメータのタグ TestNG XML ファイルはスイート レベルのパラメータになります。
- テストレベル — 内部のパラメータテスト XML ファイルのタグはテスト レベル パラメータになります。
これはスイートレベルのパラメータを使用した同じテストです
注: パラメータ名がスイート レベルとテスト レベルで同じである場合、スイート レベルよりもテスト レベルのパラメータが優先されます。 したがって、その場合、そのテスト レベル内のすべてのクラスはオーバーライドされたパラメーターを共有し、テスト レベルの外側にある他のクラスはスイート レベルのパラメーターを共有します。
トラブルシューティング
発行番号1 testng.xml 内のパラメータ値は、対応するテスト メソッドのパラメータに型変換できないため、エラーが発生します。
次の例を考えてみましょう
ここで、「author」属性は文字列である「Guru99」と等しく、対応するテスト メソッドでは整数値が期待されるため、ここで例外が発生します。
発行番号2 @Parameters には、testing.xml 内に対応する値がありません。
を追加することでこの状況を解決できます @オプション 注釈 テストメソッドの対応するパラメータで。
問題 #3: Testng.xmlを使用して同じパラメータの複数の値をテストしたい
簡単な答えは、「そんなことはできない」ということです。 複数の異なるパラメータを指定できますが、各パラメータに指定できる値は XNUMX つだけです。 これは、値がスクリプトにハードコーディングされるのを防ぐのに役立ちます。 これにより、コードが再利用可能になります。 これをスクリプトの構成ファイルと考えてください。 パラメータに複数の値を使用する場合は、DataProviders を使用します。
データプロバイダーの TestNG
データプロバイダーの TestNG 複雑なパラメータを渡す必要がある場合に使われるメソッドです。複雑なパラメータは Java 複雑なオブジェクト、プロパティ ファイルまたはデータベースからのオブジェクトなどは、データ プロバイダー メソッドによって渡すことができます。このメソッドは @DataProvider でアノテーションが付けられ、オブジェクトの配列を返します。
データプロバイダーを使用したパラメーター
@Parameters アノテーションは簡単ですが、複数のデータセットでテストするにはデータプロバイダーを使用する必要があります。
テスト フレームワークを使用して何千もの Web フォームに入力するには、単一の実行フローで非常に大規模なデータセットを提供できる別の方法論が必要です。
このデータドリブンのコンセプトは次のように実現されます。 @データプロバイダー の注釈 TestNG.
XNUMX つだけあります 属性名'。 name 属性を指定しない場合、DataProvider の名前は対応するメソッド名と同じになります。
データプロバイダーの返品 XNUMX次元のJAVAオブジェクト テスト メソッドとテスト メソッドは、M*N 型のオブジェクト配列で M 回呼び出されます。 たとえば、DataProvider が 2*3 オブジェクトの配列を返す場合、対応するテストケースは毎回 2 つのパラメーターを使用して 3 回呼び出されます。
完全な例
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 はテスト メソッドが存在するのと同じクラス、またはその基本クラスに存在します。 それを他のクラスに置くには、データプロバイダーメソッドを静的として作成する必要があり、テストメソッドに属性を追加する必要があります。 データプロバイダクラス in @テスト 注釈。
コードの例
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));
}
}
データプロバイダクラス.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 メソッドでサポートされるパラメーターは XNUMX 種類あります。
方法 - もし 同じ DataProvider は、異なるテスト メソッドでは異なる動作をする必要があり、Method パラメーターを使用します。
次の例では、
- メソッド名がtestMethodAであるかどうかを確認します。
- 「はい」の場合は、XNUMX セットの値を返します
- それ以外の場合は、別の値のセットを返します
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" }
};}
}
}
これが出力です
ITestContext– グループに基づいてテスト ケースのさまざまなパラメーターを作成するために使用できます。
実際には、ITestContext を使用して、テスト メソッド、ホスト、テストの構成に基づいてパラメーター値を変更できます。
次のコード例では
- AとBの2つのグループがあります
- 各テストメソッドはグループに割り当てられます
- グループの値が 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.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 を使用して 2 種類のパラメータ化をサポートします。 @パラメータ+TestNG。のXml を使用して@データプロバイダー
-
In @パラメータ+TestNG。のXml パラメータはスイート レベルとテスト レベルに配置できます。 もし
同じパラメータ名が両方の場所で宣言されています。 テストレベルパラメータはスーツレベルパラメータよりも優先されます。
- @Parameter+ を使用するTestNG.xml では一度に 1 つの値しか設定できませんが、@DataProvider は戻り値を返します オブジェクトの 2 次元配列.
- DataProvider が別のクラスに存在する場合は、テスト メソッドが存在するクラス、データプロバイダー でなければなりません 静的メソッド.
- でサポートされるパラメータは XNUMX つあります データプロバイダー 方法 の三脚と ITestContext。
















