使用TestNG对IE /Chrome/firefox 进行兼容性并发测试 ;
1 package testNGTest; 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.WebDriver; 5 import org.openqa.selenium.WebElement; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 import org.openqa.selenium.firefox.FirefoxDriver; 8 import org.openqa.selenium.ie.InternetExplorerDriver; 9 import org.testng.Assert; 10 import org.testng.annotations.AfterClass; 11 import org.testng.annotations.BeforeClass; 12 import org.testng.annotations.Parameters; 13 import org.testng.annotations.Test; 14 15 16 public class MultipleBrowserSearchTest { 17 public WebDriver driver; 18 String baseUrl = "http://www.sogou.com/"; 19 20 @Parameters("browser") 21 @BeforeClass 22 public void beforeTest(String Browser) { 23 if (Browser.equals("firefox")) { 24 System.setProperty("webdriver.firefox.bin", "D:\softerWare\firefox\firefox\firefox.exe"); 25 driver = new FirefoxDriver(); 26 27 28 } else if (Browser.equals("ie")) { 29 System.setProperty("webdriver.ie.driver", "D:\softerWare\selenium\IE\IE_Driver\IEDriverServer.exe"); 30 driver = new InternetExplorerDriver(); 31 32 } else if (Browser.equals("chrome")) { 33 34 System.setProperty("webdriver.chrome.driver", "D:\softerWare\selenium\chromeDriver\chromedriver.exe"); 35 driver = new ChromeDriver(); 36 } 37 } 38 @Test 39 public void testSogouSearch() { 40 driver.get(baseUrl); 41 WebElement inputBox = driver.findElement(By.id("query")); 42 Assert.assertTrue(inputBox.isDisplayed()); 43 inputBox.sendKeys("关荣的自动化测试 !"); 44 driver.findElement(By.id("stb")).click(); 45 46 Assert.assertTrue(driver.getPageSource().contains("关荣的自动化测试")); 47 } 48 49 @AfterClass 50 public void afterTest() { 51 driver.close(); 52 53 } 54 55 }
XML中的代码:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 3 <suite name="Suite" parallel="tests" thread-count="3"> 4 <test name="FirefoxTest"> 5 <parameter name="browser" value="firefox"></parameter> 6 <classes> 7 <class name="testNGTest.MultipleBrowserSearchTest"/> 8 </classes> 9 </test> 10 <test name="IEText" > 11 <parameter name="browser" value="ie"/> 12 <classes> 13 <class name="testNGTest.MultipleBrowserSearchTest"/> 14 </classes> 15 </test> 16 <test name="ChromeTest"> 17 <parameter name="browser" value="chrome"/> 18 <classes> 19 <class name="testNGTest.MultipleBrowserSearchTest"/> 20 </classes> 21 </test> 22 </suite>
说明:直接执行xml中的测试代码;
转自:http://blog.csdn.net/u011138533/article/details/52174446
thread-count
与parallel配套使用,线程池的大小,决定并行线程数量 |
parallel
该参数的值false,methods,tests,classes,instances。默认false
parallel必须和thread-count配套使用,否则相当于无效参数,thread-count决定了并行测试时开启的线程数量
parallel="mehods" TestNG将并行执行所有的测试方法在不同的线程里
parallel="tests" TestNG将并行执行在同一个<test>下的所有方法在不同线程里
parallel="classes" TestNG将并行执行在相同<class>下的方法在不同线程里
parallel="instances" TestNG将并行执行相同实例下的所有方法在不同的线程里