import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class Baidu_2 { @Parameters("browser") @Test public void startupBrowser(String browserName) throws InterruptedException { String baiduUrl = "https://www.baidu.com/"; String baiduTitle = "百度一下,你就知道"; WebDriver driver = null; if (browserName.equalsIgnoreCase("firefox")){ System.setProperty("webdriver.firefox.marionette", "src/main/resourcec/geckodriver.exe"); driver = new FirefoxDriver(); }else if (browserName.equalsIgnoreCase("chrome")){ System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe"); ChromeOptions options = new ChromeOptions(); options.addArguments("disable-infobars"); driver =new ChromeDriver(options); } if (driver != null) { driver.get(baiduUrl); driver.manage().window().maximize(); Thread.sleep(2000); Assert.assertEquals(driver.getTitle(), baiduTitle); driver.quit(); } } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="同时启动2个浏览器打开百度" verbose="2" preserve-order="true" parallel="tests" thread-count="2"> <test name="启动Firefox浏览器"> <parameter name="browser" value="firefox" /> <classes> <class name="Baidu_2" /> </classes> </test> <test name="启动Chrome浏览器"> <parameter name="browser" value="chrome" /> <classes> <class name="Baidu_2" /> </classes> </test> <listeners> <listener class-name="org.uncommons.reportng.HTMLReporter" /> <listener class-name="org.uncommons.reportng.JUnitXMLReporter" /> </listeners> </suite>