TestNG測試框架的使用-Parameter和parrallel
1:Parameter用來給test方法提供String型別的引數
1.1 testng.xml檔案的寫法
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="Test1"> <parameter name="key1" value="Parameter1"></parameter> <parameter name="key2" value="Parameter2"></parameter> <parameter name="key3" value="123"></parameter> <classes> <class name="TestNGEXT.Parameterext"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
1.2:測試方法
package TestNGEXT; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class Parameterext { @Test @Parameters({"key1"}) public void Parameter1(String key){ System.out.println("Parameter1 ="+key); } @Test @Parameters({"key2","key3"}) public void Parameter2(String key2 , String key3){ System.out.println("Parameter2 ="+key2 +" "+key3); } }
1.3:注意事項
@Parameters傳遞的引數都是String型別的引數
2:parrallel冰心執行class和methods
Parameterext1
package TestNGEXT; import org.testng.annotations.Test; public class Parameterext1 { @Test public void Parameter_1(){ System.out.println("Parameter1 = Test1.1"); } @Test public void Parameter_2(){ System.out.println("Parameter1 = Test1.2"); } }
Parameterext2
package TestNGEXT;
import org.testng.annotations.Test;
public class Parameterext2 {
@Test
public void Parameter_1(){
System.out.println("Parameter2 = Test2.1");
}
@Test
public void Parameter_2(){
System.out.println("Parameter2 = Test2.2");
}
}
2.1:並行執行class
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" thread-count="2">
<test name="Test1">
<classes>
<class name="TestNGEXT.Parameterext1"/>
</classes>
</test>
<test name="Test2">
<classes>
<class name="TestNGEXT.Parameterext2"/>
</classes>
</test>
</suite> <!-- Suite -->
2.2:並行執行methods
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods" thread-count="2">
<test name="Test1">
<classes>
<class name="TestNGEXT.Parameterext1"/>
</classes>
</test>
<test name="Test2">
<classes>
<class name="TestNGEXT.Parameterext2"/>
</classes>
</test>
</suite> <!-- Suite -->
2.3:parallel引數的特徵
如下 parallel引數有下列可選的引數值
- classes:並行執行classes
執行結果是:
Parameter1 = Test1.1
Parameter1 = Test1.2
Parameter2 = Test2.1
Parameter2 = Test2.2
methods:並行執行方法Parameter1中的兩個@Test方法併發最新後再併發執行Parameter2中的Test方法
執行結果是 Parameter1 = Test1.1
Parameter1 = Test1.2
Parameter2 = Test2.1
Parameter2 = Test2.2
- tests:並行執行@Test的註釋 Parameter1和Parameter2兩個類同步併發執行
執行結果是:
Parameter1 = Test1.1
Parameter2 = Test2.1
Parameter1 = Test1.2
Parameter2 = Test2.2
3:Parameter和parrallel與selenium的結合
selenium webdriver自動化指令碼 啟動baidu
package TestNGEXT;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class Parameterext {
private WebDriver driver;
@Test
@Parameters({"browers"})
public void Parameter_1(String browersNmae){
if(browersNmae.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
}else if(browersNmae.equalsIgnoreCase("firefox")){
driver = new FirefoxDriver();
}
driver.get("www.baidu.com");
}
}
2:testng.xml檔案
這樣配置的好處是同樣的執行指令碼 只用寫一次java指令碼 其他的動作交給TestNG測試框架來處理
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" thread-count="2">
<!-- 根據browers引數併發啟動chrome和firefox瀏覽器 -->
<test name="Test1">
<parameter name="browers" value="chrome"></parameter>
<classes>
<class name="TestNGEXT.Parameterext" />
</classes>
</test>
<test name="Test2">
<parameter name="browers" value="firefox"></parameter>
<classes>
<class name="TestNGEXT.Parameterext" />
</classes>
</test>
</suite> <!-- Suite -->