WebDriver+TestNG的一个典型例子
想让测试更加灵活,1. 可以配置使用任意支持的浏览器进行测试;2. 配置所有Google的URL;3. 配置搜索的关键字。修改后的代码:
public class GoogleTest { WebDriver driver; @Parameters({"browser"}) @BeforeTest public void setupBrowser(String browser){ if (browser.equals("firefox")){ driver = new FirefoxDriver(); } else { driver = new ChromeDriver(); } } @Parameters({ "url", "keyword" }) @Test public void search(String url, String keyword, ITestContext context) { driver.get(url); WebElement element = driver.findElement(By.name("q")); element.sendKeys(keyword); element.submit(); Assert.assertTrue(driver.getTitle().contains(keyword), "Something wrong with title"); } }
testng.xml的内容:
<suite name="Magus demo" verbose="2"> <parameter name="browser" value="firefox" /> <parameter name="url" value="http://www.google.com" /> <parameter name="keyword" value="magus" /> <test name="Search function" preserve-order="true"> <classes> <class name="test.GoogleTest"> <methods> <include name="setupBrowser" /> <include name="search" /> </methods> </class> </classes> </test> </suite>
<test name="Add friend" preserve-order="true"> <classes> <class name="test.GoogleTest"> <methods> <include name="login" /> <include name="addFriend" /> <include name="logout" /> </methods> </class> </classes> </test> <test name="Add comment to category" preserve-order="true"> <classes> <class name="test.GoogleTest"> <methods> <include name="login" /> <include name="navigateCategory" /> <include name="addComment" /> <include name="logout" /> </methods> </class> </classes> </test>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。